C program help needed, please!

Discussion in 'Programmer's Corner' started by rockandsoul, May 16, 2016.

  1. rockandsoul

    Thread Starter New Member

    May 16, 2016
    2
    0
    I have the following program detail that I need help with. The personCompareByName and personCompareByAge is working fine, I just need to put these two together to work by comparing the names first by age and then by name. I can't figure out what to write in the last line of the personCompareByAgeAndName function. I need to use the a and b variables I just can't seem to figure out how. Thank you all in advance for your help, it's greatly appreciated!
    Code (C):
    1.  
    2.     int personCompareByName(const struct Person *a, const struct Person *b){
    3.         if(!a&&!b)
    4.             return 0;
    5.         if(!a)
    6.             return 1;
    7.         if(!b)
    8.             return -1;
    9.         return strcmp(a->name, b->name);
    10.     }
    11.  
    12.     int personCompareByAge(const struct Person *a, const struct Person *b){
    13.         if(!a&&!b)
    14.             return 0;
    15.         if(!a)
    16.             return 1;
    17.         if(!b)
    18.             return -1;
    19.         return a->age-b->age;
    20.     }
    21.  
    22.    int personCompareByAgeAndName(const struct Person *a, const struct Person *b){
    23.         if(!a&&!b)
    24.             return 0;
    25.         if(!a)
    26.             return 1;
    27.         if(!b)
    28.             return -1;
    29.        if(a->age-b->age !=0)
    30.             return a->age-b->age;
    31.         personCompareByName(...);
    32.    }
    Mod edit: code tags
     
    Last edited by a moderator: May 16, 2016
  2. Papabravo

    Expert

    Feb 24, 2006
    10,142
    1,790
    I don't think your return values are unique.
    If strcmp returns:
    The strcmp() function returns an integer greater than, equal to, or less than zero, accordingly as the string pointed to by s1 is greater than, equal to, or less than the string pointed to by s2.
    If it returns values of -1,0, and 1 for valid results how do you distinguish a valid comparison from one with a null pointer?
    BTW the original functions have the same problem.
     
  3. WBahn

    Moderator

    Mar 31, 2012
    17,743
    4,795
    Since his comparison function returns an integer value, he has a hard time returning a value that specifically identifies that one or both of the input structures were NULL. What his functions are doing are basically saying that two NULL structures compare as being equal while if one is NULL and the other isn't, that the comparison goes in favor of the one that is not NULL.
     
  4. WBahn

    Moderator

    Mar 31, 2012
    17,743
    4,795
    If I gave you two functions, CompareByX(A,B) and CompareByY(A,B) and asked you to write a code snippet that compared A and B first by X and then by Y, could you do it?
     
  5. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,387
    1,605
    WBahn is giving you excellent advice on code reuse. Since you already have functions to do the individual testing you can call these functions from a third function to do both comparisons.

    That is an excellent way of doing things since the individual comparisons are only coded once, so you have less debugging to do.

    The way you have it if there is an error (hint hint) in your individual comparisons once you fix it there you need not worry about fixing it elsewhere too.
     
  6. WBahn

    Moderator

    Mar 31, 2012
    17,743
    4,795
    Here's something to ponder:

    return (m = f1(s,t))? m : f2(s,t);
     
  7. rockandsoul

    Thread Starter New Member

    May 16, 2016
    2
    0
    I'm fairly new to programming so I really don't understand much of it. I tried doing what you said, to combine the two functions in a third by calling these two functions. But I don't think I'm doing it right. Should this be what it should look like?

    int personCompareByAgeAndName(const struct Person *a, const struct Person *b)
    {
    if personCompareByAge(const struct Person *a, const struct Person *b) !=0
    return a->age-b->age;
    else personCompareByName(const struct Person *a, const struct Person *b)
    return strcmp(a->name, b->name);
    }

    Also whenever I try to run it I get the following error message:
    error: too few arguments to function 'personCompareByAgeAndName'

    initElapsed(&elapsed);
    personBubbleSort(head, ASCENDING, &personCompareByAgeAndName());
    endElapsed(&elapsed);

    I also get this message for the 'while' line below:
    warning: suggest parentheses around assignment used as truth value [-Wparentheses]
    This last code is part of the main function of my program.

    struct Person *p=head;
    int counts=0;
    while(p=findPersonByAge(p->next, 20))
    printPerson(p, ++counts);

    What can I do to fix these?
     
  8. WBahn

    Moderator

    Mar 31, 2012
    17,743
    4,795
    First, you need to conform to the syntax requirements for C.

    The if() statement REQUIRES that the test expression be contained in parentheses.

    if (x > b)
    doSomething();

    is fine, but

    if x > b
    doSomething();

    is not.

    Next, when you call a function, you provide ONLY the arguments.

    When you use sin() from the math.h library, do you call it like

    y = sin(double x); ?

    No. You call it like

    y = sin(x);

    The same applies for functions that YOU write -- they are no different.

    Third, the whole point of using simpler functions to build more complex functions is that you stop worrying or relying on the underlying representation.

    You have a function personCompareByAge(person1, person2) that compares the ages of person1 and person2 and returns an appropriate integer.

    You have a function personCompareByName(person1, person2) that compares the names of person1 and person2 and returns an appropriate integer.

    Your personCompareByAgeAndName(person1, person2) should use these two functions and should NOT dig into the internal representation of how age and name are within the person structure.
     
Loading...