Vous êtes sur la page 1sur 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. Which one is an illegal variable name? a. underdog c. dog b. dog-1 d. under2dog 2. The single most important objective of this course is to learn a. solve engineering problems c. MATLAB b. to be in class on time d. programming concepts 3. Write the following equation in one Matlab code statement:

Z = (3 x8 xy )( 2 x +5 y )

Z=sqrt((3*x^8-x*y)*(2*x+5*y)^2)

4. The MATLAB output from the last MATLAB statement would be abc = 6; for i=1:2:4 abc = abc +6; end abc =18 5. The values that the loop variable "i" takes are for i = 0:0.6:3.2 0,0.6,1.2,1.8,2.4,3 6. Evaluate the following MATLAB expressions a. 2 + ceil(6 / 9 + 3 * 2) / 2 3=2.5 7. What is the value of h in the following cases: a. T = 50 h = 0

b. 3 ^ 2 ^ 2=81

if T < 30 h = 2*T + 1; b. T = 0 h = 1 elseif T < 10 h = T 2; else h = 0; end 8. What would appear on the screen after the execution of the following script: m=123.21; n=1.1252; fprintf('m:%8.2f:%.3f',n,m);
m : 1 . 1 3 : 1 2 3 . 2 1 0

9. Determine if the following conditions are true or false. X= 10; y=0; a. X >= yTrue b. X == yfalse
Page 1

(Show your work.)

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: yourSelection = 11; Corrected Script urSelection = 11; while urSelection ~= s while urSelection ~= s fprintf('\tI: To input a grade\n'); fprintf('\tI: To input a grade\n'); fprintf('\ts: To stop\n'); fprintf('\ts: To stop\n'); urSelection = input('Enter your choice:'); urSelection = input('Enter your choice:',s); if urSelection == (I) if urSelection == I urGrade = ('Enter your grade: '); urGrade = input('Enter your grade: '); maxGrade = input('Enter the maximum grade: maxGrade = ('Enter the maximum grade: '); '); fprintf('Your grade is %.2f ',urGrade); fprintf('Your grade is %.2f ',urGrade); fprintf('out of %.2f = ',maxGrade); fprintf('out of %.2f = ',maxGrade); fprintf('%.2f%%%\n\n',urGrade/maxGrade fprintf('%.2f%%\n\n',urGrade/maxGrade*100 ); %100 ); end end end end Question 3: [3 pt] What is the output of the following script? sum1 = 0; Script Output for i = 1:1:2 Sum1=3 for j = 1:2:2 sum1 = sum1 + i*j; end end fprintf('sum1 = %d', sum1) Question 4: [4 pt] Write a script that reads positive numbers from the user, and then prints the total number of entered odd numbers and the sum of all entered odd numbers in the exact format as shown below in the example. Your script should end when the user enters a negative number. Sample Inputs/Outputs: Input : Please enter a number: 90 Please enter a number: 13 Please enter a number: 11 Please enter a number: -10 Output : Number of entered odd numbers is 2 Sum of all entered odd numbers is 24 End Of The Program!

Sum=0;
Page 2

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

Midterm Exam

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

Count=0; N=input(please enter a number); While N>0 If rem(N,2)==1 Count=Count+1; Sum=Sum+N; End N=input(please enter a number); End Fprintf(Number of entered odd numbers is %d\n Sum of all entered odd numbers is %d \n End of program, Count, Sum);

Page 3

Vous aimerez peut-être aussi