This is a homework assignment I have been stuck on. Any advise would be great!
Write a program to calculate country’s average employment rate for age group persons of 15-24 in employment years 1990, 2000, 2005 and 2012. The percentage of population data is provided in the attached file “employed_15_24yrsOld.txt”.
Use three arrays: a one-dimensional array to store the country’s names, a (parallel) two-dimensional array to store the average employment rate, and a parallel one-dimensional array to store the percentage of population data. Your program must contain at least the following functions:
1. A function to read and store data into two arrays.
2. A function to calculate the average employment rate for the four years and grade based on the following criterion:
Average Employment Rate: Grade:
Over 80%
N - Relatively No Risk
Between 60% and 79% L - Relatively Low Risk
Between 50% and 59% R - Relatively At Risk
Below 50% H - Relatively High Risk
3. A function to output the results. Have the program also calculate group’s average (The average of the average employment rate).
The main driver function is provided:
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;
const int SIZE = 40;
const int COLUMN = 5;
void getData(ifstream& inf, string n[], double tstData[][COLUMN], int count);
void calculateAverage(double tstData[][COLUMN], int count);
void calculateGrade(double tstData[][COLUMN], char gr[], int count);
void print(string n[], double tstData[][COLUMN], char gr[], int count);
int main()
{
string names[SIZE];
double testData[SIZE][COLUMN];
char grade[SIZE];
ifstream inFile;
inFile.open("employed_15-24yrsOld.txt");
if (!inFile)
{
cout << "Cannot open the input file: ch8_Ex13Data.txt." << endl;
cout << "Program terminates!" << endl;
return 1;
}
cout << fixed << showpoint << setprecision(2);
getData(inFile, names, testData, SIZE);
calculateAverage(testData, SIZE);
calculateGrade(testData, grade, SIZE);
print(names, testData, grade, SIZE);
inFile.close();
return 0;
}
This is what I have gotten so far....
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
|
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;
const int SIZE = 40;
const int COLUMN = 5;
void getData(ifstream& inf, string n[], double tstData[][COLUMN], int count);
void calculateAverage(double tstData[][COLUMN], int count);
void calculateGrade(double tstData[][COLUMN], char gr[], int count);
void print(string n[], double tstData[][COLUMN], char gr[], int count);
int main()
{
string names[SIZE];
double testData[SIZE][COLUMN];
char grade[SIZE];
ifstream inFile;
inFile.open("employed_15-24yrsOld.txt");
if (!inFile)
{
cout << "Cannot open the input file: ch8_Ex13Data.txt." << endl;
cout << "Program terminates!" << endl;
return 1;
}
cout << fixed << showpoint << setprecision(2);
getData(inFile, names, testData, SIZE);
calculateAverage(testData, SIZE);
calculateGrade(testData, grade, SIZE);
print(names, testData, grade, SIZE);
inFile.close();
return 0;
}
void getData(ifstream& inf, string n[], double tstData[][COLUMN])
{
int count = 0;
while (!inf.eof())
{
inf >> n[count] >> tstData[count][1] >> tstData[count][2] >> tstData[count][3] >> tstData[count][4];
count = count + 1;
}
}
void calculateAverage(double tstData[][COLUMN], int count)
{
int i;
int j;
for (j = 0; j<count; j++)
{
double sum = 0;
for (i = 1; i < COLUMN; i++)
{
sum = sum + tstData[j][i];
}
tstData[j][0] = sum / 4;
}
}
void calculateGrade(double tstData[][COLUMN], char gr[], int count)
{
int i;
int j;
for (i = 0; i<count; i++)
{
if (tstData[i][0]>80)
gr[i] = 'N';
else if (tstData[i][0] && tstData[i][0] < 80)
gr[i] = 'L';
else if (tstData[i][0] && tstData[i][0] < 60)
gr[i] = 'R';
else
gr[i] = 'H';
for (j = 1; j<COLUMN; j++)
{
if (tstData[i][0])
gr[i] = 'N';
else if (tstData[i][0] >= 60 && tstData[i][0]< 80)
gr[i] = 'L';
else if (tstData[i][0] >= 50 && tstData[i][0]< 60)
gr[i] = 'R';
else
gr[i] = 'H';
}
}
}
void print(string n[], double tstData[SIZE][COLUMN], char gr[], int count)
{
int i;
cout << "Name" << " " << "1990" << " " << "2000" << " " << "2005" << " " << "2012" << " " << "Average" << " " << "Grade" << endl;
cout << n[count] << " " << tstData[count][1] << " " << tstData[count][2] << " " << tstData[count][3] << " " << tstData[count][4] << " " << tstData[count][5] << " " << gr << endl;
for (i = 0; i<COLUMN; i++)
cout << n[count] << " " << tstData[count][1] << " " << tstData[count][2] << " " << tstData[count][3] << " " << tstData[count][4] << " " << tstData[count][5] << " " << gr << endl;
|