CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10

Thread: for loop issue

  1. #1
    Join Date
    Jul 2009
    Posts
    22

    for loop issue

    Hi all...I'm new to programming and working on a project to determine velocity and position. the problem is the for loop I'm using doesn't follow the rules as I'm trying to implement them. That is, if the velocity (finalVeloc) is equal to or less than -500, the position (finalPos) should be determined by subtracting 500 and the velocity should be set to -500.

    Here's what I have so far:

    Code:
    #include<iostream>
    #include<iomanip>
    #include<cmath>
    
    using namespace std;
    
    double positionCalc(double, double, double);
    double velocityCalc(double, double);
    
    int main(){
        
        double time = 0;
        double initialPos, finalPos, finalVeloc, initialVeloc; 
        
        cout << setw(60) << setfill('*') << " " << endl;
        cout << setw(53) << setfill(' ') << "Sir Isaac Newton's Position/Velocity Calculator" << endl;
        cout << setw(60) << setfill('*') << " " << endl;
        cout << endl;
        cout << endl;
        cout << "This program will calculate the position and velocity of a falling object." << endl;
        cout << endl;
        cout << "Enter the initial height in feet (above 1000): ";
        cin >> initialPos;
            while (initialPos < 1000){
                  cout << "Invalid answer. Must be height over 1000." << endl;
                  cout << "Enter the initial height in feet (above 1000): ";
                  cin >> initialPos;
                  cout << endl;
                  }
        
        cout << endl;          
        cout << "Enter the initial velocity in ft/sec: ";
        cin >> initialVeloc;
            while (initialVeloc < -500){
                 cout << "Invalid answer. Must be speed less than -500 ft/sec." << endl <<endl;
                 cout << "Enter the initial velocity in ft/sec: ";
                 cin >> initialVeloc;
                 cout << endl;
                 }
        
        finalPos = 1000;
        finalVeloc = 0;
        cout << setfill(' ') << "Time" << setw(15) << "Position" << setw(20) << "Velocity" << endl;
        cout << "----" << setw(15) << "--------" << setw(20) << "--------" << endl;
        
        for (time = 0; finalPos >= 1000; time++){
            if (finalVeloc <= -500){
                           finalPos -= 500;
                           finalVeloc = -500;
                           }
            else{
                 finalVeloc = velocityCalc(initialVeloc, time);
                 finalPos = positionCalc(initialPos, initialVeloc, time);
                 }
            cout << setw(4) << setprecision(2) << fixed << time << setw(15) << finalPos << setw(20) << finalVeloc << endl;
            }
        
        system("PAUSE");
        return 0;
    }
    
    //calculates the position of the object
    double positionCalc(double initialPos, double initialVeloc, double time){
           double finalPos;
           finalPos = -16 * pow(time, 2) + initialVeloc * time + initialPos;
           return finalPos;
           }
    //calculates the velocity of the object
    double velocityCalc(double initialVeloc, double time){
           double finalVeloc;
           finalVeloc = -32 * time + initialVeloc;
           return finalVeloc;
           }
    Last edited by reubenpatterson; July 22nd, 2009 at 10:22 AM. Reason: added code

  2. #2
    Join Date
    Jul 2009
    Posts
    22

    Re: for loop issue

    Sorry...the results are coming out as:

    Enter the initial height in feet (above 1000): 6000

    Enter the initial velocity in ft/sec: 10
    Time Position Velocity
    ---- -------- --------
    0.00 6000.00 10.00
    1.00 5994.00 -22.00
    2.00 5956.00 -54.00
    3.00 5886.00 -86.00
    4.00 5784.00 -118.00
    5.00 5650.00 -150.00
    6.00 5484.00 -182.00
    7.00 5286.00 -214.00
    8.00 5056.00 -246.00
    9.00 4794.00 -278.00
    10.00 4500.00 -310.00
    11.00 4174.00 -342.00
    12.00 3816.00 -374.00
    13.00 3426.00 -406.00
    14.00 3004.00 -438.00
    15.00 2550.00 -470.00
    16.00 2064.00 -502.00
    17.00 1564.00 -500.00
    18.00 1064.00 -500.00
    19.00 564.00 -500.00

    where the first velocity value over -500 isn't being caught and so the position is off...thanks in advance for your help.

  3. #3
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: for loop issue

    Code:
        for (time = 0; finalPos >= 1000; time++){
            if (finalVeloc <= -500)
            {
                finalPos -= 500;
                finalVeloc = -500;
            }
    Your calculation simply subtracts 500 from finalPos each time the velocity is large enough. It makes no effort to end the for loop immediately. Is that what you're concerned about?

  4. #4
    Join Date
    Jul 2002
    Location
    Portsmouth. United Kingdom
    Posts
    2,727

    Re: for loop issue

    Code:
        for (time = 0; finalPos >= 1000; time++){
            if (finalVeloc <= -500){
                           finalPos -= 500;
                           finalVeloc = -500;
                           }
            else{
                 finalVeloc = velocityCalc(initialVeloc, time);
                 finalPos = positionCalc(initialPos, initialVeloc, time);
                 }
            cout << setw(4) << setprecision(2) << fixed << time << setw(15) << finalPos << setw(20) << finalVeloc << endl;
            }
    You're calculating the new values after the check but before the print out.
    You need to calculate/check/print.
    "It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
    Richard P. Feynman

  5. #5
    Join Date
    Jul 2009
    Posts
    22

    Re: for loop issue

    It should continue the loop until the position is <= 1000, which it does, but the if statement to change the velocity and position seem ill-placed since 1.) it doesn't catch the velocity when it first exceeds -500 and 2.) modifies the position incorrectly because the functions are still run afterward. I guess I'm looking for some guidance on how to evaluate the velocity and make the changes when the condition is met, but still keep the loop going until a different condition is met.

  6. #6
    Join Date
    Jul 2009
    Posts
    22

    Re: for loop issue

    Quote Originally Posted by JohnW@Wessex View Post
    Code:
        for (time = 0; finalPos >= 1000; time++){
            if (finalVeloc <= -500){
                           finalPos -= 500;
                           finalVeloc = -500;
                           }
            else{
                 finalVeloc = velocityCalc(initialVeloc, time);
                 finalPos = positionCalc(initialPos, initialVeloc, time);
                 }
            cout << setw(4) << setprecision(2) << fixed << time << setw(15) << finalPos << setw(20) << finalVeloc << endl;
            }
    You're calculating the new values after the check but before the print out.
    You need to calculate/check/print.
    This is true, but when I put the calculations first, the finalPos is already calculated incorrectly (because the formula breaks down) and the subtraction is done from that bad value, not the previous good value. If that makes any sense...

  7. #7
    Join Date
    Jul 2002
    Location
    Portsmouth. United Kingdom
    Posts
    2,727

    Re: for loop issue

    Quote Originally Posted by reubenpatterson View Post
    , but when I put the calculations first, the finalPos is already calculated incorrectly (because the formula breaks down) and the subtraction is done from that bad value, not the previous good value.
    Store the previous value before calculation and use it if the test fails, otherwise use the new one.
    "It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
    Richard P. Feynman

  8. #8
    Join Date
    Jul 2009
    Posts
    22

    Re: for loop issue

    I got it! Just talking it out helped...thanks!

    Code:
    for (time = 0; finalPos >= 1000; time++){
            
            finalVeloc = velocityCalc(initialVeloc, time);
            if (finalVeloc <= -500){
                           finalPos -= 500;
                           }
            else{
                 finalPos = positionCalc(initialPos, initialVeloc, time);             
                 }
            cout << setw(4) << setprecision(2) << fixed << time << setw(15) << finalPos << setw(20) << finalVeloc << endl;
            }
    
    //calculates the velocity of the object
    double velocityCalc(double initialVeloc, double time){
           double finalVeloc;
           finalVeloc = -32 * time + initialVeloc;
           if(finalVeloc <= -500){
                         finalVeloc = -500;
                         }
           return finalVeloc;
           }

  9. #9
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: for loop issue

    If the velocityCalc() function returns -502, what is the desired behavior?

  10. #10
    Join Date
    Jul 2009
    Posts
    22

    Re: for loop issue

    Thanks Lindley, the velocityCalc() should never return anything less than -500, that was the logic error. By building in that test I was able to use the finalVeloc value to determine if the positionCalc() should even be called.

    Thanks again...

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured