Hello,

Can someone break down what this array loop means exactly? Mathematically I can't make any sense of it. I've reread this section of my book multiple times, but it's not sinking in.

``````int alpha[a];
int j;

for(j = 0; j< 5; j++)
{
alpha[j] = 2 * j;

if(j % 2 == 1)
alpha[j - 1] + j;
}``````

I'm new to C++ ; this was quiz question I missed. The correct answer was alpha = {3, 2, 9, 6, 8} - just not sure how/why! TY.

Sit down with pencil and paper and execute the code a line at a time. Watch what values come up. That's the best way to understand anything.

Can't compile it but I don't think that output you gave is correct for
the snippet.

There are few assumptions that the snippet makes, "a" is a const number and its greater than 5.

Now lets go through this code :

``````int alpha[a];
int j;

for(j = 0; j< 5; j++)
{
alpha[j] = 2 * j;

if(j % 2 == 1)
alpha[j - 1] + j;
}``````

when j = 0, the inside body gets executed, and so alpha[0] = 2 *0 = 0. So alpha[0] = 0.
when j = 1, the inside body gets executed and so alpha[1] = 2 * 1 = 2.
Then the statement (j % 2 == 1) evaluates to (1 % 2 == 1) = true,
the inside body of that loop gets executed, but the inside body
of that loop is just alpha[j - 1] + j which equals, alpha[1 - 1] + 1 = alpha[0] + 1. But that is just a statement that has no effect. So alpha[1]
is still 2. Keep doing this and the output, I think will be :
alpha[] = { 0,2,4,6,8}.
I think the statement alpha[j-1]+1 is copied wrong. Should it be
alpha[j-1] += 1 ?

commented: Isn't that what I asked him to do with paper? -2

I think we can't use this method to define alpha as an array
as a is not a number
and In C++ we can't use a variable for the size of the array
and there is another thing

``alpha[j-1]+j;``

means nothing as it is not an assignment statement..
that's what I think :D