Question:
Each year the HSE Unit receives accident count reports from a 10 departments across the university. To summarize these reports, the department provides a frequency distribution printout that gives the number of departments reporting accident counts in the following ranges:
0 - 19
20 - 39
40 - 59
60 – 79
80 – 99
100 or above
The HSE Unit needs a computer program to take the number of accidents for each reporting department and store as a sequence of integer values. The program should also call the following functions:
1. Function calAverage: this function receives an array of number of accidents of each department and returns the average number of accidents.
2. Function frequencyCounts: this function receives one parameter which is an array of number of accidents for each department. It should add one to the count for appropriate accident range and store into a vector. It should then return the vector that contains the frequency counts.
3. Function highestRange: this function receives a vector of frequency counts and returns the index of range with the highest count.
After all the data has been processed, the resulting frequency counts and the range with the highest frequency counts are to be displayed.
Example output:
Enter the number of accidents for each dept: 20 12 80 35 15 3 52 30 2 63
The average number of accidents: 31.2
Frequency distribution:
0 – 19 : 4
20 – 39: 3
40 – 59: 1
60 – 79: 1
80 – 99: 1
100 and above: 0
The highest range is 0 – 19 : 4 departments
I don't know what happen to my code as follow. Before I do the 3rd function, all run good.
#include<iostream>
#include <vector>
#include <string>
using namespace std;
vector<int>match;
vector<int>data;
string range[6]={"0 - 19","20 - 39","40 - 59","60 - 79","80 - 99","100 and above"};
int big=match[0];
int small=match[0];
string x;
double average (vector <int> data, int size)
{
int a=0;
double total=data[a];
for(a=0;a<size-1;a++)
{
total = total + data[a+1];
}
return total/size;
}
void frequency (int count)
{
int count1=0,count2=0,count3=0,count4=0,count5=0,count6=0;
for (int j=0;j<10;j++)
{
if (data[j]>=0 && data[j]<=19)
{
count1++;
}
}
match.push_back(count1);
for (int j=0;j<10;j++)
{
if (data[j]>=20 && data[j]<=39)
{
count2++;
}
}
match.push_back(count2);
for (int j=0;j<10;j++)
{
if (data[j]>=40 && data[j]<=59)
{
count3++;
}
}
match.push_back(count3);
for (int j=0;j<10;j++)
{
if (data[j]>=60 && data[j]<=79)
{
count4++;
}
}
match.push_back(count4);
for (int j=0;j<10;j++)
{
if (data[j]>=80 && data[j]<=99)
{
count5++;
}
}
match.push_back(count5);
for (int j=0;j<10;j++)
{
if (data[j]>100)
{
count6++;
}
}
match.push_back(count6);
}
void highest_range(vector<int>match, string range [])
{
for (int j=0;j<6;j++)
{
if (match[j]>big)
{
big=match[j];
x=range[j];
}
}
}
int main ()
{
int count=0;
cout<<"Please enter the number of accidents for each dept: "<<endl;
for (int j=0;j<10;j++)
{
int i=0;
cin>>i;
data.push_back(i);
}
cout<<endl;
cout<<"The average number of accidents: "<<average(data,10)<<endl<<endl;
cout<<"Frequency distribution: "<<endl;
frequency(count);
cout<<"0 - 19 : "<<match[0]<<endl;
cout<<"20 - 39 : "<<match[1]<<endl;
cout<<"40 - 59 : "<<match[2]<<endl;
cout<<"60 - 79 : "<<match[3]<<endl;
cout<<"80 - 99 : "<<match[4]<<endl;
cout<<"100 and above: "<<match[5]<<endl<<endl;
highest_range(match,range);
cout<<"The highest range is "<<x<<" : "<<big<<" department."<<endl<<endl;
system ("pause");
return 0;
}