I am attempting to read info from a file, calculate averages, and return the lowest percentage and the occupation associated with it. The program originally used a void function but I have been instructed modify the program to use a struct. I have tried returning the variable holding the calculation but I get a compiler error saying that conversion from struct to double isn't possible.
This is the function looking for the lowest percentage.
Occupation findLow(Occupation occupations[], int rows)
{
double avgLow = 0.0;
double lowPercent;
string occu;
lowPercent = occupations[0].numSusceptible / occupations[0].numEmployed;
// Loop while 'i' has not reached 'rows'
for (int i = 0; i < rows ; i++)
{
// Divide second row of 'occupations' array by contents of first row of 'occupations'
// array. Store results into 'avgLow' array.
avgLow = (float)occupations[i].numSusceptible / occupations[i].numEmployed;
// Find the occupation with the lowest susceptibility for automation
// and set 'lowPercent' and 'occupation' to these values.
if (avgLow < lowPercent)
{
lowPercent = avgLow;
occu = occupations[i].occupation;
}
}
lowPercent *= 100;
} // End of findLow
If the output information is just lowPercent and occu then you could return these as reference arguments.
If, on the other hand, you want to return an "Occupation" then you need to keep tabs on the index associated with the lowest percent and then return that particular occupation[] element at the end.
So, it isn't clear exactly what you want.
Smaller things:
- occu isn't necessarily set; initialise it immediately after initialising lowPercent on line 6.
- use (double), not (float), on line 13.
- avgLow isn't an array (line 12); is this your comment or a requirement?
- comments shouldn't "state the bleedin' obvious".