**Homework**-
Write a program that reads in two fractions from the
user and prints out the sum of the two numbers in lowest
terms. Recall (from grade school!) that
n1 n2 n1 d2 + n2 d1 -- + -- = ------------- d1 d2 d1 d2

and that to reduce a fraction to lowest terms you divide the numerator*n*and the denominator*d*by the greatest common divisor of*n*and*d*. Of course, further down in these lecture notes we discuss greatest common divisor! A run of your program should look something like this:Enter a fraction addition problem: 15/28 + 7/32 15/28 + 7/32 = 169/224

**Turn In**a printout of your code, along with a screen capture of your program running on input`15/28 + 7/32`

. **Lecture**-
This lecture is going to be mostly about using
`while`

loops to solve a variety of problems. There will also be a few "detail" issues that'll come up.## My Favorite Algorithm: The Euclidean GCD Algorithm

The greatest common divisor (GCD) of two positive integers`a`

and`b`

is the largest integer than evenly divides both`a`

and`b`

. You may remember that some math teacher along the way told you that an answer like 3/6 is wrong, because it can be*reduced*to 1/2. How? By dividing out by the GCD of the numerator and the denominator - 3 in this case. So an algorithm that computes the GCD of two numbers is useful ... at least for placating math teachers. It's actually useful in an unimaginable number of other contexts, in things as diverse as encryption for online-banking to controlling the motions of robots.Strangely, however, today's best known algorithm for solving this problem was invented thousands of years ago. It is called the "Euclidean Algorithm" because it appears in Euclid's book "The Elements", which contained most of the mathematics known in the aancient greek world. The basic process (algorithm) for computing the GCD of

`a`

and`b`

is this: Assuming`a`

is the larger of the two, compute the remainder when`a`

is divided by`b`

, and replace`a`

with`b`

and`b`

by the remainder. Keep doing this until you get a remainder of zero, at which point`b`

is the GCD. For example, if`a`

is 42 and`b`

is 30, thena b remainder ----------------- 42 30 12 30 12 6 12 6 0

... and there fore 6 is the GCD. We can do this with a loop, and hopefully it's clear that the loop will look something like this:while(no idea what goes here!) { r = a % b; // remember "%" is remainder a = b; b = r; }

We have two questions now: When do we stop? and Where is the anwer when we stop? We could try to be clever, but that's difficult. Let's just see what happens with the code we do have in the example from above:Before iteration 1: r = ?, a = 42, b = 30 Before iteration 2: r = 12, a = 30, b = 12 Before iteration 3: r = 6, a = 12, b = 6 Before iteration 4: r = 0, a = 6, b = 0 Stop! No 4th iteration!

As you can see, our clue to stop is that`r`

is zero (or, you could say that`b`

being zero is our clue), and you can see that the answer resides in the variable`a`

