By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,388 Members | 1,753 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,388 IT Pros & Developers. It's quick & easy.

Rating the suitors?

P: 4
well i have a project here and i dont how to start it...
here is the specs of it.
what shall i do? binary search tree? but how? use a quick sort?..
please help me... thanks


Rating the Suitors
Pretty Polly has no shortage of gentlemen suitors who come courting. Indeed, her biggest problem is keeping track of who the best ones are. She is smart enough to realize that a program which ranks the men from most to least desirable would simplify her life. She is also persuasive enough to have talked you into writing the program.

Polly really likes to dance, and has determined the optimal partner height is 180 cm tall. Her first criteria is finding someone who is as close as possible to this height; whether they are little taller or shorter doesn’t matter. Among all candidates of the same height, she wants someone as close as possible to 75 kg without going over. If all equal-height candidates are over this limit, she will take the lightest of the bunch. If two or more people are identical by all these
characteristics, sort them by last name, then by first name if necessary to break the tie.

Polly is only interested in seeing the candidates ranked by name, so the input file:

George Bush 195 110
Harry Truman 180 75
Bill Clinton 180 75
John Kennedy 180 65
Ronald Reagan 165 110
Richard Nixon 170 70
Jimmy Carter 180 77

yields the following output:

Clinton, Bill
Truman, Harry
Kennedy, John
Carter, Jimmy
Nixon, Richard
Bush, George
Reagan, Ronald

Input: Assume input is in a file having the filename suitors.txt

Output: Standard console

Hint:
1. It would be better if you will not store the actual height and weight of the suitors, since Polly’s rating criteria for heights and weights are quite fussy, revolving around how these quantities compare to a reference height/weight instead of a usual linear order (i.e., increasing or decreasing). You can alter the height and weight appropriately so the quantities were linearly ordered by desirability.

2. You might want to use the qsort() function, which is part of cstdlib, to do the sorting.
Mar 18 '07 #1
Share this Question
Share on Google+
9 Replies


DeMan
100+
P: 1,806
There is more than one approach, but here is one idea.....
(I will assume for now that you know how to read input, and a reasonably familiar with most programming techniques and describe how, without giving code)
make a Data Structure that stores FirstName, Surname, HeightDifference, WeightDifference (where The HeightDifference is abs(180 - height) and similarly for weight) (we'll call this data structure Suitor...)

Create an Array to store the Suitors that you have made....

Now as I understand, to use the qSort, you need to implement a comparison function following a prototype something like:
Expand|Select|Wrap|Line Numbers
  1. int suitorCompare(const void* first, const void* second)
  2.  
this Comparator should return 0 if both items are equal, -1 if the first is less than the second and 1 if the first is greater than the second.

Casting first and second to a pointer to Suitor (defined earlier), means we can access the elements within the struncture, and the basic comparison would be (in pseudocode to show logic, I leave iot to you to implement):

Expand|Select|Wrap|Line Numbers
  1. if(((Suitor*)first->HeightDifference) > ((Suitor*)second->HeightDifference))
  2. {
  3.   return 1;
  4. }
  5. else if )((Suitor*)first->HeightDifference) == ((Suitor*)second->HeightDifference))
  6. {
  7.   if(((Suitor*)first->WeightDifference) > ((Suitor*)second->WeightDifference))
  8.   {
  9.     return 1;
  10.   }
  11.   else if(((Suitor*)first->WeightDifference) == ((Suitor*)second->WeightDifference))
  12.   {
  13.       /* Do comparisons for Surname then Name (you can use strcmp for this) 
  14.          You want to if surnames are then compare names otherwise return the
  15.          result of the strcmp */
  16.   }
  17.   /* If we get this far, then one of the comparisons failed so second is greater */
  18.   return -1;
  19. }
  20.  
Once you have your method implemented, you can call
Expand|Select|Wrap|Line Numbers
  1.  qsort (SuitorArray, numberOfSuitors, sizeof(Suitor), suitorCompare);
  2.  
where SuitorArray is replaced by whatever you called the array of suitors, numberOfSuitors is the size of the Array, sizeof(Suitor) will return the size of each Suitor element, and suitorCompare is the name of the function you have made for the comparison.
Mar 18 '07 #2

100+
P: 1,646
Hmmm. Looks like you both have stepped outside the posting guidelines with regard to assignments or projects. This will not benefit the reputation of TSDN within academic institutions.
Mar 19 '07 #3

DeMan
100+
P: 1,806
Hmmmmm have I?
Mar 19 '07 #4

P: 4
thank you very much DeMan! thanks a lot for that....
i decided to do it in array of structure just like what you have said..
and somehow.. i'm making progress...
sorry for the trouble that i have caused you..

in your quicksort func you have given me 4 parameters... what i have developed has only 3 parameters.. can you tell me what is the inside of that function that you're telling me? does it still need partition?

thanks a lot...
Mar 19 '07 #5

100+
P: 1,646
Hmmmmm have I?
Well let's have a look:
If it appears that the question has just been pasted directly from a text book or coursework assignment our moderators have been instructed to meet this with a set response. This is a pre-written message asking you to make some attempt at the assignment yourself before asking questions about specific problems and referring you to this FAQ
Mar 19 '07 #6

P: 4
oh i'm sorry i'm not aware of that.. but of course.. my intention is that i'm just asking some other way than what i'm doing (i'm already doing it on my own before i ask this question) and i'll try to look on what approach it will better, their suggestion or my work.. again sorry for that..
Mar 19 '07 #7

DeMan
100+
P: 1,806
Well let's have a look:
If it appears that the question has just been pasted directly from a text book or coursework assignment our moderators have been instructed to meet this with a set response. This is a pre-written message asking you to make some attempt at the assignment yourself before asking questions about specific problems and referring you to this FAQ
Firstly, If there is a problem here: "moderators have been instructed to meet this with a set response" Maybe your outside the Guidelines too....

Secondly, there is a clear question here.....
The poster has made no bones about the fact that this is an assignment he (or she) wants HELP STARTING. I have not posted a solution for him (or her), but have offered some suggestions on how he (or she) might address the problem on the way to finding a solution.

Please make sure you have asked an actual question worded in English outside of the text of your assignment that you have posted.
well i have a project here and i dont how to start it...
here is the specs of it.
what shall i do? binary search tree? but how? use a quick sort?..


I'll concede that maybe I violated the letter of the law in the guidelines, but essentially the idea is to offer help not solutions....I have explained how this question might be approached, and what certain parts of the question may be, and don't believe I have breached the idea behind the guidelines. If you feel that there is a problem with any of this, perhaps you should bring this to the attention to Admin.
Mar 19 '07 #8

DeMan
100+
P: 1,806
in your quicksort func you have given me 4 parameters... what i have developed has only 3 parameters.. can you tell me what is the inside of that function that you're telling me?
The qsort used above is the one mentioned in your original post...

2. You might want to use the qsort() function, which is part of cstdlib, to do the sorting.
which takes as parameters: the Array to be sorted, the number of elements in the array, the size of each element and Comparator function to use for the sorting.
Mar 19 '07 #9

P: 4
thanks again! =) somehow everything is ok... just having problems with the parameters of my functions and making the array of structures (it seems that what i'm doing is that it does not accept a structure nor making array of it)... some arguments.. sorry for the trouble again and thanks a lot =)
Mar 20 '07 #10

Post your reply

Sign in to post your reply or Sign up for a free account.