Introduction to Arrays

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;
Last edited on
Do you have a specific question or problem?

Please post a small sample of your input file.

Please post a sample of what your output should look like.

The program compiles but then crashes. I am given an error code. Unhandled exception at 0x0FAF7EA6 (msvcp120d.dll) in Lab005_Practice.exe: 0xC0000005: Access violation reading location 0x9F5895D2.

the first few lines input file are:

Australia 62.7 62.1 63.3 59.7
Austria 0 52.8 53.1 54.6
Belgium 30.4 30.3 27.5 25.3
Canada 61.3 56.2 57.7 54.5
Chile 0 26.4 25.4 31.1
CzechRepublic 0 38.3 27.3 25.2
Denmark 65.0 67.1 62.3 55.0
Estonia 51.7 32.9 29.8 34.3
Finland 55.2 42.9 42.1 43.3
France 35.7 28.3 30.2 28.8
Germany 56.4 47.2 42.6 46.6
Greece 30.3 26.9 25.0 13.1
Hungary 0 32.5 21.8 18.6
Iceland 0 68.2 71.6 66.0
Ireland 41.4 49.3 47.8 27.9
Israel 23.6 28.2 26.6 43.5
Italy 29.8 27.8 25.5 20.5
Japan 42.2 42.7 40.9 38.5
Korea 32.5 29.4 29.9 24.2

The output is suppose to look like this:

name 1990 2000 2005 2012 Average Grade
Australia 62.70 62.10 63.30 59.70 61.95 L
Austria 0.0 52.80 63.10 54.60 40.13 H
exc.....

Average age of emploment between 15-24 is: 35.44



How many countries does your file contain?

Did you run the program with your debugger? The debugger will tell you exactly where it detected the error and then you should be able to view the variables at the time of the crash.

Your getData() function implementation doesn't match your function prototype or function call so your program really shouldn't even compile.

Topic archived. No new replies allowed.