Please help out a noob

This is a discussion on Please help out a noob within the C Programming forums, part of the General Programming Boards category; Code: #include <stdio.h> #include <math.h> main() { double init_pop, births, deaths, time, dif, final_pop, exp; printf("Enter the initial population in ...

  1. #1
    Pop
    Pop is offline
    Registered User
    Join Date
    Oct 2006
    Location
    The Matrix, of course
    Posts
    7

    Please help out a noob

    Code:
    #include <stdio.h>
    #include <math.h>
    main()
    {
      double init_pop, births, deaths, time, dif, final_pop, exp;
      printf("Enter the initial population in millions:");
      scanf("%lf, &init_pop");
      printf("Enter the number of births/thousand:");
      scanf("%lf, &births");
      printf("Enter the number of deaths/thousand:");
      scanf("%lf, &deaths");
      printf("Enter the number of years (t):");
      scanf("%lf, &time");
      dif = births - deaths;
      exp = dif * time;
      final_pop = init_pop * (pow(M_E, exp));
      init_pop *= 1000000;
      deaths *= 1000;
      births *= 1000;
    
    printf("This model predicts with an initial population of %lf,"
           "a birth rate of %lf,"
           "and a death rate of %lf,"
           "the population will be %lf in %lf years.\n", init_pop, births, deaths, final_pop, time);
    
    return 0;
    }
    please help me to make this work right... im not sure if any addition information is needed, im really new to this kind of stuff...i think the code is fairly self explanitory as to what it is trying to do, but ill answer as many questions as i can, thanx.

  2. #2
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,752
    You could start by fixing these:
    Code:
    Owner@pavilion ~
    $ splint pop.c
    Splint 3.1.1 --- 02 May 2003
    
    pop.c: (in function main)
    pop.c:7:3: No argument corresponding to scanf format code 1 (%lf):
                  "%lf, &init_pop"
      Types are incompatible. (Use -type to inhibit warning)
       pop.c:7:12: Corresponding format code
    pop.c:7:3: Return value (type int) ignored: scanf("%lf, &ini...
      Result returned by function call is not used. If this is intended, can cast
      result to (void) to eliminate message. (Use -retvalint to inhibit warning)
    pop.c:9:3: No argument corresponding to scanf format code 1 (%lf):
                  "%lf, &births"
       pop.c:9:12: Corresponding format code
    pop.c:9:3: Return value (type int) ignored: scanf("%lf, &bir...
    pop.c:11:3: No argument corresponding to scanf format code 1 (%lf):
                   "%lf, &deaths"
       pop.c:11:12: Corresponding format code
    pop.c:11:3: Return value (type int) ignored: scanf("%lf, &dea...
    pop.c:13:3: No argument corresponding to scanf format code 1 (%lf):
                   "%lf, &time"
       pop.c:13:12: Corresponding format code
    pop.c:13:3: Return value (type int) ignored: scanf("%lf, &time")
    pop.c:14:9: Variable births used before definition
      An rvalue is used that may not be initialized to a value on some execution
      path. (Use -usedef to inhibit warning)
    pop.c:14:18: Variable deaths used before definition
    pop.c:15:15: Variable time used before definition
    pop.c:16:31: Unrecognized identifier: M_E
      Identifier used in code has not been declared. (Use -unrecog to inhibit
      warning)
    pop.c:16:15: Variable init_pop used before definition
    
    Finished checking --- 13 code warnings
    splint is just the name of a type of lint program. lint programs perform a more thorough check of the code than compilers do. I recommend finding and using one for your C. If you need further explanation regarding anything, please ask.

  3. #3
    Pop
    Pop is offline
    Registered User
    Join Date
    Oct 2006
    Location
    The Matrix, of course
    Posts
    7
    er...sorry, i dont understand how to fix them. all of the errors that you gave me i thought were right. like it says i use int_pop before setting a value, but i declare it at the top, and then request it later on. and what is wrong with my scanfs? they seem to be the main part of the problem.

  4. #4
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,752
    scanf grabs a line of input and compares it with a format string. It attempts to convert your input to the type specified in the format and store it in a variable in the list.

    You wrote:
    Code:
    scanf("%lf, &init_pop");
    when it should be
    Code:
    scanf("%lf", &init_pop);
    otherwise, you get a compile time error for too-few arguments to a function.

    Variables also need to hold a value before you attempt to use them in an expression.
    double init_pop = 0.0; et cetera. Anything else is undefined and risky.

  5. #5
    Pop
    Pop is offline
    Registered User
    Join Date
    Oct 2006
    Location
    The Matrix, of course
    Posts
    7
    oh! awesome! i completely forgot the second quotes, ill go try it.

  6. #6
    Pop
    Pop is offline
    Registered User
    Join Date
    Oct 2006
    Location
    The Matrix, of course
    Posts
    7
    ok, heres my new code, but for some reason when i run it, i get all 0's for my answer... it compiles though, so thanks for that much...do you have any idea why it gives me all 0s?

    Code:
    #include <stdio.h>
    #include <math.h>
    main()
    {
      double init_pop, births, deaths, time, dif, final_pop, exp;
      printf("Enter the initial population in millions:");
      scanf("%lf", &init_pop);
      printf("Enter the number of births/thousand:");
      scanf("%lf", &births);
      printf("Enter the number of deaths/thousand:");
      scanf("%lf", &deaths);
      printf("Enter the number of years (t):");
      scanf("%lf", &time);
      dif = births - deaths;
      exp = dif * time;
      final_pop = init_pop * (pow(M_E, exp));
      init_pop *= 1000000;
      deaths = deaths * 1000;
      births = deaths * 1000;
    
    printf("This model predicts with an initial population of %i,"
           "a birth rate of %lf,"
           "and a death rate of %lf,"
           "the population will be %lf in %lf years.\n", init_pop, births, deaths, final_pop, time);
    
    return 0;
    }

  7. #7
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,752
    Quote Originally Posted by citizen
    Variables also need to hold a value before you attempt to use them in an expression.
    double init_pop = 0.0; et cetera. Anything else is undefined and risky.
    Code:
    int main(void)
    {
        double init_pop = 0.0, births = 0.0, deaths = 0.0, time = 0.0, dif = 0.0, final_pop = 0.0, exp = 0.0;
        /* Indentation is your friend */
       /*... */
    [edit] Oh, and use %g to print a double. It's becoming standard soon, last I heard.
    Last edited by whiteflags; 10-12-2006 at 02:50 AM.

  8. #8
    Pop
    Pop is offline
    Registered User
    Join Date
    Oct 2006
    Location
    The Matrix, of course
    Posts
    7
    Heres the new code...it seems to all work, except that the final answer of what the population is turns out to be 0 everytime. thanks for all your help so far, you've helped a ton, but can you help me with this last problem? i dont think it is my equations, so i think i used a command wrong or something like that, but im not sure.

    Code:
    #include <stdio.h>
    #include <math.h>
    int main(void)
    {
       double init_pop=0.0, births=0.0, deaths=0.0, time=0.0, dif=0.0, final_pop=0.0, exp=0.0;
        printf("Enter the initial population in millions:");
          scanf("%lf", &init_pop);
        printf("Enter the number of births/thousand:");
          scanf("%lf", &births);
        printf("Enter the number of deaths/thousand:");
          scanf("%lf", &deaths);
        printf("Enter the number of years (t):");
          scanf("%lf", &time);
        init_pop *= 1000000;
        deaths *= 1000;
        births *= 1000;
        dif = births - deaths;
        exp = dif * time;
        final_pop = init_pop * (pow(M_E, exp));
    
    printf("This model predicts with an initial population of %lf,\n"
           "a birth rate of %lf,\n"
           "and a death rate of %lf,\n"
           "the population will be %Lf in %lf years.\n", init_pop, births, deaths, final_pop, time);
    
    return 0;
    }

  9. #9
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,752
    I had a thought.

    M_E is an extremely tiny number. I think you are using pow() backward. It computes x to the power y, so try:

    pow(exp, M_E)

    in your calculation.

    Also, please change instances of %lf in printf to %g.

  10. #10
    Pop
    Pop is offline
    Registered User
    Join Date
    Oct 2006
    Location
    The Matrix, of course
    Posts
    7
    hmmm...the equation i was given, written in standard math format, was this->

    P=p(e^(k*t))

    where P=final pop, p=starting pop, k=births-deaths, t=time.

    note - ^=exponent

    did i write the code for that wrong?

  11. #11
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,752
    No, I'm sorry. Nevermind.

    Owner@pavilion ~
    $ ./a
    Enter the initial population in millions:3
    Enter the number of births/thousand:11
    Enter the number of deaths/thousand:7
    Enter the number of years (t):3
    This model predicts with an initial population of 3000000.000000,
    a birth rate of 11000.000000,
    and a death rate of 7000.000000,
    the population will be 0.000000 in 388079215198509284830428202732561107088596700
    00000000000000000000000000000000000000000000000000 000000000000000000000000000000
    00000000000000000000000000000000000000000000000000 000000000000000000000000000000
    00000000000000000000000000000000000000000000000000 000000000000000.000000 years.

    Pardon if my values are a bit unrealistic, but I'm not much of a statician. If this is anything like the output that you recieve then the program seems to be fine. You only need to format your output a little to make it appear sane, because double has precision to something like 10 places (not bad!).

    You will find this tutorial helpful I hope.

  12. #12
    Pop
    Pop is offline
    Registered User
    Join Date
    Oct 2006
    Location
    The Matrix, of course
    Posts
    7
    er...no, that wasnt quite what i was going for, lol. thanks for getting me this far though.

  13. #13
    Registered User SKeane's Avatar
    Join Date
    Sep 2006
    Location
    England
    Posts
    234
    Perhaps the compiler is getting "confused" between the variables exp/time and the functions exp()/time(). Mine seemed to, as if I compiled it and ran it it core dumped, but when I changed the variable names to e/t (respectively) it ran OK.

    Code:
    "the population will be %Lf in %lf years.\n", init_pop, births, deaths, final_pop, time);
    Change to a lowercase 'l'

    Code:
    "the population will be %lf in %lf years.\n", init_pop, births, deaths, final_pop, time);
    <edit> Lost the first part of my comment on previous edit (unintentionally) when I added second comment.
    Last edited by SKeane; 10-12-2006 at 08:11 AM.

  14. #14
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You won't get an executable if your compiler is "getting confused" with variables called the same thing as functions. It won't compile. This obviously isn't their problem, since they're actually getting output. Output means they get a compile. Getting a compile means their compiler isn't confusing functions with variables. Furthermore, your compiler won't get them confused either if you stop including headers you don't need.


    Quzah.
    Hope is the first step on the road to disappointment.

  15. #15
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,752
    Skeane, please do not edit your comments so that other replies become out of context. And I already explained away that problem.

    I'm being really nice and running this through a debugger. This will take a bit...

    [edit] back.
    According to my work with gdb things are fine up until you call pow(). It returns +infinity, which results in zeros being printed for final pop and generally making things all screwy. I don't think pow() works with long doubles so my suggestion is this:

    init_pop *= 1000000;
    deaths *= 1000;
    births *= 1000;

    This makes the doubles pretty huge, and then overflow easily happens. Could you work with a smaller number, or is the multiplication really necessary? You may have to whittle away at these numbers before they work in the range of a double, so don't just try one thing and come back.
    Last edited by whiteflags; 10-12-2006 at 08:55 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Noob Q: How to read a value of a byte in binary?
    By Hitsuin in forum C++ Programming
    Replies: 2
    Last Post: 06-11-2009, 03:46 PM
  2. Noob Questions...
    By Firemagic in forum C++ Programming
    Replies: 4
    Last Post: 04-19-2006, 04:57 PM
  3. my noob program doesnt work
    By Scarvenger in forum C++ Programming
    Replies: 2
    Last Post: 10-19-2005, 12:40 PM
  4. noob needs help!!!!:(
    By tykenfitz in forum C Programming
    Replies: 1
    Last Post: 07-10-2005, 09:49 AM
  5. noob: compiling error... plz help!
    By chosii in forum C Programming
    Replies: 2
    Last Post: 05-10-2002, 06:53 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21