+ 1

Why this code is taking garbage values ?

I am running this code, for 7 values and it is fine (I changed it to take 7 values at that time). But when I am trying it for 10 values, it is taking garbage values. Plz help me out 🙏 #include <iostream> using namespace std; int main() { int i,j,temp; int arr[10]; cout<<"Enter the ten numbers that are to be sorted : "<<endl; for(i=0;i<10;i++) { cin>>arr[i]; } for(i=0;i<10;i++) { for(j=0;j<10-i;j++) { if(arr[j]<arr[j+1]) { temp = arr[j]; arr[j]=arr[j+1]; arr[j+1]=temp; } } } cout<<"The sorted numbers are : "<<endl; for(i=0;i<10;i++) { cout<<arr[i]<<" "; } return 0; } Plz help 🙏

20th Dec 2021, 4:02 PM
Jainil Raval
Jainil Raval - avatar
6 odpowiedzi
+ 3
Put your code in code playground and add link (+ button) in the question description. This way, we can run it and see the problem.
20th Dec 2021, 7:55 PM
Emerson Prado
Emerson Prado - avatar
+ 2
Emerson Prado Here it is This is the actual code what I've written without any corrections https://code.sololearn.com/c83wuR1FjbKz/?ref=app
29th Dec 2021, 11:39 AM
Jainil Raval
Jainil Raval - avatar
+ 1
Martin Taylor Thanks a lot for the help 🙏🙏
29th Dec 2021, 11:36 AM
Jainil Raval
Jainil Raval - avatar
+ 1
Krishnendu Dey Good, now I see it. I was to write about the invalid array index, but Martin Taylor already did it and more. Stick with him.
30th Dec 2021, 12:52 AM
Emerson Prado
Emerson Prado - avatar
+ 1
Ok Thanks Emerson Prado
31st Dec 2021, 2:43 PM
Jainil Raval
Jainil Raval - avatar