. So, we could complete our loop like this:while (b != 0) { r = a % b; // remember "%" is remainder a = b; b = r; } cout << a << " is the GCD!" << endl;

## Interest Done Right: Counting our iterations

In the Problems from Class 3, we considered the problem of computing compound interest. The user entered an annual interest rate`r`

, and we fingured out how much you'd have after 10 years at that rate with an investment of $100.00 in the account at the beginning of each year. The bulk of the program might look like the following.double T; T = 0.0; // Before year 1 T = (T + 100.0)*(1.0 + r/100.0); // After year 1 T = (T + 100.0)*(1.0 + r/100.0); // After year 2 T = (T + 100.0)*(1.0 + r/100.0); // After year 3 T = (T + 100.0)*(1.0 + r/100.0); // After year 4 T = (T + 100.0)*(1.0 + r/100.0); // After year 5 T = (T + 100.0)*(1.0 + r/100.0); // After year 6 T = (T + 100.0)*(1.0 + r/100.0); // After year 7 T = (T + 100.0)*(1.0 + r/100.0); // After year 8 T = (T + 100.0)*(1.0 + r/100.0); // After year 9 T = (T + 100.0)*(1.0 + r/100.0); // After year 10

Now, to do this right, we'd like the user to enter the annual interest rate`r`

and the number of years you'll keep money in the account`y`

, and we'd like our program to figure out how much you'd have after`y`

years at that rate with an investment of $100.00 in the account at the beginning of each year.Why couldn't we do that in Class 4? Because you didn't know about loops! In the Class 4 solution, we needed one line in our program for each year. If the user entered 100 years, we'd need to type in a 100+ line line program. To make the program work for any value of

`y`

, you need a loop.First, let's try to rewrite the above block of code, in which the number of years is fixed at 10, using a loop. Fortunately, the above code breaks into a loop pretty easily:

double T; T = 0.0; // Initialization for the loop while (I need to loop 10 times!) { T = (T + 100.0)*(1.0 + r/100.0); // After year ? }

We need to keep track of how many times we've gone through the loop body, and once we've gone through the loop body 10 times, we stop. Let's introduce an`int`

variable`i`

to keep track of the number of times we've gone through the loop body, i.e. to keep track of the number of*iterations*of the loop.double T; T = 0.0; // We don't have any money yet int i; i = 0; // We haven't completed any iterations yet while (I need to loop 10 times!) { T = (T + 100.0)*(1.0 + r/100.0); // After year ? i = i + 1; // record that we've completed an iteration }

Now, what about the test condition for this loop? Well, we want to keep looping as long as the number of loop iterations completed is less than 10. Once we've completed 10 loop iterations we can move on an print our answer.double T; T = 0.0; // We don't have any money yet int i; i = 0; // We haven't completed any iterations yet while (i < 10) { T = (T + 100.0)*(1.0 + r/100.0); // After year i + 1 i = i + 1; // record that we've completed an iteration }

Now, what about making this work for any number of years? Here's a complete solution.## The second simplest calculator

Let's consider writing a program that reads from the user a simple arithmetic expression using + and -, like`3 + 4 - 7 + 2 - 4 =`

, and prints out the result of the expression. Essentially your program will be a big while loop, and initially you might decide to write something like this// Initialize total to 0 // Loop // read next value // add or subtract value from total

We can start filling in code here too, since we know how to do some of it at least:

// Initialize total to 0 total = 0; // Loop while(I don't know what goes here) { // read next value cin >> k; // add or subtract value from total if (I don't know what goes here either) total = total + k; else total = total - k; }

Now, this far things are easy ... they'll start to get tricky. What we haven't dealt with is reading in the +/-'s, and using them to make up test conditions for our loop or our if statement. Let's suppose we read the +/- operation into a variable of type

`char`

which we'll call`op`

. Now, which of our three problems will we face next:- when to read in
`op`

- how to do the loop test condition
- how to do the if test condition

The easiest is probably "how to do the if test condition", if

`op`

is a plus sign we add, if it's a minus we subtract. That gives us:// Initialize total to 0 total = 0; // Loop while(I don't know what goes here) { // read next value cin >> k; // add or subtract value from total if (op == '+') total = total + k; else total = total - k; }

So, let's next address the question of when to read

`op`

. We know that`op`

will have to be read somewhere in the loop, and the question is just where? The way I see it there are three choices for where we'll put`cin >> op`

:// Initialize total to 0 total = 0; // Loop while(I don't know what goes here) { <------------------------------- Choice #1 // read next value cin >> k; <------------------------------- Choice #2 // add or subtract value from total if (op == '+') total = total + k; else total = total - k; <------------------------------- Choice #3 }

Choice #2 can be ruled out immediately, since the

`op`

value we want when we do our if-test is the one that came**before**the`k`

-value, not the one that comes**after**. Choice #1 can be provisionally thrown out, because the first time through the loop there is no operation to read. (I say "provisionally", because we might be able to get around this by handleing our initialization differently. So, we're left with Choice #3, which gives us:// Initialize total to 0 total = 0; // Loop while(I don't know what goes here) { // read next value cin >> k; // add or subtract value from total if (op == '+') total = total + k; else total = total - k; // read next op value cin >> op; }

Now, the only thing left is to find a test condition for our while loop. Let's run through our code on the example input "

`3 + 5 - 2 =`

" supposing that the while loop test condition simply does what we want. Keep an eye out for what signals us that we're done with the loop:Hopefully you see that the signal that we shouldn't loop anymore is when

`op`

is =.// Initialize total to 0 total = 0; // Loop while(op != '=') { // read next value cin >> k; // add or subtract value from total if (op == '+') total = total + k; else total = total - k; // read next op value cin >> op; }

We're still not home yet though, because the first time through the while loop, we test the variable

`op`

, but it doesn't have any value! Thus,`op`

needs to be initialized appropriately. Here is a complete solution.## Problems

- Find the largest file is a directory listing. I asked the
computer to list the files in one of my directories, and this
is what it printed out:
-rwxr-xr-- 1 wcbrown faculty 7084 Aug 9 17:00 a.out* -rw-r--r-- 1 wcbrown faculty 14016 Aug 10 08:03 Class.html -rw-r--r-- 1 wcbrown faculty 1269 Aug 9 09:41 Ex1.cpp -rw-r--r-- 1 wcbrown faculty 3299 Aug 9 09:42 Ex1.html -rw-r--r-- 1 wcbrown faculty 765 Aug 9 16:55 Ex3.cpp -rw-r--r-- 1 wcbrown faculty 2445 Aug 9 16:56 Ex3.html -rw-r--r-- 1 wcbrown faculty 620 Aug 9 17:00 Ex3hack.cpp -rw-r--r-- 1 wcbrown faculty 1748 Aug 9 17:01 Ex3hack.html -rw-r--r-- 1 wcbrown faculty 711 Aug 9 16:08 Ex4.cpp -rw-r--r-- 1 wcbrown faculty 620 Aug 9 17:00 Exp3hack.cpp -rw-r--r-- 1 wcbrown faculty 620 Aug 9 17:00 test.cpp valiant>

Write a program that reads this (You can copy it, then paste it into your program window. This must be done by going to your DOS-prompt window, clicking in the upper left corner, and selecting "edit".) data, and prints out the largest file size. File size (in bytes) appears in the column after "faculty". The`valiant>`

at the end of the input is actually the command prompt waiting for the next command. It'll tell you that there are no more file entries to read.If you're really ambitious, try to also print out the name of the largest file!

- when to read in

Asst Prof Christopher Brown Last modified: Mon Sep 9 17:25:03 EDT 2002