Search code examples
cintegerunsigned

Unsigned int values


In this program, counter is declared with type unsigned int. Unsigned integer values are 0 or greater. All other variables are declared int. Those values will also only be a positive value. So why is each variable not declared as unsigned int?


Solution

  • The problem specification does not stipulate that grade is greater than or equal to zero.

    You may be familiar with grades from 0 to 100 or other non-negative scales, but that does not mean somebody else does not use a scale from −5 to +5. Some standardized test questions award −1 point for a wrong answer, 0 for no answer, and 3 for a correct answer so that, when there are four multiple-choice answers, random guessing has an average score of zero, but guessing after eliminating a known-wrong answer has a positive average score, so the test more accurately measures knowledge even if it is incomplete.

    In general, one should not make assumptions about data that are not stated in the problem. Being aware of one’s own inherent assumptions and questioning them when a problem statement is incomplete is an important skill for designing software.

    Further, even if the grade range is 0 to 100, both unsigned int and int cover that range, so there is little reason to choose one over the other.