### Array Counting Integers

I am trying to figure out how to make the code below count the number of integers entered. My given conditions are for me to make it repeat until the number 10 is entered. If a number that is negative or a number that is greater than 10 is entered it needs to re-ask the user for input.

I have been working the past 5 hours on understanding arrays and passing them into functions after a previous problem I asked since I had a reply that I need to go back to studying the basics. I hope that I am improving! (If you want to see, I only have one other post on the forums so far.)

Thanks for all the help!

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172`` ``````#include #include using namespace std; const int ARRAY_SIZE = 50; double myList[ARRAY_SIZE]; int main () { int number; cout << "Enter a one-digit number or 10 to exit: " << endl; cin >> number; for (int i = 0; i < ARRAY_SIZE; i++) { myList[i] = 0; } int placeholder = 0; while (number >= 0 && number <= 9) { myList[placeholder] = number; placeholder++; cin >> number; } int alreadyPrinted[ARRAY_SIZE]; int printed = 0; bool already = true; for (int i = 0; i < ARRAY_SIZE; i++) { int n = 0; for(int j = 0; j < ARRAY_SIZE; j++) { if(myList[j] == myList[i]) n++; } already = true; for(int j = 0; j != printed; ++j) { if(myList[i] == alreadyPrinted[j]) { already = false; } } if( myList[i] != 0 && already ) { cout << myList[i] << " appears " << n; if(n == 1) { cout << " time." << endl; } else { cout << " times." << endl; } alreadyPrinted[printed++] = myList[i]; } } return 0; }``````
A couple of quick comments. The array `myList[]` is of type double, but all the values are supposed to be single-digit integers.

The user is allowed to input 0, which is a valid single-digit number, but this value doesn't get printed. The instructions displayed should make this clear (but more on this later).

It would be better if the variable `placeholder` was given a more meaningful name, such as `inputCount`, because it is actually counting how many values the user has entered.

At lines 35 and 38, the for loop goes through the entire array `i < ARRAY_SIZE;`. It might be better to use `i < inputCount;` instead.

Firstly, that is more efficient. Secondly, if you encounter an array value which is zero, it must have been entered by the user. therefore it should be counted and output like all the other numbers. Hence line 54 `if( myList[i] != 0 && already )` becomes `if (already)`.

As for the algorithm you use for counting. Well, if I wrote this from scratch, I'd do it differently, but there are in fact lots of different ways to do this, and I've not even decided which approach I'd use.
Last edited on
Thank you so much for your help! I made the changes you suggested and 0 is now counted. However I am still trying to figure out how to make the program prompt the user to re-enter a valid number if its a negative number or greater than 10. I have a feeling it deals with:

 ``123456`` ``````while (number >= 0 && number <= 9) { myList[inputCount] = number; inputCount++; cin >> number; }``````

Is this the right section for me to look at? Or is it somewhere else in my code?
Sounds good so far.

There are three different things you are trying to do here:
• Accept numbers which are valid
• Prompt the user to try again for invalid input
• Finish inputting when the user enters some value (or range of values).

At the moment, the input will end after an out-of-range input.
If you instead prompt for a re-try, you still have to give the user some way to exit.

That's actually a design decision, not a coding one. First decide on the required behaviour, and then start to consider how to code it.
I am guessing it would be a while loop with an if loop inside? This is my guess:

 ``1234567891011121314151617181920`` `````` while (number >= 0 && number <=9) { myList[inputCount] = number; inputCount++; cin >> number; if(number == 10) { cout << "Thanks for your input! "; } else { cout << "Please enter a valid number or 10 to exit: "; cin >> number; } } ``````
Last edited on
Well, you shouldn't be guessing :)

Your suggestion above will exit for any negative number, or any number greater than 9.

I think, if I understand the requirements correctly, you want the value 10 to be the only acceptable way to terminate the input. Any other value is either within range, and you count it, or out of range and you reject it with a prompt for the user to try again.

