106k views
2 votes
Given the string variables name1 and name2, write a fragment of code that assigns the larger of the two to the variable first (assume that all three are already declared and that name1 and name2 have been assigned values).

User Necevil
by
7.5k points

1 Answer

7 votes
To screw with your teacher:

first = (name1 > name2 ) ? name1 : name2;

That code is correct, but your teacher is probably looking for:

if( name1 > name2 )
first = name1;
else
first = name2;

I love the ternary operator!

User Ankit Deshpande
by
8.5k points