Write a C++ program to sort 4 integers only using the if-else control structure no brute force tactic.

+2 Bob huginstien · March 8, 2015
Hello I can not seem to get a sequence of the same integer in the output, example if I typed in 1 1 1 1, it goes blank, or  1 2 2 1. But when I type in  2 4 5 3 its out put is  2 3 4 5.  Here is my code.  What am I doing wrong? 

#include <iostream>
#include <algorithm>


using namespace std;

int main ()
{
    int a;
    int b;
    int c;
    int d;
    int smallest; 
    int biggest;
    int middle1;
    int middle2;


    cout<<"Enter four integers here:\n"; // Enter  four numbers.
    cin >> a;
    cin >> b;
    cin >> c:
    cin >> d;

    
if ( a <= b)

    smallest = a;
    biggest = b;

else ( b <= a)

    {smallest = b;
    biggest = a;}


else if ( c <= smallest )

  {
    
   smallest = c;}
 
 else if ( c >= biggest )

   {biggest = c;}



   else if (d < smallest)
     {smallest = d;}

   else if (d >= biggest)

    {biggest = d;}
else if ( a <= b )
    {middle1 =a;
    middle2 = b;}
else if ( b <=a )
    {middle1 = b;
    middle2 = a;}
else if ( c <= middle1)
    {middle1=c ;}
 else if ( c >= middle2 )
    middle2 =c ;
 
 else if ( d <= middle1)
    {middle1=d ;}

else if ( d >= middle2 )
    {middle2 =d ;}
  



    return 0;
}


Post a Reply

Replies

- page 2
Oldest  Newest  Rating
0 Bob huginstien · March 9, 2015
// Program to send four different permutations //
// in integer form//
//***********************************************//


#include <iostream>
#include <algorithm>


using namespace std;

int main()
{
int a;
int b;
int c;
int d;
int smallestSofar;
bool firstExcluded = false;
bool secondExcluded = false;
bool thirdExcluded = false;
bool fourthExcluded = false;
cout << "Enter four integers here:\n"; // Enter four numbers.
cin >> a;
cin >> b;
cin >> c;
cin >> d;
smallestSofar = a;
if (b<a)
smallestSofar = b;
if (c<smallestSofar)
smallestSofar = c;
if (d<smallestSofar)
smallestSofar = d;
cout << smallestSofar;
if (smallestSofar == a) // add a condition here to chack if firstExcluded is false
{
firstExcluded = true;
smallestSofar = b; // don't understand why you do this
}
else if (smallestSofar == b)
{
secondExcluded = true;
smallestSofar = a;
}
else if (smallestSofar == c)
{
thirdExcluded = true;
smallestSofar = a;
}
else if (smallestSofar == d)
{
fourthExcluded = true;
smallestSofar = a;
}
if (!firstExcluded && a<smallestSofar)
smallestSofar = a; // good. This is the first thing you should do here. Everything you did above is not necessary.
if (!secondExcluded && b<smallestSofar)
smallestSofar = b;
if (!thirdExcluded && c<smallestSofar)
smallestSofar = c;
if (!fourthExcluded && d<smallestSofar)
smallestSofar = d;
cout << smallestSofar;
if (smallestSofar == a)
{
firstExcluded = true;
if (secondExcluded) // don't understand why you do this.
smallestSofar = c;
else
smallestSofar = b;
}
else if (smallestSofar == b)
{
secondExcluded = true;
if (firstExcluded)
smallestSofar = c;
else
smallestSofar = a;
}
else if (smallestSofar == c)
{
thirdExcluded = true;
if (firstExcluded)
smallestSofar = b;
else
smallestSofar = a;
}
else if (smallestSofar == d)
{
fourthExcluded = true;
if (firstExcluded)
smallestSofar = b;
else
smallestSofar = a;
}
if (!firstExcluded && a<smallestSofar) // I guess this is to determine the second smallest integer.
smallestSofar = a;
if (!secondExcluded && b<smallestSofar)
smallestSofar = b;
if (!thirdExcluded && c<smallestSofar)
smallestSofar = c;
if (!fourthExcluded && d<smallestSofar)
smallestSofar = d;
cout << smallestSofar;
if (smallestSofar == a)
firstExcluded = true; // good
else if (smallestSofar == b)
secondExcluded = true;
else if (smallestSofar == c)
thirdExcluded = true;
else if (smallestSofar == d)
fourthExcluded = true;
if (!firstExcluded) // after the second smallest integer is determined, exclude the integer like what you did above for the first smallest integer.
{
firstExcluded = true;
smallestSofar = a;
}
if (!secondExcluded)
{
secondExcluded = true;
smallestSofar = b;
}
if (!thirdExcluded)
{
thirdExcluded = true;
smallestSofar= c;
}
if (!fourthExcluded)
{
fourthExcluded = true;
smallestSofar= d;
}
cout << smallestSofar;
return 0; // continue on the third and fourth smallest integers.
}
0 Dol Lod · March 14, 2015
Here is the latest code.


#include<iostream>
#include<cmath> 
#include<fstream> 
#include<iomanip> 
#include <string>

using namespace std;

int main()
{
ifstream infile("input.txt"); //grabs file using filename
if (!infile.is_open()) //checks if the file is open
{
cout << "unable to open file..so exiting from program " << endl;
return 0;
}
int final_score, position, lineSize; //declares variables to be used
double score, total, final_s; //declares doubles to be used
string readLine, scoreAsString, totalAsString; //uses one string to read a line, one to get the score and one for the total
char readCharacter; //declares a temporary character to check the next byte
while (infile.peek() != EOF) //goes through all lines in the file
{
infile >> readLine; //puts a single line into readLine
lineSize = readLine.size(); //calculates the linesize
scoreAsString = ""; //resets the string to nothing
totalAsString = ""; //resets the string to nothing
for (position = 0; position<lineSize; position++)
{
readCharacter = readLine[position];
if (readCharacter == ' ') //searches for the space to find the end of the number
break;
scoreAsString += readCharacter;
}
for (position++; position<lineSize; position++)
{
readCharacter = readLine[position];
if (readCharacter == '\n' || readCharacter == '\r') //searches for newline
break;
totalAsString += readCharacter;
}
score = stod(scoreAsString, NULL); //converts string to double
total = stod(totalAsString, NULL);//converts string to double
final_s = (score * 100) / total;
final_score = static_cast<int> (ceil(final_s));
cout << "Final Score is " << fixed << setprecision(5) << final_s << endl;
if (final_score >= 90)
cout << "Excellent" << endl;
else if (final_score >= 80)
cout << "Well Done" << endl;
else if (final_score >= 70)
cout << "Good" << endl;
else if (final_score >= 60)
cout << "Needs Improvement" << endl;
else if (final_score <60)
cout << "Fail" << endl;
}
infile.close();
return 0;
}
  • 1
  • 2

C++

103,770 followers
About

Used in many types of software including music players, video games, and many large scale applications.

Links
Moderators
Bucky Roberts Administrator