This is a question from a practice test that I do not fully understand.
For the code fragment
int i = 0, j = 0, k = 0;
for (i=-1; i<=10; ++i){
j = i; ++k;
}
I am asked to find the values of the variables after executing the code.
The answers are:
i = 11 j = 10 k = 12
I don't understand how, can someone please help?
Understanding the value of i after the loop is very simple, much simpler than the sorts of other answers here. The loop condition is i<=10 ... in order for the loop to terminate, that condition must be false. Clearly, the value of i that makes that false is 11.
The value of j at the end of the loop is the previous value of i, which is 10, and the value of k is the number of times the loop executed, which is 1 (for -1) + 1 (for 0) + 10 (for 1 thru 10) = 12.
i must be <= 10, so it is 11 to exit the loop and inside the last iteration of the loop, i = 10 = j. k is 1 after the first iteration, while i is -1. Running through the loop, you'll see:
k = 1, i = -1
k = 2, i = 0
k = 3, i = 1
k = 4, i = 2
k = 5, i = 3
k = 6, i = 4
k = 7, i = 5
k = 8, i = 6
k = 9, i = 7
k = 10, i = 8
k = 11, i = 9
k = 12, i = 10
Therefore k = 12
Here are the steps:
When the loop begins, all three variables are zero.
The loop initializer sets i to minus 1.
Loop test: i <= 10 is true, so loop is entered.
Inside the loop, j is set to i, so j is also minus 1.
k is incremented, so k becomes 1.
the iteration ends; i is incremented because of the ++i, so i becomes 0.
Loop test: since i is zero, i <= 10 is true, so the loop is entered again.
In this way, the loop continues, changing j, k, and i in that order. So when i becomes 10, j will be 9 and k 11. At that point:
The loop is entered for the last time.
j becomes 10 as well; k becomes 12
Then i gets incremented to 11. The loop condition i <= 10 is false, and the loop terminates.
So i is 11. j is 10, k is 12 when the loop terminates.
The key point is, after the first pass, every time the loop is entered, j is one less than i, and k is one greater than i. When the loop terminates, this is still the case.
for (i=-1; i<=10; ++i){
j = i; ++k;
}
Here is the loop :
i = i +1; <-------+
| |
check condition!------|--+
| | |
j = i; | |
| | |
k++;----------------+ |
| |
+<--------------------+
|
other code
at last loop
i = 10
condition == true
j = 10;
k = 12;
Then
i= i+1 means i = 11 but the condition show false! loop end.
Take three variables separate.
You can see the variable k would be incremented , the number of times the loop is executed.
The no. of time sit would be executed from -1 to 10 it would have done 12 iterations
k = 1, i = -1, j=-1
k = 2, i = 0, j=0
k = 3, i = 1, j=1
k = 4, i = 2, j=2
k = 5, i = 3, j=3
k = 6, i = 4, j=4
k = 7, i = 5, j=5
k = 8, i = 6, j=6
k = 9, i = 7, j=7
k = 10, i = 8, j=8
k = 11, i = 9, j=9
k = 12, i = 10, j=10
After This i has reached its limit, but it will first increment and then check,
hence i=11, k=12 and j to a one less than the value of i i.e j= 10
Related
I am new to the C programming. I came across the for loop example.
I don't understand some part of the loop. Output is 8. I don't get how b is incremented till 4.
Here is my code:
int a = 4;
int b = 2;
int result = 0;
for(int count = 0; count != b; count++) {
result = result + a;
}
printf("a times b is %i\n", result);
return 0;
Sometimes, the easiest thing to do is to get the program to explain itself:
#include <stdio.h>
#include <stdlib.h>
int main(void){
int a = 4;
int b = 2;
int result = 0;
for(int count = 0; count != b; count++) {
printf("a = %3d, b = %3d, count = %3d, result = %3d\n", a, b, count, result);
result = result + a;
printf("a = %3d, b = %3d, count = %3d, result = %3d\n", a, b, count, result);
}
printf("a = %3d, b = %3d\n", a, b);
printf("a times b is %i\n", result);
return 0;
}
Output
a = 4, b = 2, count = 0, result = 0
a = 4, b = 2, count = 0, result = 4
a = 4, b = 2, count = 1, result = 4
a = 4, b = 2, count = 1, result = 8
a = 4, b = 2
a times b is 8
As you can see, b does not change. count changes and the loop is exited when count is equal to b.
The variable b is not incremented, the variable count starts from 0 and incremented in the for loop. When the count variable becomes 2 the loop ends. So, the loop runs twice (count 0 and count 1) and the result is 4 + 4 = 8.
At first, count = 0 which is not the same with b = 2 and so the loop starts. By doing so, the first iteration of loop gives a result,
result = 0 + 4
Since the loop ends and count variable should proceed count++ which means count = count + 1. Thus, count = 0 + 1 = 1 where it is not the same valeu with b = 2 again.
Proceed the loop again,
result = 4 + 4 # where the first number came from the result of first loop
Now count++ makes count = 2 which is now same value with b = 2. Then, the condition of the loop is not matched, count != 2, no further loop and print the value result = 8.
it will be executed 2 times
count= 0 ==> result=0+4
and count =1 ==> result=4+4 ==> result=8
when it will reach count= 2 the 2!=2 part will be false and we will exit the for loop
The loop runs only two times we can dry run the loop like that:
Initial values for variables: -> a = 4, b = 2, count = 0 and result = 0
loop first run ->
count = 0
count != 2 -> that is -> 0 != 2 => true
result = result + a -> 0 + 4 = 4
second run ->
count = 1
count != b -> that is -> 1 != 2 => true
result = result + a -> 4 + 4 = 8
third run ->
count = 2
count != b -> that is -> 2 != 2 => false
stop loop.
And the final resultent values are as follow: a= 4, b= 2 , count = 2 and result = 8
I tried this example of array and post increment/ pre increment on it's elements
#include<stdio.h>
int main()
{
int j[5] = {5, 1, 15, 20, 25};
int k , l, m, n;
n = ++j[1];
k = ++j[1];
l = j[1]++;
m = j[k++];
printf("\n%d, %d, %d, %d", n, k, l, m );
return 0;
}
here the output is :
2, 4, 3, 20
and if i change the order of n and k
ie instead of
n = ++j[1];
k = ++j[1];
i write
k = ++j[1];
n = ++j[1];
The output becomes :
3, 3, 3, 15
I tried this on mingw compiler on windows10 and also on Kali Linux's GCC...
Same problem.
It is just like taking different variable name alters the output of program.
What might be the cause?
Thanks Everyone for helping me out with this question.
I didnt take the last post increment of k under consideration.
The results would be same of i would have changed
m=j[k++]
with
m = j[n++]
Why would not they? The values of k, n and m are depending on their placement in the code.
For example,
m = j[k++];
this will be affected by the current value of k.
To add a bit about pre and post increment operator, quoting C11,
Chapter §6.5.3.1, pre-increment
The value of the operand of the prefix ++ operator is incremented. The result is the new
value of the operand after incrementation. [...]
Chapter §6.5.2.4, post-increment
The result of the postfix ++ operator is the value of the operand. As a side effect, the
value of the operand object is incremented (that is, the value 1 of the appropriate type is
added to it). [....]
I have added some print statements, to check how the values get affected after every statement. You can additionally print the whole array to visualize the change even more.
int j[5] = {5, 1, 15, 20, 25};
int k , l, m, n;
printf("j[1] = %d\n", j[1]);
k = ++j[1];
printf("j[1] = %d, k = %d\n", j[1], k);
n = ++j[1];
printf("j[1] = %d, n = %d\n", j[1], n);
l = j[1]++;
printf("j[1] = %d, l = %d\n", j[1], l);
m = j[k++];
printf("m= %d, k = %d\n", m, k);
printf("\n%d, %d, %d, %d", n, k, l, m );
and the output is:
j[1] = 1
j[1] = 2, k = 2
j[1] = 3, n = 3
j[1] = 4, l = 3
m= 15, k = 3
3, 3, 3, 15
In the first case:
n = ++j[1]; //j[1] is incremented and is now 2, so n is 2
k = ++j[1]; //j[1] is incremented again and is now 3, so k is 3
l = j[1]++; //j[1] is incremented yet again and is now 4, but l is 3 as it is post-increment
m = j[k++]; //m is j[3] which is 20, and k will become 4 as it is post-incremented
So output of n, k, l, m will be 2, 4, 3, 20
In the second case:
k = ++j[1]; //j[1] is incremented and is now 2, so k is 2
n = ++j[1]; //j[1] is incremented again and is now 3, so n is 3
l = j[1]++; //j[1] is incremented again and is now 3, but l is 3
m = j[k++]; //m is j[2] which is 15, and k will become 3 as it is post-incremented
So output of n, k, l, m will be 3, 3, 3, 15
Actually, it is not that complicated
int j[5] = {5, 1, 15, 20, 25};
n = ++j[1];
// n=2 j[1]=2
k = ++j[1];
// k=3 j[1]=3
l = j[1]++;
// l=3 j[1]=4
m = j[k++];
// since k=3, k becomes 4 but m is assigned j[3] which is 20
Let's see the other case
int j[5] = {5, 1, 15, 20, 25};
k = ++j[1];
// k=2 j[1]=2
n = ++j[1];
// n=3 j[1]=3
l = j[1]++;
// l=3 j[1]=4
m = j[k++];
// since k=2, k becomes 3 but m is assigned j[2] which is 15
int tab[][3] = {
{1, 2, 3},
{6, 5, 4},
{7, 8, 9}
};
main(){
int i, j, t=0;
for(i=2, j=0; i; i--, j++){
t += tab[i][j++];
printf("%d", t);
}
I don't understand why the output is 11, firstly i = 2; (7, 8, 9) and j = 0 and then t += tab[i][j++] this means 7, 8, 9 + (j++ which is 1 now) (2, 5, 8) which means 7 + 8 + 9 + 2 + 5 + 8? I don't get this. t += tab[i][j++]
Two things:
First, it seems that you assume that t += tab[i][j] somehow adds a complete vector or a row to t, i.e. something like (7,8,9). Yet an access to tab[i][j] stands for one particular cell value (not a row), and with i==2, j==0, this means tab[2][0] and yields value 7.
Second, note that you have two statements that increment j, one in the for-part, and one in tab[i][j++] (note the j++). So when i decreases to 1, j has actually increased to 2, such that the second value will be tab[1][2], which is 4.
Then 7 + 4 = 11 should not be a surprise any longer :-).
Hope it helps.
First change printf() to printf("%d\n", t); to see each t value in one line.
running program produce this output:
as you see we have : 7 for first t,
before that you can use two simple for loop to print matrix all elements
and have better view on its elements order.
If you make for loop indentation better you can decode the output results.
grab a pen and paper and draw variables of loop in each iteration.
for(i=2, j=0 ; i != 0 ; i--, j++ ){
t += tab[i][j++];
printf("%d\n", t);
}
first iteration i:2 , j=0 as the condition i != 0(i.e i is not equals to 0 ) is true, then body of for is executed and we have t += tab[2][0];' 'tab [2][0] pointing to 7 (array index in C starts from Zero 0) and because of value of t is 0, now t holds 7 and simply printed on screen. after that j++ in the tab[i][j++] add one to j and now j:1 as this for iteration has been finished two commands in the third part of for is executed now (i--, j++) and make values to i:1 , j:2.
we start second for iteration.
as the condition i != 0 i still true we pass inside of for and
t += tab1[2]; tab[1][2] :4 and added to t:7 we have now t:7+4=11
and print t:11 on screen. now j added by one j:3 and this iteration is finished goto third part of for i--,j++ we have i:0, j:4.
in later iteration first we must check the for condition i != 0 as now i:0 this condition is False and obviously for is finished.
This is my version
int tab[][3] = {
{1, 2, 3},
{6, 5, 4},
{7, 8, 9}
};
//Main Program Function, Execution Part
int main()
{
int i, j, t=0;
for (int i=0;i<3;i++){
printf("\n");
for (int j=0;j<3;j++){
printf("%d\t", tab[i][j]);
}
}
printf("\n\n");
for(i=2, j=0 ; i != 0 ; i--, j++ ){
t += tab[i][j++];
printf("%d\n", t);
}
system ("pause");
}
This C program will take the value stored in the variable a and print them one by one.
#include <stdio.h>
void foo(int n, int sum)
{
int k = 0, j = 0;
if (n == 0)
return;
k = n % 10;
j = n / 10;
sum = sum + k;
foo (j, sum);
printf ("%d, ", k);
}
int main ()
{
int a = 2048, sum = 0;
foo (a, sum);
printf("\n");
return 0;
}
Output:
2, 0, 4, 8,
When the function foo executes:
1) For the first time: n = 2048, k = 8, j = 204, sum = 8
2) For the second time: n = 204, k = 4, j = 20, sum = 12
3) For the third time: n = 20, k = 0, j = 2, sum = 12
4) For the fourth time: n = 2, k = 2, j = 0, sum = 14
If I replace the line (present in the foo function):
printf ("%d, ", k);
with this:
printf ("%d | %d, ", k, sum);
Output:
2 | 14, 0 | 12, 4 | 12, 8 | 8,
Can someone please explain how this program works:
1) How it's printing value stored in a?
2) And in this order: 2, 0, 4, 8, ?
3) Why is the value of sum is changing when we're printing values of k?
4) What would happen when n become 0?
You are calling the function foo on a.
That is the order since you are printing AFTER you are processing the rest of the number. Try moving the call to printf before the call to foo in foo and see if you get anything different.
sum is changing because you are doing sum = sum + k and passing it to all the future calls.
When n eventually becomes 0 due to repeated divisions, the last call to foo starts returning and following them all the previous calls start returning after printing the digit they had extracted using n % 10
Shouldn't the output of following program be -
2 3 20
instead it is showing
3 2 15
Can anyone explain the reason behind this?
#include<stdio.h>
main()
{
int a[5] = {5,1,15,20,25};
int i,j,m;
i = ++a[1];
j = a[1]++;
m = a[i++];
printf("%d %d %d",i,j,m);
}
3 2 15
is the correct output.
i is 3, because i became 2 in i = ++a[1]; for pre-increment and then it got post-incremented in m = a[i++];
j is 2, because j = a[1]++;, no changes afterwards.
m is 15 because m = a[i++]; i is being post-incremented, the old value of i (which is 2) is used in indexing and the post-increment on i is sequenced after the evaluation of the = statement.
Having said that, the recommended signature of main() is int main(int argc, char *argv[]) or at least, int main(void).
At this point, values of variables are:
a = {5,1,15,20,25};
i = uninitialized
j = uninitialized
m = uninitialized
Now,
i = ++a[1];
Gets the value of a[i] which is 1, increments it and it becomes 2, and then, it is stored in i.
At this point, values of variables are:
a = {5,2,15,20,25};
i = 2
j = uninitialized
m = uninitialized
Next,
j = a[1]++;
Gets the value in a[1] which is 2 (since it was incremented in the previous statement), stores this value in j and then, increments the value stored in a[1].
At this point, values of variables are:
a = {5,3,15,20,25};
i = 2
j = 2
m = uninitialized
Then,
m = a[i++];
Gets the value in a[i](a[2] since i is currently 2) which is 15 and this value is stored in m. Then, i is incremented.
At this point, values of variables are:
a = {5,3,15,20,25};
i = 3
j = 2
m = 15
3 2 15 is correct
#include<stdio.h>
main()
{
int a[5] = {5,1,15,20,25};
int i,j,m;
i = ++a[1];
j = a[1]++;
m = a[i++];
printf("%d %d %d",i,j,m);
}
Now lets go line by line assume i , j , m equals 0 {better to initialize}
from line 3 , i = ++a[1];
i = 2 as (++ pre increment , change then use , and a[1] = 1 so , i = 2)
from line 4, j = a[1]++;
j = 2 as (++ here is post increment , use then change , a[1] becomes 3 but j is equals to 2)
from line 5, m = a[i++];
i = 2 by line 3 , here ++ post increment then i will increment to 3 but a[2] will be used .
Hence i = 3 , j = 2 , m = 15
Hope you got it ..........