DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 9 of 9
  1. #1
    Join Date
    Dec 2004
    Location
    Porto - Portugal
    Posts
    34

    Variable doesn't increment in FOR statement

    Hi,

    I have a code like this:

    Code:
    for(i=0;i<n;i++){
    
    vstep = jd + i * increment
    
    calculate(vstep,....)
    
    
    }
    When I run it, the calculate function gives me the results all equal to the first value - vstep doesn't increase.

    Anybody has any idea why?

    Kind regards,

    Kepler

  2. #2
    Join Date
    Dec 2003
    Posts
    3,366
    could be anything. What does calculate have for side effects, does it mess up vstep? What is increment, if it were zero, would that be the issue? Is jd constant or changed by calculate?

    In short, there is no answer without more info ... but the most likely cause is probably a zero increment, assuming you didnt do anything weird.

  3. #3
    Join Date
    Dec 2004
    Location
    Porto - Portugal
    Posts
    34
    Quote Originally Posted by jonnin View Post
    could be anything. What does calculate have for side effects, does it mess up vstep? What is increment, if it were zero, would that be the issue? Is jd constant or changed by calculate?

    In short, there is no answer without more info ... but the most likely cause is probably a zero increment, assuming you didnt do anything weird.
    Hi,

    I've putted the increment equal to 3 and vstep equal to 0:

    Code:
    vstep = 0;
    
    for(i=0;i<n;i++){
    
    vstep = jd + i * 3;
    
    calculate(vstep,....)
    
    
    }
    The function calculates for the first value ( jd ) and then repeats all the values...

    I don't know what's wrong. The routine is simple. The function "calculate" gives different values for different inputs...

    This is puzzling me...

    Kind regards,

    Kepler

  4. #4
    Join Date
    Dec 2003
    Posts
    3,366
    well, if i is 0 to start and increments by 1, the step is 3, and jd is a constant, you should get

    jd, jd+3, jd+6, ... jd + 3* i

    for vstep. The code is correct to do this ASSUMING that jd is not changed each time.

    So my advice is to just do this:

    vstep = 0;
    jd = 0;
    calculate(vstep,....) (to get jd value???)

    for(i=0;i<n;i++)
    {
    vstep = jd + i * 3;
    cout << vstep << endl;
    }

    See what that prints. If vstep is correct, then calculate has an error. If vstep is wrong, what values are expected?

  5. #5
    Join Date
    Dec 2004
    Location
    Porto - Portugal
    Posts
    34
    Hi,

    I did the test. The first part, went well - gave the correct result.
    As for the second part - All repeated....

    I don't understant...

    Since vstep is a double and i is an integer I've tryed:

    vstep = (double) jd + i * 3;

    but it didn't work either. I made an output of vstep for n=20, and vstep came out equal to jd 20 times...

    Kind regards,

    Kepler

  6. #6
    Join Date
    Dec 2003
    Posts
    3,366
    OK other stuff to look at.

    Print i each time in the loop when it is going wrong. 'i' may be a global that is being reset somewhere, or something (???) along those lines? You may also have a memory error (clobber) that changes the value of 'i' or something.

    All I know to do, since all I have is your black box snippets of code, is to tell you to keep printing every possible variable in the system until you figure out what is going wrong and where it goes wrong.

  7. #7
    Join Date
    Dec 2004
    Location
    Porto - Portugal
    Posts
    34
    Hi,

    The i prints allright - but I'm making something wrong... I made a test program:

    Code:
    #include <iostream.h>
    
    
    int main (int argc, char *argv[]) {
    
    double jd, vstep,calc;
    int i,n;
    
    jd = 425.65;
    n = 20;
    
    
    
              for(i = 0;i < n;i++){
    
              vstep = jd + i * 3;
              
              calc = 2 * vstep;
    
              cout << vstep << "\n";
    		  
              }
    
    
    return(0);
    }
    that outputs vstep correctly:

    Code:
    425.65
    428.65
    431.65
    434.65
    437.65
    440.65
    443.65
    446.65
    449.65
    452.65
    455.65
    458.65
    461.65
    464.65
    467.65
    470.65
    473.65
    476.65
    479.65
    482.65
    It's really puzzling me...

    Kepler

  8. #8
    Join Date
    Dec 2003
    Posts
    3,366
    Something odd is going on in the calculate funciton then, if the test program works and the original does not. That is the only difference between them, really.

    If 'i' prints out ok in the original, look at JD or make JD a constant there too.

    The problem can only really be 1 of 4 things:

    1) vstep is broken somewhere
    2) i is broken somewhere
    3) jd is broken somewhere
    4) calculate function does something really funky.

    Also:
    5) Your program is correct and you think its wrong, but it is not (it can happen and at this point is a possible issue).

    If you look in depth (with print statements) at all 4 of those, something has to bubble out as the issue.

    Post the calculate function if you can? I can try to look at that too.
    Last edited by jonnin; 04-04-2011 at 03:32 PM.

  9. #9
    Join Date
    Aug 2004
    Location
    Orange, California
    Posts
    1,252
    You can eliminate 'i' as a problem by making it local to the for block like this:
    Code:
    for(int i=0;i<n;i++){
    
    vstep = jd + i * 3;
    
    calculate(vstep,....)
    
    }

Similar Threads

  1. Structures and Data Referance Methods
    By rrjii2000 in forum .NET
    Replies: 60
    Last Post: 05-04-2010, 04:17 PM
  2. Replies: 1
    Last Post: 09-03-2007, 11:36 PM
  3. I have a funny variable
    By AM003295 in forum VB Classic
    Replies: 4
    Last Post: 07-27-2007, 02:26 PM
  4. GetString Help
    By Chris Yard in forum VB Classic
    Replies: 5
    Last Post: 02-08-2007, 05:44 AM
  5. How to retain session variable
    By NikhilSriv in forum Java
    Replies: 1
    Last Post: 06-14-2006, 05:24 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links