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

 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 #include 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;}

## Replies

- page 1

 Dol Lod · March 8, 2015 Here is much simpler code. No need for that many complications. I adopted the idea from merge sort. #include #include 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#include 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;} 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 #include 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#include 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#include 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#include 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#include 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
• 1
• 2

## C++

126,473 followers