Total Grade (30 pts)
COSC 1436 Weeks 5-6 Lab Assignment -3
Name (First Last):
Data Conversions and Control structures I
1. (3 points, each for 0.5 point ) Convert each of following numbers to Octal numbers, showing all steps.
a. (111110110)2
b. (1100010)2
c. (2001)10
d. (1492)10
e. (A9)16
f. (5FF71 )16
2. (3 points, each for 0.5 point ) Convert each of following numbers to Binary numbers, showing all steps.
a. (572) 8
b. (1604) 8
c. (1066)10
d. (99)10
e. (80E)16
f. (135AB)16
3. (3 points, each for 0.5 point ) Convert each of following numbers to Hexadecimal numbers, showing all steps.
a. (1111 0111)2
b. (1010 1010 1010)2
c. (777) 8
d. (443) 8
e. (998)10
f. (1066)10
4. (3 points, each for 0.5 point ) Evaluate the following expressions. For each expression, keep the same number system as it is. Do not convert the number systems. E.g. if the expression is written using hexadecimal number system, your answer should also be written in hexadecimal number system.
a. (1110 1111)2+(1011 1101)2
b. (11000001)2-(100010)2
c. (202) 8+( 667) 8
d. (1066)16+ (ABCD)16
e. (7766)8-(5544) 8
f. (9988)16–(AB)16
5. (1.5 points ) Write a program to ask the user to type the height of a person in inches, and convert it to feet and inches. You may assume the user only types numbers. Your program should run like this:
Enter your height in inches: 71
Your height is 5 ' 11 ".
(0.5 point) Write the program
#python program
(0.5 point) Screenshot of the output
(0.5 point) Save the program as “height.py”
6. (1.5 points) Write a program to ask the user to input the radius of a sphere, and then display the volume of the sphere. You must determine the proper variable names. The formula to compute the volume given the radius is provided:
, where is approximately 3.14159
You may assume the user only types numbers. Your program should run like this:
Enter the radius of the sphere: 10
The volume of the sphere is 4188.786666666666
(0.5 point) Write the program
#python program
(0.5 point) Screenshot of the output
(0.5 point) Save the program as “sphere.py”
7. (2point, each for 0.2 point) The following truth table shows various combinations of the values true and false connected by a logical operator (and, or, not). Complete the table by circling T or F to indicate whether the result of such a combination is true or false.
Logical Expression
Result (circle T or F)
True and False
T
F
True and True
T
F
False and True
T
F
False and False
T
F
True or False
T
F
True or True
T
F
False or True
T
F
False or False
T
F
not True
T
F
not False
T
F
8. (2 points, each for 0.2 point) Assume the variables a = 2, b = 4, c = 6, x=0, y=2, and z=4. Circle T or F for each of the following conditions to indicate whether its value is true or false.
Expression (Condition)
Result (circle T or F)
a == 4 or b > 2
T
F
6 <=c and a > 3
T
F
1 != b and c != 3
T
F
a >= -1 or a<=b
T
F
not ( ...
Student Profile Sample - We help schools to connect the data they have, with ...
Total Grade (30 pts)COSC 1436 Weeks 5-6 Lab Assignment -3Na
1. Total Grade (30 pts)
COSC 1436 Weeks 5-6 Lab Assignment -3
Name (First Last):
Data Conversions and Control structures I
1. (3 points, each for 0.5 point ) Convert each of following
numbers to Octal numbers, showing all steps.
a. (111110110)2
b. (1100010)2
c. (2001)10
d. (1492)10
e. (A9)16
f. (5FF71 )16
2. (3 points, each for 0.5 point ) Convert each of following
numbers to Binary numbers, showing all steps.
a. (572) 8
b. (1604) 8
c. (1066)10
d. (99)10
e. (80E)16
f. (135AB)16
2. 3. (3 points, each for 0.5 point ) Convert each of following
numbers to Hexadecimal numbers, showing all steps.
a. (1111 0111)2
b. (1010 1010 1010)2
c. (777) 8
d. (443) 8
e. (998)10
f. (1066)10
4. (3 points, each for 0.5 point ) Evaluate the following
expressions. For each expression, keep the same number system
as it is. Do not convert the number systems. E.g. if the
3. expression is written using hexadecimal number system, your
answer should also be written in hexadecimal number system.
a. (1110 1111)2+(1011 1101)2
b. (11000001)2-(100010)2
c. (202) 8+( 667) 8
d. (1066)16+ (ABCD)16
e. (7766)8-(5544) 8
f. (9988)16–(AB)16
5. (1.5 points ) Write a program to ask the user to type the
height of a person in inches, and convert it to feet and inches.
You may assume the user only types numbers. Your program
should run like this:
Enter your height in inches: 71
Your height is 5 ' 11 ".
(0.5 point) Write the program
#python program
4. (0.5 point) Screenshot of the output
(0.5 point) Save the program as “height.py”
6. (1.5 points) Write a program to ask the user to input the
radius of a sphere, and then display the volume of the sphere.
You must determine the proper variable names. The formula to
compute the volume given the radius is provided:
, where is approximately 3.14159
You may assume the user only types numbers. Your program
should run like this:
Enter the radius of the sphere: 10
The volume of the sphere is 4188.786666666666
(0.5 point) Write the program
#python program
(0.5 point) Screenshot of the output
(0.5 point) Save the program as “sphere.py”
7. (2point, each for 0.2 point) The following truth table shows
various combinations of the values true and false connected by
a logical operator (and, or, not). Complete the table by circling
T or F to indicate whether the result of such a combination is
true or false.
5. Logical Expression
Result (circle T or F)
True and False
T
F
True and True
T
F
False and True
T
F
False and False
T
F
True or False
T
F
True or True
T
F
False or True
T
F
False or False
T
F
not True
T
F
not False
T
F
8. (2 points, each for 0.2 point) Assume the variables a = 2, b =
4, c = 6, x=0, y=2, and z=4. Circle T or F for each of the
following conditions to indicate whether its value is true or
6. false.
Expression (Condition)
Result (circle T or F)
a == 4 or b > 2
T
F
6 <=c and a > 3
T
F
1 != b and c != 3
T
F
a >= -1 or a<=b
T
F
not (a>2)
T
F
x or y
x and y
x or z
(x and y) or (y and z)
not y
9. (1 points, each for 0.2 point) Write logical expressions for
the following conditions:
a. x is not a negative number
b. x is a multiple of 7
c. x is greater than the sum of y and z
d. x is the greatest number among x, y, and z
7. e. x is less than 99 but greater than 49
10. (0.5 point, each 0.25 point) What would the following code
display?
a)
if 'z' < 'a':
print('z is less than a.')
else:
print('z is not less than a.')
(0.25 point) Screenshot of the output
b)
s1 = 'New York'
s2 = 'Boston'
if s1 > s2:
print(s2)
print(s1)
else:
print(s1)
print(s2)
8. (0.25 point) Screenshot of the output
11. (6 points, each 1 point) Draw flowcharts (only for
conditions with branches as shown on the left) and write code
fragments based on the flowcharts (not complete programs) to
complete the following task. Note: screenshots are not required
in this question.
a) if the variable x is greater than 100, assigns 20 to the
variable y and assigns 40 to the variable z
Flowchart
Code Fragments (python source code, not Screenshot)
b) if the variable a is less than 10, assigns 0 to the variable b ,
and assigns 1 to the variable c
Flowchart
Code Fragments (python source code, not Screenshot)
9. c) if the variable a is less than 10, assigns 0 to the variable b.
Otherwise, it should assign 99 to the variable b .
Flowchart
Code Fragments (python source code, not Screenshot)
d) if the variable speed is within the range of 24 to 56, displays
'Speed is normal'. Otherwise, display 'Speed is abnormal'
Flowchart
Code Fragments (python source code, not Screenshot)
e) Determine whether the variable points is outside the range of
9 to 51. If the variable’s value is outside this range, it should
display “Invalid points.” Otherwise, it should display “Valid
points.”
Flowchart
10. Code Fragments (python source code, not Screenshot)
f) If amount1 is greater than 10 and amount2 is less than 100,
display the greater of amount1 and amount2 .
Flowchart
Code Fragments (python source code, not Screenshot)
12. (2 points) Write a program that prompts the user for two
integers and then prints
a) The sum
b) The difference
c) The product
d) The average
e) The distance (absolute value of the difference)
f) The maximum (the larger of the two)
g) The minimum (the smaller of the two)
Hint: Python defines max and min functions that accept a
sequence of values, each separated with a comma.
11. (1 point) Write the program
#python program
(0.5 point) Screenshot of the output
(0.5 points) Save the program as “Calculation.py”
13. (1.5 points) Write a program that initializes a string variable
and prints the first three characters, followed by three periods,
and then the last three characters. For example, if the string is
initialized to ”Mississippi”, then print Mis...ppi.
(0.5 point) Write the program
#python program
(0.5 point) Screenshot of the output
(0.5 points) Save the program as “shorterString.py”
12. 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
13. 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
14. 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.
15. 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
16. 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)
17. 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
18. 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).
19. 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.
20. · 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”
COSC 1436 Final Exam Review Exercises
21. Q1: Which of the following is a relational operator?
A. = B. == C. and D. or
Q2: Which print statement displays the value of s without
starting a new line?
A. print(end="s")
B. print(s)
C. print("s")
D. print(s, end="")
Q3: What is the output of the following code snippet?
i = 1
while i < 10 :
print(i, end = " ")
i = i + 2
if i == 5 :
i = 9
A. 1 3 5 B. 1 3 9 C. 1 3 5 7
9 D. 1 3 5 9
Q4: What is the minimum number of bits needed to represent 16
things?
A. 2 B. 3 C. 4 D. 8
Q5: What causes numeric overflow?
A. using fixed-sized numbers in a floating-point calculation
B. a calculation producing an invalid result
C. a calculation producing a value that won't fit into the
allotted space
22. D. using a radix point instead of a decimal point
E. using a radix point in a fixed-sized number calculation
Q6: A ___________________________ is a sequence of
instructions with a name.
A. variable B. argument C. parameter
D. function
Q7: True or False:
a) Audio data is often represented on a computer by sampling
the continuous signal that represents a sound wave.
____________
b) An AND gate and an OR gate produce opposite output.
____________
c) Vector graphics represent images in terms of lines and
geometric shapes. ____________
d) An absolute path begins at the current working directory.
____________
e) The IP address 121.122.500.124 is valid. ____________
f) RAM is non-volatile and ROM is volatile. ____________
Q8: The gate that produces the opposite results of an AND gate
is called a(n) _______________ gate.
Q9: A(n) _______________ circuit has multiple gates
embedded into it.
23. Q10: IPv4 IP addresses are ___ (2, 8 ,32, or128) bits.
Q11: Convert (345.28125)10 to binary number. Show all
steps.
Q12: What is the function of the ALU?
Q13: What is difference between Domain name and IP address?
Q14: Give the Boolean expression and draw the truth table for
the following circuit
Q15: 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.)