need help on array problem

This is a discussion on need help on array problem within the C++ Programming forums, part of the General Programming Boards category; hi i am having trouble with a hw that uses arrays..the problem directiosn r..... Write a program that counts occurances ...

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    1

    need help on array problem

    hi i am having trouble with a hw that uses arrays..the problem directiosn r.....
    Write a program that counts occurances of each character in the input (128 ascii codes). Note this is different than counting classes (categories) of characters, because we want to know how many times each specific character appeared in the input. For control characters (those that are not printable) print the ascii code in angle brackets

    so far i hav....
    #include <iostream.h>



    void initializeCountersToValue (int counterArray[], int value )
    {
    for (int i =0; i < 127; i++)
    counterArray [i] = value;
    }
    void incrementCounterForCharacter (int counterArray [], char ch)
    {

    for (int i=0; i<127; i++)
    if (counterArray [i] == ch )
    counterArray [i]++;

    }
    void reportTotals ( int counterArray [])
    {
    for (int i = 0; i <127; i++)
    cout << counterArray [i];
    }

    int main()
    {

    int characterArray[127];

    initializeCountersToValue (characterArray, 0);

    cout << "enter a bunch of text for counterin. /n";
    cout << "type < ctrl-z> when done to indicate the end-of-file. /n";


    for ( char ch; cin.get(ch)
    incrementCounterForCharacter (characterArray, ch);
    reportTotals (characterArray);




    return 0;
    }

    itz not working ryte....so can neone help me figure out wat im doin wrong?

  2. #2
    Unregistered
    Guest
    This line is wrong:

    if (counterArray [i] == ch )

    you don't want to know if the value of element counterArray[i] equals ch, then increment the counter; you want to increment the counter whose index equals ch. Try this:

    void incrementCounterForCharacter (int counterArray [], char ch)
    {
    counterArray [ch]++;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Array problem
    By TomBoyRacer in forum C++ Programming
    Replies: 3
    Last Post: 04-08-2007, 11:35 AM
  2. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  3. Replies: 6
    Last Post: 02-15-2005, 10:20 PM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Need desperate help with two dimensional array problem
    By webvigator2k in forum C++ Programming
    Replies: 4
    Last Post: 05-10-2003, 02:28 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21