Programming in C array help
I have a general understanding of arrays, but for some reason this problem is confusing me. The question is asking for the output to the code,
int a[5], i;
for(i=0; i<5; ++i)
a[(i+3)%5] = 2*i;
for(i=0; i<5; ++i)
printf("%d", a);
I am getting confused in the middle, a[(i+3)%5] = 2*i;. And where will it get the number to print?
2*i is the number to print. (for each i in the forloop, means i going from 0 to 5 resulting in 0,2,4,6,8).
Quote:Original post by davepermen
2*i is the number to print. (for each i in the forloop, means i going from 0 to 5 resulting in 0,2,4,6,8).
Except the output is shifted, the array is not being indexed by i alone.
Quote:Original post by davepermen
2*i is the number to print. (for each i in the forloop, means i going from 0 to 5 resulting in 0,2,4,6,8).
THis does help, but what does the [(i + 3)%5] do? I can understand how i is incremented, and how the 2*i (2*0 = 0, 2*1= 2, etc), but what is this part doing?
the book you are using doesn't explain the mod operator (%), indexing arrays (a), and assignment (a = 10) ?????
It changes which element in the array is being assigned to.
work it out on paper ( look up about the modulo operator '%' if you are unsure of it ):
i = 0
a[ (0 + 3) % 5 ]
a[ 3 % 5 ]
a[3]
i = 1
a[ (1 + 3) % 5 ]
a[ 4 % 5 ]
a[4]
i = 2
a[ (2 + 3) % 5 ]
a[ 5 % 5 ]
a[0]
i = 3
a[ (3 + 3) % 5 ]
a[ 6 % 5 ]
a[1]
i = 4
a[ (4 + 3) % 5 ]
a[ 7 % 5 ]
a[2]
loop ends, i is not less than 5...
work it out on paper ( look up about the modulo operator '%' if you are unsure of it ):
i = 0
a[ (0 + 3) % 5 ]
a[ 3 % 5 ]
a[3]
i = 1
a[ (1 + 3) % 5 ]
a[ 4 % 5 ]
a[4]
i = 2
a[ (2 + 3) % 5 ]
a[ 5 % 5 ]
a[0]
i = 3
a[ (3 + 3) % 5 ]
a[ 6 % 5 ]
a[1]
i = 4
a[ (4 + 3) % 5 ]
a[ 7 % 5 ]
a[2]
loop ends, i is not less than 5...
% is the modulus operator. It returns the remainder of a division.
It is often used(as in this case) to do kind of a "wrapping" of a value. For instance:
for(int i=0;i<100;++i) cout<<i%8<<endl;
prints:
0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7 0 1 2 3 4 5 ...
So, the expression a[(i+3)%5] = 2*i becomes for each i:
i=0: a[(0+3)%5]=2*0=> a[3]=0;
i=1: a[(1+3)%5]=2*1=> a[4]=2;
i=2: a[(2+3)%5]=2*2=> a[0]=4;
i=3: a[(3+3)%5]=2*3=> a[1]=6;
i=4: a[(4+3)%5]=2*4=> a[2]=8;
Basically, the code fills the array with values (0,2,4,6,8) but starting from a[3]. When it reaches the limit of the array, it "rewinds" to a[0] and continues.
It is often used(as in this case) to do kind of a "wrapping" of a value. For instance:
for(int i=0;i<100;++i) cout<<i%8<<endl;
prints:
0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7 0 1 2 3 4 5 ...
So, the expression a[(i+3)%5] = 2*i becomes for each i:
i=0: a[(0+3)%5]=2*0=> a[3]=0;
i=1: a[(1+3)%5]=2*1=> a[4]=2;
i=2: a[(2+3)%5]=2*2=> a[0]=4;
i=3: a[(3+3)%5]=2*3=> a[1]=6;
i=4: a[(4+3)%5]=2*4=> a[2]=8;
Basically, the code fills the array with values (0,2,4,6,8) but starting from a[3]. When it reaches the limit of the array, it "rewinds" to a[0] and continues.
Thanks, I get it now. The book I am using is very poor and does not explain things well at all. I have looked at a couple of other books, but I have still had problems understaing certain concepts. I tend to learn best when I can ask questions. Books don't let me ask them too many. So thank again to you all.
a[(i+3)%5] is simply the [(i+3)%5]th element in the array. For instance, on the first iteration, when i = 0,
a[(0+3)%5] = 2*0
OR a[3%5] = 0
OR a[3] = 0
The elements are essentially assigned in the following order: 3, 4 , 0, 1, 2. A little familiar, isn't it?
Perhaps you will find this code snippet, which does something equivalent, a bit easier to understand:
a[(0+3)%5] = 2*0
OR a[3%5] = 0
OR a[3] = 0
The elements are essentially assigned in the following order: 3, 4 , 0, 1, 2. A little familiar, isn't it?
Perhaps you will find this code snippet, which does something equivalent, a bit easier to understand:
[SOURCE]for (int i=0, i<5, i++){a = (2*i) % 10}
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement