Thread: while and for loops!

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    16

    Unhappy while and for loops!

    ok, so basically heres my problem...in my program i have to write to my output file column of time, against x(t)..so i have to output time[i] and x[i], where
    time[0]=0 and x[0] is given by reader input.

    but i have to only display time until some parameter known as p..But I cant seem to get it to stop when time[i]<p..it just keeps on listing time for all values up to the size of my array..with a while loop i can get time[i]<p but then my values of x[i] are all 0 which is cleary wrong..just need some tips on how to constuct the loops..at the moment it looks like this:

    Code:
    time[0]=0;
    time_increment =1.0;
    fprintf(output, "%f %f\n", time[0], x[0]);
     i=0;
      i++;
      while((time[i]>=0)&&(time[i] < p))
      {
       i++;
        
        time[i] = i*time_increment;
        v[i+1] = v[i] - (g*v[i] + w2*x[i])*time_increment + (1/m)*f[i]*time_increment;
        x[i+1] = x[i] + v[i+1]*time_increment;
      
    	fprintf(output, "%f %f\n", time[i], x[i]);
      }
    also, as u can see i cant seem to get it to display time[0] and x[0] automically for i=0, in my output file..how can i fix this?

    thanx for any help at all!
    }

    my first draft i used a for loop, but i did not know what the statment to be evaluted could be other than time[i]<p, which clearly doesnt work for the for loop.

    Code:
    
      for(i=0; time[i] <p; i++)
      {
     
       
     
        
        time[i] = i*time_increment;
        v[i+1] = v[i] - (g*v[i] + w2*x[i])*time_increment + (1/m)*f[i]*time_increment;
    	x[i+1] = x[i] + v[i+1]*time_increment;
      
    	fprintf(output, "%f %f\n", time[i], x[i]);
      }

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    You should avoid using variables called time -- time() is a function from <time.h>, and calling variables time hides time() and might make readers confused.

    I'll quote a recent PM here:
    A for loop of the form
    Code:
    for(initialization; condition; increment) {
        code;
    }
    is syntactically equivalent to a while loop of the form
    Code:
    initialization;
    while(condition) {
        code;
        increment;
    }
    Perhaps that will help you figure out what to do . . . .
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    As you can see, the forum messes up your code when you mix spaces and tabs - so don't! Stick to one and keep it that way. I recommend tabs.
    Keep your indentation consistent! Don't indent one space at once space and two more spaces at one line in the same block! All should be the same - one tab for each block - no more, no less.
    This is helpful in making others reading your code correctly. No one wants to go through a poorly indented code! Make yourself favor (and everyone else) - indent properly.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Quote Originally Posted by Elysia View Post
    As you can see, the forum messes up your code when you mix spaces and tabs - so don't! Stick to one and keep it that way. I recommend tabs.
    Actually, I don't. Here's why: if you go to edit code with spaces in the forum's quick or advanced reply boxes directly, you can type spaces enough to match the OP's indentation. However, typing a tab doesn't work: you just tab to the next widget or whatever. You have to copy a tab and paste it whenever you want to add a new line.

    Not to mention that tabs are 8 spaces on this forum, which is usually more than (double?) what tabs are usually set to in our editors. Posts take up a lot of horizontal space this way; you might even have to end up scrolling sideways, depending on your screen etc.

    Plus I always use spaces instead of tabs in all of my code anyway.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by dwks View Post
    Actually, I don't. Here's why: if you go to edit code with spaces in the forum's quick or advanced reply boxes directly, you can type spaces enough to match the OP's indentation. However, typing a tab doesn't work: you just tab to the next widget or whatever. You have to copy a tab and paste it whenever you want to add a new line.
    So what does the have to do with it? You should be using tabs in your editor!
    And there's a very handy extension for FF that allows you to insert tabs into text fields. Plus when I don't reply from home, I just open notepad, do a tab and copy it.
    Problem solved.

    Not to mention that tabs are 8 spaces on this forum, which is usually more than (double?) what tabs are usually set to in our editors. Posts take up a lot of horizontal space this way; you might even have to end up scrolling sideways, depending on your screen etc.
    Forum problem. If you really want, you can use tabs -> spaces conversion for temporary posting of code on forum. But that doesn't mean you shouldn't use tabs in your editor.

    Plus I always use spaces instead of tabs in all of my code anyway.
    Well, I don't - I always use tabs and no exceptions and no spaces
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed