Search code examples
cexponentialpowmath.h

Setting up Exponential growth function X = a(1+b)^t in C language


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;

}


Solution

  • 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.