1. ## please walk me through this code

Can somebody please walk me through this problem and show me how you get an output of:
[0]
[14]
[28]

I've been looking at it for the past 30 minutes and I still can't figure out how this code is supposed to work? Thanks in advance!

Code:
```What is the output of the following program?
--------------------------------------------------------
#include <stdio.h>

int main()
{
int x[20];
int count1, count2;

for( count1 = 0; count1 < 20; count1++ )
{
x[count1] = count1 * 2;
}

for( count2 = 0; count2 < 20; count2 += 7 )
{
printf( "%d\n", x[count2] );
}
return 0;
}```

2. What's the problem with it?

count1 is being increased by one each loop, and count two is being increased by seven. the zeroth element of x is 2*0=0. The seventh is 2*7=14 and the 14th is 2*14=28. The next iteration for the second loop gives a value of 21 for count2 which is greater than 20 so the loop terminates and the program completes.

3. The first loop puts all of the even numbers:
Code:
`0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38`
into the array. The second loop prints every 7th number after 0, so it prints the red ones before count2 is bigger than 20:
Code:
`0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38`

4. Oh I see, I was confused as to what the relationship between count1 and count2 was. So for this code, the first part sets freq[1]-freq[4] equal to zero? Then I'm not sure what the second part is supposed to do. The output should be (3 1 2 0 1)

main()
{
int i;
int freq[5];
int a[] = {0,1,4,0,2,2,0};

for(i=0;i<5;i++) {
freq[i]=0;
}

for(i=0;i<7;i++) {
freq[a[i]]++;
}

for(i=0;i<5;i++) {
printf("&#37;d ",freq[i]);
}
printf("\n");
}

5. > the first part sets freq[1]-freq[4] equal to zero?
No, both count variables are assigned zero so that we can count indices.
Something in pseudocode might be more legible:
Code:
```Let A[] be an array of 20 items

count = 0
for each item in A[]:
A[item] = count * 2
count++

for each seventh item in A[]:
print A[item]```
Does it make sense now?

6. I forgot to put the second code in my last post sorry! But yeah that makes perfect sense, do you mine writing me a psuedo code for that second code? If you don't mind of course

7. Basically
Code:
```Let A[] be an array of some number of items
Let B[] be an array of 5 items

for each item in B[]:
B[item] = 0

for each item in A[]:
save = A[item]
access B[save] then increment B[save]

for each item in B[]:
print B[item]```
Skipping the pseudocode step is reserved for expert programmers.

8. I'm being picky here but your pseudocode is misleading. In fact, the for each operation is used incorrectly:

If you were to write "for each item in B[]", then item already refers to the value, not to the index. You can output all the values of an array like this:

Code:
```for each item in B[]
print item```
The correct way of writing is to say:
Code:
```Let A[] be an array of some number of items
Let B[] be an array of 5 items

for each index in 0 to length(B[]) -1:
B[index] = 0

for each item in A[]:
B[item] = B[item] + 1

for each item in B[]:
print item```
I know it's not "that" important to write correct pseudocode, but your construction of for each is against every syntax of every language implementing the foreach sugar.

9. I know that Koni. Thanks for the concern anyways.