# Thread: Program to count how many of each letter has been typed

1. ## Program to count how many of each letter has been typed

im trying to create a simple program since im just starting out ,that prints out a histogram telling how many of each letter has been typed in here's my program so far:

Code:
```#include <stdio.h>
#define AMOUNT 47
int main()
{
int c,b,i;
int biggestnum = 0;
int letters;
int ndigit[AMOUNT];
int numbers[AMOUNT];
for( i = 0; i <= 47; i++)
numbers[i] = 0;
for( i = 0; i <= 23; i++)
{
for( letters = 'A'; letters <= 'Z' ; letters ++ )
ndigit[i] = letters;
}
for ( i = 24; i <= 47; i++)
{
for( letters = 'a'; letters < 'z'; letters ++ )
ndigit[i] = letters;
}
while((c = getchar()) != EOF )
{
for ( i = 0; i <= 47; i++)
{
if ( c == ndigit[i])
++numbers[i];
if ( numbers[i] > biggestnum )
biggestnum = numbers[i];
}
}
for ( i = biggestnum; i > 0; --i )
{
printf("%4d   [" , i);
for ( b = 0; b <= AMOUNT; b++ )
{
if (numbers[b] >= i)
printf("x  ");
else
printf("   ");
}
printf("\n");
}
}```
ive been having some problems with it and wondered if anyone could help so far i havent included to the bottom part of the histogram as i cant get this section to work

2. For one thing, there are 26 letters in the English alphabet. Accounting for upper and lower case, that's 26 * 2 = 52 symbols. Your 47 character array isn't large enough. Also, you probably don't need two arrays; just one array of counts will do. All you need to do is to map each individual letter to a particular element of the array, then ignore all other characters.

3. ow thanks my maths has gone out the window -_-

4. how would you impliment using one array, that part confuses me

5. Okay, simplify: pretend that you're only going to count uppercase letters. How would you map these uppercase letters to the elements of a 26 integer array?

6. well first you'd dfine an array of 26 ie int array[26]; ,then im not to sure about the second bit

7. Okay, assume that you are working with the ASCII character set. Now, 'A' has the value of 65, 'B' has the value of 64, etc. So, if you want to map 'A' to index 0, 'B' to index 1, etc, you just need to subtract 65 (or subtract 'A'). Can you see how to proceed from here?

8. Do you understand the meaning of map in this context? To map a value to a location, you need to be able to choose other key values that will give up the data being stored.

9. yeah i think so thanks

10. Code:
```#define AMOUNT 47
...
int numbers[AMOUNT];
...
for( i = 0; i <= 47; i++)
numbers[i] = 0;```
1. You're declaring an array with a named constant ("AMOUNT"). I'm guessing you're using the "for" loop to initialize all values of the array to zero. If this is the case, I would suggest using "AMOUNT" instead of a number constant in the comparison expression in your "for" loop. Therefore, if you make a change to the length of this array (such as laserlight recommends in post #2), you only have to change one line of code.

2. Arrays in C - You're declaring the "numbers" array to have 47 elements. The first index number starts at zero ("numbers[0]"), so the last index number would be "n-1" ("numbers[46]" in this case). Your "for" loop exceeds the bounds of this array by one (check your relational operator in the comparison expression).

11. ive tried all this but when i compile and type in a letter ,the screen just stays blank

13. Cos I'm away from my computer atm I can't but basically I still have two arrays as when I attempted it one array confused me and as I'm still a beginner I decided to stick with what I know ,the only thing Ichanged was the array size to 52 And all the for loops that relate to the array size so that parts correct

14. here's my current code i changed it to one array and shortened it to only pick lower case characters im still having problems
Code:
```#include <stdio.h>
#define AMOUNT 26
int main()
{
int i,b,c;
int currentnum,biggestnum;
int numbers[AMOUNT];
currentnum = biggestnum = 0;
for ( i = 0; i < AMOUNT; ++i)
numbers[i] = 0;
while((c = getchar()) != EOF)
{
if( (c - 'a') < AMOUNT && (c - 'a') >= 0 )
{
++numbers[c - 'a'];
currentnum = numbers[c - 'a'];
if (currentnum > biggestnum)
biggestnum = currentnum;
}
}
for ( i = biggestnum; i > 0; i--)
{
printf("%4d   ]",i);
for ( b = 0; b > AMOUNT; b++)
{
if ( numbers[b] >= i )
printf("x  ");
else
printf("   ");
}
printf("\n");
}
}```

15. How often is zero greater than 26?

for ( b = 0; b > AMOUNT; b++)
Tim S.