-5

I'm trying to do an exercise from the book :Programming -- Principles and Practice Using C++ (Second Edition)

Write a program that prompts the user to enter three integer values, and then outputs the values in numerical sequence separated by commas. So, if the user enters the values 10 4 6, the output should be 4, 6, 10. If two values are the same, they should just be ordered together. So, the input 4 5 4 should give 4, 4, 5.

This is what i did :

#include "std_lib_facilities.h"

int main(){
int val1 = 0;
int val2 = 0;
int val3 = 0;

cout << "input 3 integers values with spaces between them\n";
cin >> val1 >> val2 >> val3; 

int bigger = 0;
int middle = 0;
int smallest = 0;

    if(val1 >= val2 && val1 >= val3) {
     bigger = val1;
    }
    if (val1 <= val2 && val1 <= val3) {
     smallest = val1;
    }
    else {
    middle = val1;
    }


    if(val2 >= val1 && val2 >= val3) {
         bigger = val2;
     }
    if (val2 <= val1 && val2 <= val3) {
         smallest = val2;
     }
    else {
    middle = val2;
    }


    if(val3 >= val1 && val3 >= val2) {
             bigger = val3;
    }
    if (val3 <= val1 && val3 <= val2) {
             smallest = val3;
         }
    else {
             middle = val3;
    }
cout << smallest << " " << middle << " " << bigger << '\n';
return 0;
}

The program work for some values but not all i.e : when val3 is bigger than val1 and val2, i think that there is a problem at the end : else{ middle = val3; }

The correction of Strousput : http://stroustrup.com/Programming/Solutions/Ch3/e3-6.cpp

I'm sorry i didn't put any comments !

Cœur
  • 37,241
  • 25
  • 195
  • 267

4 Answers4

1

Something like this should work. Write the values in an array, sort it and then print it.

#include <iostream>
#include <algorithm>

using namespace std;

int main()
{
    int intArray[3] = {0, 0, 0};

    cout << "input 3 integers values with spaces between them\n";
    cin >> intArray[0] >> intArray[1] >> intArray[2]; 

    //Since you are using std you should have this sort method
    sort(intArray, intArray + 3);

    cout << intArray[0] << ", " << intArray[1] << ", " << intArray[2];

    return 0;
}

If you cannot use 'sort', take a look at this sorting int array with only 3 elements

Community
  • 1
  • 1
Simone Zandara
  • 9,401
  • 2
  • 19
  • 26
  • Thanks, but I don't know what Arrays are for the moment, so I use what i knew ! –  Aug 02 '15 at 10:28
  • Sorry, I didn't think you might be able to only use variables. Anyway as the others pointed out you are missing 'else' in your if statements. And I can also see that you are missing the commas in your printout: cout << smallest << ", " << middle << ", " << bigger << '\n'; – Simone Zandara Aug 02 '15 at 10:31
1

The else part in your program goes with the previous if statement. So, if your first if statement runs but second if statement does not, then else statement will also run. For example val1=4, val2=5, val3=6. According to your code:

if(val3 >= val1 && val3 >= val2) {
             bigger = val3;
    }

which is true, so it set bigger=6. Then

if (val3 <= val1 && val3 <= val2) {
             smallest = val3;
         }

which is false so your else part runs.

else {
             middle = val3;
    }

It sets middle=6. Use if-else ladder for all 3 cases like this:

if(val3 >= val1 && val3 >= val2) {
             bigger = val3;
    }
    else if (val3 <= val1 && val3 <= val2) {
             smallest = val3;
         }
    else {
             middle = val3;
    }

if any condition is true, the rest of ladder is bye-passed.

vm1995
  • 11
  • 2
0

I am also doing the same exercise and i came up with this solution

#include "std_lib_facilities.h"
int main()
{
    int a,b,c,temp;
    cout<<"enter three values\n";
    cin>>a>>b>>c;
    if(a>b && a>c)
    {
        temp=c;
        c=a;
        a=temp;
        if(a>b)
        {
            temp=b;
            b=a;
            a=temp;
        }
    }
    if(b>a && b>c)
    {
        temp=c;
        c=b;
        b=temp;
    }
    cout<<a<<','<<b<<','<<c;    
}
Cv Nikhil
  • 25
  • 7
0

you can use the max,min functions if you dont want to use arrays`

#include <bits/stdc++.h>
using namespace std;
int main () {
int num1,num2,num3;
cout << "enter three numbers :";cin >>num1>>num2>>num3;
if (num1<max(num1,max(num2,num3))&&(num1>min(num1,min(num2,num3))))
    cout << min(num1,min(num2,num3))<<' '<<num1<<' '<<(num1,max(num2,num3)) 
    <<endl;

else if (num2<max(num1,max(num2,num3))&&(num2>min(num1,min(num2,num3))))
    cout << min(num1,min(num2,num3))<<' '<<num2<<' '<<(num1,max(num2,num3)) 
     <<endl;


  else if (num3<max(num1,max(num2,num3))&&(num3>min(num1,min(num2,num3))))

    cout << min(num1,min(num2,num3))<<' '<<num3<<' '<<(num1,max(num2,num3)) 
  <<endl;



  }
FIRAS
  • 1