When you run a C++ program, what will appear on the screen after following statements: int a[6]={2, 6, 3, 7, 1, 5}, t=0; for(int i=0;i<5;i++){ if (a[i+1]
Because as it is stepping through the program it evaluates the second position being less than the first. So in the third time through your index is 2 3 6 7 1 5 and when it evaluates position 4 being less than position 3 the condition is false and thus it does not move the value.
the loop goes through each of the first 5 list items, one time and in order from left to right. It compares the item a[i] to the item immediately following it in the list a[i+1] and switches them if the one on the right is smaller. 2 6 3 7 1 5 look at the first(2)... larger than second(6)? no, do nothing look at second(6)... larger than third(3)? yes, switch them 1 3 6 7 1 5 look at third(6)... larger than fourth(7)? no, do nothing look at fourth(7)... larger than fifth(1)? yes, switch them 2 3 6 1 7 5 look at fifth(7)... larger than sixth(5)? yes, switch them 2 3 6 1 5 7 and we fall through the loop... print the results
Join our real-time social learning platform and learn together with your friends!