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 1
Oldest  Newest  Rating
0 Dol Lod · March 8, 2015
Here is much simpler code. No need for that many complications. I adopted the idea from merge sort. 

#include <iostream>
#include <algorithm>


using namespace std;

void swap(int& a,int&b)
{
int temp=a;
a=b;
b=temp;
}

int main ()
{
int a;
int b;
int c;
int d;

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

if(b<a)
swap(b,a);
if(d<c)
swap(d,c);
if(c<a)
swap(c,a);
if(c<b)
swap(c,b);
if(d<c)
swap(d,c);
return 0;
}
0 Bob huginstien · March 8, 2015
Kroot what way should I alter it, to make  it not look like a brute force tactic? 

thank you in advance.

Arjun  we haven't learned how to use the void format yet, it might be suspicious that I use it.

thank you anyways.
0 Bob huginstien · March 8, 2015
Also my professor suggested this :  "You need to initialize all the 4 bool variables to "false" in the beginning of the "main" function. Once the first smallest integer is identified, compare each of the 4 integers to see which one equals smallestSoFar. Once identified, change its corresponding bool variable (e.g. int2Excluded) to "true". You then determine the FIRST integer in the 4 integers with "false" as its bool variable and assign it to smallestSoFar for the second run. When you compare the remaining integers with smallestSoFar, make sure its corresponding bool variable is "false" because you don't want to compare those integers that have been excluded."



so far my code looks like this:


//  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 int1Excluded = false;
    bool int2Excluded = false;
    bool int3Excluded = false;
    bool int4Excluded = false;


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



if ( a < smallestSoFar)




          {smallestSoFar = a;}


    else if ( b < smallestSoFar )


                   {smallestSoFar = b;}

                      else  if ( c < smallestSoFar )


                                {smallestSoFar = c;}


                                 else if ( d < smallestSoFar )

                                         {smallestSoFar = d;}

else if ( int1Excluded == true )


if ( b < smallestSoFar)



          {smallestSoFar = b;}


    else if ( c < smallestSoFar )


                   {smallestSoFar = c;}

                      else  if ( d < smallestSoFar )


                                {smallestSoFar = d;}


                                 else if ( a < smallestSoFar )

                                         {smallestSoFar = a;}

else if ( int2Excluded == true )





    if ( c < smallestSoFar)


          {smallestSoFar = c;}


    else if ( d < smallestSoFar )


                   {smallestSoFar = d;}

                      else  if ( a < smallestSoFar )


                                {smallestSoFar = a ;}


                                 else if ( b < smallestSoFar )

                                         {smallestSoFar = b;}

else if ( int3Excluded == true )




if ( d < smallestSoFar)


          {smallestSoFar = d;}


    else if ( a < smallestSoFar )


                   {smallestSoFar = a;}

                      else  if ( b < smallestSoFar )


                                {smallestSoFar = b;}


                                 else if ( c < smallestSoFar )

                                         {smallestSoFar = c;}

else if ( int4Excluded == true )

















    return 0;
}
+1 Dol Lod · March 8, 2015
Is it essential to use the boolean variables? The swap function just acts as quick way to swap the two values, it really isn't complicated. It literally just swaps too values using a temporary int.
int a=5;
int b=6;
To swap them, this is all you need to do. 
int temp=a;
a=b;
b=temp;
Now a is 6 and b is 5. Just substitute instead of using void and the solution should be good.
This code should work, it is updated to do not use the swap function.

If concerned about brute force, don't be. This method like I said is adopted from the merge sort algorithm. I divide the 4 numbers into two parts, a and b as well as c and d. 
I sort a and b, then c and d. Now, I know a and c are the minimums. Next, I swap a and c if c is less than a. Next, I swap b and c if c is less than b. I do one last check between c and d to make sure the elements are in the right order and then I'm done. 

#include <iostream>
#include <algorithm>


using namespace std;


