CHS – Spring 2014 Faculty of Engineering
Introduction to Computers and Cairo University
Engineering (GENN004) Page 1 of 5
Lab 7 – Practice Session 1
1. If A is false and B is true, then which of the following expressions is false?
o ~A && B || B Clearly circle only one answer.
o A || A && B
o ~(A && B || A)
o All of the above
2. The expression (fix(1.5) - rem(15,3) ) < 2 is
o Invalid Clearly circle only one answer.
o True
o False
o None of the above
3. What is the R value after the Matlab code below executes, if Q=5 and T=2?
if (Q>T || Q>8) && (T<=4)
R=Q*T;
end
if (T==0 || Q==2 || Q>T) && (T>-5)
R=4;
end
4. What is the C value after the Matlab code below executes, if B=60 and C=30?
if (B/C)>=2
C=3;
elseif (B/C) = =2
C=11;
end
5. Use one if-statement to rewrite the following nested if-statement:
if w < x
if w > y
w = x*y
end
end
6. Which Matlab command is usually used to repeat a set of commands an unknown
number of times?
o while Clearly circle only one answer.
o for
o if
Page 1
CHS – Spring 2014 Faculty of Engineering
Introduction to Computers and Cairo University
Engineering (GENN004) Page 2 of 5
Lab 7 – Practice Session 1
7. The following is a correct for statement.
o for 1:10 Clearly circle only one answer.
o for i=10:-1
o for i=10:-3:0
o for i<10
8. What is the value of JJ after the Matlab code below executes?
JJ=0;
while JJ<4
JJ=JJ+1;
end
9. In order to print formatted integers with the following format,
998,999,1000,1001, for i=998:1001
fprintf(.)
end
Which fprintf statement should be used in the above code (on the right hand side)?
o fprintf('%d\n', i) Clearly circle only one answer.
o fprintf('%5d\n', i)
o fprintf('%d,', i)
o fprintf('%5.5d\n', i)
o None of the above
10. if x=95, y= 120 , s=x*100/y , which is the correct statement that will give the
following output when executed,
Success rate is 79.17 %, as 95 passed out of 120.
o fprintf(‘Success rate is %d %%, as %4.1f passed out of %-10.1f. \n',s, x,y)
o fprintf(‘Success rate is %6.3f %%, as %d passed out of %d.\n', x, s,y)
o fprintf(‘Success rate is %6.2f %%, as %d passed out of %8d.\n',s, x,y)
o None of the above
11. What would appear on the screen after the execution of the following script:
i = 1;
for x = 1:3
i = i * x;
fprintf(‘%d ‘, x) ;
end
Page 2
CHS – Spring 2014 Faculty of Engineering
Introduction to Computers and Cairo University
Engineering (GENN004) Page 3 of 5
Lab 7 – Practice Session 1
12. Write a script that will prompt the user for a temperature in degrees Celsius, and
then an ‘F’ for Fahrenheit or ‘K’ for Kelvin. The script will print the
corresponding temperature in the scale specified by the user. For example, the
output might look like this:
Enter the temp in degrees C: 29.3
Do you want K or F? F
The temp in degrees F is 84.7
The format of the output should be exactly as specified. The conversions follow:
9
F = C + 32
5
K = C + 273.15
13. Write a program that reads customers IDs and waiting time from a 2D array. The
program first calculates the average waiting time. Then it prints the following data on
the screen
o The average waiting time
o Customer ID and waiting time for customers who waits for a time longer
than the average waiting time.
Example:
Input 2D array: [34 15; 12 2; 21 24; 35 32; 78 13; 18 8]
Output: Average Waiting Time = 15.7
Customers who waited longer:
ID Waiting
21 24
35 32
14. Write a program that reads the coordinates of a point from the user and several
points coordinates from an array. The program calculates and prints the distance
between the user input point and the stored points. The program prints also the
number of the nearest point. Use a function to calculate the distance.
Example:
Input: X=[0 6; 9 0; 5 5; 2 2; 2 7] UserPoint= [0 0]
Output:
Dist= 6.0 9.0 7.0 2.8 7.3
Nearest point is 4
Page 3
CHS – Spring 2014 Faculty of Engineering
Introduction to Computers and Cairo University
Engineering (GENN004) Page 4 of 5
Lab 7 – Practice Session 1
15. Write a script called prtemps that will prompt the user for a maximum Celsius
value in the range from –16 to 20; error-check to make sure it’s in that range.
Then, print a table showing degrees Fahrenheit and degrees Celsius until this
maximum is reached. The first value that exceeds the maximum should not be
printed. The table should start at 0 degrees Fahrenheit, and increment by 5 degrees
Fahrenheit until the max (in Celsius) is reached. Both temperatures should be
printed with a field width of 6 and one decimal place. The formula is C = 5/9 (F –
32). For example, the execution of the script might look like this (the format
should be exactly like this):
>> prtemps
When prompted, enter a temp in degrees C in range -16 to 20.
Enter a maximum temp: 30
Error! Enter a maximum temp: 9
F C
0.0 -17.8
5.0 -15.0
.
.
.
40.0 4.4
45.0 7.2
16. Write a program that reads a number N from the user and prints the following
multiplication table:
1 2 3 4 5
2 4 6 8 10
3 6 9 12 15
4 8 12 16 20
5 10 15 20 25
17. Write a program that reads a number N from the user and prints the following
multiplication table:
1
2 4
3 6 9
4 8 12 16
5 10 15 20 25
Page 4
CHS – Spring 2014 Faculty of Engineering
Introduction to Computers and Cairo University
Engineering (GENN004) Page 5 of 5
Lab 7 – Practice Session 1
18. Write a program that reads from the user two limits, the first limit is the low grade
while the second limit is the high grade. Then, the program accepts grades from the user
until the user enters a negative value. The program should only accept grades from zero
to 100. The program should print out to the user at the end the following: total number
of entered grades, number of entered grades between the low and high grade, and the
maximum entered grade between the low and high grade.
The input and output of the program should look like the shown examples.
Example 1 Example 2
Please enter the low grade:70 Please enter the low grade:120
Please enter the high grade:80 Error: valid range is [0 - 100]
Enter Grade:100 Please enter the low grade:-50
Enter Grade:120 Error: valid range is [0 - 100]
Error: valid range is [0 - 100] Please enter the low grade:300
Enter Grade:77 Error: valid range is [0 - 100]
Enter Grade:73 Please enter the low grade:60
Enter Grade:79 Please enter the high grade:75
Enter Grade:88 Enter Grade:130
Enter Grade:99 Error: valid range is [0 - 100]
Enter Grade:0 Enter Grade:100
Enter Grade:75 Enter Grade:0
Enter Grade:-1 Enter Grade:55
Total number of entered grades = 8 Enter Grade:200
Number of entered grades between 70 and 80 = 4 Error: valid range is [0 - 100]
Maximum entered grade between 70 and 80 = 79 Enter Grade:-1
*** End of Program *** Total number of entered grades = 3
No grades entered between 60 and 75
*** End of Program ***
Page 5