Thread: logic question

  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
    Angola
    Posts
    8,442
    > 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.
    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
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,794
    BTW (w+x+y+z)/4 is int
    It cannot be 2.5
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    David J. Wheeler

  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
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,794
    also the first fin>>temp;
    can make a problem if the file contains only 4 values...
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    David J. Wheeler

  6. #6
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Angola
    Posts
    8,442
    > 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.
    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, 04: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