Problem: Write a program that asks the user for a file name. Assume the filecont
ID: 3611321 • Letter: P
Question
Problem:Write a program that asks the user for a file name. Assume the filecontains a series of numbers, each written on a separate line. Theprogram should read the contents of the file into an array and thendisplay 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
THANKS!
Explanation / Answer
#include #include #include using namespace std; // Globals int *arrayOfInts; int size; string temp; string input; int counter; char *buffer; int total; double avg; // Function Prototypes int getSize(); int getSize() { int retSize = 0; ifstream numberFile(input.c_str()); if (numberFile.is_open()) { while(! numberFile.eof()) { getline(numberFile, temp); retSize++; } numberFile.close(); } return retSize; } void main() { total = 0; avg = 0; buffer = new char[10]; counter = 0; int max = 0; int min = 100; cout > input; size = getSize(); arrayOfInts = new int[size]; ifstream numberFile(input.c_str()); if (numberFile.is_open()) { while(! numberFile.eof()) { numberFile.getline(buffer,10); arrayOfInts[counter] = atoi(buffer); if(arrayOfInts[counter] > max) max = arrayOfInts[counter]; if(arrayOfInts[counter]Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.