This C program will also calculate profit for the design products sold; however,
ID: 3553705 • Letter: T
Question
This C program will also calculate profit for the design products sold; however, in addition to prompting the user for data it will ask the user if he or she would like to process more records or is finished processing records.
Each product has a unique item number, price, and cost. The program allows the staff to enter and keep track of the product number, revenue and expense and calculates the net profit.
Data Entry Format
The data must be entered using a separate function for each record and return the value then call another function to process the data. The program must validate the data that has been entered and prompt the user for the required data when incorrect data is entered.
Explanation / Answer
#include<stdio.h>
int input_item()
{
int x;
printf("Enter item number ");
scanf("%d",&x);
while(x<100 || x>900)
{
printf("Invalid input... Item no. should be between 100 and 900 ");
printf("Enter item number ");
scanf("%d",&x);
}
return x;
}
double input_revenue()
{
double x;
printf("Enter revenue ");
scanf("%lf",&x);
while(x<=0)
{
printf("Invalid input.. revenue should be greater than 0 ");
printf("Enter revenue ");
scanf("%lf",&x);
}
return x;
}
double input_expense()
{
double x;
printf("Enter expense ");
scanf("%lf",&x);
while(x<=0)
{
printf("Invalid input.. expense should be greater than 0 ");
printf("Enter expense ");
scanf("%lf",&x);
}
return x;
}
double prof(double a, double b)
{
return a-b;
}
void disp(int i, double p)
{
printf("Profit for %d is %lf ",i,p);
}
double process_revenue(double a[], int i)
{
int j;
double rev=0;
for(j=0;j<i;j++)
{
rev = rev+a[j];
}
return rev;
}
double process_expense(double a[], int i)
{
int j;
double exp=0;
for(j=0;j<i;j++)
{
exp = exp+a[j];
}
return exp;
}
double process_profit(double a[], int i)
{
int j;
double pro=0;
for(j=0;j<i;j++)
{
pro = pro+a[j];
}
return pro;
}
void disp1(double rev, double exp, double pro, int i)
{
printf("Total no. of items %d ",i);
printf("Total Revenue %lf ",rev);
printf("Total Expense %lf ",exp);
printf("Total Income %lf ",pro);
}
int main()
{
int item[1000];
double revenue[1000];
double expense[1000];
double profit[1000];
int i=0;
char ch;
double rev,exp,pro;
do
{
item[i] = input_item();
revenue[i] = input_revenue();
expense[i]= input_expense();
profit[i] = prof(revenue[i],expense[i]);
disp(item[i], profit[i]);
printf("Do you want to enter more data.. press y for yes and any other key for no.. ");
while(getchar()!=' ');
ch=getchar();
i++;
}
while(ch=='y' || ch== 'Y');
rev= process_revenue(revenue,i);
exp= process_expense(expense,i);
pro= process_profit(profit,i);
disp1(rev,exp,pro,i);
scanf("%d",&i);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.