0
Can you please help me?
The code c++ does not give correct results for the ascending arrangement of an array (one dimension), what is fail here? int arr[4] , val=0; cout<<"Enter Value Array:\n"<<"--------------------\n"; for(int i=0 ; i<4 ; i++) cin>>arr[i]; for(int i=0 ; i<4 ; i++){ for(int j=1 ; j<4; j++) if (arr[j]<arr[i]){ val=arr[j]; arr[j]=arr[i]; arr[i]=val; } } for(int i=0 ; i<4 ; i++) cout<<arr[i]<<"\t";
3 Answers
0
for(int i=0 ; i<4 ; i++){
for(int j=0 ; j<4; j++) // initialise 'j' with 0.
if (arr[j]<arr[i]){
val=arr[j];
arr[j]=arr[i];
arr[i]=val;
}
}
0
thank's
0
You can also initialise the 'i' to 1, together with the 'j' to 0;