CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Feb 2009
    Posts
    16

    for loop stop condition help

    hallo, I am new to this forum so I hope this isn't a trivial question. I have written a simple program to calculate the stopping distance of a vehicle and was wondering how I stop the for loop when the function gets to 0 or less. I was thinking maybe I could assign a character to the solution "s" with an if statement. Like s=NEG if s<=0 then using that as the stop condition.
    Any help would be appreciated.

    Code:
    #include <iostream.h>
    #include <iomanip.h>
    float f(float, float, float); //function prototype
    int main(){
            float v,u,g,m,h,s[100],T[100];
            int i, n;
            cout << "values of v,u,m,n,h are (n must be < 100) \n";
            cin >> v >> u >> m >> n >> h;
            s[0] = 0;
            T[0] = 0.5*m*v*v;
            for(i = 0; i<n ;i++){//this is the euler loop
                    T[i+1] = T[i] + h * f(u,m,g);
                    s[i+1] = s[i] + h;
            }
            //set headings for table and then print table
            cout << setw(0) << "s (Distance)" << setw(20) << "T (Kinetic Energy)" << "\n";
            cout.setf(ios :: fixed);
            for(i = 0; i <= n ; i++)
            cout << setprecision(0) << setw(7) << s[i]
                 << setprecision(0) << setw(20) << T[i] << endl;
            return 0;
            }
            //this is the function f of the differential equation
            float f(float u, float m, float g){
                    g = 9.8;
                    return -g*m*u;
            }
    Last edited by Marc G; February 12th, 2009 at 01:48 PM. Reason: Added code tags

  2. #2
    Join Date
    Sep 2004
    Location
    Holland (land of the dope)
    Posts
    4,123

    Re: for loop stop condition help

    Welcome to Codeguru. Please use tags for posting code.

    Like s=NEG if s<=0 then using that as the stop condition.
    Something like this ?
    Code:
    for(i = 0; i<n && s<=0;i++)
    Extra tip for your code. Stop using names like x, y, f, s, m. Give your functions and variables decent names.

  3. #3
    Join Date
    Apr 1999
    Posts
    27,449

    Re: for loop stop condition help

    Quote Originally Posted by revolution2000 View Post
    hallo, I am new to this forum so I hope this isn't a trivial question. I have written a simple program
    Code:
    #include <iostream.h>
    #include <iomanip.h>
    Incorrect. The correct, standard C++ headers are <iostream> and <iomanip>, not <iostream.h> and <iomanip.h>. Your code will not compile for Visual Studio 2003 and above.

    If you got this information to use <iostream.h> and <iomanip,h> from a book, get another book (one that teaches C++ as of 1998). If you got this information from a teacher, get another teacher.

    A proper C++ "Hello World" program is as follows:
    Code:
    #include <iostream>
    
    int main()
    {
       std::cout << "Hello World";
    }
    Note the things that your code is missing from the "Hello World" example above.

    Regards,

    Paul McKenzie

  4. #4
    Join Date
    Feb 2009
    Posts
    16

    Re: for loop stop condition help

    Thank you for your help firstly. I will use the tags in future. I use a numerical methods in C++ book dated from 1999. I am also restricted to using a Borland C++ edit program and haven't learnt to use anything else yet. Very, very new to this. It's not that easy, making your help greatly appreciated.

    So this means I can stick any logic statement (not sure if that's what it is) in the for loop. Would a while loop do something similar?

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