Question: Modify the code below uch that the program will give an average number of iphones sold by Apple. You will read the data from an

Modify the code below uch that the program will give an average number of iphones sold by Apple. You will read the data from an input file and write the results to an output file. Do not use any libraries we have not yet learned in your solution (i.e. cmath).

//TO-DO: Fill in the Authors Name Here //TO-DO: Fill in the current date here //CS1428 Lab //Lab 2 //Description: this program will calculate the average number // of iphones sold by Apple and save the values to a // file

#include #include // library for setw() and other i/o manipulators #include // library for file input and output using namespace std;

int main() {

string sale_period, version; int year1, year2, year3; double average;

// Declare input file stream variable and open the input file: ifstream fin; fin.open("Apple_iphone_sales_data.txt");

// TODO: Declare output file stream variable and open the output file:

// Error check to ensure the input file opened: if (!fin) { cout << "Could not open file. Terminating program." << endl; return -1; }

// Write the column headers to the output file: fout << left; fout << setw(10) << "Sale Period" << setw(13) << "Version" << "Average" << endl;

// TODO: Read data from input file, perform calculation, // and write data to output file (x3):

cout << "Data written to averages.txt" << endl;

// Close the input and output files: fout.close(); fin.close();

// Exit the program: return 0; } --------------------------------------------------------------------------- Input file .txt

Q1 Iphone11 3500 4200 5000 Q2 Iphone10 2300 2000 1500 Q3 Iphone9 2500 2600 1800 Q4 Iphone8 1500 1000 2000

-----------------------------------------------------------------------------

Your program will read in this data. The format for each line of the file is as follows: (Sale Period, Iphone Version, year 1, year 2, year 3) Q1 Iphone11 3500 4200 5000 Q2 Iphone10 2300 2000 1500 Q3 Iphone9 2500 2600 1800 Q4 Iphone8 1500 1000 2000

Calculate the average (your average should be a floating point value) and print everything to a file named averages.txt. Print Data written to averages.txt. to the console. Correct Console Output: Data written to averages.txt

Correct File Output: SalePeriod Version Average Q1 Iphone11 4233.333 Q2 Iphone10 1933.333 Q3 Iphone9 2300.000 Q4 Iphone8 1500.000

Assume that all data from the file are valid (meaning that there is no bad data i.e. negative amounts etc.)

HINT : Get your program to work for the first entry and then worry about the other two. You only need 5 variables for importing the data (not 20).

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!