You are on page 1of 8

Computer Engineering Department.

Faculty of Engineering Cairo University

Introduction to Computers and Engineering (GENN004) Final Exam CHS Fall 2011 (2 hours) Total Marks: 40
Question 1 (10) Question 2 (10) Question 3 (10) Question 4 (10) Total (40)

Question 1: [10 pt] Choose the correct answer. Clearly circle only one answer. 1) If a1 is true and a2 is false, then which of the following expressions is false? o ~a1 || a2 o ~(a1 && a2) o a1 || a2 o a1 && ~a2 2) Which of the following statement is false? o Developers are allowed to define their own functions in MATLAB. o The statement x1 == x2 makes the value of variable x2 equal to the value of variable x1 o MATLAB considers capital X and small x as two different variables. o Developers are allowed to use nested if statements in MATLAB 3) Which of the following statements will print the following output without the outer box to the screen. *********** Total= 35 LE o o o o total = 34.792 ; fprintf(***********\nTotal=%7f LE\n,ceil(total)) ; total = 34.792 ; fprintf(***********\nTotal=%7.0f LE\n,total) ; total = 34.792 ; fprintf(***********\nTotal=%-7.0f LE\n,total) ; total = 34.792 ; fprintf(***********\nTotal=%.0f LE\n,total) ;

4) Consider the following matrix: X = [ 1, 7, 4 ; 8, 5, 6 ; 3, 2, 9 ] ; What will be the value of X(2,3) after executing the following statement: X(2,3) = X(2,3) * X(1,2) + X(2,3) / X(1,3) * X(3,2) ; o 6 o 45 o 20 o None of the above GENN004 Final Exam /18 Fall 2011

5) Which of the following is not a valid variable name? o floorY o forloop o Pseudo_Code o x#0 6) What would appear on the screen after the execution of the following statements: p = 1; for count = 1:5 p = p * count; end fprintf("p = %d\n", p) ; o o o o p = 720 p=5 p = 120 None of the above

7) The expression (~ ( 3 rem(4,3) < 5 && 6 / 4 < 3 ) ) is o Invalid o True o False o None of the above 8) The following is a correct for statement. o for 0:1:10 o for i=10:-1 o for i=0:-1:10 o for i=10:0 9) The math equation o o o o Z=(3*x+5*y)/2*x+2*x*y Z=(3*x+5*y)/(2*x)+2*x*y Z=3*x+5*y/2*x+2*x*y None of the above can be written in matlab as follows:

10) Consider the following array: int X = [ 11, 12, 13 ; 21, 22, 23; 31, 32, 33; 41, 42, 43 ] ; What will be the value of X(1:2:4, end) o o o o [11, 12, 13; 21, 22, 23; 41, 42, 43] [13 ; 23; 43] [13 ; 33] None of the above

GENN004 Final Exam

/28

Fall 2011

Question 2: [10 pt] 2.a) [3 pt] What is the output of the shown program: X=[1 2 3 4 5 6]; L=length(X); for i = 1 : L/2 tmp=X(i); X(i)=X(L-i+1); X(L-i+1)=tmp; end disp(X) Solution:

2.b) [3 pt] The following program reads 10 numbers from the user, inserts them in an array, and computes and displays the average of these 10 numbers. The program has errors. Correct the program so that it compiles and functions correctly. Hint: You need to make at least 6 changes. N=10 for i = 0 : N arr[i]=(Enter value :, s) ; sum = sum + arr[i] ; endfor average = sum \ N ; fprintf(average = %d\n, avg) ; end

GENN004 Final Exam

/38

Fall 2011

2.c) [4 pts] Write a function that takes as an input an array A and returns the number of positive, negative and zero elements in three variables p, n, and z respectively.

GENN004 Final Exam

/48

Fall 2011

Question 3: [10 pt] Study carefully the following program: x=input('Enter first number:'); op=input('Enter an operation + or -:','s'); y=input('Enter second number:'); z=input('Enter your answer:'); if op=='+' answer=x+y; else answer=x-y; end if answer==z fprintf('correct answer\n'); else fprintf('try again\n'); end 1) [2 pts] What is the purpose of this program?

2) [2 pts] Modify the previous program to print to the user the correct answer if his answer is wrong. [Rewrite the modified statements only]

GENN004 Final Exam

/58

Fall 2011

3) [2 pts] Extend the previous program to support the multiplication and the division operation. [Rewrite the modified statements only]

4) [4 pts] Modify the previous program to give the user 8 chances to try again. If the user is not able to get the correct answer after the 8 trials, then the program prints the correct answer. [Rewrite the entire program]

GENN004 Final Exam

/68

Fall 2011

Question 4: [10 pt] Write a program to solve the following problem: The program reads from the user two arrays, the first array contains unknown number of elements while the second array (pattern) contains exactly two elements. The program searches the first array to count and record all occurrences of the second array. Then it prints how many times the second array (pattern) is found and where. The program should also ask the user to continue or not and according to the user decision the program re-ask the user to enter the arrays or to stop and say Good Bye!. The input and output of the program should look like the shown example. Example: Enter array: [1 2 3 4 5 6 7 8 1 2 3 1 2 3 ] Enter pattern: [1 2] The pattern is found 3 time(s) at the following positions 1 9 12 Do you want to continue (y/n):y Enter array: [1 2 3 4 5 6 7 8 1 2 3 1 2 3 ] Enter pattern: [1 6] The pattern is not found Do you want to continue (y/n):y Enter array: [1 2 3 4 5 6 7 8 1 2 3 1 2 3 ] Enter pattern: [8 1] The pattern is found 1 time(s) at the following positions 8 Do you want to continue (y/n):n Good Bye!

GENN004 Final Exam

/78

Fall 2011

GENN004 Final Exam

/88

Fall 2011

You might also like