Q2. 19 Marks] Study the mainD function carefully, and then write the following f
ID: 3914461 • Letter: Q
Question
Q2. 19 Marks] Study the mainD function carefully, and then write the following functions Total Cos a. Parameters: i int array quantity W double array price int size b. Each item in the quantity array will have the quantity of an item, and the same tem in the price array will have the price of that item. The function should print both arrays side-by-side as shown in the sample output and Etum the total cost Of all the items. For example, 5atnjol " 5. and pricel0-2. then the total cost for that item is 10. 2) Print table a. Paramaters L int array quontity W. double array price int size The function prints the total price of each value in quantity anray in a tubular format see the output sample ) Max price a. Parameters: i double array price int site the function returns the value and the index of maximum value. 22.5 otal cost 120 e maximun price is 12.5 he index of maimon price is 3 Page 8 of 11 int main 0 ( double max nt Imax nt gfl-12, 3,4, 5 double pll-12.5,7.5,125, 8.5 double cost Total Costiq. p. 4 Print tablele.p4): Max_pricelp,4, max, Imax): coutsc The maximum price isec maxccendi coutec The index of maimum price is "edmaxecend return 0Explanation / Answer
#include <iostream>
using namespace std;
//function prototypes
double Total_Cost(int[],double[],int);
void Print_Table(int[],double[],int);
void Max_Price(double[],int,double*,int*);
int main() {
double max;
int Imax;
int q[] = {2,3,4,5};
double p[] = {2.5,7.5,12.5,8.5};
double cost = Total_Cost(q,p,4);
Print_Table(q,p,4);
cout<<endl<<"Total cost = "<<cost<<endl<<endl;
Max_Price(p,4,&max,&Imax);
cout<<"The maximum price is "<<max<<endl;
cout<<"The index of the maximum price is "<<Imax<<endl;
return 0;
}
double Total_Cost(int quantity[],double price[],int n)
{
double totalCost = 0;
for(int i=0;i<n;i++)
{
totalCost = totalCost + quantity[i]*price[i];// compute total cost for all items
}
return totalCost;
}
void Print_Table(int quantity[],double price[],int n)
{
for(int i=0;i<n;i++)
{
cout<<quantity[i]<<" "<<price[i]<<" "<<quantity[i]*price[i]<<endl;// display
}
}
void Max_Price(double price[],int n,double *max,int *Imax)// pointers used for max and Imax
{
*max = price[0];
for(int i=1;i<n;i++)
{
if(price[i] > *max)
{
*max = price[i];// max price
*Imax = i+1; // index of max price
}
}
}
Output:
2 2.5 5
3 7.5 22.5
4 12.5 50
5 8.5 42.5
Total cost = 120
The maximum price is 12.5
The index of the maximum price is 3
DO ask if any doubt. Please upvote.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.