GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 02-Nov-2009, 22:26
d0ll3rb1ll d0ll3rb1ll is offline
New Member
 
Join Date: Nov 2009
Posts: 6
d0ll3rb1ll is on a distinguished road

Need help creating a counting loop


I need a loop that will increase a variable start_pop by 10% each time and stop when it is larger than end_pop. While I do this I need to increase another variable year by 1 each time the loop runs.

I got this but I doesn't do what I want it to do, hopefully someone can help me. Thanks. So if start_pop is 10 and end_pop is 11 I need to return 1 for year.


CPP / C++ / C Code:
 year = 0;

    while (start_pop < end_pop)
       
        {
                start_pop = (.1 * start_pop) + start_pop;
                
                year++;

        }
Last edited by admin : 04-Nov-2009 at 03:14. Reason: Please insert your example C/C++ codes between [CPP] and [/CPP] tags
  #2  
Old 03-Nov-2009, 09:10
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,218
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Need help creating a counting loop


Quote:
Originally Posted by d0ll3rb1ll
...doesn't do what I want it to do...

I have a couple of observations and a couple of suggestions:


Observation number 1:
I made a program with that loop that does exactly what I want it to do. See Footnote.

However, in general (and this is, in my opinion, more important)...

Observation number 2:
Because of the ever-present possibility of roundoff error in in the representation of non-integer values of floating point variables and because of the ever-present possibility of roundoff error in calculations involving floating point variables, making a loop that depends comparisons of the values floating point variables may not do what you want it to do.


Suggestion number 1:
Put a print statement inside the loop so that each time through the loop, it prints out values of start_pop, 0.1*start_pop, and year. Maybe use different values for start_pop and end_pop so that you might learn more about the behavior of your loop.

If you see something that you don't understand then:

Suggestion number 2:
Show us the code. The entire code so that we can see things like your variable declarations, etc.

Show us the output from your program (with the extra print stuff that I recommended).

Tell us exactly what you don't understand about the difference between what you expected to see and what the program actually presented.


Regards,

Dave

Footnote: In addition to posting your complete program, you might tell us what compiler and what version of compiler you are using. Sometimes it makes a difference to people who would like to help.
Last edited by davekw7x : 03-Nov-2009 at 09:44.
  #3  
Old 03-Nov-2009, 17:22
d0ll3rb1ll d0ll3rb1ll is offline
New Member
 
Join Date: Nov 2009
Posts: 6
d0ll3rb1ll is on a distinguished road

Re: Need help creating a counting loop


I am using the gcc compiler, I don't know the version but I'm guessing that its the latest.


CPP / C++ / C Code:
#include <stdio.h>

void instruction(void);
double years_to_pop(double start_pop, double end_pop, double year);

int main ()
{
double start_pop, end_pop;
double year = 0.0;

instruction();
        scanf("%lf %lf", &start_pop, &end_pop);


double years_to_pop (double start_pop, double end_pop, double year);


        printf("It will take %.2d years to get to a population %f\n", year, end_pop);

return 0;
}

void instruction(void)
{
        printf("Enter value of start and end population.\n");
        printf("Starting population: \n");
        printf("Ending population: \n");
}

double years_to_pop (double start_pop, double end_pop, double year)
{

    while (start_pop <= end_pop)
        {
                printf("%f\n", start_pop);
                start_pop = (.1 * start_pop) + start_pop;

                printf("%f\n", start_pop);

                year = year + 1;
                printf("%f\n", year);

        }
return year;
}

When returning values I get some random number like 12737924593 for years and 0.00 for ending population. And the printfs in the loop don't show up, and I'm completely lost.
Last edited by admin : 04-Nov-2009 at 03:15. Reason: Please insert your example C/C++ codes between [CPP] and [/CPP] tags
  #4  
Old 03-Nov-2009, 18:47
fakepoo fakepoo is offline
Regular Member
 
Join Date: Oct 2007
Posts: 713
fakepoo is a jewel in the roughfakepoo is a jewel in the roughfakepoo is a jewel in the rough

Re: Need help creating a counting loop


First, you should probably print the user input after you read it so that you know you read it correctly. Many times, scanf() can be a major pain.

