Loop won't iterate

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • abrown07
    New Member
    • Jan 2010
    • 27

    Loop won't iterate

    I am trying to create a program and it works perfectly except the program ignores my loop. Here is the piece of the code that this involves:

    Code:
    printf("Please indicate how many generations you wish to calculte: ");
       scanf("%d", &generations);
     
     
     
      for(n=1; n==generations; n++) 
         {
          Population=initial*exp (growth*(1.0-(initial/capacity)));
                
           printf("The population at the next generation will be: %.6f\n", Population);
           
           initial=Population;
         }
          
         
     return(0);
       
    }


    The problem I'm having is that the program simply ends after the number of desired generations is indicated. Is this because I cannot ask the loop to execute for the number of generations? If so, how would I be able to make that happen?
  • jkmyoung
    Recognized Expert Top Contributor
    • Mar 2006
    • 2057

    #2
    You want n <= generations.

    This is a common mistake. The for loop continues for as long as the rule is true, beginnning with the first iteration. (It's not the ending criteria)

    Comment

    • abrown07
      New Member
      • Jan 2010
      • 27

      #3
      Thanks,
      that was an easy fix!! Im glad haha

      also do you know of anyway that the output statment can include a count as well, like say I want to give 50 generations of output right now I have it stating

      "The population at the next generation will be : "
      50 times all the way down.

      is there anyway that I can make the output say:

      "The population at generation 2 will be: "
      "The population at generation 3 will be: "
      and so on

      Comment

      • boltster
        New Member
        • Mar 2010
        • 7

        #4
        I believe you can just use the n in your print statement

        printf("The population at the", n, "generation will be: %.6f\n", Population);

        Comment

        • abrown07
          New Member
          • Jan 2010
          • 27

          #5
          that is too many arguments for the format i had tried it previously do you know of any other way it could work?

          Comment

          • newb16
            Contributor
            • Jul 2008
            • 687

            #6
            Yes. Start with google, type 'printf', go to the very first link, scroll down to examples section, look at the line
            printf ("Decimals: %d %ld\n", 1977, 650000L);
            Each format specifier will be replaced with corresponding value in the output.
            if you change it to
            printf ("Decimals: %d sometextsometex t %ld\n", 1977, 650000L);
            it will work too.

            Comment

            Working...