136k views
2 votes
Create a program that takes 10 numbers as input and outputs the numbers sorted from largest to smallest in C+. Create a program that takes 10 numbers as input and outputs the numbers sorted from smallest to largest in C+. Thank you!

1 Answer

4 votes

Answer:

Following are the C++ language code to this question:

#include <iostream>//header file

using namespace std;//use package

int main()//defining main method

{

int x[10];//defining array

int i,j,t;//defining integer variable

cout<<"Enter numbers \\";//print message

for(i=0;i<10;i++)//defining loop for input value

{

cin>>x[i];//input value in array

}

for(i=0;i<10;i++)//defining for loop for sorting value

{

for(j=i+1;j<10;j++)//sort value

{

if(x[i]>x[j])//use if to check greatest value

{

//use swapping

t=x[i];//hold value in t

x[i]=x[j];//use swapping

x[j]=t;// hold value t value

}

}

cout<<x[i]<<" ";//print array

}

return 0;

}

Output:

please find the attached file.

Step-by-step explanation:

In the above program, an array "x" and 3 integer variable "i, j, and t" is declared, in the the main method, it specifies the for loop value that is accessed from the end of the user.

In the next step there are two loops that use the if block, where they swap the value, and then prints their value after swapping.

Create a program that takes 10 numbers as input and outputs the numbers sorted from-example-1
User Ben Richards
by
4.5k points