Secondly, you are not calling your function correctly:
CPP / C++ / C Code:
#include <stdio.h>

void instruction(void);
double years_to_pop(double start_pop, double end_pop, double year);

int main ()
{
  double start_pop, end_pop;
  double year = 0.0;

  instruction();
  scanf("%lf %lf", &start_pop, &end_pop);

  // THIS IS NOT THE RIGHT WAY TO CALL A FUNCTION
  //double years_to_pop (double start_pop, double end_pop, double year);

  // TRY THIS INSTEAD
  year = years_to_pop (double start_pop, double end_pop, double year);

  printf("It will take %.2d years to get to a population %f\n", year, end_pop);

  return 0;
}

void instruction(void)
{
  printf("Enter value of start and end population.\n");
  printf("Starting population: \n");
  printf("Ending population: \n");
}

double years_to_pop (double start_pop, double end_pop, double year)
{

  while (start_pop <= end_pop)
  {
    printf("%f\n", start_pop);
    start_pop = (.1 * start_pop) + start_pop;
    printf("%f\n", start_pop);
    year = year + 1;
    printf("%f\n", year);
  }

  return year;
}
  #5  
Old 04-Nov-2009, 08:49
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,218
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Need help creating a counting loop


Quote:
Originally Posted by d0ll3rb1ll
I am using the gcc compiler, I don't know the version but I'm guessing that its the latest.
In this case it really doesn't matter, but for future reference, you don't need to guess; you can find the version by entering the following from a command line:

Code:
gcc --version
Quote:
Originally Posted by d0ll3rb1ll
...When returning values I get some random number like 12737924593 for years and 0.00 for ending population.
I strongly recommend that you turn on all compiler warnings. For example, if my program file is named population.c, I would always use the following. (Yes, always. Well usually almost always.)

Code:
gcc -Wall -W -pedantic population.c -o population


Now as for debugging: Pay attention to all compiler messages. For simple programs, I don't think there is any excuse for not letting the compiler help you as much as it can. Sometimes warnings are benign, but for beginners, I think they should take all warnings to heart and I personally would not accept a program that was not clean.
With the warning flags turned on, you will see something like the following:
Code:
z.c:15: warning: ISO C90 forbids mixed declarations and code
Maybe the message doesn't mean much to you, so I suggest that you look, really look at line 15

CPP / C++ / C Code:
double years_to_pop (double start_pop, double end_pop, double year);
That's how you declare a function, not how you call a function. (You don't put the data type for the arguments.)

Change it to something that calls the function and uses the return value:
CPP / C++ / C Code:
        year = years_to_pop (start_pop, end_pop, year);

and try again.

This time pay attention to all warnings and try to fix them. If you don't understand what happens, then

1. Post the latest code that you are using.

2. If there were any compiler messages, paste them into the post.

3. If the program runs but gives unexpected results, then show us the output and tell us what you don't understand.


Regards,

Dave

Footnote: Why not make the debugging messages a little more verbose so that you (and we) can know exactly where the output came from.

For example

CPP / C++ / C Code:
double years_to_pop(double start_pop, double end_pop, double year)
{
    printf("In years_to_pop: start_pop = %f, end_pop = %f, year = %f\n",
            start_pop, end_pop, year);
    while (start_pop <= end_pop) {
        printf("1: start_pop = %f\n", start_pop);
        start_pop = (.1 * start_pop) + start_pop;
        printf("2: start_pop = %f\n", start_pop);
        year = year + 1;
        printf("3: year = %f\n\n", year);
    }

    printf("4: returning %f\n", year);
    return year;
}
 
 

Recent GIDBlogProgramming ebook direct download available by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Error compiling Apache 2.2.10 tiagofgarcia Apache Web Server Forum 2 10-Nov-2009 14:31
Classic recursion game of 8 Queens errors BKizzle77 Java Forum 11 11-Aug-2008 01:35
Text-Based Roulette Game mfm1983 C++ Forum 5 29-Nov-2006 13:20
How to prevent caption bar from blocking message loop? ckorda MS Visual C++ / MFC Forum 0 12-Apr-2006 15:22
Counting a certain interval through a loop Darth Predator C++ Forum 7 28-Sep-2005 01:34

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 13:15.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.