assignment python
Total Grade (45 pts) |
COSC 1436 Lab 4 Assignment
Name (First Last):
Data Representation II
1.
(3 points, each for 0.5 point) Convert the following positive binary numbers to their decimal number equivalents, showing all steps.
a) 111111
b) 1110.11
c) 101111.101
d) 10111110.00001
e) 110011.011
2.
(4 points, each for 0.5 point) Convert the following positive decimal numbers to their binary equivalents showing all steps. If needed, you can attach more papers.
a) 4.25
b) 0.9375
c) 254.75
d) 0.5625
e) 127.875
f) 104
g) 0.7578125
h)
3.125
3. (2 points) How many things can be represented with:
a. four bits
b. five bits
c. six bits
d. seven bits
4. (2 points) Using the following number line representation, evaluate the expressions by showing all steps:
999
500 501 502 … 997 998 0 1 2 3 … 497 498 499
-500 -499 -498 … -3 -2 -1 0 1 2 3 … 497 498 499
a. 498 + (-7)
b. -5 + (-2)
c. 5 – 8
d. -500+6
5. (3 points) Given a fixed-sized number scheme where k in the formula for the ten’s complement is
6
, answer the following questions.
a. How many positive integers can be represented?
b. How many negative integers can be represented?
c. Draw the number line showing the three smallest and largest positive numbers, the three smallest and largest negative numbers, and zero.
6. (3 points) Using the number line in Qn5, find out the ten’s complement representation of the following numbers. Show all steps.
a. 35768
b. –35768
c. –444455
d. –123456
e. -23456
7. (5 points) Roulette Wheel Colors: On a roulette wheel, the pockets are numbered from 0 to 36. The colors of the pockets are as follows:
· Pocket 0 is green.
· For pockets 1 through 10, the odd-numbered pockets are red and the even-numbered pockets are black.
· For pockets 11 through 18, the odd-numbered pockets are black and the even-numbered pockets are red.
· For pockets 19 through 28, the odd-numbered pockets are red and the even-numbered pockets are black.
· For pockets 29 through 36, the odd-numbered pockets are black and the even-numbered pockets are red.
By referring to Figure 1, write a program that asks the user to enter a pocket number and displays whether the pocket is green, red, or black. The program should display an error message if the user enters a number that is outside the range of 0 through 36. (Hint: use if-elif-else and nested if-else statements)
Before programming, we can have some fun first. An online game is available at
https://www.vegasslotsonline.com/table-games/american-roulette/
Figure 1: Roulette Wheel Colors
· Source code of the project
#python program
· Screenshot of the output
· Save the program as “Roulette.py”
8. (1 point) What does the following loop print?
n = 1
while n < 100 :
n = 2 * n
print(n)
9. (1 point) Hand-trace the following code, showing the value of n and the output.
n = 5
while n >= 0 :
n = n – 1
print(n)
10. (1 point) Hand-trace the following code, showing the value of n and the output.
n = 1
while n <= 3 :
print(n)
n = n + 1
11. (1 point) Hand-trace the following code, assuming that a is 2 and n is 4. Then explain what the code does for arbitrary values of a and n.
r = 1
i = 1
while i <= n :
r = r * a
i = i + 1
12. (1 point) Hand-trace the following code. What error do you observe?
n = 1
while n != 50 :
print(n)
n = n + 10
13. (2 point) What is wrong with the following loop for finding the smallest input value?
smallest = 0
inputStr = input(“Enter a value: “)
while inputStr != “” :
value = int(inputStr)
if value < smallest :
smallest = value
inputStr = input(“Enter a value: “)
14. (1 point) How many numbers does this loop print?
for n in range(10, -1, -1) :
print(n)
15. (2 points) Write a for loop that prints all even numbers between 10 and 20 (inclusive).
16. (5 points) Write while loops that prints
a) All squares less than n. For example, if n is 100, print 0 1 4 9 16 25 36 49 64 81.
b) All positive numbers that are divisible by 10 and less than n. For example, if n is 100, print 10 20 30 40 50 60 70 80 90
c) All powers of two less than n. For example, if n is 100, print 1 2 4 8 16 32 64.
· Source code of the project
#python program
· Screenshot of the output
· Save the program as “Qn16.py”
17. (3 points) Number guessing game – First, we use randint to generate a random number N between 1 and 100:
from random import randint
N = randint(1, 100)
Complete the program. The program shall ask the user to guess what N is. Every time the user makes a guess, the program shall tell the user whether the guessed number is greater than, equal to, or less than N. If the guessed number equals to N, the program should terminate. You may assume that the user only types integers.
· Source code of the project
#python program
· Screenshot of the output
· Save the program as “Qn17.py”
18. (5 points) Write loops that computes
a) The sum of all even numbers between 2 and 100 (inclusive).
b) The sum of all squares between 1 and 100 (inclusive).
c) The sum of all odd numbers between a and b (inclusive).
d) The sum of all odd digits of n. (For example, if n is 32677, the sum would be 3 + 7 + 7 = 17.)
· Source code of the project
#python program
· Screenshot of the output
· Save the program as “Qn18.py”