logic question

This is a discussion on logic question within the C++ Programming forums, part of the General Programming Boards category; Suppose a file has a sequence of integers:1 2 3 4 5 6 The following is the code. It should ...

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    50

    logic question

    Suppose a file has a sequence of integers:1 2 3 4 5 6
    The following is the code. It should read all the integers and output averages of 4 integers until the file ends.
    The output should be
    2.5 ((1+2+3+4)/4)
    3.5 ((2+3+4+5)/4)
    4.5 ((3+4+5+6)/4)

    Code:
    string filename;
    ifstream fin;
    cin>>filename;
    fin.open(filename.data());
    
    int w,x,y,z,temp;
    fin>>w>>x>>y>>z;
    fin>>temp;
    while (!fin.eof())
    {
        w=x; x=y; y=z; z=temp;
        cout<<(w+x+y+z)/4;
        fin>>temp;
    }
    fin.close();
    }
    I just can't get the logic in the while loop.
    If w=x, why is the first integer not skipped?
    Last edited by Roy01; 12-09-2006 at 08:35 AM.

  2. #2
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,532
    > If w=x, why is the first integer not skipped?

    I suppose your question is why it is being skipped.

    The only explanation I find is that the first character in the file is 0, not a 1.
    If the first character is anything but 0, then that code will be wrong and the solution would be to move w=x; x=y; y=z; z=temp; after the cout statement.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  3. #3
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    BTW (w+x+y+z)/4 is int
    It cannot be 2.5
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  4. #4
    Registered User
    Join Date
    Oct 2006
    Posts
    50
    Thanks! Actually the code is the suggested solution of the task. Maybe there is a mistake.I wonder if w=x; x=y; y=z; z=temp; should be moved after the cout statement.

  5. #5
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    also the first fin>>temp;
    can make a problem if the file contains only 4 values...
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  6. #6
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,532
    > Thanks! Actually the code is the suggested solution of the task. Maybe there is a mistake.I wonder if w=x; x=y; y=z; z=temp; should be moved after the cout statement.

    If the first number in the file is not a zero, then it must.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. platform game logic, problem
    By Akkernight in forum Game Programming
    Replies: 7
    Last Post: 02-23-2009, 10:49 AM
  2. OOP Question DB Access Wrapper Classes
    By digioz in forum C# Programming
    Replies: 2
    Last Post: 09-07-2008, 05:30 PM
  3. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 09:22 PM
  4. God
    By datainjector in forum A Brief History of Cprogramming.com
    Replies: 746
    Last Post: 12-22-2002, 12:01 PM
  5. Very simple question, problem in my Code.
    By Vber in forum C Programming
    Replies: 7
    Last Post: 11-16-2002, 03:57 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21