# Thread: Loops & variables

1. ## Loops & variables

I'm writing a program which requires me to use a do while loop.

The specifications of the program include

1. Allow the user to enter any numbers and when the user presses zero to follow the following information..

The numbers that the user has entered (not including 0)
The average of the numbers (not including 0)
The smallest number entered (not including 0)
The largest number entered (not including 0)
The difference between the largest and the smallest number.

The problem that I am having is how do I get the variable to store more than one number???

2. At a maximum,

You'll need an array of variables for the values the user entered.
You'll use another variable for the accumulator
You'll use another for the number of variables you process
You'll use another for the min
You'll use another for the max
You'll need another for the delta between the min and max.

Todd

3. The problem that I am having is how do I get the variable to store more than one number???
Use an array, or rather, a std::vector since you need a dynamic array.

4. We haven't learned std or vectors

we have learned character arrays would I be able to use that?

I do not have my written program at hand but I use a for loop as a counter so when I got the average I was able to divide it

5. we have learned character arrays would I be able to use that?
No, you need an array of int, not char. Also, since you only know about arrays, your loop must check that the user does not enter so many numbers before entering 0 that it goes past the bounds of your array.

6. Yes, of course you can use an array of chars. You'll just have to convert them to ints each time you need a value. More work, but certainly a valid approach.

Todd

7. Yes, of course you can use an array of chars. You'll just have to convert them to ints each time you need a value. More work, but certainly a valid approach.
True, but that would limit the range of each input entered to the range of a char, and I suspect that that is an unnecessary limitation here.

EDIT:
Besides, by "array of characters" SLM is probably thinking of null terminated C-style strings.

8. Well, an array of chars would indeed be limiting. An array of strings would not.

Todd

Popular pages Recent additions