Passing a Value between an Array and Another Function
Expand Post »
I'm trying to pass the valuee from the arrays for age, sex, and wTime to the cal_Fitness_Level function, but I can't seem to get it right. I either get the same level for everyone or garbage. code and data follows.
outFile << '\n' << "Number of records: " << num << endl;
return0;
}
int cal_Fitness_Level(int age[], char sex[], int wTime[])
{
int i = 0, fL;
if(age[i] >= 13 && age[i] <= 19)
{
if(wTime[i] >= 48)
fL = 1;
elseif(wTime[i] > 43 && wTime[i] <= 47)
fL = 2;
elseif(wTime[i] > 39 && wTime[i] <= 43)
fL = 3;
elseif(wTime[i] > 35 && wTime[i] <= 39)
fL = 4;
elseif(wTime[i] < 35)
fL = 5;
}
else
fL = 0;
return fL;
}
void sortData(string lName[], string fName[], int age[], char sex[], int wTime[], int noOfRows)
{
int i, j;
int min;
// selection sort
for(i = 0; i < noOfRows - 1; i++)
{
// step a
min = i;
for(j = i + 1; j < noOfRows; j++)
if(lName[j] < lName[min])
min = j;
if(min!=i)// step b
lName[i].swap(lName[min]);
fName[i].swap(fName[min]);
age[i] = age[min];
sex[i] = sex[min];
wTime[i] = wTime[min];
}
}
Michael Brooks 13 M 33
Amy Shields 30 F 40
Clara Miles 50 F 30
Robert Davidson 20 M 45
Joshua Chase 25 M 42
Jackie Choker 20 F 29
Sarla Kothari 60 F 37
George Runner 53 M 49
Sally Jones 19 F 47
Re: Passing a Value between an Array and Another Function
Quote originally posted by Saint48198 ...
I get a compiler error (C2662) when I try that. I do not think that you can reference an array in that format when calling a function.
If you are only interested in a single value inside each of the arrays, passing a pointer to the array makes no sense, instead, change your function declaration to be:
Re: Passing a Value between an Array and Another Function
That somewhat worked. The last three employees data is out of order. I've include the level variable in the sort function and modified the cal_fitness_level to look like Cup of Crazy's suggestion. I'm not sure why the last three are getting mixed up.
Re: Passing a Value between an Array and Another Function
> I'm not sure why the last three are getting mixed up.
Is the data OK before you sort it?
> if(min!=i)
Consider using a lot more { } in this function, because it seems to me that not everything is being run at the correct time. Yes, braces can be optional in some circumstances, but if you take the approach of always using them, you minimise your surprises later on.
No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.