Homework 2

advertisement
Computer Science 101
Homework Set 2
Due Friday, October 1
1. For this problem, you should step through the execution of the function each time. It is
not expected that you know what the function is suppose to calculate. For this type of
problem, you should make a list of the variables and keep track of their values in a step
by step fashion as the algorithm executes. You should only use a computer to check
your answers, but this is not required.
a. What will be printed if the user inputs the number 12 followed by 32?
first
second rem
12
32
12
32
12
8
12
8
4
8
4
0
OUTPUT: The number you seek is: 4
b.
What will be printed if the user inputs the two numbers 235 and 620?
first
second rem
235
620
235
620
235
150
235
150
85
150
85
65
85
65
20
65
20
15
20
15
5
15
5
0
OUTPUT: The number you seek is 5
2. Suppose our football team has a ten game season. Write Python code that would allow
the user to enter the scores for the ten games. The program should then print out our
team’s record for the season, something like
Wins: 5
Losses: 3
Ties: 2
If our team has a perfect record, you should also print out a congratulatory message.
Note: You should use a loop for this program. You should not use new variables for
each of the ten pairs of scores. Each time through the loop, you should get a pair of
scores and process them. The same pair a variables can be used over and over.
wins = 0
losses = 0
ties = 0
games = 0
while games < 10 :
home = input (“Enter score for home team: “)
visitor = input (“Enter score for visiting team: “)
if home > visitor :
wins = wins + 1
if home < visitor :
losses = losses + 1
if home == visitor :
ties = ties + 1
games = games + 1
print “Wins: “, wins, “ Losses: “, losses, “ Ties: “, ties
if wins == 10 :
print “Congratulations on a perfect season!”
3. Below is the function we went over in class to compute the average of a set of scores.
Show how you would modify the function so that it also prints out the highest score
entered. The basic strategy is to have a variable, say largest, that keeps up with the
highest score entered so far. Each time through the loop, you check to see if largest
needs to be changed. In the end it should have the largest score.
4. For the decimal number 406 give
a. The binary representation: 406 = 256 + 128 + 16 + 4 + 2
Binary: 110010110
b. The octal representation:
110 010 110
6 2 6
so Octal: 626
c. The hexadecimal representation:
1 1001 0110
1 9
6
so Hex: 196
5. Convert the following numbers to decimal:
a. The binary number 101101101
256 + 64 + 32 + 8 + 4 + 1 = 365
b. The octal number 523
5 * 82 + 2 * 8 + 3 *1 = 339
c. The hexadecimal number BAD
11 * 162 + 10 * 16 + 13 = 2989
6. Suppose that three memory cells of a computer hold the following 24 bits (3 bytes)
01010100 01101111 01101101
a. If this number (all 24 bits) represents the amount of dollars in your friend’s bank
account, circle the correct statement:
The leftmost 1 is in position 22; so the number is more that 222 but less than 223 .
My friend’s account has at least $1,000,000, but not as much as $10,000,000
b. If these bytes represent the ASCII codes (in binary) for your friend’s first name,
what is his name?
01010100 is 84 decimal – this is ASCII code for T
01101111 is 111 decimal – this is ASCII code for 0
01101101 is 109 decimal – this is ASCII code for m
c. If these bytes represent the color (in RGB) of your friend’s shirt, indicate which of
the following is the color?
Mixing 84 red, 111 green and 109 blue gives
Download