## CS107 - Lab 2

### Introduction to algorithmic problem solving (part 2: loops)

The goal for this lab is to get you confortable using loops. Start by reviewing the problems we did in class and read carefully the lecture notes. Then go ahead and start on the problems below. Work individually, and call me if you need help. You are encouraged to discuss ideas and techniques broadly with other class members, but not specifics. Discussions should be limited to questions that can be asked and answered without using any written medium (e.g. pencil and paper or email).
1. Write an algrithm that uses a loop to read in 100 pairs of scores, where each pair represents the score of a football game between Bowdoin College and some other team. Determine, for each pair of numbers, whether Bowdoin won or lost, and after reading the 100 pairs of values, print out the won/lost/tie record of Bowdoin. In addition, if the record is a perfect 100-0, print out the message "Congratulations on your undefeated season!!!". For example:
```Enter Bowdoin score: 5
Enter oponent score:  2

[... repeat this 99 more times..]

Bowdoin game statistics:
wins: 53
loses: 40
ties: 7
```
Feel free to use your imagination to make it more "user friendly". For example, you can display the game number every time you ask the user for a score:
```game 1:
Enter Bowdoin score: 5
Enter oponent score: 3
game 2:
Enter Bowdoin score: 2
Enter oponent score: 3
[... repeat this 97 more times..]
game 100:
Enter Bowdoin score: 1
Enter oponent score: 0
Thank you.
Bowdoin game statistics:
wins: 53
loses: 40
ties: 7
```

2. Write an algorithm that asks the user for a positive integer n and computes and prints out the sum of the squares of all numbers smaller than or equal to n, that is, 1 + 22 + 32 + 42 +...+n2 For example,
```Please enter a positive number: 4
The sum of squares of all number up to 4 is 30.
Goodbye.
```

3. Write an algorithm that repeatedly asks the user for a positive number n and prints the sum 1 + 2 + 3 + ... + n. When the user enters 0 or a negative number, it terminates. For example:
```Enter a number: 5
The sum is 15.

Enter a number: 10
The sum is 55.

Enter a number: 2
The sum is 3.

Enter a number: -1
Fine, i quit.
```

4. Recall Gauss's idea for computing the sum 1+2+....+n. Using a similar reasoning, find a formula for the following sum:
`2+4+6+...+{the last even number smaller or equal to n}`
Your formula should work for both odd and even values of n.

5. Suppose you are assigned to write an algorithm to bore the user. I'm sure there are better ways to do it, but here is one way to do it: your algorithm repeatedly asks if the user is bored. If yes, it keeps repeating, the same question, over and over. Until the user is finally bored, at which point it terminates. For example:
```Are you bored yet? [Press 1 for Yes, 0 for No]: 0
Hmm..
Are you bored yet? [Press 1 for Yes, 0 for No]: 0
Hmm..
Are you bored yet? [Press 1 for Yes, 0 for No]: 0
Hmm..
Are you bored yet? [Press 1 for Yes, 0 for No]: 0
Hmm..
Are you bored yet? [Press 1 for Yes, 0 for No]: 1
Finally!
```
Extra points: Can you come up with a more ingenious way to be boring (while using a loop)? Feel free to share!

6. Modify your previous algorithm so that it keep strack of how many times it asks the question. If the user is not bored after 10 attempts, it gives up (terminates).
```Are you bored yet? [Press 1 for Yes, 0 for No]: 0
Hmm..
Are you bored yet? [Press 1 for Yes, 0 for No]: 0
Hmm..

[....7 more times ]

Are you bored yet? [Press 1 for Yes, 0 for No]: 0
I give up.
```