In: Computer Science
In C programming language,
After studying gross annual revenues of Broadway shows over a 20-year period, you model the revenue as a function of time:
R(t) = 203.265 X (1.071)^t, where R is in millions of dollars and t is the years since 1984. Create the following functions to implement this model:
revenue - calculates R given the input parameter t. R is an output parameter.
predict - predicts the year in which revenues (in millions) will first equal or exceed the value of the input parameter. For example, predict(200) would return 1984. The year should be returned through an output parameter.
Write a main () function that calls predict () to determine when revenues will likely exceed $1 trillion. Then create an output file that contains a table of estimated revenues for all the year from 1984 through the year when revenues should exceed $1 trillion.
Code for your program is provided below. Code is explained in the code comments. I have attached the screenshot of code in IDE, you can refer to the screenshot for your better understanding. Screenshot of output is provided in the last. If you need any further clarification please feel free to ask in comments. I would really appreciate if you let me know if the answer is upto your satisfaction or not.
#####################################################################
CODE
#include <stdio.h> //including for standard input output
#include <stddef.h> //including for using NULL
#include <stdlib.h> //including for using exit()
//function declarations
double revenue(int);
int predict(double);
//main function
int main()
{
//creating a pointer of FILE type
FILE *outputFile;
//Opening the file in "write" mode
outputFile=fopen("output.txt","w");
double money; //variable to hold the revenue
//if file is not opened or not created
if(outputFile==NULL)
{
//print descriptive message on screen
printf("Error occured while creating file. Press any key to Close the program.");
getchar(); //hold the screen until a character is entered by user
exit(0); //exit the program
}
//cal predict method and store returned year in the variable
int year=predict(1000000); //1 trillion has 1000000 millions
//for each year until the year when revenue is 1 trillion
for(int t=1984;t<=year;t++)
{
money=revenue(t-1984); //find the revenue of this year
fprintf(outputFile,"%d",t); //write the year in file
fprintf(outputFile,"\t"); //write tab spaces in file
fprintf(outputFile,"%lf",money); //write the revenue in the file
fprintf(outputFile, "\n"); //write new line
}
fclose(outputFile); //close the file
return 0;
}
//function to find revenue of the year t since 1984, hence t=0 is 1984,t=1 is 1985 and so on
double revenue(int t)
{
double result;
result=203.265 *(pow(1.071,t)); //calculate revenue using the equation
return result; //return revenue
}
//function to find the year in which the revenue will reach the amount given in argument of function
int predict(double millions)
{
int years=0; //initial year is zero, means year is 1984
//loop until revenue is less than what is asked
while(revenue(years)<millions)
{
years++; //increment year
}
return (1984+years); //return year in which revenue is reached the target
}
###########################################################################
OUTPUT IN FILE
so on upto the last year
############################################################################
SCREENSHOT OF CODE IN IDE