How can I input an unknown number of numbers and have eachone declared as some sore of int ? Is this even possible?
How can I input an unknown number of numbers and have eachone declared as some sore of int ? Is this even possible?
use a loop, type a number, then store the number in either an array or std::vector. The vector would be best because you don't have to know how may items it will hold, it will manage all the memory requirements for you. If you use an int array, such as int*, you have to learn how to do that yourself.
I'm trying to write a program that will add in an unknown series of numbers then divide it by the number of numbers to get an average. Can I use an array for that?Originally Posted by Ancient Dragon
It would be a pain because arrays are fixed size. You could dynamically allocate an array and reallocate everytime you get a new number, but that would just be a waste of time and CPU. As Ancient said, you're easiest choice would be a std::vector.
Sent from my iPadŽ
If you use an array you would have to either have a max amount of numbers, or learn to use dynamic memory. You could also use an std::vector<int> and not have to worry about memory, as the other poster said.
Why drink and drive when you can smoke and fly?
You don't have to store the numbers that have been read in. Just keep a variable that stores the sum and one that stores the count. Each time through your loop you can read in a variable, add it to the sum, and increment the count. When you exit the loop your sum and count will be correct.