Like Tree2Likes
  • 2 Post By Adak

Program due in an hour... (simple errors)

This is a discussion on Program due in an hour... (simple errors) within the C Programming forums, part of the General Programming Boards category; Code: #include <stdio.h> 3 #define beg_population 52.966 4 #define growth_rate 2.184 5 #define beg_year 1990 6 double population(); 7 int ...

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    140

    Program due in an hour... (simple errors)

    Code:
     #include <stdio.h>
      3 #define beg_population 52.966
      4 #define growth_rate 2.184
      5 #define beg_year 1990
      6 double population();
      7 int main()
      8 {
      9
     10         population();
     11         double pred_pop;
     12         pred_pop=population();
     13
     14         FILE *fp;
     15         fp= fopen("pop.txt", "w");
     16         fprintf(fp,"%lf",pred_pop);
     17         fscanf(fp,"%lf", &pred_pop);
     18         fclose(fp);
     19         return 0;
     20 }// main
     21
     22
     23
     24         double population()
     25 {
     26
     27         printf("Enter a year after 1990> ");
     28         double year;
     29         scanf("%lf",&year);
     30         printf("Predicted Gotham City population is %lf (in thousands) : %lf    ", year, population());
     31        return beg_population+growth_rate*(year-beg_year);
     32
     33 } // population function
    it keeps looping the same statement
    "Enter a year after 1990>
    "Enter a year after 1990>
    "Enter a year after 1990>
    I believe that the year i entered is stored in population function.

    Is my program completely messed up? Or it is there a simple fix?

    whereas pred_pop= predicted population

    basically what i want to do is write the result of the function population into the file pop.txt.
    Last edited by tmac619619; 10-17-2012 at 01:41 AM.

  2. #2
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    643
    Line 31 you dont need the key word double.. And You are closing the file pointer on line 18 but then immediately trying to scan from it right after. You should only use fclose after you are completely done with the file.

  3. #3
    Registered User
    Join Date
    Oct 2012
    Posts
    140
    Thanks for responding camel-man. I have made some adjustments but i have a new 'warning' now.

  4. #4
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    643
    The warning is because the variable year is not initialized before you pass it into the function.

    You really do not even need to pass in that variable since you are already manipulating it in the function. You could create a local variable in that function and I dont think you want to return 0 in this function. Look a little closer and tell me what you are trying to manipulate and what you are trying to return.
    Last edited by camel-man; 10-17-2012 at 01:02 AM.

  5. #5
    Registered User
    Join Date
    Oct 2012
    Posts
    140
    Oh i see what you're saying. year shouldn't even be apart of my function population()

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    You're assuming that the growth is linear, but I would not make that assumption.
    I would assume that the 2.184 means a growth of 2.184% over the previous year, each year. I.e. the growth rate is exponential, unless otherwise clearly stated.

    But hang on, that raises serious questions about what an initial population of 52.966 means! Perhaps the assignment is from a country where the meaning of the dot character and the comma character are reversed. (I've had that drama with writing CSV files before) Meaning the initial population would be 52966. Then the growth rate must be 2184 persons per year, from which you are probably meant to calculate the growth rate (~ 4.123%) and use that calculated rate with an exponential growth formula.

    What can I say but: Welcome to programming. Writing the code is not always the hardest part!
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  7. #7
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    643
    You can take out the parameter pred_pop also, and just return beg_population+growth_rate*(year-beg_year)

  8. #8
    Registered User
    Join Date
    Oct 2012
    Posts
    140
    But then how do i store the result of return into the text file pop.txt?

  9. #9
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    643
    You need to return it back to main.
    ex) pred_pop=population();
    Now pred_pop will have the value of what ever was returned in that function.

  10. #10
    Registered User
    Join Date
    Oct 2012
    Posts
    140
    Oh ok great. Program compiled without warnings, but it tells me that the population will be 0.00000000000

  11. #11
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    643
    You might want to include that last printf statement in main over to your population function, that way it will know what year is.

  12. #12
    Registered User
    Join Date
    Oct 2012
    Posts
    140
    10 mins left before due date. I really appreciate your advice camel. I'm learning alot by doing this. God bless you

  13. #13
    Registered User
    Join Date
    Oct 2012
    Posts
    140
    Code:
            double population()
     25 {
     26
     27         printf("Enter a year after 1990> ");
     28         double year;
     29         scanf("%lf",&year);
     30         printf("Predicted Gotham City population for %lf (in thousands) : %lf", year, population());
     31
     32         return beg_population+growth_rate*(year-beg_year);
    Almost there. Just a quick minor clear-up. I keep getting a loop that prompts me to enter the year after 1990.
    Enter a year after 1990> 2015
    Enter a year after 1990> 2015
    Enter a year after 1990> 2015

    How come i can't see the printf statement? I thought since return only returned the calculations. That i could simply put population() in the printf statement and it would display the results.
    "

  14. #14
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    That causes the endless looping. You have no logic to escape the recursive calls.

    Do your calculation, and then print it, inside the function. THEN return. Keep it simple.
    Salem and laserlight like this.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ simple program errors in declaring variables
    By brack in forum C++ Programming
    Replies: 9
    Last Post: 10-09-2010, 10:26 PM
  2. Few Simple Program Errors
    By pobri19 in forum C Programming
    Replies: 3
    Last Post: 05-22-2008, 07:12 PM
  3. Simple Windows program errors
    By Kespoosh in forum Windows Programming
    Replies: 2
    Last Post: 07-17-2004, 01:05 AM
  4. Unknown Errors in simple program
    By neandrake in forum C++ Programming
    Replies: 16
    Last Post: 04-06-2004, 03:57 PM
  5. Small simple one-hour game
    By Nutshell in forum Game Programming
    Replies: 7
    Last Post: 10-13-2002, 10:03 PM

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