Hi I'm new to programming and currently stuck at trying to get the output of the program to show in ascending order ,
int foo[10];
for (int i=0; i<=10; i++)
{
cout<<"Please enter a number: "<<endl;
cin foo[];
}
I'm stuck at trying to make it display in ascending order, I tried different methods but to my luck fails me I've seen tons of examples for this question but unfortunately for me I'm only limited to answering this in C++ please can anyone help.
If your assignment allows you to use standard algorithms, then it's simply
std::sort(std::begin(foo), std::end(foo));
followed by a loop to print them.
If the object of the exercise is to write your own sorting algorithm, then you should research them yourself .
Firstly, you should run your for (int i = 0; i < 10; i++)
instead of for (int i = 0; i <= 10; i++)
, because the array has 0-9 indexes, rather than 0-10. Secondly, the syntax for cin foo[]
is cin >> foo[i];
in this case.
For the sort, you could look into std::sort, or you could look into some sorts yourself, I would recommend starting with bubble sort, selection sort or insertion sort, as they are the easiest ones to implement.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.