Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

HELLO im very slow and basic with these programs, please keep it as simple as po

ID: 3546323 • Letter: H

Question

HELLO im very slow and basic with these programs, please keep it as simple as possible. keep in mind that the data file has  a large amount of numbers.


A given data file called data.txt (see attached file included) contains integer numbers. Write a program that will read all the integer numbers and find the smallest, the largest, the sum, and the average values. Sort all numbers read in ascending order and output all results into another file name.


A given data file called data.txt (see attached file included) contains integer numbers. Write a program that will read all the integer numbers and find the smallest, the largest, the sum, and the average values. Sort all numbers read in ascending order and output all results into another file name.

Explanation / Answer

TRY THIS //
#include<iostream> #include<fstream> #include<vector> #include<string> #include<algorithm>
using namespace std;
int main() { vector<int> vec;
ifstream inFile; ofstream outFile; string inFilename; string outFilename = "results.txt"; string number;
cout << "Enter the name of the .txt file to be analyzed : "; cin >> inFilename; cout << endl;
inFile.open(inFilename.c_str());
if(inFile.fail()) { cout << "Unable to find file " << endl; inFile.close(); return 1; }
while(!inFile.eof()) { getline(inFile, number, ' '); if(number.length() == 0 || number == " ") continue; int i = atoi(number.c_str()); vec.push_back(i); }
inFile.close();
int min = vec[0]; int max = vec[0]; int sum = 0; for(int i = 1; i < vec.size(); i++) { if (vec[i] < min) min = vec[i]; if (vec[i] > max) max = vec[i]; sum += vec[i]; }
double average = (double)sum/vec.size(); outFile.open(outFilename, ofstream::out); outFile << "Smallest is " << min << endl; outFile << "Largest is " << max << endl; outFile << "Sum is " << sum << endl; outFile << "Average is " << average << endl;
sort(vec.begin(), vec.end()); // write them to a line on separate lines outFile << endl << "The sorted numbers are" << endl << endl; for(int i = 0; i < vec.size(); i++) outFile << vec[i] << endl;
outFile.close(); return 0; }