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.)

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.

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:

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.

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
}

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:

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.

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!

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.