ARRAYS!!! need some help.

Okay so i have been asked to do this program below.. however i have no clue where to start and i am now back logged on homework and dont quite have anywhere else to turn any help or assistance would be much appreciated. i just dont understand the logic or where to even begin. -.- not so good at arrays i am.

================================================================================
Write a program that asks the user to enter a series of one-digit positive numbers (you will need to
do error checking). When they have finished (Note: the user should indicate that they are finished
by entering the value 10), print out how many of each number the user entered. There must be
two functions. Name your program Lab10_Ex2.cpp.
Here is a sample output if the user entered 0,7,7,2,7,10:
Hint: Use an array to hold an accumulator (counter) for each digit.
Note: Do not output a value of 0 for the number of digits entered.
Enter a one-digit number or 10 to exit: 0 [enter]
Enter a one-digit number or 10 to exit: 7 [enter]
Enter a one-digit number or 10 to exit: 7 [enter]
Enter a one-digit number or 10 to exit: 2 [enter]
Enter a one-digit number or 10 to exit: 7 [enter]
Enter a one-digit number or 10 to exit: 10 [enter]
You entered 1, 0(s)
You entered 1, 2(s)
You entered 3, 7(s)
You could use an std::map<int, int> where the key is the number being inputed by the user, and the value is the number of times that number has been entered. That way the problem is easily solved inside a while-loop.
Start by defining an array of ten integers, this will be used to count how many of each there are. Initialise each value to zero.

After you have validated the input, to confirm that it is a single-digit integer between 0 and 9 inclusive, it will be safe to use that value as a subscript to access an array. Increment the corresponding element.

The rest of the program is concerned with input and output, which I'll leave for you to have a go at.

Array tutorial: http://www.cplusplus.com/doc/tutorial/arrays/
okay ima try this real quick and repost what i have if you guys dont mind taking a look at it. I just know where my skill is at ima mess something up.
also do you think i should use an array for the user input? im not sure how else to indefinitely store a users input until they enter ten, also im not entirely sure an array will work for that either.
closed account (DEUX92yv)
First, imagine the program this way: you only accept one single-digit number from the user, then immediately print out the results. Try to come up with code to accomplish this, including printing out "Enter a ... (etc)".

Once you've done that, think about how you can repeat that process until the user enters 10. If you come up with a working solution here, you've tackled the most complicated part of the assignment.

Lastly, consider the array you'll use to count the number of instances of each digit. Since you only allow single-digit numbers, what size does the array need to be? Each element corresponds to one digit.
Last edited on
No you don't need an array to store the user input. The way Chervil mentioned, which is better than mine if the numbers only range from 0-9, you keep an array of 10 integers, ranging from 0 to 9. Then you increment the corresponding value for each number entered.
#include <iostream>
using namespace std;

void printstatement(int[], int);

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

cout << "enter a single didgit number between 0-9 enter 10 to exit" << endl;
cin >> num;

while (num >=0 && num <= 9)
{
totalcount[num]++;

cout << "enter a single didgit number between 0-9 enter 10 to exit" << endl;
cin >> num;
}

printstatement(totalcount, 10);

system ("pause");
return 0;
}

void printstatement(int totalcount[10],int size)
{
for ( int i = 0; i < size; i++)
{
if ( totalcount[i] != 0)
{
cout << "you entered " << totalcount[i]<< ", " << i << "(s)" << endl;
}
}
}





okay this is what i got but for some reason it wont compile and my compiler wont tell me what the errors are. do you guys see anything wrong here?
The word "didgit" is misspelled :)

Seriously, it compiled and ran ok for me.
Though it exits for any out-of-range value, not just 10.
hahah must just be an issue on my end... -.- haha i just realized i wrote digit wrong, and okay ill try and fix that.
wow i fixed the misspelled word and then my compiler decided to work.. i know its not because of that but either way very ironic.

anyways thanks for the help!
Topic archived. No new replies allowed.