20.09.2015 Views

Programming in C

Kochan - ProgramminginC

Kochan - ProgramminginC

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

The if Statement<br />

69<br />

would do the trick. However, recall that if the preced<strong>in</strong>g statement were used, the decimal<br />

portion of the result of the division would be lost.This is because an <strong>in</strong>teger division<br />

would be performed because both the numerator and the denom<strong>in</strong>ator of the division<br />

operation are <strong>in</strong>tegers.<br />

Two different solutions are possible for this problem. One is to declare either<br />

numberOfGrades or gradeTotal to be of type float.This then guarantees that the division<br />

is carried out without the loss of the decimal places.The only problem with this<br />

approach is that the variables numberOfGrades and gradeTotal are used by the program<br />

to store only <strong>in</strong>teger values. Declar<strong>in</strong>g either of them to be of type float only obscures<br />

their use <strong>in</strong> the program and is generally not a very clean way of do<strong>in</strong>g th<strong>in</strong>gs.<br />

The other solution, as used by the program, is to actually convert the value of one of<br />

the variables to a float<strong>in</strong>g-po<strong>in</strong>t value for the purposes of the calculation.The type cast<br />

operator (float) is used to convert the value of the variable gradeTotal to type float<br />

for purposes of evaluation of the expression. Because the value of gradeTotal is cast<br />

<strong>in</strong>to a float<strong>in</strong>g-po<strong>in</strong>t value before the division takes place, the division is treated as the<br />

division of a float<strong>in</strong>g value by an <strong>in</strong>teger. Because one of the operands is now a float<strong>in</strong>gpo<strong>in</strong>t<br />

value, the division operation is carried out as a float<strong>in</strong>g-po<strong>in</strong>t operation.This<br />

means, of course, that you obta<strong>in</strong> those decimal places that you want <strong>in</strong> the average.<br />

After the average has been calculated, it is displayed at the term<strong>in</strong>al to two decimal<br />

places of accuracy. If a decimal po<strong>in</strong>t followed by a number (known collectively as a precision<br />

modifier) is placed directly before the format character f (or e) <strong>in</strong> a pr<strong>in</strong>tf format<br />

str<strong>in</strong>g, the correspond<strong>in</strong>g value is displayed to the specified number of decimal places,<br />

rounded. So <strong>in</strong> Program 6.2, the precision modifier .2 is used to cause the value of<br />

average to be displayed to two decimal places.<br />

After the program has displayed the number of fail<strong>in</strong>g grades, execution of the program<br />

is complete.<br />

The if-else Construct<br />

If someone asks you whether a particular number is even or odd, you most likely make<br />

the determ<strong>in</strong>ation by exam<strong>in</strong><strong>in</strong>g the last digit of the number. If this digit is either 0, 2, 4,<br />

6, or 8, you readily state that the number is even. Otherwise, you claim that the number<br />

is odd.<br />

An easier way for a computer to determ<strong>in</strong>e whether a particular number is even or<br />

odd is affected not by exam<strong>in</strong><strong>in</strong>g the last digit of the number to see if it is 0, 2, 4, 6, or<br />

8, but by simply determ<strong>in</strong><strong>in</strong>g whether the number is evenly divisible by 2. If it is, the<br />

number is even; else it is odd.<br />

You have already seen how the modulus operator % is used to compute the rema<strong>in</strong>der<br />

of one <strong>in</strong>teger divided by another.This makes it the perfect operator to use <strong>in</strong> determ<strong>in</strong><strong>in</strong>g<br />

whether an <strong>in</strong>teger is evenly divisible by 2. If the rema<strong>in</strong>der after division by 2 is<br />

zero, it is even; else it is odd.<br />

Look at Program 6.3—a program that determ<strong>in</strong>es whether an <strong>in</strong>teger value typed <strong>in</strong><br />

by the user is even or odd and that displays an appropriate message at the term<strong>in</strong>al.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!