Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

can someone please correct me ? i have to put in order 3 any numbers and i dont know what i do wrong.

+2 votes
asked Feb 20 by slendy (140 points)
#include <iostream>
using namespace std;
int main () {
int a, b, c;
cin>>a>>b>>c;
if (a=>b=>c) {
     cout<<c;
    cout<<" ";
     cout<<b;
    cout<<" ";
     cout<<a;
    cout<<" ";
}
 if else (a=>c=>b) {
      cout<<b;
    cout<<" ";
     cout<<c;
    cout<<" ";
     cout<<a;
    cout<<" ";
 }
 if  (b=>a=>c) {
      cout<<c;
    cout<<" ";
     cout<<a;
    cout<<" ";
     cout<<b;
    cout<<" ";
 }
 if else (b=>c=>a) {
      cout<<a;
    cout<<" ";
     cout<<c;
    cout<<" ";
     cout<<b;
    cout<<" ";
 }
  if (c=>a=>b) {
     cout<<b;
    cout<<" ";
     cout<<a;
    cout<<" ";
     cout<<c;
    cout<<" ";
  }
  if else (c=>b=>a) {
       cout<<a;
    cout<<" ";
     cout<<b;
    cout<<" ";
     cout<<c;
    cout<<" ";

  }
  return 0;
}

2 Answers

–1 vote
answered Feb 21 by Shubham-09-09 (120 points)
#include <iostream>
using namespace std;
int main ()
{
    int a, b, c;
    cin>>a>>b>>c;
    if(a>b && b>c )
    {
        cout<<a<<endl<<b<<endl<<c;
    }
    else if(a>b && b<c )
    {
        cout<<a<<endl<<c<<endl<<b;
    }
    else if(b>a &&  c>a)
    {
        cout<<b<<endl<<c<<endl<<a;
    }
    else if(b>a  && c<a)
    {
        cout<<b<<endl<<a<<endl<<c;
    }
    else if(c>a && a>b)
    {
        cout<<c<<endl<<a<<endl<<b;
    }
    else if(c>a && a<b)
    {
        cout<<c<<endl<<b<<endl<<a;
    }

}

this is a kind of format you can use to compare values where > should be before = and for two conditions in one step use &&.i just created basic now you can edit according to you and condition is else if not if else
commented Feb 25 by Peter Minarik (19,180 points)
This is wrong.

I can easily provide input that would break your logic of returning numbers in ascending orders. E.g.: 5, 3, 7. It would return 5, 7, 3; which is clearly not in ascending order.

The problem is that you did apply the transitivity of the less than operator wrong. You should have done a > c && c > b (, instead of a > b && B < c).

Please, read https://en.wikipedia.org/wiki/Inequality_(mathematics)#Transitivity
0 votes
answered Feb 22 by Peter Minarik (19,180 points)
edited Feb 25 by Peter Minarik

In Mathematics you can make expressions like a >= b >= c, but in most programming languages, you cannot. You should do

if (a >= b && b >= c)
{
    // ...
}

instead.

Also, in the last condition, you have the if and the else in the wrong order:

if (c => a && a => b)
{
    // ...
}
else if (c => b && b => a)
{
    // ...
}

An alternative is to keep only the else, and you remove the if and the condition, as c >= b >= a is the only remaining case, so the else should cover it, no need to check whether this is happening with the if statement.

After these changes, I think you code would work fine.

Also, I'd suggest looking into various sorting algorithms. You'll make a good use of them.

https://en.wikipedia.org/wiki/Sorting_algorithm#Popular_sorting_algorithms

Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...