Write a program which reads a stream of integers from a file and stores them in
ID: 3568085 • Letter: W
Question
Write a program which reads a stream of integers from a file and stores them in an array. The array is then analyzed to compute the average of all the values in the array and finally all of the values that are above the average should be printed out to the screen. Specifically, you must write three functions: main(), read_into_array(), and print_above_average().
main() gets the name of the input file from the user and opens the file. read_into_array() is then called to read values from the file and store them in the array. It must be passed at least three arguments: the opened file stream, the array, and the total number of slots in the array. You can assume that there will be a maximum of 100 values in the input file and size your array appropriately. Note, however, that there might be less than 100 values in the file.
Finally, print_above_average() should be called to read through the array, compute the average, and then print out all values in the array that are above the average. In particular, for each value above the average it should print the index in the array and the value itself. print_above_average() should take two arguments: the array and the actual number of values in the array. Note that this second argument is not the total number of elements that the array can hold, but is instead the number of values read from the file. For example, the array should be able to hold up to 100 values, but there might have only been 15 values in the file.
Explanation / Answer
#include #include #include #include using namespace std; void read_into_array(ifstream& input, int vals[], int size); void print_above_avg(int vals[], int size); int main() { int vals[100]; ifstream input; string inf; cout > inf; input.open(inf.c_str()); if(input.fail()) { coutRelated Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.