Rating the suitors?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bluevega
    New Member
    • Mar 2007
    • 4

    Rating the suitors?

    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.
  • DeMan
    Top Contributor
    • Nov 2006
    • 1799

    #2
    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, HeightDifferenc e, WeightDifferenc e (where The HeightDifferenc e 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:
    Code:
    int suitorCompare(const void* first, const void* second)
    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):

    Code:
    if(((Suitor*)first->HeightDifference) > ((Suitor*)second->HeightDifference))
    {
      return 1;
    }
    else if )((Suitor*)first->HeightDifference) == ((Suitor*)second->HeightDifference))
    {
      if(((Suitor*)first->WeightDifference) > ((Suitor*)second->WeightDifference))
      {
        return 1;
      }
      else if(((Suitor*)first->WeightDifference) == ((Suitor*)second->WeightDifference))
      {
          /* Do comparisons for Surname then Name (you can use strcmp for this) 
             You want to if surnames are then compare names otherwise return the
             result of the strcmp */
      }
      /* If we get this far, then one of the comparisons failed so second is greater */
      return -1;
    }
    Once you have your method implemented, you can call
    Code:
     qsort (SuitorArray, numberOfSuitors, sizeof(Suitor), suitorCompare);
    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.

    Comment

    • willakawill
      Top Contributor
      • Oct 2006
      • 1646

      #3
      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.

      Comment

      • DeMan
        Top Contributor
        • Nov 2006
        • 1799

        #4
        Hmmmmm have I?

        Comment

        • bluevega
          New Member
          • Mar 2007
          • 4

          #5
          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...

          Comment

          • willakawill
            Top Contributor
            • Oct 2006
            • 1646

            #6
            Originally posted by DeMan
            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

            Comment

            • bluevega
              New Member
              • Mar 2007
              • 4

              #7
              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..

              Comment

              • DeMan
                Top Contributor
                • Nov 2006
                • 1799

                #8
                Originally posted by willakawill
                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.

                Originally posted by Guidelines
                Please make sure you have asked an actual question worded in English outside of the text of your assignment that you have posted.
                Originally posted by bluevega
                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.

                Comment

                • DeMan
                  Top Contributor
                  • Nov 2006
                  • 1799

                  #9
                  Originally posted by bluevega
                  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...

                  Originally posted by bluevega
                  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.

                  Comment

                  • bluevega
                    New Member
                    • Mar 2007
                    • 4

                    #10
                    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 =)

                    Comment

                    Working...