'can somebody explain the value of array numbers[3] and numbers[6] in following c code? [closed]
Sample Program Using Arrays here we need to find final output of code but I can not get the same output as the compiler gets. I am getting wrong output in numbers[3] and numbers[6].
#include <stdlib.h>
#include <stdio.h>
int main(void)
{
int numbers[10];
int i, index = 2;
for (i = 0; i < 10; i++)
numbers[i] = i * 10;
numbers[8] = 25;
numbers[5] = numbers[9] / 3;
numbers[4] += numbers[2] / numbers[1];
numbers[index] = 5;
++numbers[index];
numbers[numbers[index++]] = 100;
numbers[index] = numbers[numbers[index + 1] / 7]--;
for (index = 0; index < 10; index++)
printf("numbers[ %d ] = %d\n", index, numbers[index]);
return 0;
}
Solution 1:[1]
- The easiest thing is to display the value of
indexandnumbersin your debugger then execute each line till you are surprised. If your debugger supports it, you can also run your program in reverse. - You could modify your program with a function that prints out
indexandnumbersthen run that function after every significant change. - If you want to do it manually you could reason about the program in reverse by hand. From the output of your program:
numbers[ 3 ] = 100
numbers[ 6 ] = 99
so let's start with the last assignment:
numbers[ index ] = numbers[ numbers[ index + 1 ] / 7 ]-- =>
numbers[3] = numbers[numbers[4] / 7]-- =>
= numbers[(40 + 20 / 10) / 7 ]-- =>
= numbers[6]--
As index = 3 from the post increment in numbers[ numbers[ index++ ] ] = 100; of the initial value of 2 set with int i, index = 2. numbers[ 4 ] += numbers[ 2 ] / numbers[ 1 ]; which just uses the initial values set in the loop numbers[ i ] = i * 10.
We know, from the output, that numbers[3] the assigned the value 100 and numbers[6] is decremented to 99. The only place we assign the value 100 is the previous line numbers[ numbers[ index++ ] ] = 100 so we just need to convince ourselves that the left hand side is numbers[6]:
numbers[ numbers[ index++ ] ] =>
numbers[numbers[2]] =>
numbers[6]
As index = 2 from the initial assignment discussed above, and numbers[2] = 6 from ++numbers[ index ] from the previous value of 5 set with numbers[ index ] = 5;.
The moral of the story is to prefer single assignment :-)
Solution 2:[2]
All the magic has happened in the last two lines:
numbers[numbers[index++]] = 100;
numbers[index] = numbers[numbers[index + 1] / 7]--;
I would suggest you to use a debugger and add numbers[3] and numbers[6] to the watcher. And then set a breakpoint at this numbers[numbers[index++]] = 100; line.
number[6]
indexwas 2, sonumbers[index++]will give6due to these two lines
numbers[index] = 5; // numbers[2] = 5
++numbers[index]; // numbers[2] = 5 + 1 = 6
- Now,
numbers[numbers[index++]] = 100;will becomenumbers[6] = 100; - Now,
index++will execute making it3 - Hence,
numbers[6]is now100
numbers[3]
- Now decode this line:
numbers[index] = numbers[numbers[index + 1] / 7]--;
indexis now3, sonumbers[index + 1]is equal tonumbers[4]which is equal to 42 which when divided by 7 becomes6- Now,
numbers[3] = numbers[6] = 100 - After
numbers[3]becomes 100, thennumbers[6]will be decresed by 1 due to post-decrement operator-- - Hence,
numbers[3] = 100andnumbers[6] = 99
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 |
