+ 3

What's the wrong with this code?

I want to write a C++ program that sort the elements of a one-dimentional array in ascending order, but the output is wrong. This is the code: #include <iostream.h> int main() { int a[10]={3,6,1,4,9,6,3,1,2,8}; int b, i,j; b=a[0]; for(i=0;i<10;i++) { j=i+1; if (a[i]>a[j]) { b=a[i]; a[i]=a[j]; a[j]=b; cout<<a[i]; } } return 0; }

19th Feb 2019, 9:30 PM
Janna Al-Ward
Janna Al-Ward - avatar
1 Respuesta
+ 2
Try to sort the first three numbers in your mind with your method. You can see that you will sort only one the biggest number 6. You need to apply the same sorting method for remaining two numbers 3,1. For 10 numbers it will be 9 times. Also you can save your code on Sololearn code playground and paste the link here. It help others people to help you more quickly.
19th Feb 2019, 10:04 PM
Earl_Grey
Earl_Grey - avatar