This question pertains to a programming problem that I have stumbled across in my C++ programming class.
The Assignment:
-----------------------------------------------------------------------------------
Number Analysis Program
Write a program that asks the user for a file name. Assume the file contains a series of numbers, each written on a separate line. The program should read the contents of the file into an array and then display the following data:
The lowest number in the array
The highest number in the array
The total of the numbers in the array
The average of the numbers in the array
-----------------------------------------------------------------------------------
My question pertains mainly to reading these numbers from a file, the question doesn't indicate any specific amount of numbers, the list could be any size, I'm at a loss here.
How can I find out how many numbers are in a file,and then use that amount as the iteration count in a for loop to put these numbers in an array?
You can store the numbers in a resizable container ( eg: std::deque ) and then create a dynamic array using the size of that container.
Or you can read all the numbers and increasing a variable each time you find one and when you reach the end of the file, restart but this time filling the array
pseudocode:
1 2 3 4 5 6 7 8
open file
size = 0
while you can read a number
size++
int *array = newint [size]
restart reading
for i=0 -> size
read array[i] from file