Incrementing arrays in C,SPECIAL CASE - c

I am confused by the following code:
#include <stdio.h>
int main()
{
int a[] = {5, 15, 1, 20, 25};
int i, j, m;
i = ++a[1]; /*statement 1*/
j = a[1]++; /*statement 2*/
m = a[i++]; /*statement 3*/
printf("\n%d\n%d\n%d\n", i, j, m);
return 0;
}
Statements 1, 2, 3 are a bit confusing for me; I don't not get the way these are producing the output for me. Can anyone shed some light on this please?

i=++a[1]; /*statement 1*/ // increments the value of a[1] and assigns to i
j=a[1]++; /*statement 2*/ // assign the value of a[i] and then increments the value of a[i] to j
m=a[i++]; /*statement 3*/ // assign a[i] to m, and increment i
i=++a[1]; // a[1] is 15 here so i =16, and a[1] =16
j=a[1]++; // a[1] is 16 so j =16 and a[1] =17
m=a[i++]; // i is 16 here but index 16 does not exists here, so program fails

i = ++a[1]; /*statement 1*/
Hear a[1] will be 15. and Pre Increment operator will increase a[1] with 1 S0 i will be 16
j = a[1]++; /*statement 2*/
Post increment operator will also increment the value of a[1] by 1. so j will be 16.
m = a[i++]; /*statement 3*/
Here, it is i++, so post increment oprator will increase i by 1..earlier i was computed 16 . now i will now be 17.
So a[17] has no value. so m will be junk value

In case of doubt, what you can do is use gdb:
Compile your code setting debug flag (-g for gcc) gcc -g -o so stackoverflow.c
run gdb gdb ./so
identify lines to break list in gdb
set breakpoints break 6 (first break is line 6)
Step forward step
Display values print i or print a[1] for instance.
But beware, if you call print ++a[1] in gdb you may change your program behaviour!

The First Statement
i = ++a[1] ---> Increments the value of a[1] (i.e) i value will be 16
Second Statement
j = a[1]++ ---> Assign the value of a[1] to j and then incremented(i.e) j would be 16.
Third Statement
m = a[i++] --> Assigning value of a[i] to m and i is incremented.

Related

2D array in C, what t+= tab[i][j] means?

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");
}

Wrong value being printed by printf

I have this piece of C code
#include <stdio.h>
int main(){
int i , j , m , A[5]={0,1,15,25,20};
i = ++A[1];
printf("%d:\n",i);
j = A[1]++;
printf("%d:\n",j);
m = A[i++];
printf("%d:\n",m);
printf("%d %d %d",i,j,m);
return 0;
}
and It's output is
2:
2:
15:
3 2 15
Shouldn't the printf print the values as 2 , 2, 15 but why is it printing 3 , 2, 15
P.S : I really didn't abuse this code , someone else did (my professor perhaps) and I'm just learning C .
The line
m = A[i++];
will increment the variable i in-place after it gets the cooresponding value from the array A.
i is incremented as a part of below statement
m = A[i++];
Lets see what we got here..
int i , j , m , A[5]={0,1,15,25,20};
i = ++A[1]; // takes the value of A[1], increment it by 1 and assign it to i. now i = 2, A[1] = 2
printf("%d:\n",i);
j = A[1]++; // takes the value of A[1](which is 2), assign it to j and increment the value of A[1] by 1. now j = 2, A[1] = 3
printf("%d:\n",j);
//remember the value of i? its 2
m = A[i++]; // takes the value of A[2](which is 15), assign it to m and increment the value of i by 1. now m = 15, i = 3
printf("%d:\n",m);
printf("%d %d %d",i,j,m); // Hola! we solve the mystery of bermuda triangle :)
return 0;
m = A[i++];
this code assign A[2] which is 15 to the variable m ,and then +1 to the current value of i to become 3.

Unexpected Output involving array values with post and pre-increment

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 ..........

What is the process behind this output?

The value of i is 2 when I comment out statements 2 and 3, but when I don't, i becomes 3. Why is that?
#include <stdio.h>
int main()
{
int a[5]={5,1,15,20,25};
int i,j,k=1,m;
i=++a[1]; //Statement 1
j=a[1]++; //Statement 2
m=a[i++]; //Statement 3
printf("%d %d %d",i,j,m);
return 0;
}
In Statement 3, your code is incrementing the value of i:
m = a[i++];
This is easier to notice is you break it up as the following two lines:
m = a[i];
i++;
Note: The order is important! Since this is post-increment (i++, not ++i), the value of i is used first, and then incremented.
m=a[i++] causes i to be incremented by one. It is the i++ part that increments i.
after statement 1: i = a[1] + 1 which means you are adding 1 to a[1] then storing that value in i
i = 2 ; a[1] = 2'
after statement 2: j = a[1]++ which means you are adding 1 to a[1] i.e 2 + 1
j = 2; a[1] = 3;
after statement 3: m = a[i + 1] which means you are adding 1 to the index value
i already equals 2 so you do m = a[2+1] which is 15 but since you have i++ that operation still preforms the add to i making i =4
m = 15; i = 4; thus a[i] = 20

What does "<" do here in the expression j = i++ < 5 do?

What does the operator < (less-than) do?
Also, can you explain why did for-loop ended?
main()
{
int i = 1, j = 1 ;
for ( ; j ; printf ("%d%d\n", i,j))
{
j = i++ < 5 ;
}
return 0;
}
I got the output as
21
31
41
51
60
The code checks if i is smaller than 5, then increments i. The result of the comparison is stored in j.
It is quite an obscure way to make a loop like this. Not very readable.
It returns 1 if i is less than 5, otherwise it returns 0. Note that increment does not affect the comparison, until the next loop iteration.
when your loop is executing i++ it is actually incrementing the value of i . The condition
j= i++ < 5 will check for the return value '1' or '0' so when accordingly your output print statement printf ("%d%d\n", i,j) will print
i=2 ; j=2<5 -> return 1 so print (2,1)
i=3; j=3<5 -> return 1 so print (3,1)
and so on till the condition becomes false and your output is finally j=return '0' for i=6
this will give you ans (6,0)

Resources