Ask your own question, for FREE!
Computer Science 13 Online
OpenStudy (maram):

11. What is the output of the following section of a program? int a[8] = {2, 5, 1, 0, 0, 0, 0, 0}; for(int i=7;i>0;i--) a[i]=a[i -1]; for(int k=0;k<8;k++) cout<

OpenStudy (anonymous):

I am not sure, i'd like to know the proper reason too, but i think its because of a[i]=a[i-1].

OpenStudy (anonymous):

the loop goes from 7 down to 1 and it copies the previous (in the array) value each time you can get a much better answer to questions like this by either 1) single stepping through the code if your environment supports it OR 2) setting a breakpoint in the loop and using the debugger to browse

OpenStudy (espex):

Because your last iteration through the loop, when i = 1, it copies a[0] to a[1] and exits. So all of the values are moved over one location--replacing the original value--with the exception of a[0] because there is no mechanism that replaces the initial value.

OpenStudy (anonymous):

in the loop for(i=7;i>0;i--) on encountering the statement a[i]=a[i-1] ie, a[7]=a[6],a[6]=a[5],a[5]=a[4],a[4]=a[3],a[3]=a[2],a[2]=a[1],a[1]=a[0] the array will come in the order 2 2 5 1 0 0 0 0 after that in the next loop v r just printing the array

OpenStudy (anonymous):

before execution of first loop:a[7]=0,a[6]=0,a[5]=0,a[4]=0,a[3]=0,a[2]=1,a[1]=5,a[0]=2 after the first for loop: a[7]=a[6]=0 similarly, a[3]=a[2]=1 a[2]=a[1]=5 a[1]=a[0]=2 here loop exits and value of a[0] is unchanged so the result will come a[0]=2, a[1]=2, a[2]=5 .........

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!