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];
}

}

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;

}``````

line 10: int big=match[0];
That won't work because its declared as a global and vector match does not contain anything.

>>void highest_range(vector<int>match, string range [])

You should pass that vector by reference, not by value, so that the entire vector doesn't have to be duplicated. `void highest_range(vector<int>& …`

## All 4 Replies

line 10: int big=match[0];
That won't work because its declared as a global and vector match does not contain anything.

>>void highest_range(vector<int>match, string range [])

You should pass that vector by reference, not by value, so that the entire vector doesn't have to be duplicated. `void highest_range(vector<int>& match, string range [])` You must make that function return an int value instead of void (read your instructions) then move the declaration from line 10 (mentioned earlier) into this function. Also, there is no reason for it to have that second parameter

``````int highest_range(vector<int>& match)
{
int big=match[0];

// rest of that function goes here

return big;
}``````

Actually the vector match is used to store the frequency count of each range.

I am not intended to use "pass the vector by reference" because i am comparing the value of each element to find out the highest frequency count.

I need to return the highest frequency count and also its range for the conclusion.

I hope you understand my question.

>>I am not intended to use "pass the vector by reference" because i am comparing the value of each element to find out the highest frequency count.

That's ok -- it still should be passed by reference to avoid having to duplicate the entire vector. If you had a vector with a million integers in it duplicating that vector would be very very time consuming as well as a lot of unnecessary extra memory. IMO c++ objects should almost never be passed by value.

Use the const keyword to prevent the functtion from changing it.

``````int highest_range(const vector<int>& match, int& index)
{
int highest = match[0];
index = 0;
int sz = match.size();
for (int j=1;j<sz;j++)
{
if (match[j]>highest)
{
highest=match[j];
index = j;
}
}
return highest;
}``````

Thanks for your help. This is the program that can function

``````#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"};

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];
}

}

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[])
{
int big=match[0];
int size = match.size();
string x=range[0];

for (int j=1;j<size;j++)
{
if (match[j]>big)
{
big=match[j];
x=range[j];
}
}
cout<<"The highest range is "<<x<<" : "<<big<<endl<<endl;
}

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);

system ("pause");
return 0;

}``````
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.