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 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;
}
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;
}
  • 1
  • 2

C++

107,036 followers
About

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

Links
Moderators
Bucky Roberts Administrator