Modify this by replacing all arrays by one array of structures. You will use nes
ID: 3716778 • Letter: M
Question
Modify this by replacing all arrays by one array of structures. You will use nested structures. Array should be passed to functions through Parameters/arguments.
#include
#include
#include
#include
using namespace std;
int n;
const double FALL = 0.39,
SPRING = 0.37,
SUMMER = 0.24;
const int SIZE = 9;
const int SIZE2 = 9;
void inputdata(ifstream &, string[][SIZE], double[][3][3], double[][SIZE2], int, int);
void validatedata(string[][SIZE], int, int);
void validatedata(double[][3][3], int, int);
void fwe(double[][3][3], double[][SIZE2], int, int);
void tafwe(double [][SIZE2], int , int);
void srinpercent(double [][SIZE2], string [][SIZE], int , int );
void salary(double [][SIZE2], int , int );
void letter(string [][SIZE], double [][SIZE2], int , int );
void report(ofstream &, string [][SIZE], double [][3][3], double [][SIZE2], int , int );
int main()
{
cout << "How many Employees? ";
cin >> n;
string mystring[n][SIZE];
double grades[n][3][3];
double totalval[n][SIZE2];
while(n < 1 || n > 3)
{
cout << "Invalid! emp must range from 1 - 3" << endl;
cout << "How many Employees? ";
cin >> n;
}
ifstream fin;
fin.open("project1.txt");
if(!fin)
{
cout << "error" << endl;
return 1;
}
ofstream fout;
fout.open("project2.txt");
for(int emp = 0;emp < n; emp++)
{
inputdata(fin, mystring, grades, totalval, n, emp);
validatedata(mystring, n, emp);
validatedata(grades, n, emp);
fwe(grades, totalval, n, emp);
tafwe(totalval, n, emp);
srinpercent(totalval, mystring, n, emp);
salary(totalval, n, emp);
letter(mystring, totalval, n, emp);
report(fout, mystring, grades, totalval, n, emp);
}
fin.close();
fout.close();
return 0;
}
void inputData(ifstream &fin, string mystring[][SIZE], double grades[][3][3], double totalval[][SIZE2], int n, int emp)
{
for(int i = 0;i < SIZE;i++)
{
getline(fin, mystring[emp][i]);
}
for(int year =0; year < 3; year++)
{
for(int j = 0;j < 3;j++)
{
fin >> grades[emp][year][j];
}
}
fin >> totalval[emp][5];
fin.ignore();
}
void validateData(string mystring[][SIZE], int n, int emp)
{
//sending the information from the input file to the cout file with the loop
for(int i = 0;i < SIZE;i++)
{
//inner loop for non numeric strings
if(mystring[emp][i].length() < 1 || mystring[emp][i].length() > 100)
{
cout << "error" << endl;
continue;
}
}
}
void validateData(double grades[][3][3], int n, int emp)
{
//reads the numeric values from the input file
for(int year = 0; year < 3; year++)
{
for(int j = 0;j < 3;j++)
{
for(int k = 0;k < 3;k++)
{
if(grades[emp][year][j] < 1 || grades[emp][year][j] > 1000)
{
cout << "error";
break;
}
}
}
}
}
void fwe(double grades[][3][3], double totalval[][SIZE2], int n, int emp)
{
totalval[emp][0] = (grades[emp][0][0] * SPRING) + (grades[emp][0][1] * SUMMER) + (grades[emp][0][1] * FALL);
totalval[emp][1] = (grades[emp][1][2] * SPRING) + (grades[emp][1][1] * SUMMER) + (grades[emp][1][2] * FALL);
totalval[emp][2] = (grades[emp][2][0] * SPRING) + (grades[emp][2][1] * SUMMER) + (grades[emp][2][2] * FALL);
totalval[emp][3] = totalval[emp][0] + totalval[emp][1] + totalval[emp][2];
}
void tafwe(double totalval[][SIZE2], int n, int emp)
{
totalval[emp][3] = totalval[emp][0] + totalval[emp][1] +totalval[emp][2];
totalval[emp][4] = totalval[emp][3]/3;
}
void srinpercent(double totalval[][SIZE2], string mystring[][SIZE], int n, int emp)
{
if(totalval[emp][4] < 75.)
{
totalval[emp][6] = 0;
}
else if(totalval[emp][4] > 75 && totalval[emp][4] <= 80)
{
totalval[emp][6] = 1;
}
else if(totalval[emp][4] > 80 && totalval[emp][4] <= 90)
{
totalval[emp][6] = 3;
}
else if(totalval[emp][4] > 90 && totalval[emp][4] <= 100)
{
totalval[emp][6] = 5;
}
else if(totalval[emp][4] > 100)
{
totalval[emp][6] = 10;
}
}
void salary(double totalval[][SIZE2], int n, int emp)
{
totalval[emp][7] = (totalval[emp][6]/100) * totalval[emp][5];
totalval[emp][8] = totalval[emp][5] + totalval[emp][7];
}
void letter(string mystring[][SIZE], double totalval[][SIZE2], int n, int emp)
{
if(totalval[emp][4] < 70)
{
mystring[emp][7] = "Warning!";
}
else if(totalval[emp][4] >= 95)
{
mystring[emp][8] = "Congrats!";
}
}
void report(ofstream &fout, string mystring[][SIZE], double grades[][3][3], double totalval[][SIZE2], int n, int emp)
{
fout << mystring[emp][0] << endl;
fout << " " << mystring[emp][1] << endl;
fout << "Name: " << mystring[emp][2] << endl;
fout << "Supervisor:" << mystring[emp][3] << endl;
fout << "ID: " << mystring[emp][4] << endl;
fout << "num: " << mystring[emp][5] << endl;
fout << "addy: " << mystring[emp][6] << endl;
for(int year = 0;year < 3;year++)
{
for(int j = 0;j < 3;j++)
{
switch(j)
{
case 0: //case for 2013
fout << " Spring Semester Evaluation, " << fixed << setprecision(2) << 2015 + year << ": " << grades[emp][year][j] << endl;
break;
case 1: //case for 2014
fout << " Summer Semester Evaluation, " << fixed << setprecision(2) << 2015 + year << ": " << grades[emp][year][j] << endl;
break;
case 2: //case for 2015
fout << " Fall Semester Evaluation, " << fixed << setprecision(2) << 2015 + year << ": " << grades[emp][year][j] << endl;
break;
}
}
fout << "final, 2015: " << totalval[emp][0] << endl;
fout << "final, 2016: " << totalval[emp][1] << endl;
fout << "final, 2017: " << totalval[emp][2] << endl;
fout << "total: " << totalval[emp][3] << endl;
fout << "Average: " << totalval[emp][4] << endl;
fout << "salary: " << totalval[emp][5] << endl;
fout << "in percent: " << totalval[emp][6] << endl;
fout << "in dollar: " << totalval[emp][7] << endl;
fout << "both: " << totalval[emp][8] << endl;
if(totalval[emp][4] < 70)
{
fout << mystring [emp][7] << endl;
fout << endl;
}
else if(totalval[emp][4] >= 95)
{
fout << mystring[emp][8] << endl;
}
}
}
Explanation / Answer
#include
#include
#include
#include
using namespace std;
int n;
const double FALL = 0.39, SPRING = 0.37,SUMMER = 0.24;
const int SIZE = 9;
const int SIZE2 = 9;
struct Employee {
string mystring[SIZE];
grades[3][3];
totalval[SIZE2];
};
void inputdata(ifstream &, struct Employee[], int, int);
void validatedata(struct Employee[], int, int);
void validatedata(struct Employee[], int, int);
void fwe(struct Employee[], int, int);
void tafwe(struct Employee[], int , int);
void srinpercent(struct Employee[], int , int );
void salary(struct Employee[], int , int );
void letter(struct Employee[], int , int );
void report(ofstream &, struct Employee[], int , int );
int main()
{
cout << "How many Employees? ";
cin >> n;
struct Employee employees[n];
while(n < 1 || n > 3)
{
cout << "Invalid! emp must range from 1 - 3" << endl;
cout << "How many Employees? ";
cin >> n;
}
ifstream fin;
fin.open("project1.txt");
if(!fin)
{
cout << "error" << endl;
return 1;
}
ofstream fout;
fout.open("project2.txt");
for(int emp = 0;emp < n; emp++)
{
inputdata(fin, employees, n, emp);
validatedata(employees, n, emp);
validatedata(employees, n, emp);
fwe(employees, n, emp);
tafwe(employees, n, emp);
srinpercent(employees, n, emp);
salary(employees, n, emp);
letter(employees, n, emp);
report(fout, employees, n, emp);
}
fin.close();
fout.close();
return 0;
}
void inputData(ifstream &fin, struct Employee employees[], int n, int emp)
{
for(int i = 0;i < SIZE;i++)
{
getline(fin, employees[emp].mystring[i]);
}
for(int year =0; year < 3; year++)
{
for(int j = 0;j < 3;j++)
{
fin >> employees[emp].grades[year][j];
}
}
fin >> employees[emp].totalval[5];
fin.ignore();
}
void validateData(struct Employee employees[], int n, int emp)
{
//sending the information from the input file to the cout file with the loop
for(int i = 0;i < SIZE;i++)
{
//inner loop for non numeric strings
if(employees[emp].mystring[i].length() < 1 || employees[emp].mystring[i].length() > 100)
{
cout << "error" << endl;
continue;
}
}
}
void validateData(struct Employee employees[], int n, int emp)
{
//reads the numeric values from the input file
for(int year = 0; year < 3; year++)
{
for(int j = 0;j < 3;j++)
{
for(int k = 0;k < 3;k++)
{
if(employees[emp].grades[year][j] < 1 || employees[emp].grades[year][j] > 1000)
{
cout << "error";
break;
}
}
}
}
}
void fwe(struct Employee employees[], int n, int emp)
{
employees[emp].totalval[0] = (employees[emp].grades[0][0] * SPRING) + (employees[emp].grades[0][1] * SUMMER) + (employees[emp].grades[0][1] * FALL);
employees[emp].totalval[1] = (employees[emp].grades[1][2] * SPRING) + (employees[emp].grades[1][1] * SUMMER) + (employees[emp].grades[1][2] * FALL);
employees[emp].totalval[2] = (employees[emp].grades[2][0] * SPRING) + (employees[emp].grades[2][1] * SUMMER) + (employees[emp].grades[2][2] * FALL);
employees[emp].totalval[3] = employees[emp].totalval[0] + totalval[emp][1] + employees[emp].totalval[2];
}
void tafwe(struct Employee employees[], int n, int emp)
{
employees[emp].totalval[3] = employees[emp].totalval[0] + totalval[emp][1] + employees[emp].totalval[2];
employees[emp].totalval[4] = employees[emp].totalval[3]/3;
}
void srinpercent(struct Employee employees[], int n, int emp)
{
if(employees[emp].totalval[4] < 75.)
{
employees[emp].totalval[6] = 0;
}
else if(employees[emp].totalval[4] > 75 && employees[emp].totalval[4] <= 80)
{
employees[emp].totalval[6] = 1;
}
else if(employees[emp].totalval[4] > 80 && employees[emp].totalval[4] <= 90)
{
employees[emp].totalval[6] = 3;
}
else if(employees[emp].totalval[4] > 90 && employees[emp].totalval[4] <= 100)
{
employees[emp].totalval[6] = 5;
}
else if(employees[emp].totalval[4] > 100)
{
employees[emp].totalval[6] = 10;
}
}
void salary(struct Employee employees[], int n, int emp)
{
employees[emp].totalval[7] = (employees[emp].totalval[6]/100) * employees[emp].totalval[5];
employees[emp].totalval[8] = employees[emp].totalval[5] + employees[emp].totalval[7];
}
void letter(struct Employee employees[], int n, int emp)
{
if(employees[emp].totalval[4] < 70)
{
employees[emp].mystring[7] = "Warning!";
}
else if(employees[emp].totalval[4] >= 95)
{
employees[emp].mystring[8] = "Congrats!";
}
}
void report(ofstream &fout, struct Employee employees[], int n, int emp)
{
fout << employees[emp].mystring[0] << endl;
fout << " " << employees[emp].mystring[1] << endl;
fout << "Name: " << employees[emp].mystring[2] << endl;
fout << "Supervisor:" << employees[emp].mystring[3] << endl;
fout << "ID: " << employees[emp].mystring[4] << endl;
fout << "num: " << employees[emp].mystring[5] << endl;
fout << "addy: " << employees[emp].mystring[6] << endl;
for(int year = 0;year < 3;year++)
{
for(int j = 0;j < 3;j++)
{
switch(j)
{
case 0: //case for 2013
fout << " Spring Semester Evaluation, " << fixed << setprecision(2) << 2015 + year << ": " << employees[emp].grades[year][j] << endl;
break;
case 1: //case for 2014
fout << " Summer Semester Evaluation, " << fixed << setprecision(2) << 2015 + year << ": " << employees[emp].grades[year][j] << endl;
break;
case 2: //case for 2015
fout << " Fall Semester Evaluation, " << fixed << setprecision(2) << 2015 + year << ": " << employees[emp].grades[year][j] << endl;
break;
}
}
fout << "final, 2015: " << employees[emp].totalval[0] << endl;
fout << "final, 2016: " << employees[emp].totalval[1] << endl;
fout << "final, 2017: " << employees[emp].totalval[2] << endl;
fout << "total: " << employees[emp].totalval[3] << endl;
fout << "Average: " << employees[emp].totalval[4] << endl;
fout << "salary: " << employees[emp].totalval[5] << endl;
fout << "in percent: " << employees[emp].totalval[6] << endl;
fout << "in dollar: " << employees[emp].totalval[7] << endl;
fout << "both: " << employees[emp].totalval[8] << endl;
if(employees[emp].totalval[4] < 70)
{
fout << employees[emp].mystring[7] << endl;
fout << endl;
}
else if(employees[emp].totalval[4] >= 95)
{
fout << employees[emp].mystring[8] << endl;
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.