modify the following program, using C++, Create two and three-dimensional arrays
ID: 3740881 • Letter: M
Question
modify the following program, using C++,
Create two and three-dimensional arrays to store all input and output data and fill these arrays by the data read from the input file and by the computed data. The data of all employees will be stored in these arrays before writing it to the output file.
As you know, all elements of an array must be of same type. Therefore, you will be creating different arrays to store different types of data. For an example you could have the following array declarations:
A two-dimensional array NonNumeric of type string to store headings, Employee name, and supervisor’s name. Employee ID, Employee Telephone, Employee Address, warning/appreciation letter, and Note for nemployees. For three employees it may look like NonNumeric1 [3] [8]
A three-dimensional array Numeric1 of type double to store three years and three semesters input data read from the input file for n employees. For three employees it may look like Numeric1 [3] [3] [3]
A two-dimensional array Numeric2 of type double to store Final Weighted Evaluation, 2013, Final Weighted Evaluation, 2014, Final Weighted Evaluation, 2015, Total Final Weighted Evaluation, Average Final Weighted Evaluation, Current Salary, Salary Raise in %, Salary Raise in Dollars, Salary in dollars with the raise. For three employees it may look like Numeric2 [3] [9].
At the end when all the input and output data has been stored in arrays, you will generate all reports and write them to the output file.
Of course, you will need nested loops in the program.
You must use named constants to declare the sizes of arrays.
In addition to input validation, you should also validate if the file opened correctly.
In your loops, you should also check that arrays do not get out of bound.
INPUT
Name of the Employee: Nancy Price
Name of the Supervisor: John Albert
Employee ID #: A-5971-359182
Telephone Number: (512) 354 – 9855
Address: 510 Albatross Dr., Austin, TX 78310
Spring Semester Evaluation, 2013: 80.00
Summer Semester Evaluation, 2013: 75.00
Fall Semester Evaluation, 2013: 90.00
Spring Semester Evaluation, 2014: 83.00
Summer Semester Evaluation, 2014: 78.00
Fall Semester Evaluation, 2014: 93.00
Spring Semester Evaluation, 2015: 86.00
Summer Semester Evaluation, 2015: 79.00
Fall Semester Evaluation, 2015: 95.00
Current Salary: $90,000.00
NOTE: Information in Red color is for information only. It should not be read.
OUTPUT
University, San Marcos
Annual Employee Evaluation Report from January 1, 2013 to December 31, 2015
Name of the Employee: Nancy Price
Name of the Supervisor: John Albert
Employee ID #: A-5971-359182
Telephone Number: (512) 354-9855
Address: 510 Albatross Dr., Austin, TX 78310
Spring Semester Evaluation, 2013: 80.00
Summer Semester Evaluation, 2013: 75.00
Fall Semester Evaluation, 2013: 90.00
Spring Semester Evaluation, 2014: 83.00
Summer Semester Evaluation, 2014: 78.00
Fall Semester Evaluation, 2014: 93.00
Spring Semester Evaluation, 2015: 86.00
Summer Semester Evaluation, 2015: 79.00
Fall Semester Evaluation, 2015: 95.00
Final Weighted Evaluation, 2013: Computed Value
Final Weighted Evaluation, 2014: Computed Value
Final Weighted Evaluation, 2015: Computed Value
Total Final Weighted Evaluation: Computed Value
Average Final Weighted Evaluation: Computed Value
Current Salary: $90,000.00
Salary Raise in %: Computed Value
Salary Raise in Dollars: Computed Value
Salary in dollars with the raise: Computed Value
Warning/Appreciation Letter: Drafted by the student
Note: This report for Nancy Price was prepared according to the fair practice of the University.
Any discrepancies must be reported by Nancy Price to her supervisor, John Albert.
Modify this program
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
//Use the namespace
using namespace std;
//Declare named constants
const double baseWeight = 75.00;
const double weight2 = 80.00;
const double weight3 = 90.00;
const double weight4 = 100.00;
//main
int main ()
{
//Declaring variables
string heading,heading2,employee,supervisor,id,address,number,warning,appreciation;
double totFWE , avgFWE, currentSalary, salaryRaisePer, salaryRaiseAmount, revisedSalary;
//Declare double arrays to store the 3 years evaluation
double fall[3], spring[3], summer[3], fwe[3];
//Declare int array to store 3 years
int year[] = {2015, 2016, 2017};
string tp;
//Declare input file stream variable
ifstream fin;
//Declare output file stream variable.
ofstream fout;
//Declare nEmployees to store "n" value
int nEmployees;
//Loop to read valid "n" value
while(1)
{
//Prompt for "n" value
cout<<"Enter the number of employees (it should be between 1 and 5): ";
//Read n value
cin>>nEmployees;
//Check n value is valid
if(nEmployees>=1 && nEmployees<=5)
//If valid exit
break;
//If not valid
else
//Print error message
cout<<"Number of employees must be in range of 1-5"<<endl;
}
//Open the input file
fin.open("Input.txt");
//Open the output file
fout.open("Output.txt");
//read the lines of the file
getline(fin,heading);
getline(fin,heading2);
getline(fin,warning);
getline(fin,appreciation);
//Writing to the output file
fout << " " << heading << endl;
fout << " " << heading2 << endl << endl;
//Loop to read "n" employees details from file
for(int kk=0;kk<nEmployees; kk++)
{
//Read employee name
getline(fin,employee);
//Check for length of employee name
if(employee.length() > 50 || employee.length()<1)
{
//If not meeting the criteria, print message
cout<<"Employee name is not valid"<<endl;
//Go to start of the loop to read another employee details
continue;
}
//Read supervisor name
getline(fin,supervisor);
//Check for length of supervisor name
if(supervisor.length()>50 || employee.length()< 1)
{
//If not meeting the criteria, print message
cout<<" Supervisor name is not valid."<<endl;
//Go to start of the loop to read another employee details
continue;
}
//Read employee id
getline(fin,id);
//Read employee number
getline(fin,number);
//Read employee address
getline(fin,address);
//Check for length of address
if(address.length()>50 || address.length()<1)
{
//If not meeting the criteria, print message
cout<<" Address is not valid."<<endl;
//Go to start of the loop to read another employee details
continue;
}
//Set value
totFWE = 0;
//Variable to indicate error flag in reading semester evaluation
int fg=0;
//Loop to 3 years semester evaluation
for(int aa=0;aa<3;aa++)
{
//Read fall semester evaluation
fin>>fall[aa];
//Check condition
if(fall[aa]<1 || fall[aa]>100.00)
{
//Print message
cout<<"Invalid weight for fall semester evaluation."<<endl;
//Update value
fg =1;
//Break
break;
}
//Read spring semester evaluation
fin>>spring[aa];
//Check condition
if(spring[aa]<1 || spring[aa]>100.00)
{
//Print message
cout<<"Invalid weight for spring semester evaluation."<<endl;
//Update value
fg =1;
//break
break;
}
//Read summer semester evalution
fin>>summer[aa];
//Check condition
if(summer[aa]<1 || summer[aa]>100.00)
{
//Print message
cout<<"Invalid weight for summer semester evaluation."<<endl;
//Update value
fg =1;
//Break
break;
}
//If error in reading evalution,
if(fg==1)
//Read next employee details
continue;
//final grade calculation
fwe[aa]=(fall[aa]*.36)+(spring[aa]*.38)+(summer[aa]*.26);
//Update the total grade
totFWE += fwe[aa];
}
//Read employee's salary
fin>> currentSalary;
//Read the newline
getline(fin, tp);
//Find the average FWE
avgFWE = totFWE/ (double) (3);
//Salary Raise Calculation
if (avgFWE < baseWeight)
salaryRaisePer = 0;
if (avgFWE >= baseWeight && avgFWE <= weight2)
salaryRaisePer = 1;
if (avgFWE > weight2 && avgFWE <= weight3)
salaryRaisePer = 3;
if (avgFWE > weight3 && avgFWE <= weight4)
salaryRaisePer= 5;
if (avgFWE > weight4)
salaryRaisePer=10;
//Find the raise amount
salaryRaiseAmount = (currentSalary * salaryRaisePer)/100.00;
//Find the new salary
revisedSalary = currentSalary + salaryRaiseAmount;
//Output employee name
fout << right << setw(45) << "Name of the Employee: " << employee << endl;
//Output supervisor name
fout << setw(45) << "Name of the Supervisor: " << left << setw(50) << supervisor<< endl;
//Output employee id
fout << right << setw(45) << "Employee ID: " << left << setw(50) << id<< endl;
//Output telephone number
fout << right << setw(45) << "Telephone Number: " << number << endl;
//Output address
fout << right << setw(45) << "Address: " << address << endl;
//Loop to print semester evaluation
for(int aa=0;aa<3;aa++)
{
//Print fall semester evaluation
fout << setw(39) << "Fall Semester Evaluation, "<<year[aa]<<": " << setprecision(2) << fixed << fall[aa]<< endl;
//Print spring semester evaluation
fout << setw(39) << "Spring Semester Evaluation, "<<year[aa]<<": " << setprecision(2) << fixed << spring[aa]<< endl;
//Print summer semester evaluation
fout << setw(39) << "Summer Semester Evaluation, "<<year[aa]<<": " << setprecision(2) << fixed << summer[aa]<< endl;
}
//Loop to print final semester evaluation
for(int aa=0;aa<3;aa++)
{
//Output the final weighted evaluation to output file
fout << setw(39) << "Final Weighted Evaluation, "<< year[aa]<<": "<<setprecision(2)<< fixed << fwe[aa]<< endl;
}
//Print total final weighted evaluation
fout << setw(45) << "Total Final Weighted Evaluation: "<< setprecision(2)<< fixed << totFWE<< endl;
//Print average weighted evaluation
fout << setw(45) << "Average Weighted Evaluation: "<< setprecision(2)<< fixed << avgFWE<< endl;
//writing current salary on output file
fout << setw(45) << "Current Salary : "<<"$"<< currentSalary<< endl;
//writing total salary raise on output file
fout << setw(45) << " Salary Raise in % : "<< salaryRaisePer << endl;
//writing salary raise in dollars on output file
fout << setw(45) << " Salary Raise in Dollars: "<<"$"<< salaryRaiseAmount << endl;
//writing revised salary in dollars on output file
fout << setw(45) << "Salary in dollars with the raise: "<<"$"<< revisedSalary << endl<<endl;
//Warning / Appreciation Letter Calculation, Writing statement to output file
if (avgFWE<70)
{
fout << warning << endl <<endl;
}
if (avgFWE>=95)
{
fout << appreciation << endl << endl;
}
//Following statements will be displayed on the output file
fout << "Note: This report for " << employee << " was prepared according to the fair practice of the University"<< endl;
fout << "Any discrepancies must be reported by " << employee << " to his supervisor, "<< supervisor << "." << endl<<endl;
}
//close the files
fout.close();
fin.close();
//Stop
return 0 ;
}
Explanation / Answer
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
//Use the namespace
using namespace std;
//Declare named constants
const double baseWeight = 75.00;
const double weight2 = 80.00;
const double weight3 = 90.00;
const double weight4 = 100.00;
//main
int main ()
{
//Declaring variables
string heading,heading2,employee,supervisor,id,address,number,warning,appreciation;
double totFWE , avgFWE, currentSalary, salaryRaisePer, salaryRaiseAmount, revisedSalary;
//Declare double arrays to store the 3 years evaluation
double fall[3], spring[3], summer[3], fwe[3];
//Declare int array to store 3 years
int year[] = {2015, 2016, 2017};
string tp;
//Declare input file stream variable
ifstream fin;
//Declare output file stream variable.
ofstream fout;
//Declare nEmployees to store "n" value
int nEmployees;
//Loop to read valid "n" value
while(1)
{
//Prompt for "n" value
cout<<"Enter the number of employees (it should be between 1 and 5): ";
//Read n value
cin>>nEmployees;
//Check n value is valid
if(nEmployees>=1 && nEmployees<=5)
//If valid exit
break;
//If not valid
else
//Print error message
cout<<"Number of employees must be in range of 1-5"<<endl;
}
//Open the input file
fin.open("Input.txt");
//Open the output file
fout.open("Output.txt");
//read the lines of the file
getline(fin,heading);
getline(fin,heading2);
getline(fin,warning);
getline(fin,appreciation);
//Writing to the output file
fout << " " << heading << endl;
fout << " " << heading2 << endl << endl;
//Loop to read "n" employees details from file
for(int kk=0;kk<nEmployees; kk++)
{
//Read employee name
getline(fin,employee);
//Check for length of employee name
if(employee.length() > 50 || employee.length()<1)
{
//If not meeting the criteria, print message
cout<<"Employee name is not valid"<<endl;
//Go to start of the loop to read another employee details
continue;
}
//Read supervisor name
getline(fin,supervisor);
//Check for length of supervisor name
if(supervisor.length()>50 || employee.length()< 1)
{
//If not meeting the criteria, print message
cout<<" Supervisor name is not valid."<<endl;
//Go to start of the loop to read another employee details
continue;
}
//Read employee id
getline(fin,id);
//Read employee number
getline(fin,number);
//Read employee address
getline(fin,address);
//Check for length of address
if(address.length()>50 || address.length()<1)
{
//If not meeting the criteria, print message
cout<<" Address is not valid."<<endl;
//Go to start of the loop to read another employee details
continue;
}
//Set value
totFWE = 0;
//Variable to indicate error flag in reading semester evaluation
int fg=0;
//Loop to 3 years semester evaluation
for(int aa=0;aa<3;aa++)
{
//Read fall semester evaluation
fin>>fall[aa];
//Check condition
if(fall[aa]<1 || fall[aa]>100.00)
{
//Print message
cout<<"Invalid weight for fall semester evaluation."<<endl;
//Update value
fg =1;
//Break
break;
}
//Read spring semester evaluation
fin>>spring[aa];
//Check condition
if(spring[aa]<1 || spring[aa]>100.00)
{
//Print message
cout<<"Invalid weight for spring semester evaluation."<<endl;
//Update value
fg =1;
//break
break;
}
//Read summer semester evalution
fin>>summer[aa];
//Check condition
if(summer[aa]<1 || summer[aa]>100.00)
{
//Print message
cout<<"Invalid weight for summer semester evaluation."<<endl;
//Update value
fg =1;
//Break
break;
}
//If error in reading evalution,
if(fg==1)
//Read next employee details
continue;
//final grade calculation
fwe[aa]=(fall[aa]*.36)+(spring[aa]*.38)+(summer[aa]*.26);
//Update the total grade
totFWE += fwe[aa];
}
//Read employee's salary
fin>> currentSalary;
//Read the newline
getline(fin, tp);
//Find the average FWE
avgFWE = totFWE/ (double) (3);
//Salary Raise Calculation
if (avgFWE < baseWeight)
salaryRaisePer = 0;
if (avgFWE >= baseWeight && avgFWE <= weight2)
salaryRaisePer = 1;
if (avgFWE > weight2 && avgFWE <= weight3)
salaryRaisePer = 3;
if (avgFWE > weight3 && avgFWE <= weight4)
salaryRaisePer= 5;
if (avgFWE > weight4)
salaryRaisePer=10;
//Find the raise amount
salaryRaiseAmount = (currentSalary * salaryRaisePer)/100.00;
//Find the new salary
revisedSalary = currentSalary + salaryRaiseAmount;
//Output employee name
fout << right << setw(45) << "Name of the Employee: " << employee << endl;
//Output supervisor name
fout << setw(45) << "Name of the Supervisor: " << left << setw(50) << supervisor<< endl;
//Output employee id
fout << right << setw(45) << "Employee ID: " << left << setw(50) << id<< endl;
//Output telephone number
fout << right << setw(45) << "Telephone Number: " << number << endl;
//Output address
fout << right << setw(45) << "Address: " << address << endl;
//Loop to print semester evaluation
for(int aa=0;aa<3;aa++)
{
//Print fall semester evaluation
fout << setw(39) << "Fall Semester Evaluation, "<<year[aa]<<": " << setprecision(2) << fixed << fall[aa]<< endl;
//Print spring semester evaluation
fout << setw(39) << "Spring Semester Evaluation, "<<year[aa]<<": " << setprecision(2) << fixed << spring[aa]<< endl;
//Print summer semester evaluation
fout << setw(39) << "Summer Semester Evaluation, "<<year[aa]<<": " << setprecision(2) << fixed << summer[aa]<< endl;
}
//Loop to print final semester evaluation
for(int aa=0;aa<3;aa++)
{
//Output the final weighted evaluation to output file
fout << setw(39) << "Final Weighted Evaluation, "<< year[aa]<<": "<<setprecision(2)<< fixed << fwe[aa]<< endl;
}
//Print total final weighted evaluation
fout << setw(45) << "Total Final Weighted Evaluation: "<< setprecision(2)<< fixed << totFWE<< endl;
//Print average weighted evaluation
fout << setw(45) << "Average Weighted Evaluation: "<< setprecision(2)<< fixed << avgFWE<< endl;
//writing current salary on output file
fout << setw(45) << "Current Salary : "<<"$"<< currentSalary<< endl;
//writing total salary raise on output file
fout << setw(45) << " Salary Raise in % : "<< salaryRaisePer << endl;
//writing salary raise in dollars on output file
fout << setw(45) << " Salary Raise in Dollars: "<<"$"<< salaryRaiseAmount << endl;
//writing revised salary in dollars on output file
fout << setw(45) << "Salary in dollars with the raise: "<<"$"<< revisedSalary << endl<<endl;
//Warning / Appreciation Letter Calculation, Writing statement to output file
if (avgFWE<70)
{
fout << warning << endl <<endl;
}
if (avgFWE>=95)
{
fout << appreciation << endl << endl;
}
//Following statements will be displayed on the output file
fout << "Note: This report for " << employee << " was prepared according to the fair practice of the University"<< endl;
fout << "Any discrepancies must be reported by " << employee << " to his supervisor, "<< supervisor << "." << endl<<endl;
}
//close the files
fout.close();
fin.close();
//Stop
return 0 ;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.