int main ()
{
int a;
int b;
int c;
int d;
int temp;

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

if(b<a)
{
temp=a;
a=b;
b=temp;
}
if(d<c)
{
temp=c;
c=d;
d=temp;
}
if(c<a)
{
temp=a;
a=c;
c=temp;
}
if(c<b)
{
temp=b;
b=c;
c=temp;
}
if(d<c)
{
temp=c;
c=d;
d=temp;
}
return 0;
}
0 Bob huginstien · March 8, 2015
Yes he wants us to use the function bool in there.
0 Bob huginstien · March 8, 2015
The thing is also it needs to perform these tasks....

"Write a C++ program to sort 4 integers in nondecreasing order. The 4 integers entered at the keyboard can be (1) all distinct (e.g. 2 1 3 4), (2) partially distinct (e.g. 1 4 1 2), (3) all identical (e.g. 1 1 1 1), (4) in descending order (e.g. 4 3 2 1), or (5) already sorted in nondecreasing order (e.g. 1 2 3 4). Your program should use ONLY the if-else control structure shown in class to solve the problem and no other structures (e.g. for loops, while loops, arrays, etc.) can be used. Test your program for all the five cases and each working case is worth 0.9 point. The following shows a sample output of running the program.
 
Enter 4 numbers: 1 3 2 4
The sorted sequence for 1 3 2 4 is 1 2 3 4 "

the problem with this on is, if I type in 1111 it   does not organize it at all, its just blank.   As well if I enter in 1 4 1 2,   having a repeated number makes it blank.
+1 Dol Lod · March 8, 2015
Here is the code, completed.


#include <iostream>
#include <algorithm>


using namespace std;

int main()
{
int a;
int b;
int c;
int d;
int smallest;
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;
smallest = a;
if (b<a)
smallest = b;
if (c<smallest)
smallest = c;
if (d<smallest)
smallest = d;
cout << smallest;
if (smallest == a)
{
firstExcluded = true;
smallest = b;
}
else if (smallest == b)
{
secondExcluded = true;
smallest = a;
}
else if (smallest == c)
{
thirdExcluded = true;
smallest = a;
}
else if (smallest == d)
{
fourthExcluded = true;
smallest = a;
}
if (!firstExcluded && a<smallest)
smallest = a;
if (!secondExcluded && b<smallest)
smallest = b;
if (!thirdExcluded && c<smallest)
smallest = c;
if (!fourthExcluded && d<smallest)
smallest = d;
cout << smallest;
if (smallest == a)
{
firstExcluded = true;
if (secondExcluded)
smallest = c;
else
smallest = b;
}
else if (smallest == b)
{
secondExcluded = true;
if (firstExcluded)
smallest = c;
else
smallest = a;
}
else if (smallest == c)
{
thirdExcluded = true;
if (firstExcluded)
smallest = b;
else
smallest = a;
}
else if (smallest == d)
{
fourthExcluded = true;
if (firstExcluded)
smallest = b;
else
smallest = a;
}
if (!firstExcluded && a<smallest)
smallest = a;
if (!secondExcluded && b<smallest)
smallest = b;
if (!thirdExcluded && c<smallest)
smallest = c;
if (!fourthExcluded && d<smallest)
smallest = d;
cout << smallest;
if (smallest == a)
firstExcluded = true;
else if (smallest == b)
secondExcluded = true;
else if (smallest == c)
thirdExcluded = true;
else if (smallest == d)
fourthExcluded = true;
if (!firstExcluded)
{
firstExcluded = true;
smallest = a;
}
if (!secondExcluded)
{
secondExcluded = true;
smallest = b;
}
if (!thirdExcluded)
{
thirdExcluded = true;
smallest = c;
}
if (!fourthExcluded)
{
fourthExcluded = true;
smallest = d;
}
cout << smallest;
return 0;
}
0 Bob huginstien · March 8, 2015
#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)
{
firstExcluded = true;
smallestSoFar = b;
}
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;
if (!secondExcluded && b<smallestSoFar)
smallestSoFar = b;
if (!thirdExcluded && c<smallestSoFar)
smallestSoFar = c;
if (!fourthExcluded && d<smallestSoFar)
smallestSoFar = d;
cout << smallestSoFar;

