Lab 3: Experiment 3


Arithmetic Expressions

The original version of our program prints the sum of i and j. C++ provides us with several arithmetic operators:

+ addition, computes the sum of two (integer or real) operands
- subtraction, computes the difference of two (integer or real) operands
* multiplication, computes the product of two (integer or real) operands
/ division, computes the quotient of the division two (integer or real) operands
% modulus, computes the remainder of the division two integer operands

Suppose we wanted to compute the product (i.e., the multiplication) of i and j instead of their sum.

Question #3.3.1: What change would we have to make to the program?

Test your change to make sure.

These arithmetic operators should be familiar to you, except for the last one. Let's spend some time looking at it.

Integer and Real Division

There is an important difference between the division of two integers and two real numbers. Let's make some changes to our program so that we can explore these differences.

The first change is to use an input statement to get values for i and j. Experiment #2 had you add the input statement. With this input statement you can compile the program once but execute it many times for different values for i and j.

The second change is to compute two values. The output statement already computes one value; make it compute i/j. Then add to the output expression so that it also prints out i%j. Be sure to change the string labels so that you can read your output easily.

Once you have these changes made, compile and execute the program to make sure it's correct. Then, using this code as a basis, write similar lines of code for double variables x and y. For these real-number variables, you shouldn't compute x%y since C++ won't let you.

Question #3.3.2: Use your program to fill in this chart:
i j i / j i % j x y x / y
4 1     4.0 1.0  
4 2     4.0 2.0  
4 3     4.0 3.0  
4 4     4.0 4.0  
4 5     4.0 5.0  
4 6     4.0 6.0  
4 7     4.0 7.0  
4 8     4.0 8.0  
4 9     4.0 9.0  

Recall that an integer is a whole number without any fractional part. So when you divide the integer 4 by the integer 5 (i.e, 4/5), you can't do this evenly, not even once. So the integer division is 0.

But as a real number, you can compute fractional amounts. So 4.0/5.0 is 0.8 as a real number. Note that this is the decimal equivalent of the fraction 4.0/5.0.

But what about that modulus computation? "Modulus" is (for the most part) just another name for "remainder". When you first learned about division, you probably learned to talk about your result in terms of quotient and remainder:

"28 divided by 3 is 9 with a remainder of 1."

Splitting Apart Integers

Division and modulus by 10 allow us to split up integers into their decimal digits.

Question #3.3.3: Use your program to fill in this chart:
i j i / j i % j
1234 1    
1234 10    
1234 100    
1234 1000    

Can you see a pattern? How significant is the number of 0s in j?

Question #3.3.4: Use your observation to fill in this chart:
i j i / j i % j
5678 1    
5678 10    
5678 100    
5678 1000    
Use your program if you get stuck and to check your answers.

Multiples

The multiples of 2 are 0, 2, 4, 6, 8, 10, 12, ... The multiples of 3 are 0, 3, 6, 9, 12, ...

What do the multiples of 2 have in common? Two evenly divides each multiple of two; that is, two divides each multiple without any remainder. Ah! So if m % 2 is 0, then m must be a multiple of two.

Use this same thinking to come up with an arithmetic expression to use as a test for these multiples:

Question #3.3.5: What do the multiples of 3 have in common?

Question #3.3.6: What do the multiples of 4 have in common?

The multiples of 2 are also known as the even numbers.

Question #3.3.7: What do the even numbers have in common?

The odd numbers are all the whole numbers that aren't even.

Question #3.3.8: What do the odd numbers have in common?

Terminology

modulus, remainder
Back to the Lab Exercise  |  Forward to the Next Experiment
© 2003 by Prentice Hall. All rights reserved.
Report all errors to Jeremy D. Frens.