Programming in C Question Financial Records Create a program to store weekly exp
ID: 3738663 • Letter: P
Question
Programming in C Question
Financial Records
Create a program to store weekly expenses and revenues over a period of time. The program should store the expenses and revenues in two global array variables. Values stored in the array at index k represent the total revenues/expenses recorded during the kth week. Assume that the maximum number of weeks tracked is 250.
Add the following functions to your program:
1. A function printOptions that prints all the options for the user to choose from
2. A function expensesStartingAt that finds the total of all expenses incurred since week t for a given t
3. A function recordWeekData that records the total expenses and revenues for the next unrecorded week. It must check if there is still space in the array and if not, print “The array is full”.
4. A function printNumberOfWeeks that prints the number of weeks recorded
5. A function printAllData that prints all revenues and expenses recorded
6. A function largestRevenue that returns a pointer to the largest revenue recorded
Organize the program so the function declarations are presented first before the main function, and the function definitions after the main function. Your program should keep asking the user to select one of the following options A through F and execute the corresponding function based on the user choice:
A: compute the total of all expenses,
B: add a new expense/revenue,
C: print the number of weeks recorded,
D: print all revenues and expenses recorded,
E: print the largest revenue recorded, or
F: exit the program.
Call your program fincance.c. Compile and test your program.
Make sure your code is properly indented, your variables have meaningful names, and macro definitions are used when appropriate.
Explanation / Answer
As you can see , there can 6 functions defined for the program given :
1. char printOptions - this function will display the choices and return the choice selected by the user.
2. double expensesStartingAt(int t) - this function returns a total in double , for the expenses starting from week t until the records goto
3. int recordWeekData() - this function will record the week data. if this returns -1 , then that means the array is full and no more data can be entered.
4. void printNumberOfWeeks() - this functions simply prints the number of weeks for which data has been recorded (the week name start from #0 , according to the index).
5. void printAllData - this function prints all data from both of the arrays expense and revenue;
6. int largestRevenue - this function finds the record having largest revenue and return its postion in the expnses array.
Try checking and running the program below in an online/local compiler.
#include <stdio.h>
char printOptions();
double expensesStartingAt(int);
int recordWeekData();
void printNumberOfWeeks();
void printAllData();
int largestRevenue();
float expenseArr[250]; // the global array for recording the expenses
float revenueArr[250]; // the global array for recording the revenues
int curr=-1; // the cursor for tracking the position of uptil how many records have been filled in both of the arrays
int main()
{
char c='G';
int ret=-1;
while((c=printOptions())!='F')
{
switch(c)
{
case 'A': expensesStartingAt(0);break;
case 'B': ret=recordWeekData(); if(ret==-1) printf("The array is full"); break;
case 'C': printNumberOfWeeks(); break;
case 'D': printAllData(); break;
case 'E': { ret=largestRevenue();
if(ret==-1)printf(" The data might not be there "); // exception since, data might be empty hence returned -1
else printf("The largest revenue is for week %d having %f ",ret,revenueArr[ret]);break; }
default:break;
}
}
return 0;
}
char printOptions()
{
char returnChar='G';
do
{
printf(" A: compute the total of all expenses, ");
printf("B: add a new expense/revenue, ");
printf("C: print the number of weeks recorded, ");
printf("D: print all revenues and expenses recorded, ");
printf("E: print the largest revenue recorded, ");
printf("F: exit the program. ");
scanf(" %c",&returnChar);
fflush(stdin);
}while(returnChar!='A' && returnChar!='B' && returnChar!='C' && returnChar!='D' && returnChar!='E' && returnChar!='F');
// the while loop will run again its iteration if a wrong character not corresponding to any options has been entered
return returnChar;
}
double expensesStartingAt(int t)
{
if(curr<0)
return 0.0f;
double total=0.0f; // the double has been taken , as the floats might add upto make the range longer than what can be filled in float
int i=0;
for(i=t;i<=curr;i++)
{
total+=expenseArr[i];
}
return total;
}
int recordWeekData()
{
if(curr<=248) //if there is one more space in the bucket, then only allow for incrementing the pointer, for next record entry
curr++;
else
return -1; // throw an exception
printf("Enter the expenses data for the week %d: ",curr);
scanf("%f",&expenseArr[curr]);
printf("Enter the revenue data for the week %d: ",curr);
scanf("%f",&revenueArr[curr]);
return 1; // signify that everything went fine
}
void printNumberOfWeeks()
{
printf("The total numbers of weeks for which expenses and revenues recorded are %d",curr+1);
}
void printAllData()
{
int i=0;
if(curr<0)
{
printf("No expenses and revenues recorded yet ");
return;
}
for(i=0;i<=curr;i++)
{
printf(" The total expenses for the week %d are %f , and the total revenues are %f ",i,expenseArr[i],revenueArr[i]);
}
return;
}
int largestRevenue()
{
float largestRevenue=-1.0f;
int cursor=-1;
int i=0;
if(curr>=0)
{
for(i=0;i<=curr;i++)
{
if(revenueArr[i]>largestRevenue) // if the current record's revenue is greater than what has been found uptil now
{
largestRevenue=revenueArr[i];
cursor=i;
}
}
}
return cursor;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.