1

I'm writing a program to track the number of rectangles being made in one week using the exponential growth function (X = a(1+b)^t). One person can make 60 rectangles per day.

a represents the initial population, this is the number of people already making rectangles on the first of the week. b represents the growth rate, this is the number of new people making rectangles each day. t represents the time interval, which is 7 days for this program.

I'm having difficulties getting started with the problem, and I need some guidance please.

I was thinking using using math.h and pow, something like this (this code is not compiling)


#include <math.h>
#include <stdio.h>
#include <stdlib.h>


int main() {

    int initial_POPULATION;
    int time_INTERVAL; 
    double growth_RATE;

    printf("How many people are making rectangles at the 
    biginning of the week?\n");
    scanf("%d", &initial_POPULATION);

    printf("How many people are making rectangles each day? 
    \n");
    scanf("%1f", &growth_RATE);

    //Exponential growth function X = a(1+b)^t
    printf("%d rectangles will be made this week!\n",
        initial_POPULATION(pow(1+growth_RATE),time_INTERVAL));

    return 0;

}

Kakarotto
  • 65
  • 7
  • what is this doing: `initial_POPULATION(pow(1+growth_RATE),time_INTERVAL))` ? – Z4-tier May 21 '20 at 23:03
  • 2
    I wonder why this is modeled exponentially. Are the people who make rectangles also made of rectangles? – aschepler May 21 '20 at 23:11
  • I was trying to put the exponential growth equation in there to get the result of how many rectangles will be made. xD @Z4-tier – Kakarotto May 21 '20 at 23:12
  • Yeah... the rate kf increase in number of persons making rectangles would have to be proportional to the number of persons...? – Martin James May 21 '20 at 23:15
  • a represents the initial population, this is the number of people already making rectangles on the first of the week. b represents the growth rate, this is the number of new people making rectangles each day. t represents the time interval, which is 7 days for this program. – Kakarotto May 21 '20 at 23:31

2 Answers2

1

There are a couple problems, but the most apparent is that you are not setting the value of time_INTERVAL anywhere. Next is the line where the final value is calculated: in C, you need to use * to denote multiplication. Parens do not work as implied multiplication operators like in regular math (at any rate, the way the parenthesis are being used in the last printf is not right). Finally, make sure to read growth_RATE as a double by using %lf as the format specifier in scanf (using %f reads it as a single precision 4-byte value, even though it's declared as a double which is... well, double that).

Try this:

    scanf("%lf", &growth_RATE);
    time_INTERVAL=7;
    printf("%f rectangles will be made this week!\n", initial_POPULATION * pow(1+growth_RATE, time_INTERVAL));

Also, as mentioned by @Asadefa, remove the line breaks from the string literals.

Z4-tier
  • 7,287
  • 3
  • 26
  • 42
  • Hey, thank you for the help. I have made the advised changes plus the correction, and now the program does compile, but when I run the program I get -1.#INF00 as a result. – Kakarotto May 21 '20 at 23:28
  • Thank you again, I made the changes and I'm getting the expected result when I use integers, but when I enter a decimal for the growth_RATE I get the -1.#INF00 – Kakarotto May 21 '20 at 23:50
  • sorry, my last comment had a typo. use this: `scanf("%lf", &growth_RATE);` – Z4-tier May 21 '20 at 23:54
0

There are 4 possible reasons your code isn't compiling.

Did you try compiling like this if you're using unix, with -lm?

gcc /path/to/file.c -lm -o /path/to/outputfile

Also, you cannot have line breaks in string literals. This isn't allowed:

char *MyString = "Hello

World";

On top of that, you are trying to call initial_POPULATION (int) as if it were a function:

initial_POPULATION(pow(1+growth_RATE),time_INTERVAL)

Maybe you meant:

initial_POPULATION*(pow(1+growth_RATE),time_INTERVAL)

And lastly, time_INTERVAL is uninitialized, as brought up by @Z4-tier.

  • Hey, thank you for the help. I have made the advised changes plus the correction, and now the program does compile, but when I run the program i get -1.#INF00 as a result. – Kakarotto May 21 '20 at 23:27