Counting # of single digits with arrays?

Discussion in 'C++' started by tman88g@gmail.com, Nov 1, 2006.

  1. Guest

    I've been trying to learn arrays, and I was wondering how one would go
    about making a program that would take from the user as many single
    digit numbers (0 - 9) as the user wants to enter, and then the program
    would output the number of times each digit was inputted, (and only the
    digits that were inputted). I've been toying around with this all day,
    but can't seem to nail it. I've been trying to use an array as a
    counter, and then putting it in a loop. Not a whole lot of luck so far,
    but I'm probably doing it wrong.. Any ideas?
     
    , Nov 1, 2006
    #1
    1. Advertising

  2. wrote:
    > I've been trying to learn arrays, and I was wondering how one would go
    > about making a program that would take from the user as many single
    > digit numbers (0 - 9) as the user wants to enter, and then the program
    > would output the number of times each digit was inputted, (and only the
    > digits that were inputted). I've been toying around with this all day,
    > but can't seem to nail it. I've been trying to use an array as a
    > counter, and then putting it in a loop. Not a whole lot of luck so far,
    > but I'm probably doing it wrong.. Any ideas?
    >


    Is your problem with the algorithm or with the control flow?

    int array[10] = {0};
    ....get user number 0...9
    array[user_number]++;

    It sounds like homework, so show what you've got if you need more help.

    --
    Scott McPhillips [VC++ MVP]
     
    Scott McPhillips [MVP], Nov 1, 2006
    #2
    1. Advertising

  3. wrote:
    > I've been trying to learn arrays, and I was wondering how one would go
    > about making a program that would take from the user as many single
    > digit numbers (0 - 9) as the user wants to enter, and then the program
    > would output the number of times each digit was inputted, (and only the
    > digits that were inputted). I've been toying around with this all day,
    > but can't seem to nail it. I've been trying to use an array as a
    > counter, and then putting it in a loop. Not a whole lot of luck so far,
    > but I'm probably doing it wrong.. Any ideas?


    Maybe you could post your source code. You might be on the right track,
    but could have some misconceptions about how to proceed. It sounds like
    you might have idea.
     
    doug turnbull, Nov 1, 2006
    #3
  4. Guest

    Scott McPhillips [MVP] wrote:
    > wrote:
    > > I've been trying to learn arrays, and I was wondering how one would go
    > > about making a program that would take from the user as many single
    > > digit numbers (0 - 9) as the user wants to enter, and then the program
    > > would output the number of times each digit was inputted, (and only the
    > > digits that were inputted). I've been toying around with this all day,
    > > but can't seem to nail it. I've been trying to use an array as a
    > > counter, and then putting it in a loop. Not a whole lot of luck so far,
    > > but I'm probably doing it wrong.. Any ideas?
    > >

    >
    > Is your problem with the algorithm or with the control flow?
    >
    > int array[10] = {0};
    > ...get user number 0...9
    > array[user_number]++;
    >
    > It sounds like homework, so show what you've got if you need more help.
    >
    > --
    > Scott McPhillips [VC++ MVP]


    The algorithm wasn't the problem, so I guess it was the control flow?
    I'm just trying to practice using arrays as I have a test coming soon.
    I think I may have my problems figured out all anyways. I will post
    everything up if I run into more problems. Thanks
     
    , Nov 1, 2006
    #4
  5. Phlip Guest

    tman88g wrote:

    > I've been trying to learn arrays, and I was wondering how one would go
    > about making a program that would take from the user as many single
    > digit numbers (0 - 9) as the user wants to enter, and then the program
    > would output the number of times each digit was inputted, (and only the
    > digits that were inputted). I've been toying around with this all day,
    > but can't seem to nail it. I've been trying to use an array as a
    > counter, and then putting it in a loop. Not a whole lot of luck so far,
    > but I'm probably doing it wrong.. Any ideas?


    A hint:

    ++array['7' - '0'];

    Replace one of those constants with one of your input variables.

    --
    Phlip
    http://www.greencheese.us/ZeekLand <-- NOT a blog!!!
     
    Phlip, Nov 1, 2006
    #5
  6. Guest

    ughh, the same problem has been resurfacing. Everything compiles and
    executes, but I get some debug error. "Run-Time Check Failure #2 -
    Stack around the variable 'numArray' was corrupted." I was getting some
    other run-time error message before, so I wasn't sure what I was doing
    wrong. All I have left that I want to figure out is how to output only
    the count of the digits that are entered, but I think I have the basic
    idea covered. Here is my code, (don't be too hard)...

    #include <iostream>
    using namespace std;

    void printNumDigits(int Array[])
    {
    cout << "\nThere are " << Array[0] << " 0's. " << endl;
    cout << "There are " << Array[1] << " 1's. " << endl;
    cout << "There are " << Array[2] << " 2's. " << endl;
    cout << "There are " << Array[3] << " 3's. " << endl;
    cout << "There are " << Array[4] << " 4's. " << endl;
    cout << "There are " << Array[5] << " 5's. " << endl;
    cout << "There are " << Array[6] << " 6's. " << endl;
    cout << "There are " << Array[7] << " 7's. " << endl;
    cout << "There are " << Array[8] << " 8's. " << endl;
    cout << "There are " << Array[9] << " 9's. " << endl << endl;
    }

    int main()
    {
    int num;
    int numArray[10] = {0};

    do
    {
    cout << "Enter an one-digit number, or 10 to quit: ";
    cin >> num;

    numArray[num]++;

    } while ( num <= 9 );


    if ( num = 10 )
    {
    printNumDigits(numArray);
    }

    else if ( num > 10 )
    {
    cout << "One-digit numbers only! " << endl << endl;
    }

    return 0;
    }




    wrote:
    > Scott McPhillips [MVP] wrote:
    > > wrote:
    > > > I've been trying to learn arrays, and I was wondering how one would go
    > > > about making a program that would take from the user as many single
    > > > digit numbers (0 - 9) as the user wants to enter, and then the program
    > > > would output the number of times each digit was inputted, (and only the
    > > > digits that were inputted). I've been toying around with this all day,
    > > > but can't seem to nail it. I've been trying to use an array as a
    > > > counter, and then putting it in a loop. Not a whole lot of luck so far,
    > > > but I'm probably doing it wrong.. Any ideas?
    > > >

    > >
    > > Is your problem with the algorithm or with the control flow?
    > >
    > > int array[10] = {0};
    > > ...get user number 0...9
    > > array[user_number]++;
    > >
    > > It sounds like homework, so show what you've got if you need more help.
    > >
    > > --
    > > Scott McPhillips [VC++ MVP]

    >
    > The algorithm wasn't the problem, so I guess it was the control flow?
    > I'm just trying to practice using arrays as I have a test coming soon.
    > I think I may have my problems figured out all anyways. I will post
    > everything up if I run into more problems. Thanks
     
    , Nov 1, 2006
    #6
  7. Guest

    As soon as I ask around for possible solutions, I end up solving the
    problem myself. It figures... Thanks for the help anyways guys.



    > wrote:
    > > Scott McPhillips [MVP] wrote:
    > > > wrote:
    > > > > I've been trying to learn arrays, and I was wondering how one would go
    > > > > about making a program that would take from the user as many single
    > > > > digit numbers (0 - 9) as the user wants to enter, and then the program
    > > > > would output the number of times each digit was inputted, (and only the
    > > > > digits that were inputted). I've been toying around with this all day,
    > > > > but can't seem to nail it. I've been trying to use an array as a
    > > > > counter, and then putting it in a loop. Not a whole lot of luck so far,
    > > > > but I'm probably doing it wrong.. Any ideas?
    > > > >
    > > >
    > > > Is your problem with the algorithm or with the control flow?
    > > >
    > > > int array[10] = {0};
    > > > ...get user number 0...9
    > > > array[user_number]++;
    > > >
    > > > It sounds like homework, so show what you've got if you need more help.
    > > >
    > > > --
    > > > Scott McPhillips [VC++ MVP]

    > >
    > > The algorithm wasn't the problem, so I guess it was the control flow?
    > > I'm just trying to practice using arrays as I have a test coming soon.
    > > I think I may have my problems figured out all anyways. I will post
    > > everything up if I run into more problems. Thanks
     
    , Nov 1, 2006
    #7
  8. Jim Langston Guest

    <> wrote in message
    news:...
    > As soon as I ask around for possible solutions, I end up solving the
    > problem myself. It figures... Thanks for the help anyways guys.


    You did figure to use == isntead of = in your if statement right?
    >
    >
    >
    >> wrote:
    >> > Scott McPhillips [MVP] wrote:
    >> > > wrote:
    >> > > > I've been trying to learn arrays, and I was wondering how one
    >> > > > would go
    >> > > > about making a program that would take from the user as many single
    >> > > > digit numbers (0 - 9) as the user wants to enter, and then the
    >> > > > program
    >> > > > would output the number of times each digit was inputted, (and only
    >> > > > the
    >> > > > digits that were inputted). I've been toying around with this all
    >> > > > day,
    >> > > > but can't seem to nail it. I've been trying to use an array as a
    >> > > > counter, and then putting it in a loop. Not a whole lot of luck so
    >> > > > far,
    >> > > > but I'm probably doing it wrong.. Any ideas?
    >> > > >
    >> > >
    >> > > Is your problem with the algorithm or with the control flow?
    >> > >
    >> > > int array[10] = {0};
    >> > > ...get user number 0...9
    >> > > array[user_number]++;
    >> > >
    >> > > It sounds like homework, so show what you've got if you need more
    >> > > help.
    >> > >
    >> > > --
    >> > > Scott McPhillips [VC++ MVP]
    >> >
    >> > The algorithm wasn't the problem, so I guess it was the control flow?
    >> > I'm just trying to practice using arrays as I have a test coming soon.
    >> > I think I may have my problems figured out all anyways. I will post
    >> > everything up if I run into more problems. Thanks

    >
     
    Jim Langston, Nov 1, 2006
    #8
  9. Jim Langston Guest

    >> wrote:
    >> > Scott McPhillips [MVP] wrote:
    >> > > wrote:
    >> > > > I've been trying to learn arrays, and I was wondering how one
    >> > > > would go
    >> > > > about making a program that would take from the user as many single
    >> > > > digit numbers (0 - 9) as the user wants to enter, and then the
    >> > > > program
    >> > > > would output the number of times each digit was inputted, (and only
    >> > > > the
    >> > > > digits that were inputted). I've been toying around with this all
    >> > > > day,
    >> > > > but can't seem to nail it. I've been trying to use an array as a
    >> > > > counter, and then putting it in a loop. Not a whole lot of luck so
    >> > > > far,
    >> > > > but I'm probably doing it wrong.. Any ideas?
    >> > > >
    >> > >
    >> > > Is your problem with the algorithm or with the control flow?
    >> > >
    >> > > int array[10] = {0};
    >> > > ...get user number 0...9
    >> > > array[user_number]++;
    >> > >
    >> > > It sounds like homework, so show what you've got if you need more
    >> > > help.
    >> > >
    >> > > --
    >> > > Scott McPhillips [VC++ MVP]
    >> >
    >> > The algorithm wasn't the problem, so I guess it was the control flow?
    >> > I'm just trying to practice using arrays as I have a test coming soon.
    >> > I think I may have my problems figured out all anyways. I will post
    >> > everything up if I run into more problems. Thanks



    <> wrote in message
    news:...
    > As soon as I ask around for possible solutions, I end up solving the
    > problem myself. It figures... Thanks for the help anyways guys.


    Please don't top post in this newsgroup. Okay, now that you got it, I would
    of done it a bit different.

    I would of had the user input a whole string, then go through the stream
    adding up the digits. What if you wanted to see how many of any character
    they put int?

    Pseudo code:

    std::string Line;
    std::cout << "Enter a line:: ";
    std::cin >> Line;

    unsigned int Characters[256] = (0);
    // Exercise for the reader, use a std::vector instead and preallocate 256
    spaces

    for ( int i = 0; i < Line.length(); ++i )
    {
    Characters[Line]++;
    }

    for ( int i = 0; i < 256; ++i )
    {
    if ( Characters > 0 )
    {
    if ( i >= 32 && i < 128 )
    std::cout << "You entered " << Characters << " " <<
    static_cast<char>( i ) << "'s" << std::endl;
    else
    std::cout << "You entered " << Character << " chars with the ASCII
    value " << i << std::endl;
    }
    }
     
    Jim Langston, Nov 1, 2006
    #9
  10. wrote:
    > int num;
    > int numArray[10] = {0};
    >
    > do
    > {
    > cout << "Enter an one-digit number, or 10 to quit: ";
    > cin >> num;
    >
    > numArray[num]++;
    >
    > } while ( num <= 9 );


    You've got a bug here that will cause an out-of-bounds access error. If
    the user enters 10 your code attempts to increment numArray[10], which
    does not exist.

    --
    Scott McPhillips [VC++ MVP]
     
    Scott McPhillips [MVP], Nov 1, 2006
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. loris_p
    Replies:
    3
    Views:
    376
    Roedy Green
    Jun 15, 2007
  2. Replies:
    2
    Views:
    1,006
    Martin Honnen
    Apr 12, 2008
  3. Philipp
    Replies:
    21
    Views:
    1,190
    Philipp
    Jan 20, 2009
  4. edwardfredriks

    counting up instead of counting down

    edwardfredriks, Sep 6, 2005, in forum: Javascript
    Replies:
    6
    Views:
    241
    Dr John Stockton
    Sep 7, 2005
  5. Replies:
    5
    Views:
    97
Loading...

Share This Page