1 Write an ifelse statement that prints Excellent when score
1. Write an if/else statement that prints “Excellent” when score is 90 or higher, “Good” when score is between 80 and 89, and “Try Harder” when score is less than 80. Note that you may use the following C++ statement format for displaying:
cout<<“Excellent”; //printExcellent
2. Write an if statement that sets the variable hours to 10 when the flag variable minimum is set to true.
3. Convert the following conditional expression into an if/else statement.
q = (x < y) ? (a + b) : (x * 2);
4. Convert the following if/else statement into a switch statement:
if (choice == 1)
{
cout << fixed << showpoint << setprecision(2);
}
else if ((choice == 2) || (choice == 3))
{
cout << fixed << showpoint << setprecision(4);
}
else if (choice == 4)
{
cout << fixed << showpoint << setprecision(6);
}
else
{
cout << fixed << showpoint << setprecision(8);
}
5. Using the following chart, write a C++ statement that assigns .10, .15, or .20 to commission, depending on the value in sales.
sales
commission Rate
Up to $10,000
10%
$10,000 to $15,000
15%
Over $15,000
20%
6. Write one or more C++ statements that assign the correct value to discount, using the logic described here:
Assign .20 to discount if dept equals 5 and price is $100 or more.
Assign .15 to discount if dept is anything else and price is $100 or more. Assign .10 to discount if dept equals 5 and price is less than $100.
Assign .10 to discount if dept equals 5 and price is less than $100.
Assign .05 to discount if dept is anything else and price is less than $100.
7. Write a for loop that displays the following set of numbers: 0, 10, 20, 30, 40, 50 ... 1000
Note that for display statement, you may use the following statement format:
cout << x; //x is a variable, and this statement will display the value of x.
8. What will each of the following program segments display?
(a) int x=1;
while (x<10); x++;
cout << x; //this statement is used to display value of the variable x
(b) int x=1;
while (x<10)
x++;
cout << x;
9. Write a complete NXC program to implement the following follow flowchart. Note that the flowchart is only abstract and not detailed for coding. Whenever the robot hits an object, we let it move back a bit, make a turn, and then continue. Assume that a touch sensor is connected to Port 1, and motors are on output Port C (left wheel) and Port A (right wheel)
Start
Configure sensors
On Forward
T
Not hit anything
F
Back off a little
Make turn
| sales | commission Rate |
| Up to $10,000 | 10% |
| $10,000 to $15,000 | 15% |
| Over $15,000 | 20% |
Solution
1. Write an if/else statement that prints “Excellent” when score is 90 or higher, “Good” when score is between 80 and 89, and “Try Harder”
when score is less than 80. Note that you may use the following C++ statement format for displaying:
cout<<“Excellent”; //printExcellent
---------
Answer:
---------
if(score>=90) {
cout<<\"Excellent\";
}
else if(score>=80 && score<=89) {
cout<<\"Good\";
}
else {
cout<<\"Try Harder\";
}
==================================================================
2. Write an if statement that sets the variable hours to 10 when the flag variable minimum is set to true.
---------
Answer:
---------
if(minimum==true) {
hours = 10;
}
==================================================================
3. Convert the following conditional expression into an if/else statement.
q = (x < y) ? (a + b) : (x * 2);
---------
Answer:
---------
if(x<y) {
q = a + b;
}
else {
q = x * 2;
}
==================================================================
4. Convert the following if/else statement into a switch statement:
if (choice == 1)
{
cout << fixed << showpoint << setprecision(2);
}
else if ((choice == 2) || (choice == 3))
{
cout << fixed << showpoint << setprecision(4);
}
else if (choice == 4)
{
cout << fixed << showpoint << setprecision(6);
}
else
{
cout << fixed << showpoint << setprecision(8);
}
---------
Answer:
---------
switch(choice) {
case 1:
cout << fixed << showpoint << setprecision(2);
break;
case 2:
case 3:
cout << fixed << showpoint << setprecision(4);
break;
case 4:
cout << fixed << showpoint << setprecision(6);
break;
default:
cout << fixed << showpoint << setprecision(8);
break;
}
==================================================================
5. Using the following chart, write a C++ statement that assigns .10, .15, or .20 to commission, depending on the value in sales.
sales
commission Rate
Up to $10,000
10%
$10,000 to $15,000
15%
Over $15,000
20%
---------
Answer:
---------
if(sales<10000) {
commission = sales*0.10;
}
else if(sales>=10000 && sales<=15000) {
commission = sales*0.15;
}
else {
commission = sales*0.20;
}
==================================================================
6. Write one or more C++ statements that assign the correct value to discount, using the logic described here:
Assign .20 to discount if dept equals 5 and price is $100 or more.
Assign .15 to discount if dept is anything else and price is $100 or more.
Assign .10 to discount if dept equals 5 and price is less than $100.
Assign .05 to discount if dept is anything else and price is less than $100.
---------
Answer:
---------
if(dept==5 && price>=100) {
discount = price*0.20;
} else if(price>=100) {
discount = price*0.15;
} else if(dept==5 && price<100) {
discount = price*0.10;
} else if(price<100) {
discount = price*0.05;
}
==================================================================
7. Write a for loop that displays the following set of numbers: 0, 10, 20, 30, 40, 50 ... 1000
Note that for display statement, you may use the following statement format:
cout << x; //x is a variable, and this statement will display the value of x.
---------
Answer:
---------
for(int x=0;x<=1000;x+=10) {
cout<<x;
}
==================================================================
8. What will each of the following program segments display?
(a) int x=1;
while (x<10); x++;
cout << x; //this statement is used to display value of the variable x
(b) int x=1;
while (x<10)
x++;
cout << x;
---------
Answer:
---------
a) x value is 2 since after while semicolon is present.
b) x value is 10 since it iterates over the while loop till value 9 and post increment to 10
==================================================================




