Sign in to follow this  
RollinStang

Programming in C array help

Recommended Posts

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]); I am getting confused in the middle, a[(i+3)%5] = 2*i;. And where will it get the number to print?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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...

Share this post


Link to post
Share on other sites
% 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.





Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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:

[SOURCE]
for (int i=0, i<5, i++){
a[i] = (2*i) % 10
}

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this