If that's the case, you would need something like this:

 ``12345678`` ``````while (number != 10) { // either store and count // or // prompt for retry // get the next number from the user }``````

Last edited on
I didn't really mean I was guessing :D (figure of speech I always use for some reason)

Anyways, I am getting extremely close to the solution! Here's the code I have now:

 ``123456789101112131415`` `````` while (number != 10) { if ( number < 0 || number > 10) { cout << "Please enter a valid number or 10 to exit: "; cin >> number; } myList[inputCount] = number; inputCount++; cin >> number; } ``````

My problem now is the output:

Please enter a valid number or 10 to exit: 5
6
9
5
6
9
6
6
55
Please enter a valid number or 10 to exit: 55
55
Please enter a valid number or 10 to exit: 55
55
Please enter a valid number or 10 to exit: 10
10
5 appears 2 times.
6 appears 4 times.
9 appears 2 times.
55 appears 2 times.
10 appears 1 time.

Apparently it does re-ask which is great, however if the user inputs a bad number twice in a row, it counts the wrong number one time. I know your probably tired of reading my code but I really can't thank you enough! So close!

Also the final part after I get this loop done, I want to be able to display my output in a sequential number order like this:

1 appears 2 times
2 appears 5 times
....etc.

Thank you again!
Well, I didn't really mind the figure of speech. What I really was getting at was, before you write any code at all, you need to think about how the program should behave. If necessary, write a description in ordinary English of what you should happen.

After that, you might write a version in pseudocode, which is basically a mixture of ordinary English to summarise a group of actions, together with various programming constructs such as while or if - else.

The benefit of that approach is to avoid getting bogged-down in the fine detail of the language syntax, but instead come up with an overview.

If we return to the fragment I posted previously, it could be expanded a little like this:
 ``1234567891011121314`` ``````while (number != 10) { if (number is in acceptable range) { store it in the array add 1 to count } else { output message for invalid number } get the next number from the user }``````

I try to get people to think at this higher level, what it is they want the program to do, rather than focussing on actual code. The code itself is mostly straightforward, and if not, it can usually to be found in any reference book.
Thank you so much for the pseudocode! I completely forgot about doing that! My program now works 100%!

This was what I changed for any future people who have a similar question:

 ``1234567891011121314151617`` `````` while (number != 10) { if ( number >= 0 && number <= 9) { myList[inputCount] = number; inputCount++; cin >> number; } else { cout << "Please enter a valid number or 10 to exit: "; cin >> number; } } ``````

Again, thank you so much!
2 problems I want to show you, up to you to fix or not.

`int number; ` should be` int number = 0;`

The problem above doesn't appear until you crash your program. you can crash it by typing in a period such as
 0.02
.

when it crashes, number will = the last number entered, really doesn't matter since the program has crashed anyway.

Change your code to this to see what happens when it crashes. It's really stuck in a loop. The comment lines are what I added, remove the // to run it.

 ``1234567891011121314151617181920212223`` ``````int main () { int number; cout << "Enter a one-digit number or 10 to exit: " << endl; cin >> number; // cout << " " << number << endl; // Test for (int i = 0; i < ARRAY_SIZE; i++) { myList[i] = 0; } int placeholder = 0; while (number != 10) { if ( number >= 0 && number <= 9) { myList[inputCount] = number; inputCount++; cin >> number; // cout << " " << number << endl; // Test }``````

So I wanted to learn how to fix that myself and came up with this.
It's not perfect but checks if cin is valid. I chose to exist by changing the number to 10.

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103`` `````` #include #include using namespace std; const int ARRAY_SIZE = 50; int myList[ARRAY_SIZE]; int inputCount=0; int main () { int number=0; cout << "Enter a one-digit number or 10 to exit: " << endl; cin >> number; // Check cin if(!cin) // or if(cin.fail()) { cout << " Input is not a valid number"<< endl; number=10; // Exit } for (int i = 0; i < ARRAY_SIZE; i++) { myList[i] = 0; } int placeholder = 0; while (number != 10) { if ( number >= 0 && number <= 9) { myList[inputCount] = number; inputCount++; cin >> number; // Check cin if(!cin) { cout << " Input is not a valid number"<< endl; number=10; // Exit } } else { cout << "Please enter a valid number or 10 to exit: "; cin >> number; // Check cin if(!cin) { cout << " Input is not a valid number"<< endl; number=10; // Exit } } } int alreadyPrinted[ARRAY_SIZE]; int printed = 0; bool already = true; for (int i = 0; i < ARRAY_SIZE; i++) { int n = 0; for(int j = 0; j < ARRAY_SIZE; j++) { if(myList[j] == myList[i]) n++; } already = true; for(int j = 0; j != printed; ++j) { if(myList[i] == alreadyPrinted[j]) { already = false; } } if( myList[i] != 0 && already ) { cout << myList[i] << " appears " << n; if(n == 1) { cout << " time." << endl; } else { cout << " times." << endl; } alreadyPrinted[printed++] = myList[i]; } } return 0; } ``````

PS. I think you can make this code a little better. There seems to be too many lines for what your doing. I didn't look at that, but for example, you have
 `` `` `` int placeholder = 0;``

Which I don't think is being used currently.

One idea you might play with, is have the user enter numbers until they type a period (or other invalid char such as A-Z a-z etc) which exist the program.
Last edited on
After getting the `cin.fail()` condition, you can pick up the pieces and carry on by using `cin.clear()`, rather than simply exiting.
good idea, I wish I had thought of that :)
Actually one last thing, I swear! My output does not follow sequential order, I am trying to figure out myself how to do it so if anyone could just give me a hint at where I need to look, that would be great!

Please enter a valid number or 10 to exit: 1
3
2
6
4
5
9
8
7
5
2
4
9
1
5
5
5
0
10

My current output is:

0 appears 2 times.
1 appears 2 times.
3 appears 1 time.
2 appears 2 times.
6 appears 1 time.
4 appears 2 times.
5 appears 5 times.
9 appears 2 times.
8 appears 1 time.
7 appears 1 time.

I need it to output like this:

0 appears 2 times.
1 appears 2 times.
2 appears 2 times.
3 appears 1 time.
4 appears 2 times.
5 appears 5 times.
6 appears 1 time.
7 appears 1 time.
8 appears 1 time.
9 appears 2 times.

Thanks for everyone's help! I am learning a lot from all of this.
You could use an array of ten integers, each one will hold the count of how many times each number 0 to 9 occurred.
 ``1234`` `````` int total[10]; for (int i=0; i<10; i++) total[i] = 0; // set all totals to zero ``````

After that, simply loop once through your array `myList[]`, and increment the corresponding element of `total[]`.

Finally, loop once through array `total[]` and print out the index, and the corresponding total.

This suggestion will replace most of your current counting and output section. The only part which remains useful is the bit where you output the text along with the values, and that will need to be adapted.
Last edited on
Topic archived. No new replies allowed.