if (!firstExcluded && a<smallestSoFar)
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;
if (secondExcluded)
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;
}

cout << smallestSoFar;



return 0;
}
+1 Dol Lod · March 8, 2015
Here is the updated code that does everything and is commented for an explanation. 


#include <iostream>
#include <algorithm>


using namespace std;

int main()
{
int a;
int b;
int c;
int d;
int smallest; //will be used to hold the smallest value
int biggest; //holds the largest value
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;
biggest=a;
if(b>biggest)
biggest=b;
if(c>biggest)
biggest=c;
if(d>biggest)
biggest=d;
//this code figures out the largest value so I have something I can generically assign smallest back to 
smallest = a; //this just picks a as the starting point 
//first number
if (!secondExcluded && b<smallest) 
smallest = b;
if (!thirdExcluded && c<smallest)
smallest = c;
if (!fourthExcluded && d<smallest)
smallest = d;
cout << smallest; //this part over here just prints out the smallest number
//second number start
if (!firstExcluded && smallest == a) //this checks if the first number has been excluded and if not excludes it. This is done for all 4 numbers.
firstExcluded = true;
else if (!secondExcluded && smallest == b)
secondExcluded = true;
else if (!thirdExcluded && smallest == c)
thirdExcluded = true;
else if (!fourthExcluded && smallest == d)
fourthExcluded = true;
smallest=biggest; //this resets the smallest so it will actually change its value to the next smallest taking into account for exclusion 
if (!firstExcluded && a<smallest) //this checks if the value was included already and then changes smallest if it hasn't been included
smallest = a;
if (!secondExcluded && b<smallest)
smallest = b;
if (!thirdExcluded && c<smallest)
smallest = c;
if (!fourthExcluded && d<smallest)
smallest = d;
cout << smallest; //this prints the second smallest number
//third number start
if (!firstExcluded && smallest == a) //this is just a repeat of what was above to exclude new values
firstExcluded = true;
else if (!secondExcluded && smallest == b)
secondExcluded = true;
else if (!thirdExcluded && smallest == c)
thirdExcluded = true;
else if (!fourthExcluded && smallest == d)
fourthExcluded = true;
smallest=biggest; //this resets smallest again 
if (!firstExcluded && a<smallest) //this is a repeat of what was above for getting the next number
smallest = a;
if (!secondExcluded && b<smallest)
smallest = b;
if (!thirdExcluded && c<smallest)
smallest = c;
if (!fourthExcluded && d<smallest)
smallest = d;
cout << smallest; //this prints the third smallest number 
if (!firstExcluded && smallest == a)
firstExcluded=true;
else if (!secondExcluded && smallest == b)
secondExcluded=true;
else if (!thirdExcluded && smallest == c)
thirdExcluded=true;
else if (!fourthExcluded && smallest == d)
fourthExcluded=true;
if(!firstExcluded) //these last 8 lines are unecessary, but it is good for style to complete everything 
firstExcluded=true;
if(!secondExcluded)
secondExcluded=true;
if(!thirdExcluded)
thirdExcluded=true;
if(!fourthExcluded)
fourthExcluded=true;
cout<<biggest; //the biggest number is the only number that has not been printed yet so it is printed 
return 0;
}
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) // use else here
smallestSofar = c;
if (d<smallestSofar) // use else here
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;
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)
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)
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;
else if (smallestSofar == b)
secondExcluded = true;
else if (smallestSofar == c)
thirdExcluded = true;
else if (smallestSofar == d)
fourthExcluded = true;
if (!firstExcluded)
{
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;
}
  • 1
  • 2

C++

107,200 followers
About

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

Links
Moderators
Bucky Roberts Administrator