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.

how to get the ascending and descending of 4 integers with out using array,&&,or only using function

+4 votes
asked Nov 8, 2022 by Biruk Nigussie (160 points)
#include <iostream>

using namespace std;

void sort4Nums(int,int,int,int,short);

int main()

{

 char tr;

 short option;

 do

 {

  int num1,num2,num3,num4;

  system("cls");

  cout<<"Enter four integers : \n";

  cin>>num1>>num2>>num3>>num4;

  cout<<"How would you like to display the result ? \n";

  cout<<"1. Ascending order \n2. Descending order \n";

  cin>>option;

  switch(option)

  {

   case 1:

    cout<<"Ascending order : \n";

    sort4Nums(num1,num2,num3,num4,1);

    break;

   case 2:

    cout<<"Descending : \n";

    sort4Nums(num1,num2,num3,num4,2);

    break;

   default:

    cout<<"\n Invalid option. \n";

    break;

  }

  cout<<"\nPress Y/y to try again.\n";

  cin>>tr;

 }while(tr=='y' or tr=='Y');

 system("pause");

return 0;

}

void sort4Nums(int n1,int n2,int n3,int n4,short op)

{

 if(n1>=n2)

 {

  if(n2>=n3)

  {

  if(n3>=n4)

  {

   if(op==1)

    cout<<n4<<" => "<<n3<<" => "<<n2<<" => "<<n1;

   else   

    cout<<n1<<" => "<<n2<<" => "<<n3<<" => "<<n4;

  }

  else if(n1>=n4)

  {

   if(op==1)

   cout<<n2<<" => "<<n3<<" => "<<n4<<" => "<<n1;

   else

   cout<<n1<<" => "<<n4<<" => "<<n3<<" => "<<n2;

  }

  else

  {

   if(op==1)

   cout<<n2<<" => "<<n1<<" => "<<n4<<" => "<<n3;

   else

   cout<<n3<<" => "<<n4<<" => "<<n1<<" => "<<n2;

  }

 }

 else if(n1>=n4)

 {

  if(op==1)

  cout<<n4<<" => "<<n3<<" => "<<n1<<" => "<<n2;

  else

  cout<<n2<<" => "<<n1<<" => "<<n3<<" => "<<n4;

 }

 else if(n2>=n4)

 {

  if(op==1)

  cout<<n1<<" => "<<n3<<" => "<<n4<<" => "<<n2;

  else

  cout<<n2<<" => "<<n4<<" => "<<n3<<" => "<<n1;

 }

 else

 {

  if(op==1)

  cout<<n1<<" => "<<n2<<" => "<<n3<<" => "<<n4;

  else

  cout<<n4<<" => "<<n3<<" => "<<n2<<" => "<<n1;

 }

}

}

1 Answer

0 votes
answered Nov 9, 2022 by Peter Minarik (86,180 points)

I'm not sure about the restrictions. Maybe it's a special assignment to learn about the std::sort() method?

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.
...