Thread: Speed Challenge, my solution:

  1. #1
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331

    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).

    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;
    }

  2. #2
    xmdvp
    Guest
    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

  3. #3
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    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%.

  4. #4
    xmdvp
    Guest
    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!)

  5. #5
    Unreg1
    Guest

    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.

  6. #6
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    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?

  7. #7
    Has a Masters in B.S.
    Join Date
    Aug 2001
    Posts
    2,263
    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.
    Last edited by no-one; 03-17-2003 at 01:34 PM.
    ADVISORY: This users posts are rated CP-MA, for Mature Audiences only.

  8. #8
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>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? 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.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  9. #9
    ! |-| /-\ +3 1337 Yawgmoth's Avatar
    Join Date
    Dec 2002
    Posts
    187
    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.
    L33t sp3@k sux0rz (uZ it t@k3s 10 m1|\|ut3s 2 tr@nzl@te 1 \/\/0rd & th3n j00 h@\/3 2 g3t p@$t d@ m1zpelli|\|gz, @tr0(i0u$ gr@mm@r @|\|d 1n(0/\/\pr3#3|\|$1bl3 $l@|\|g. 1t p\/\/33nz j00!!

    Speling is my faverit sujekt

    I am a signature virus. Add me to your signature so that I may multiply.

  10. #10
    Registered User
    Join Date
    Jan 2003
    Posts
    648
    This is better and faster:

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

  11. #11
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    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.

  12. #12
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398

    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!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. I am very new . . . :(
    By Eternalglory47 in forum C++ Programming
    Replies: 6
    Last Post: 09-05-2008, 11:29 AM
  2. Flight Simulator Wind Speed!!
    By Dilmerv in forum C++ Programming
    Replies: 6
    Last Post: 03-20-2006, 12:40 AM
  3. Replies: 6
    Last Post: 01-08-2006, 02:49 PM
  4. anyone know the solution?
    By heeroyung in forum C Programming
    Replies: 15
    Last Post: 09-30-2005, 06:46 AM
  5. Future Contests: Speed Coding
    By Eibro in forum Contests Board
    Replies: 58
    Last Post: 03-09-2003, 08:31 AM