Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
doc
1 A fruit processor using ifs and a do..while
1.
#include <iostream>
using namespace std;
int main()
{
const int APPLE = 1, PEAR = 2;
int fruit, weight;
float price;
char key;
do {
cout << "\nPlease enter the fruit code and weight: ";
cin >> fruit >> weight;
if (fruit == APPLE)
{
price = weight * 0.5;
cout << "\nThe price of APPLE is : RM " << price ;
}
else if (fruit == PEAR)
{
price = weight * 0.8;
cout << "\nThe price of PEAR is : RM " << price ;
}
else cout << "Illegal fruit ";
cout << "\nPlease enter Y to continue : ";
cin >> key;
}
while (key == 'Y' || key == 'y');
system("PAUSE");
return 0;
}
10-Aug-16
Write a program that continuously requests a grade to be entered. If the grade is less than 0 or greater than 100, your
program should print an appropriate message informing the user that an invalid grade has been entered; else, the grade
should be added to a total. When a grade of 999 is entered, the program should exit the repetition loop and compute
and display the average of the valid grades entered.
3. Write a C++ program that adds ed to the end each of these words below. Use counter-controlled since you
know how many input to enter. Run your program with the following input data.
Input:
add
cheat
delete
post
Output:
added
cheated
deleted
posted
4. Write a program ask the user to enter more than one numbers and to do the following arithmetic to the
number if the number entered is an odd number. You need to break out from the loop using a sentinel value
of your choice.
Odd_Number = Odd_Number - 10 / 3
5. Draw flowchart for the following C++ program.
#include <iostream>
using namespace std;
int main()
{
int maxNums;
int count;
double num, total;
cout << "Type in the total number of data values to be added: ";
cin >> maxNums;
count = 1;
total = 0;
while (count <= maxNums)
{
cout << "\nEnter a number: ";
cin >> num;
total = total + num;
count++;
}
cout << "\n\nThe final total is " << total << endl;
return 0;
}
ChewBP@SEGi UC
10-Aug-16
a.
Use while..loop
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
b.
ChewBP@SEGi UC
10-Aug-16
122.7
127.0
124.9
123.8
124.7
123.0
125.1
126.0
126.7
122.6
9. Modify the program written for Q8 so that the number of experiments is entered by the user and also that a
different number of test results can be entered for each experiment.
ChewBP@SEGi UC
10-Aug-16