# C program help needed, please!

#### rockandsoul

Joined May 16, 2016
2
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!
C:
    int personCompareByName(const struct Person *a, const struct Person *b){
if(!a&&!b)
return 0;
if(!a)
return 1;
if(!b)
return -1;
return strcmp(a->name, b->name);
}

int personCompareByAge(const struct Person *a, const struct Person *b){
if(!a&&!b)
return 0;
if(!a)
return 1;
if(!b)
return -1;
return a->age-b->age;
}

int personCompareByAgeAndName(const struct Person *a, const struct Person *b){
if(!a&&!b)
return 0;
if(!a)
return 1;
if(!b)
return -1;
if(a->age-b->age !=0)
return a->age-b->age;
personCompareByName(...);
}
Mod edit: code tags

Last edited by a moderator:

#### Papabravo

Joined Feb 24, 2006
18,978
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.

#### WBahn

Joined Mar 31, 2012
26,871
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.

#### WBahn

Joined Mar 31, 2012
26,871
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!
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?

#### ErnieM

Joined Apr 24, 2011
8,328
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.

#### WBahn

Joined Mar 31, 2012
26,871
Here's something to ponder:

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

#### rockandsoul

Joined May 16, 2016
2
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);
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.

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

What can I do to fix these?

#### WBahn

Joined Mar 31, 2012
26,871
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.