Uploaded by Zinhle Khanyile

Zinhle Khanyile - tutorial 2

advertisement
Tutorial 2 (if …else and switch)
Answer all questions on a new document then upload (submit) it on Google class.
Useful information:
1. Total marks = 100
2. Due date = 27/01/2021
3. This tutorial will add to your final mark.
Question 1
[5 marks]
Mark the following statements as true or false.
a) In a one-way selection, if a semicolon is placed after the expression in an if statement, the
expression in the if statement is always true.
[1 mark]
b) Every if statement must have a corresponding else. [1 mark]
c) The expression in the if statement:
if ( score = 30)
grade = ‘A' ;
always evaluates to true.
[1 mark]
d) Suppose the input is 5. The output of the code:
cin >> num;
if ( num > 5)
cout << num;
num = 0;
else
cout << “ Num is zero \n";
Is: Num is zero
[1 mark]
e) The expression !( x > 0 ) is true only if x is a negative number.
[1 mark]
Question 2
[10 marks]
Choose the best answer.
a) if ( 60 <= 12 * 5)
cout << “ Hello” ;
cout << “ There" ;
Outputs the following:
i)
Hello There ii) Hello iii) Hello iv)There
[2 marks]
b) If ( 7 <= 8 )
cout << 6 – 9 * 2 / 6 <<endl;
Outputs the following:
i)
-1 ii) 3 iii) 3.0 iv) none of these
[4 marks]
c) if ( 7 < 8 )
{
cout << “2 4 6 8” << endl;
cout << “1 3 5 7” << endl;
}
Outputs the following:
i)
2468
1357
ii)
iii)
1357
None of these
[2 marks]
d) if ( 5 < 3 )
cout << “*”;
else if ( 7 == 8 )
cout << “&”;
else
cout << “$”;
Outputs the following:
i)
* ii) & iii) $ iv) none of these
[2 marks]
Question 3
[10 marks]
There
Suppose that x, y, z, and w are int variables, and x = 3, y = 4, z = 7, and w = 1. What is the output of
the following statements?
a) cout << “x == y: “ << ( x == y) << endl;
[2 marks]
b) cout << “x != z: “ << ( x != z) << endl;
[2 marks]
c) cout << “y == z - 3: “ << ( y == z - 3) << endl; [2 marks]
Question 4
d) cout << “x + y > z: “ << ( x + y > z) << endl;
[2 marks]
e) cout << “!(z > w ): “ << !( z > w) << endl;
[2 marks]
[16 marks]
i)
Suppose that sale and bonus are double variables. Write an if. . .else statement that
assigns a value to bonus as follows: if sale is greater than E20 000, the value assigned to
bonus is 0.10; if sale is greater than E10 000 and less than or equal to E20 000, the value
assigned to bonus is 0.05; otherwise, the value assigned to bonus is 0.
[6 marks]
ii)
Suppose that overspeed and fine are double variables. Assign the value to fine as follows: if
0 < overspeed <= 5, the value assigned to fine is E20.00; if 5 < overspeed <= 10, the value
assigned to fine is E75.00; if 10 < overspeed <= 15, the value assigned to fine is E150.00; if
overspeed > 15, the value assigned to fine is E150.00 plus E20.00 per mile over 15.
[10 marks]
Question 5
i)
[18 marks]
Rewrite the following expressions using the conditional operator. ( Assume all variables are
declared properly. )
a) if ( x >= y )
z = x – y;
else
z = y – x;
[3 marks]
b) if ( hours >= 40.0)
wages = 40 * 7.50 + 1.5 *7.5 * (hours – 40);
else
wages = hours * 7.50;
[3 marks]
c) if ( score >= 60)
str = “Pass";
else
str = “Fail";
[3 marks]
ii)
Rewrite the following expressions using an if. . .else statement. ( Assume all variables
are declared properly. )
a) ( x < 5 ) ? y = 10 : y = 20;
[3 marks]
b) ( fuel >= 10 ) ? drive = 150 : drive = 30;
[3 marks]
c) ( books >= 3 ) ? discount = 0.15 : discount = 0.0;
[3 marks]
Question 6
[11 marks]
Write the missing statements in the following program so that it prompts the user to input two
numbers. If one of the numbers is 0, the program should output a message indicating that both numbers
must be nonzero. If the first number is greater than the second number, it outputs the first number
divided by the second number; if the first number is less than the second number, it outputs the second
number divided by the first number; otherwise, it outputs the product of the numbers.
#include <iostream>
using namespace std;
int main ()
{
double firstNum, secondNum;
cout << “Enter two nonzero numbers: \n”;
cin >> firstNum >> secondNum;
cout << endl;
//missing statements
return 0;
}
Question 7
[30 marks]
In a right angled triangle, the square of the length of the longer side is equal to the sum of the squares
of the lengths of the other two sides. Write a program that prompts the user to enter the lengths of
three sides of a triangle and then outputs a message indicating whether the triangle is a right angled
triangle.
Note: remember to include your name, surname, student number and class.
Download