Program: struct Here is the struct that you need to create for your program: C++
ID: 3803581 • Letter: P
Question
Program:
struct
Here is the struct that you need to create for your program:
C++ struct
Manufacturer (string), Model (string), Year built (string), Car miles per gallon (double)
Functions
Here are the functions to create:
Function Name
Return Type
Parameter List
(Car &c)
This function should load all car information from the CarInfo.txt file. The data is passed back in the reference parameter. Just read the data from the file and put the data into the appropriate fields of the Car struct instance.
(double m[])
The array to populate is passed in. Fill the array with data from the CarMileage.txt file.
(Car c, double mileage[], double gasCostPerGallon)
Use the information passed in to calculate the yearly gas cost. This method should return the cost. IT SHOULD NOT PRINT ANYTHING!!! Print the result from main.
CarInfo.txt Format
Year built
Car miles per gallon
CarMileage.txt Format(just numbers)
Jan. mileage
Feb. mileage
Dec. mileage
C++ struct
Manufacturer (string), Model (string), Year built (string), Car miles per gallon (double)
Explanation / Answer
Please refer below code
#include<iostream>
#include<string>
#include<fstream>
using namespace std;
typedef struct Car
{
string Manufacturer;
string Model;
string Year_built;
double car_miles;
}Car;
void read_info(Car &c)
{
std::ifstream myfile("CarInfo.txt");
myfile >> c.Manufacturer >> c.Model >> c.Year_built >> c.car_miles;
}
void read_mileage(double m[])
{
double value;
int index = 0;
std::ifstream myfile("CarMileage.txt");
while(myfile >> value)
{
m[index] = value;
index++;
}
}
double gas_cost(Car c, double mileage[], double gasCostPerGallon)
{
double sum = 0;
for(int i = 0; i < 12; i++)
sum += mileage[i];
return (sum / c.car_miles) * gasCostPerGallon;
}
int main()
{
Car c;
double mileage[12];
double cost,gas_cost_per_gallon;
read_info(c);
read_mileage(mileage);
cost = gas_cost(c,mileage,gas_cost_per_gallon);
cout<<"Please enter Gas cost per gallon : ";
cin>>gas_cost_per_gallon;
cout<<"Yearly gas cost = "<<cost<<endl;
return 0;
}
Input:
CarMileage.txt
12.3
14.2
11.1
10.7
9.8
8.8
12.9
11.4
11.2
14.1
9.9
5.7
CarInfo.txt
BMW G310R 2010 13.4
Please refer below output
Please enter Gas cost per gallon : 34.6
Yearly gas cost = 341.094
Process returned 0 (0x0) execution time : 3.596 s
Press any key to continue.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.