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

  1. #1
    Registered User
    Join Date
    Jun 2012
    Posts
    9

    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. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jun 2012
    Posts
    9
    ow thanks my maths has gone out the window -_-

  4. #4
    Registered User
    Join Date
    Jun 2012
    Posts
    9
    how would you impliment using one array, that part confuses me

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Jun 2012
    Posts
    9
    well first you'd dfine an array of 26 ie int array[26]; ,then im not to sure about the second bit

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    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. #9
    Registered User
    Join Date
    Jun 2012
    Posts
    9
    yeah i think so thanks

  10. #10
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    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. #11
    Registered User
    Join Date
    Jun 2012
    Posts
    9
    ive tried all this but when i compile and type in a letter ,the screen just stays blank

  12. #12
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Show your current code.

  13. #13
    Registered User
    Join Date
    Jun 2012
    Posts
    9
    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. #14
    Registered User
    Join Date
    Jun 2012
    Posts
    9
    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. #15
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    How often is zero greater than 26?

    for ( b = 0; b > AMOUNT; b++)
    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Count lowercase letter
    By krakatao in forum C Programming
    Replies: 23
    Last Post: 11-16-2011, 12:01 AM
  2. Replies: 7
    Last Post: 09-27-2011, 08:32 PM
  3. Help for letter count generating program
    By $ingh in forum C Programming
    Replies: 6
    Last Post: 06-17-2010, 02:42 PM
  4. Problems with my c letter count program
    By cram55 in forum C Programming
    Replies: 10
    Last Post: 05-30-2007, 04:10 AM
  5. Letter count
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 12-24-2001, 01:23 PM