Final Exam 2019
Total Page:16
File Type:pdf, Size:1020Kb
Page 0 German University in Cairo January 18, 2020 Media Engineering and Technology Faculty Prof. Dr. Slim Abdennadher Dr. Nada Sharaf Dr. Mohamed Abdel Megeed Salem CSEN102: Introduction to Computer Science Winter Semester 2019-2020 Final Exam Bar Code Instructions: Read carefully before proceeding. 1) Please tick your major Major Engineering BI 2) Duration of the exam: 3 hours (180 minutes). 3) No books or other aids are permitted for this test. 4) This exam booklet contains 17 pages, including this one. Three extra sheets of scratch paper are attached and have to be kept attached. Note that if one or more pages are missing, you will lose their points. Thus, you must check that your exam booklet is complete. 5) Write your solutions in the space provided. If you need more space, write on the back of the sheet containing the problem or on the four extra sheets and make an arrow indicating that. Scratch sheets will not be graded unless an arrow on the problem page indicates that the solution extends to the scratch sheets. 6) When you are told that time is up, stop working on the test. Good Luck! Don’t write anything below ;-) Exercise 1 2 3 4 5 6 7 (Bonus) P Possible Marks 16 14 6 30 15 20 6 91 Final Marks CSEN102: Introduction to Computer Science, Final Exam, January 18, 2020 Page 1 Exercise 1 General arithmetics (3+5+4+4=16 Marks) Number conversions In this exercise you have to convert numbers from one base to another. A number n of a base b other than 10 will be given on this page as nb For decimal numbers, the subscript base can be omitted (i. e., we write n instead of “n10”). For bases b that are greater than 10, please use uppercase Latin letters (A, B, . ) as digits beyond 9, just as it is done with hexadecimal numbers. a) Convert the following numbers into binary. Show your workout. 1. 7458 2. F ACE16 3. 467 b) Convert the following and show your workout. • The binary integer number 0101100111001101011101001110012 into hexadecimal. • Let m = 2113 and n = 2114. Find the base 5 representation of m + n. CSEN102: Introduction to Computer Science, Final Exam, January 18, 2020 Page 2 c) Give the least number of bits needed to perform the following operation in two’s complement and perform the operation afterward. −21 − 15 d) Let A = 11111010 and B = 00001010 be two 8-bit 2’s complement numbers. Compute the subtraction of A − B in 2’s complement notation and find out the corresponding decimal value. CSEN102: Introduction to Computer Science, Final Exam, January 18, 2020 Page 3 Exercise 2 Boolean functions (14 Marks) Circuit improvement Given the following boolean expression: AB0 + A(B + C)0 + B(B + C)0 a) Draw a truth table for the Boolean expression. b) Simplify the Boolean expression to reach a circuit that can be designed with 2 gates. Use the axioms of the Boolean Algebra. Please mention the applied rules. x + 0 = x x ∗ 1 = x x + 1 = 1 x ∗ 0 = 0 x + x = x x ∗ x = x x + x0 = 1 x ∗ x0 = 0 (x0)0 = x x + y = y + x xy = yx Commutativity x + (y + z) = (x + y) + z x(yz) = (xy)z Associativity x(y + z) = xy + xz x + yz = (x + y)(x + z) Distributivity (x + y)0 = x0y0 (xy)0 = x0 + y0 DeMorgan’s Law c) What is the functionality of the circuit? CSEN102: Introduction to Computer Science, Final Exam, January 18, 2020 Page 4 Exercise 3 Boolean Circuits (6 Marks) Functionality Given the following truth table A B C X5 X4 X3 X2 X1 X0 0 0 0 1 1 1 1 1 0 0 0 1 0 0 0 0 1 0 0 1 0 0 0 0 1 0 1 0 1 1 0 0 1 0 0 0 1 0 0 0 0 1 0 1 1 1 0 1 0 0 1 1 1 0 1 1 0 0 1 0 0 0 1 1 1 1 0 1 0 1 0 0 What is the functionality of the circuit where A; B; C are the 3 input variables and X5;X4;X3;X2;X1 and X0 are the 5 output variables? Justify your answer. CSEN102: Introduction to Computer Science, Final Exam, January 18, 2020 Page 5 Exercise 4 (30 Marks) The 9’s complement of a decimal number is found by subtracting each digit in the number from 9. Decimal Digit 0 1 2 3 4 5 6 7 8 9 9’Complement 9 8 7 6 5 4 3 2 1 0 a) If you want to design a circuit for 9’s complement converter of digits, how many input variables and how many output variables do you need? Justify your answer. b) Draw a truth table that corresponds to the above table. Note: You should neglect all rows that do not correspond to digits. c) Find the Boolean expressions of the output variables using the sum of product method. CSEN102: Introduction to Computer Science, Final Exam, January 18, 2020 Page 6 d) Simplify all Boolean expressions e) Draw the circuit for the 9’s complement converter using and, or and negation gates. CSEN102: Introduction to Computer Science, Final Exam, January 18, 2020 Page 7 f) In the following, assume that we want to perform the subtraction of two decimal digits A and B: A − B We will be using 9’s complement to perform subtraction through addition. The algorithm is as follows: 1. Get the binary representation of A using 4 bits (let us call it ABin) 2. Get the 9’s complement of B (let us call it Bcomp). 3. Convert the 9’s complement of B to binary using 4 bits (let us call it BcompBin). 4. Add ABin and BcompBin 5. If the output is greater than 9: i. Correct the output by adding 6 (0110) to it 6. Now that you have an output, you check if a carry was produced to generate the output 7. If no carry is generated then: i. The subtraction is negative ii. To compute the actual value, take the 9’s complement of the result 8. If a carry is generated, then i. The final result is positive. ii. To compute the actual value, add the carry (fifth bit/most significant bit) to the rest of the number Examples: 1. Computing 8 - 3 • 8 in binary is 1000 • The 9’s complement of 3 is 6: 0110 • 1 0 0 0 0 1 1 0 ------- 1 1 1 0 • Output is greater than 9, thus we add 6 1 1 1 0 0 1 1 0 ------- (1) 0 1 0 0 • There is a carry, thus the number is positive. • We add the carry to the other bits 0 1 0 0 0 0 0 1 ------- 0 1 0 1 • The final output is 5 2. Computing 3-8 • 3 in binary is 0011 • The 9’s complement is 8 is 1: 0001 • 0 0 1 1 0 0 0 1 ------- 0 1 0 0 CSEN102: Introduction to Computer Science, Final Exam, January 18, 2020 Page 8 • The number is not greater than 9 • There is not carry, thus the output is negative • The actual value is the 9’s complement of the output 0100: 4 which is 5 • Thus, the final output is -5 Your task is to design a 9’s complement subtractor for digits represented in binary 4-bits numbers. Assume that we have already • 9’s complement converters of digits as well as 4-bit adders (presented in lectures) in addition to and, or and not gates. • a Greater-Comparator-Than-9 components. This takes as an input a 4-bit binary number and returns 1 if the input is greater than 9 and 0 otherwise. Hint: Since we have branches in the algorithm presented above, we need to handle the if condition. To handle an if C then A else B statement, you could use the following Boolean expression: (C ∗ A) + (C0 ∗ B) CSEN102: Introduction to Computer Science, Final Exam, January 18, 2020 Page 9 Exercise 5 (4+2+4+4+1=15 Marks) Given the following program arr = eval(input()) x = eval(input()) n = len(arr) r1 = -1 r2 = -1 i = 0 while i<n: if (x != arr[i]) : i+=1 else: r2 = i if (r1 == -1) : r1 = i i += 1 if (r1 != -1) : print( "First Mystery = " , r1, "Second Mystery = " , r2) else : print("Third Mystery") a) What is the output of the program for the following input arr = [1, 2, 2, 2, 2, 3, 4, 7 ,8 ,8 ] x = 8 Trace your program. b) What is the functionality of the program for any sorted list of integers with duplicates. Please determine the meaning of the three mysteries. CSEN102: Introduction to Computer Science, Final Exam, January 18, 2020 Page 10 c) Give the best case and worst case scenarios. Justify your answer in English without calculating the total number of operations. d) Give the total number of operations for the execution of the program for the worst case scenario. arr = eval(input()) -------------------------> 1 operation executed once x = eval(input()) n = len(arr) r1 = -1 r2 = -1 i = 0 while i<n: if (x != arr[i]) : i+=1 else: r2 = i if (r1 == -1) : r1 = i i += 1 if (r1 != -1) : print( "First Mystery = " , r1, " Second Mystery = " , r2) else : print("Third Mystery") e) Give the order of magnitude of the program above. CSEN102: Introduction to Computer Science, Final Exam, January 18, 2020 Page 11 Exercise 6 Iterative Algorithms (20 Marks) As presented in Exercise 4, the 9’s complement of a decimal number is the subtraction of each of its digits from 9.