• Tidak ada hasil yang ditemukan

Find the Sum of Two Lengths

Dalam dokumen Learn to - Springer Link (Halaman 86-89)

4.3 The if Construct

4.3.1 Find the Sum of Two Lengths

Suppose that a length is given in meters and centimeters, for example, 3m 75cm. You are given two pairs of integers representing two lengths. Write a program to prompt for two lengths and print their sum such that the centimeter value is less than 100.

For example, the sum of 3m 25cm and 2m 15cm is 5m 40cm, but the sum of 3m 75cm and 5m 50cm is 9m 25cm.

Assume the program works as follows:

Enter values for m and cm: 3 75 Enter values for m and cm: 5 50 Sum is 9m 25cm

Observe that the data must be entered with digits only. If, for instance, we type 3m 75cm we will get an error since 3m is not a valid integer constant. Our program will assume that the first number entered is the meter value and the second number is the centimeter value.

We find the sum by adding the two meter values and adding the two centimeter values. If the centimeter value is less than 100, there is nothing more to do. But if it is not, we must subtract 100 from it and add 1 to the meter value. This logic is expressed as follows:

m = sum of meter values cm = sum of centimeter values if cm >= 100 then

subtract 100 from cm add 1 to m

endif

As a boundary case, we must check that our program works if cm is exactly 100. As an exercise, verify that it does.

Program P4.2 solves the problem as described.

Program P4.2

//find the sum of two lengths given in meters and cm

#include <stdio.h>

int main() {

int m1, cm1, m2, cm2, mSum, cmSum;

printf("Enter values for m and cm: ");

scanf("%d %d", &m1, &cm1);

printf("Enter values for m and cm: ");

scanf("%d %d", &m2, &cm2);

mSum = m1 + m2; //add the meters cmSum = cm1 + cm2; //add the centimeters if (cmSum >= 100) {

cmSum = cmSum - 100;

mSum = mSum + 1;

}

printf("\nSum is %dm %dcm\n", mSum, cmSum);

}

We use the variables m1 and cm1 for the first length, m2 and cm2 for the second length, and mSum and cmSum for the sum of the two lengths.

The program assumes that the centimeter part of the given lengths is less than 100 and it works correctly if this is so. But what if the lengths were 3m 150cm and 2m 200cm?

The program will print 6m 250cm. (As an exercise, follow the logic of the program to see why.) While this is correct, it is not in the correct format since we require the centimeter value to be less than 100. We can modify our program to work in these cases as well by using integer division and % (the remainder operator).

The following pseudocode shows how:

m = sum of meter values cm = sum of centimeter values if cm >= 100 then

add cm / 100 to m set cm to cm % 100 endif

Using the above example, m is set to 5 and cm is set to 350. Since cm is greater than 100, we work out 350 / 100 (this finds how many 100s there are in cm ) which is 3, using integer division; this is added to m, giving 8. The next line sets cm to 350 % 100, which is 50. So the answer we get is 8m 50cm, which is correct and in the correct format.

Note that the statements in the “then part” must be written in the order shown. We must use the (original) value of cm to work out cm / 100 before changing it in the next statement to cm % 100.

As an exercise, work out what value will be computed for the sum if these statements are reversed.

(The answer will be 5m 50cm, which is wrong. Can you see why?) These changes are reflected in Program P4.3.

Program P4.3

//find the sum of two lengths given in meters and cm

#include <stdio.h>

int main() {

int m1, cm1, m2, cm2, mSum, cmSum;

printf("Enter values for m and cm: ");

scanf("%d %d", &m1, &cm1);

printf("Enter values for m and cm: ");

scanf("%d %d", &m2, &cm2);

mSum = m1 + m2; //add the meters

cmSum = cm1 + cm2; //add the centimeters if (cmSum >= 100) {

mSum = mSum + cmSum / 100;

cmSum = cmSum % 100;

}

printf("\nSum is %dm %dcm\n", mSum, cmSum);

}

The following is a sample run of this program:

Enter values for m and cm: 3 150 Enter values for m and cm: 2 200 Sum is 8m 50cm

The astute reader may recognize that we do not even need the if statement.

Consider this:

mSum = m1 + m2; //add the meters

cmSum = cm1 + cm2; //add the centimeters mSum = mSum + cmSum / 100;

cmSum = cmSum % 100;

where the last two statements come from the if statement.

We know therefore that this will work if cmSum is greater than or equal to 100 since, when that is the case, these four statements are executed.

What if cmSum is less than 100? Originally, the last two statements would not have been executed since the if condition would have been false. Now they are executed. Let us see what happens. Using the example of 3m 25cm and 2m 15cm, we get mSum as 5 and cmSum as 40.

In the next statement 40 / 100 is 0 so mSum does not change and in the last statement 40 % 100 is 40 so cmSum does not change. So the answer will be printed correctly as Sum is 5m 40cm

You should begin to realize by now that there is usually more than one way to express the logic of a program. With experience and study, you will learn which ways are better and why.

Dalam dokumen Learn to - Springer Link (Halaman 86-89)