Program9

Hello,

I decided to write a program as follows:

Write a program that prompts the user to enter three integer values, and then outputs the values in numerical secuence 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.

Now I have know idea how to do this smartly (just few lines of code) but I have experimented sorting each case separately. Is there an operator for and? I have used the & operator which means or.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include<iostream>
#include<string>
#include<algorithm>
#include<cmath>

using namespace std;

int main()

{

int a=0;
int b=0;
int c=0;
cout<<"Enter three integer values:\n";
cin>>a>>b>>c;

if (a<b && b<c) { cout<<" "<< a <<", "<< b <<", "<< c <<" "; }

else if (a==b && b!=c && c<a)

            {
                  cout<<" "<< c <<", "<< a <<", "<< b <<" "; 
            }

else if (a!=b && b==c && a<b)
          {
              cout<<""<< a <<", "<< b <<", "<< c <<" ";
          }

 cout << endl; 
    return 0;

}

Last edited on
I have used the & operator which means or.
You have use && operator which means and. Like "left hand condition and right hand condition must be true for expression to be true".
There is logical or operator: || (or or if you wish)
Yep, that might be true but I haven`t touched the code at all. I don`t think I need the or-statement at all if I can cover all the possibilities with and-statements.

Now however my program acts as follows:

Enter three integer values:
10 4 6

Followed by empty space!
10 4 6 doesn't fit any of the conditions you've defined so far.

With three numbers, it's not that bad to figure out all the possible sequences they could be in, but with a larger set of numbers, I'd want to use some sort of sorting algorithm.

If you were going to sort the numbers into low to high order, they could be in order different ways.

abc, acb, bac, bca, cab, cba


OT - but when you include a file at the top of the program, you're essentially cutting and pasting the contents of that file into your program, so if you include files you don't need, you're making your program bigger than it has to be.
Hi @pacman here is 2
of the 6 possible combinations;
(as @wildblue said) complete the rest
by your self;

To sort numbers, it's a better idea to use
containers (arrays,vectors) + sort algorithms;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
//SortValues.cpp
//##


#include <iostream>

using namespace std;


int main(){

        int a,b,c;


        cout<<"Enter three integer values:\n";
        cin>>a>>b>>c;

        if(a<b&&b<c)cout<<a<<' '<<b<<' '<<c;
        else if (a<b&&b>c)cout<<a<<' '<<c<<' '<<b;



        cout<<endl;

return 0; //indicates success
}//@end of main
Enter three integer values:
3 5 7
3 5 7

Enter three integer values:
3 7 5
3 5 7


EDIT: I am not considering repeated numbers
Last edited on
Topic archived. No new replies allowed.