Anda di halaman 1dari 4

Lab 6-Repetition Statments-V16-7.

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;
}

a) Have a look at above


program and
try and
understand how it works.
Note the use of const and
also note the use of a
do...while loop allowing us
to decide whether to exit the
program or go again. (See the
flow chart for this program
the doted boxes have been
added to help you pick out
various features such as:
outer dotted box is the
do..while loop, middle dotted
box is the 1st if and the inner
most dotted box is the 2nd
if.)
ChewBP@SEGi UC

10-Aug-16

Lab 6-Repetition Statments-V16-7.doc


b) Run the program entering numbers such as 1 for apple and weight 20 units, etc. What happens if you
enter a fruit number out of the defined range?
c) Can you think how it might be modified to work with switch statements instead of the if..else
statements? Have a go at changing it.
2.

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

Lab 6-Repetition Statments-V16-7.doc


6. Draw flowchart for the following C++ program.
#include <iostream>
using namespace std;
int main()
{
int counter;
int sum;
int N;
cout << "Enter the number of positive " integers to be added: ";
cin >> N;
sum = 0;
cout << endl;
for (counter = 1; counter <= N; counter++)
sum = sum + counter;
cout << "The sum of the first " << N;
cout << " positive integers is "; << sum << endl;
return 0;
}

Nested loop (optional)


7. Using nested loop, Write a segments of C++ statements to output the following:

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.

Use do while Loop


1
1 2
1 2 3
1 2 3 4
1 2 3 4 5

ChewBP@SEGi UC

10-Aug-16

Lab 6-Repetition Statments-V16-7.doc


8. An electrical manufacturer tests five generators by measuring their output voltages at three different times.
Write a C++ program that uses a nested loop to enter each generators test results, and then computes and
displays the average voltage for each generator. Lastly your program should display the average for the all
the generator. Assume the following generator test results:
1st generator: 122.5
2nd generator: 120.2
3rd generator: 121.7
4th generator: 122.9
5th generator: 121.5

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

Anda mungkin juga menyukai