## CS107 - Lab 1

### Introduction to algorithmmic problem solving (part 1: basic instructions and if-then-else)

1. Below are two shampooing algorithms. Which do you think is a better general-purpose solution? Why? (Hint: What if you wanted to wash your hair 1000 times?)
```Algorithm 1:
1. wet hair
2. set value of washCount to 0
3. repeat steps  4 through 6 until the value of washcount equals 2
4. lather hair
5. rinse hair
6. add 1 to the value of washCount
7. stop
```
```Algorithm 2:
1. wet hair
2. lather hair
3. rinse hair
4. lather hair
5. rinse hair
5. stop
```

2. Here is Euclid's 2300-year-old algorithm for finding the greatest common divisor of two positive integers I and J.
```Euclid's algorithm:

1. Get two positive integers as input. Call the larger value I and the
smaller value J.
2. Divide I by J, and call the remainder R.
3. If R is not 0, then reset I to the value of J, reset J to the value
of R, and go back to step 2.
4. Print out the answer, which is the value of J.
5. Stop.
```
a) Go through this algorithm using the input values 20 and 32. After each step of the algorithm is completed, give the values of I, J and R. Determine the final output of the algorithm.

b) Does the algorithm work correctly when the two inputs are 0 and 32? Describe exactly what happens, and modify the algorithm so that it gives an appropriate error message.

3. Write pseudocode instructions to carry out each of the following operations:

a) Read from the user the values for the base and height of a triangle, and print out its area.

b) Read from the user a starting balance and the annual interest rate. Compute (and print) the interest earned in one year assuming simple interest, that is, no compounding. Also determine the final balance at the end of the year.

c) Read from the user the mileage M between them and the average speed of the airplane, and print out the flying time beween two cities.

4. Write an algorithm that read from the user 4 numbers corresponding to the scores received on three semester tests and a final examination. Your algorithm should compute and display the average of all four tests, weighting the final exam wtice as heaavily as a regular test.

5. Write an algorithm that inputs the length and width of a carpet in feet and the price in dollar per square yard. The algorithm should print out the total cost of the carpet, including a 6% sales tax.

6. Write an if-then-else instruction to do each of the following:

a) Compute and display the value x/y if the value of y is not 0. Otherwise, display the massage "Unable to perform division!".

b) Compute the area and circumference of a circle given the radius r if the radius is greater than or equal to 1.0. Otherwise you should compute only the circumference.

7. Write an algorithm that asks the user to enter three numbers, and computes and prints out the largest number. For example:
```Enter three numbers: 5 9 2
The largest is 9
```

8. Write an algorithm that asks the user to enter a number. If the value entered is negative, print "disqualified" and terminate. If the value entered is positive, ask the user for another number. Then print a number that is larger than the one entered by user, "I win" and terminate. For example:
```Enter a number: 3
Great. Now enter another number: 24
25. I win. Bye!
```
```Enter a number: -10
You are disqualified. Bye!
```

9. Write and algorithm that asks the user to enter three numbers, and prints them in increasing order. For example:
```Enter three numbers: 4 8 3
The numbers in order are: 3 4 8
```

Note: You may write by hand or use a word processor. If you write by hand, do your best to write legibly. Leave space between problems. Be sure to briefly justify your answers.