# 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 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 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;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;} Bob huginstien · March 8, 2015 Yes he wants us to use the function bool in there. 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 4The 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. Bob huginstien · March 8, 2015 #include #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 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++

129,872 followers