# Speed Challenge, my solution:

• 03-17-2003
RoD
Speed Challenge, my solution:
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).

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; }```
• 03-17-2003
xmdvp
that's not the fastest way.

the fastest is: get the five values in an array of five int
save the whole array in the file
and reload the array from the file:

total file operations: 2: 1 write and 1 read.
you're doing 5 write and 5 read
• 03-17-2003
RoD
I considerd it, but i didnt think she would accept it because arrays is beyond the scope of what she has taught. But yes, i agree 100%.
• 03-17-2003
xmdvp
actually, in terms of speed you're making 1 write and 5 read.

(because the write after an input prompt are speedless because of the prompt!)
• 03-17-2003
Unreg1
Re: Speed Challenge, my solution:
>>the fastest possible solution
>>Take in 5 integers from user
These two sentences don't go together very well. What's the point of a program that works like lightning, but has to wait for the user to input data, which in CPU terms, is like waiting for eternity. And don't tell me the point is "its an exercise", because their are better speed style lesson.
• 03-17-2003
RoD
Not speed as in the programs execution, speed as in the quickest coded solution. Example, say we both did this challenge and i completed it in 10 minutes, and you in 11, i was faster. I dont like these contests because they diminish program quality imo.

Can we hear from an actual member, please? :p
• 03-17-2003
no-one
on the output, your using buffered I/O so it dont matter anyway, since the data probably wasn't written till you closed the file.

on the input the same thing since the data was probably all read in on the first read.

then again the buffer size is OS an Compiler dependant... so.

well actually so is buffering altogether... but you get the point.

edit:: you forgot toclose your input file. tisk tisk.
• 03-17-2003
Hammer
>>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.
>> speed as in the quickest coded solution.
What is this, a speed typing competition? :rolleyes: Are you learning to be a typist or a programmer?

>>I dont like these contests because they diminish program quality imo
Agreed. Where I work, people can spend ages (days/weeks/months) documenting solutions to problems, before even writing a single line of code. Some, but not all, "quick fixes" lead to expensive mistakes and hours of extra work recovering from their screw-up.
• 03-17-2003
Yawgmoth
Quote:

Originally posted by Hammer

>>I dont like these contests because they diminish program quality imo
Agreed. Where I work, people can spend ages (days/weeks/months) documenting solutions to problems, before even writing a single line of code. Some, but not all, "quick fixes" lead to expensive mistakes and hours of extra work recovering from their screw-up.

I agree too. Haste makes waste.
• 03-17-2003
Speedy5
This is better and faster:

Code:

```if (!infile.fail()) {         while (infile >> value)         {                 sum += value;         } }```
• 03-17-2003
RoD
Quote:

Originally posted by Speedy5
This is better and faster:

Code:

```if (!infile.fail()) {         while (infile >> value)         {                 sum += value;         } }```

I never even thought to do it that way! Hmm, thnx man i'll change that tommorow.
• 03-17-2003
DougDbug
3.5 minuits sounds pretty fast to me!
I think there is some value to this type of exercise. When you are a beginner, you have to 1- Figure out how you're going to do it. 2- Look in books to find the functions and how to use them. 3- Debug all of your errors.... Well... even experienced programmers have to take all these steps, but they don't have to look-up as much, and they don't have to do nearly as much debugging! So, an exercise lile this can let you know how much you "know" and how much you're haviing to look-up and re-do!

A friend of mine, who is an experienced-professional programmer, described a process similar to Hammer. By the time they start writing the code, almost every line has been documented! The actual writing of the code is the easiest part.

Personally, I don't spend that much time "up-front". I seem to spend more time improving the program as I do "making it work". It seems like most of the "improvements" are tweeking the user interface to make it user-proof and making the output display more "meaningful". Of course, this should be planned-out in advance.

P.S.
At least your teacher is still interested in keeping you challenged, and is letting you show your stuff!