Solution to Lecture Quiz 3

Solution to Lecture Quiz 3


int CompareHands(char hand1[MAX_HAND_SIZE][CARD_STR_LEN],
        char hand2[MAX_HAND_SIZE][CARD_STR_LEN]) {

    int i;                              // Loop and array index
    int sum1 = 0;                       // Sum of hand1 cards
    int sum2 = 0;                       // Sum of hand2 cards

    //
    // Loop through the cards of hand1, summing the adjusted card values.
    //
    for (i = 0; strcmp(hand1[i], BLANK_CARD) != 0; i++) {
        sum1 = sum1 + 10 * ComputeFaceValue(hand1[i]) +
            ComputeSuitValue(hand1[i]);
    }

    //
    // Ditto for hand2.
    //
    for (i = 0; strcmp(hand2[i], BLANK_CARD) != 0; i++) {
        sum2 = sum2 + 10 * ComputeFaceValue(hand2[i]) +
            ComputeSuitValue(hand2[i]);
    }

    //
    // Return the result as the difference of the sums, which will produce the
    // desired result in terms of positive, negative, or zero value.
    // Alternatively, this could be done with an if-then-else statement.
    //
    return sum1 - sum2;
}


//
// NOTE: An alternative summing algorithm is to compute both sums in a single
// loop, using conditional logic to stop summing the shorter array when its end
// is reached.  The two-loop solution above is probably easier to understand.
//


index | lectures | labs | handouts | assignments | solutions | grades | help