473,325 Members | 2,308 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

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:

Expand|Select|Wrap|Line Numbers
  1.  
  2. printf("Please indicate how many generations you wish to calculte: ");
  3.    scanf("%d", &generations);
  4.  
  5.  
  6.  
  7.   for(n=1; n==generations; n++) 
  8.      {
  9.       Population=initial*exp (growth*(1.0-(initial/capacity)));
  10.  
  11.        printf("The population at the next generation will be: %.6f\n", Population);
  12.  
  13.        initial=Population;
  14.      }
  15.  
  16.  
  17.  return(0);
  18.  
  19. }  
  20.  
  21.  


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?
Mar 16 '10 #1

✓ answered by jkmyoung

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)

5 2088
jkmyoung
2,057 Expert 2GB
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)
Mar 16 '10 #2
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
Mar 17 '10 #3
I believe you can just use the n in your print statement

printf("The population at the", n, "generation will be: %.6f\n", Population);
Mar 17 '10 #4
that is too many arguments for the format i had tried it previously do you know of any other way it could work?
Mar 17 '10 #5
newb16
687 512MB
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 sometextsometext %ld\n", 1977, 650000L);
it will work too.
Mar 17 '10 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: Wolfgang Buechel | last post by:
Hi, I want to iterate over all 2x2 matrices with elements in range 0..25 (crypto-stuff). To produce them, first I wrote a fourfold nested for loop: M=26 for a in range(M): for b in range...
15
by: Ian Osgood | last post by:
I just wanted to let folks know of one more reason not to use for/in loops on Arrays. In the following code: var u = for (var i in u) print(.join()); print(typeof(i) + ' ' + typeof(i-0));
4
by: Kathy | last post by:
Hi All, I am using Access 2000. I would like to streamline this code by using a variable for the column name. I have three tables with 255 columns each that I would like to populate with the...
16
by: Joona I Palaste | last post by:
Reading Skybuck Flying's obvious troll message, I thought of how to properly do a for loop that would iterate over the complete range of an unsigned variable. As you all know, Skybuck's method...
18
by: Ken Varn | last post by:
Is there any way to reset a foreach loop to re-iterate through the collection as if it were starting from the beginning? Namely, if I delete an item out of a collection, I want to be able to reset...
6
by: eBob.com | last post by:
How do you make a loop iterate early without using a GoTo? (I guess I've done too much structured programming and I really don't like using GoTos.) Here's my code ... For Each Thing As OFI...
9
by: pointer noob | last post by:
Hi, I am trying to write a bit of code to iterate through memory addresses and if the address is divisable by 2 then write one byte, if not write a different byte. I am having trouble with the...
8
by: Jim Langston | last post by:
There's the thing about iterating though a map or vector when you may delete one of the elements, where you simply assign the iterator to the map.erase() statement or increment it if you don't. ...
22
by: Yakov | last post by:
What would be the nicest way to write the loop for all values of unsigned short (0..0xffff), usnig 'unsigned short port;' as an index ? For comparison, there is just one way to write the for(k=0;...
0
by: Gary Herron | last post by:
Dan Upton wrote: Yes. Create a list of keys, and loop through it: pids = procs_dict.keys() for pid in pids: if procs_dict.poll() != None # do the counter updates del procs_dict Then the...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.