In: Computer Science
In the space provided below write a C++ program that asks the user to enter their quarterly earnings for the past two years stores the data in a 2-dimensional array. The program then computes both the annual earnings as well as the total earning and prints the results along with the 2-dimensional array on screen as well as onto a file.
CODE
#include<iostream>
#include<fstream>
using namespace std;
int main(){
//opening the file
fstream file;
file.open("output.txt",ios::out);
//earning 2D array
float earnings[2][4];
//annual earning array
float annualEarnings[] = {0,0};
float totalEarning=0;
//asking the user to enter the earnings
for(int i=0;i<2;i++){
//asking the user to enter the earnings quarter by quarter
cout<<"Enter for year "<<(i+1)<<":
"<<endl;
for(int j=0;j<4;j++){
cout<<"Enter for quarter: "<<(j+1)<<": ";
cin>>earnings[i][j];
}
}
//printing the output on the screen and file
for(int i=0;i<2;i++){
cout<<"\nEarning for year "<<(i+1)<<":
"<<endl;
file<<"\nEarning for year "<<(i+1)<<":
"<<endl;
for(int j=0;j<4;j++){
//printing the 2d array in the file and on the screen
cout<<"Quarter "<<(j+1)<<":
"<<earnings[i][j]<<endl;
file<<"Quarter "<<(j+1)<<":
"<<earnings[i][j]<<endl;
annualEarnings[i] += earnings[i][j];
}
cout<<endl;
file<<endl;
}
//printing the total earnings in the file and on the screen
totalEarning = annualEarnings[0] + annualEarnings[1];
cout<<"\nEarning for year 1:
$"<<annualEarnings[0]<<endl;
cout<<"Earning for year 2:
$"<<annualEarnings[1]<<endl;
cout<<"Total Earning
$"<<totalEarning<<endl;
file<<"\nEarning for year 1:
$"<<annualEarnings[0]<<endl;
file<<"Earning for year 2:
$"<<annualEarnings[1]<<endl;
file<<"Total Earning
$"<<totalEarning<<endl;
//closing the file
file.close();
return 0;
}
_____________________________________
CODE IMAGES:
________________________________________
OUTPUT:
output.txt
___________________________________________
Feel free to ask any questions in the comments section
Thank You!