I have been bashing away at this for hours and am not making any new ground.

I have to find the employee that is closest to the all the average.

Originally I thought I would just see how far each value was away from the average (eg ageRemainder = averageAge - age), add all of the remainders together for each employee, then organise the results. Then score that is lowest would be the winner. BUT this is discriminatory, especially if I was to add in more catagories with large figures like for an example an income.

SO I then thought that it would be best to find the difference from the average for each employee variable and then rank each employee for that variable. Eg if the average height is 160 cms and Rob is 180cm his score for heightDifference would be 20. I would then find out who had the least difference of height from the average and rank everyone accordingly. I wanted to do this for all the variables.. eg age, income etc.

Finally to add up all the rank scores for each individual and who ever is lowest overall rank score would have the overall closest average.

I have coded the first part with dynamically allocated memory depending on how many employees the user wanted to input. I am up to where it returns the average. But I am damned if I can figure out a good way using code reuse to do this. Which makes me question if I am thinking about this in the totally wrong way. I have been scribbling away pen and paper and have not made any progress in hours.

Hence I though I might come on here to discuss if I am even going about the algorithm in the right way.

Regards,
Nate

You aren't being very clear due to grammatical errors in your posting!

ASSUMING You want the employee closest to the average

First sum and count the number of employees.
If 0 then none
If 1 then that employee (or include into the averaging).
If more then one

sum = 0
for n = 0; n < count; n++
sum += age
end for

float avg = (float)sum / (float)count

Now loop again but remember ages occur below and above the average age!

for n = 0 n < count n++
da = abs( age - sum)
loop while tracking closest employee with closest absolute age
end for

Hmm sorry if that is not clear..

I want to find the employee that has falls closest to the average OF ALL THE VALUES. To solve my problem I have worked out a rank system.

name------age-------height----------income
mary-------25--------170--------------30000
wayne------30--------180-------------100000
olga---------30--------164-------------120000
juno----------21--------20--------------------0

The average are -
--------------26.5------178.5-----------355000

Deviations from the average is as follows -

name--------age-------height----------income
mary---------1.5---------8.5------------5500
wayne--------3.5----------1.5 --------- 64500
olga ----------3.5---------14.5 ---------23000
juno-----------5.5---------168.5--------35500

If I rank each person from 1 to 4 on each catagory, meaning on how close they are to the average. Then I add all the rank scores for each individual.
Eg In terms of closeness to the average - for example Mary ranks 1 on age, 2 on height and 1 on income.

Mary 4
Wayne 7
Olga 7
Juno 10

Hence Mary is the closest over all, to the averages.

So it is all good on my piece of paper, but I unsure how to transfer it cleanly into some code. I had defined a struct as such, dynamic allocation of the employee array, and I have written up to, the returning of the averages. But on paper I am unable to nail a clean way to tie it all together.

As I will need to rank and sort the values whilst still keeping them associated to the employee.

typedef struct
{
int id;
char name[20];
int age;
int height;
int weight;
int income;
}employee;

I think it will be quite a tedious process to process, sort etc the way I am currently focusing and was seeing if anyone has some better suggestions on where I should be looking/thinking.

I hope this makes sense I am trying to explain as best I can.

Be a part of the DaniWeb community

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