Write a program that would read the following information from an input file: Th
ID: 3770186 • Letter: W
Question
Write a program that would read the following information from an input file: The first number in the file represents the number of cars sold, the second information indicates the type of a car and the last number represents the cost, of the car your program should include the following functions: A function to read the information from the input file and add it to a Queue called "CarQueue". A function to calculate the total cost of each type of car and save it in a variable called "TotalCost", so each type of car will have a variable called "TotalCost" Calculate the Maximum TotalCost (maximum from all the types of cars) Print the Maximum TotalCost along with the type of car that was associated with the Maximum You will make use of the functions listed above in Problem 1(b).Explanation / Answer
#include <iostream>
#include <fstream>
using namespace std;
class car
{
int NumSold;
string Name;
int Price;
int TotalCost;
public:
car(int sold,string name,int price)
{
NumSold=sold;
Name=name;
Price=price;
}
int calculateTotalCost()
{
TotalCost=NumSold*Price;
}
int getTotalCost()
{
return TotalCost;
}
};
int readFile(car carQueue[])
{
int i=0;
int sold,price;
string name;
ifstream infile("car.txt");
while(!infile.eof())
{
infile>> sold>>name>>price;
carQueue[i++].car(sold,name,price);
}
return i;
}
int MaxTotalCost(car carQueue[],int n)
{
int max=carQueue[0].TotalCost;
int pos=0;
for(int i=1;i<n;i++)
{
if(carQueue[i].TotalCost>max)
{
max=carQueue[i].TotalCost;
pos=i;
}
}
return pos;
}
void print(MaxAtPos,carQueue)
{
cout<<endl<<"Type of car:"<<carQueue[MaxAtPos].name;
cout<<endl<<"Maximum TotalCost:"
}
int main()
{
car carQueue[10];
int n=readFile(carQueue);
int MaxAtPos=MaxTotalCost(carQueue,n);
print(MaxAtPos,carQueue);
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.