Today in class my teacher proposed a simple (for me) challenge and wanted the fastest possible solution, while still being well done, etc. I was the first one done, and mine was imo the best (some ppl had a variable for each input, no loops, etc).
The problem was this:
Take in 5 integers from user, write to a file.
Read values in, and display the sum and average of them.
My solution. I am posting this too see if there was a faster way to do it, or what your opinion is. I wrote it in 3 1/2 minutes.
Code:#include "stdafx.h" #include <iostream> #include <fstream> using namespace std; int main(int argc, char* argv[]) { int value, sum = 0, avg; ofstream outfile; ifstream infile; outfile.open("c:\\myfile"); for (int i = 1; i <= 5; i++) { cout << "Enter an integer value: "; cin >> value; outfile << value << endl; } outfile.close(); infile.open("c:\\myfile"); while (! infile.fail()) { if (infile >> value) { sum += value; } } avg = sum / 5; cout << "Sum: " << sum << endl; cout << "Avg: " << avg << endl; return 0; }