Sie sind auf Seite 1von 3

CHS Spring 2012 Introduction to Computers and (Engineering (GENN004 Cairo University

Midterm Exam

Time Allowed: 60 minutes Date: 11 April 2012 Page 1 of 3 Maximum Grade: 20 Points

Name:

I.D.:

Group:

Question 1: [10 pts] 1) What is the output of the following code segment? x=123.21; y=1.1252; fprintf(%.3f,%-10.2f,y,x); 1 . 1 2 5 , 1 2 3 . 2 1 2) After executing the following scripts, the memory will contain the following value for Z ( y=2; k=5; Z = k+4*k/4*y+3; 3) Write the shown math equation using Matlab code. xy 3 x8 Z=(x*y-3*x^8)/(2*x)+5*y Z= + 5y 2x 4) The following is a valid variable name (circle one): 2for x-y x2x 18 )

5) True or False a. The statement x1 == x2 makes the value of variable x2 equal to the value of variable x1 ( F ) b. MATLAB considers capital X and small x as the same variable. ( F ) 6) Determine if the following condition true or false. Show your work. floor(10.8)>=abs(ceil(-10.8)) || rem(3,5) == fix(3.99) 10>=10 True || 3==3 True => the condition is True 7) How many X is printed when the following loop is executed? ( 6 for i=0:4:20 fprintf(X); end )

8) What would appear on the screen after the execution of the following script: i = 1 ; 1 2 3 for x = 1:3 i = i + x ; fprintf(%d, x) ; end 9) If x is true and y is false, then which of the following expressions is true and which is false? a. ~x || ~y True b. (~x && y) False 10) What should be the value of x and y so the following loop prints exactly 5 stars?(x= 1 , y= 13 for i=x:3x:y fprintf(*); end
Page 1

CHS Spring 2012 Introduction to Computers and (Engineering (GENN004 Cairo University

Midterm Exam

Time Allowed: 60 minutes Date: 11 April 2012 Page 2 of 3 Maximum Grade: 20 Points

Question 2: [3 pt] Find and correct all the syntax errors in the following script: Corrected Script for i=5:2:1 x=( Enter num 1 ); y=( Enter num 2 ); x+y==z; printf(z=%f); endfor For i=5:-2:1 X=input(Enter num 1); Y=input(Enter num 2); Z=X+Y; Fprintf(z=%f,Z); end

Question 3: [3 pt] What is the output of the following script? for i=1:3 for j=3:-1:1 fprintf('%5d',i+j); end fprintf('\n'); end 4 5 6 3 4 5 2 3 4

Question 4: [4 pt] Write a matlab program to count the number of cars driving at a speed greater than 100 km/hr. The program reads first a positive number N that represents the total number of cars the user would like to test. The program prints an error message if the entered number is not positive. Otherwise the program reads the travel distance and the time of travel of each car one at a time, calculate its speed and count those that has a speed greater than 100 km/hr. Then the program prints to the screen, the number of speedy cars. The input and output of the program should be as shown below. Sample Inputs/Outputs: Enter N: 0 Error! Re-enter N: -1 Error! Re-enter N: 3 Enter distance (km): 120 Enter time (hr): 1 Enter distance (km): 10 Enter time (hr): 1 Enter distance (km): 120 Enter time (hr): 0.8 Num of speedy cars = 2
Page 2

CHS Spring 2012 Introduction to Computers and (Engineering (GENN004 Cairo University N=input(Enter N); While N<=0 N=input(Error!! Re-enter N:); End Count=0; For i=1:N D=input(Enter Distance (Km)); T=input(Enter Time (hr)); S=D/T; If S>100 Count=Count+1; end end fprintf(Num of Speedy Cars= %d),Count;

Midterm Exam

Time Allowed: 60 minutes Date: 11 April 2012 Page 3 of 3 Maximum Grade: 20 Points

Page 3

Das könnte Ihnen auch gefallen