Lab 4 Due Date by Mon 11 Nov Question 1

advertisement
Lab 4
Due Date by Mon 11 Nov
Question 1
Rewrite the following fragment using switch:
// declare a variable to keep track of the interest
float interest = 0.0;
// decide which interest rate to use.
if (account_type == 1){
interest = 2.3;
}
else {
if (account_type == 2) {
interest = 2.6;
}
else {
if (account_type == 3){
interest = 2.9;
}
else {
if (account_type == 4){
interest = 3.3;
}
else {
if (account_type == 5){
interest = 3.5;
}
else {
// account type must be 6
interest = 3.8;
}
}
}
}
}
Question 2
Write a program that get a grade from the user then print a statement showing the
grade which are ( A, B, C, D, F) if the user entered an invalid choice an error message
should be appeared.
Hint ( Use Switch statement )
1
Question 3
Write a program that defines four functions to round a number x in various ways:
a) roundToInteger( number )
b) roundToTenths( number )
c) roundToHundredths( number )
d) roundToThousandths( number )
For each value read, your program should print the original value, the number
rounded to the nearest integer, the number rounded to the nearest tenth, the number
rounded to the nearest hundredth and the number rounded to the nearest thousandth.
Enter a number: 8.22
8.220000 rounded is 8.0
Enter a number: 7.98
8.0 rounded is 8.0
Enter a number: 4.52
4.5 rounded is 5.0
Enter a number: 6.9999
7.0 rounded is 7.0
Enter a number: 3.345
3.3 rounded is 3.0
2
Question 4
Write a complete C++ program with the two alternate functions specified below, of
which each simply triples the variable count defined in main. Then compare and
contrast the two approaches. These two functions are
a) Function tripleCallByValue that passes a copy of count call-by-value, triples the
copy and returns the new value.
b) Function tripleByReference that passes count with true call-by-reference via a
reference parameter and triples the original copy of count through its alias (i.e., the
reference parameter).
Enter
Value
Value
Value
Value
Value
Good Luck
Rana Al Abdan
3
an integer: 8
before call to tripleCallByValue() is: 8
returned from tripleCallByValue() is: 24
(in main) after tripleCallByValue() is: 8
before call to tripleByReference() is: 8
(in main) after call to tripleByReference() is: 24
Download