1. ## Simple Alphabet/Array Problem

That I can't get. My instructor went over this last night and I'm trying to study up but apparently didn't save the program. Here is my code:

Code:
```#include <stdio.h>

int main(void)
{
int alphabet[26] = {'A'};
int i = 0;

for(i = 0;i < 25; i++)
{
printf ("%d\t%c\n", alphabet[i], alphabet[i]);
alphabet[i]++;

}

return 0;
}```
The elements within the array are not incrementing. I get 25 outputs, the first being A and 65 but then all zero's. What am I missing that is needed to increment the elements within the array?

2. I'm sure the elements are being incremented... but you printed out the original value so you won't see the updated one. I think they'll all be 'B's.
Oh and your loop should probably go for (i = 0; i < 26; i++)

3. Originally Posted by nonoob
I'm sure the elements are being incremented... but you printed out the original value so you won't see the updated one. I think they'll all be 'B's.
Oh and your loop should probably go for (i = 0; i < 26; i++)
Thanks, no they are not B's but one 65 A then all garbage.

4. Got it:

Code:
```#include <stdio.h>

int main(void)
{
int alphabet[26];
int i = 0;

alphabet[0] = 'A';
for(i = 0;i < 26; i++)
{
alphabet[i] = alphabet[0];
printf ("%d\t%c\n", alphabet[i], alphabet[i]);
alphabet[0]++;
}
return 0;
}
}```

5. Originally Posted by dolfaniss
Thanks, no they are not B's but one 65 A then all garbage.
Because you're only intializing the first element of the array.

6. Hint I learned early on... should help you when you are under pressure.

Without getting into the dangers of "magic numbers", when you want to go through a loop MAX_NUM times (say 26 times like in your case) or you've declared a variable with MAX_NUM elements (say 26) and you want to use them all, your for loop should be from 0 to <MAX_NUM.

Like this:

Code:
```int foo[MAX_NUM];
for(i = 0;i < MAX_NUM; i++)```
Note, it's not

Code:
`for(i = 0;i < MAX_NUM-1; i++)`
nor is it

Code:
`for(i = 0;i <= MAX_NUM; i++)`

7. Originally Posted by CommonTater
Because you're only intializing the first element of the array.
Technically he is initializing all elements of the array. Default behavior is to auto populate the uninitialized elements to zero, so long as you have actually initialized at least one of them, which is why we can safely do this:
Code:
`int x[5] = {0};`
To set everything to zero.

True, it's not what he expects it to do, but they are initialized.

Quzah.

8. Originally Posted by quzah
True, it's not what he expects it to do, but they are initialized.
Quzah.
Picky picky picky... Yes, the first character is initialized to his 'A'... the rest will be set to 0.