Professional Documents
Culture Documents
Lecture 9
4
Assignment 7
• Write a C++ program that takes the sales from the user then calculates
and display the commission amount.
• Commission amount = sales * commission rate
• The commission rate is depending on the value of sales as the
following table:
• Problem statement
A class of ten students took a quiz. The grades
(integers in the range 0 to 100) for this quiz are
available to you. Calculate and display the total of all
student grades and the class average on the quiz.
13
1 Set total to zero
2 Set grade counter to one
3
4 While grade counter is less than or equal to ten
5 Prompt the user to enter the next grade
6 Input the next grade
7 Add the grade into the total
8 Add one to the grade counter
9
10 Set the class average to the total divided by ten
11 Print the total of the grades for all students in the class
12 Print the class average
Fig. 4.7 | Pseudocode algorithm that uses counter-controlled repetition to solve the
class average problem.
14
# include<iostream>
using namespace std;
int main ()
{
// declaration phase
int total;
int gradecounter;
int grade;
double average;
// initialization phase
total= 0;
gradecounter =1;
// loop until 10
while(gradecounter <= 10)
{
cout <<“enter grade :”;
cin >> grade;
total = total + grade;
gradecounter = gradecounter + 1;
}
average = total / 10;
cout << “\n Total of all 10 grades is” << total;
cout << “ \n class average is “<< average;
}
15
Outline
16
Case 2: Sentinel-Controlled
while Loops
• Sentinel variable is tested in the condition
and loop ends when sentinel is encountered
• Problem statement
Develop a class average program that
processes grades for a number of students
each time it is run.
18
1 Initialize total to zero
2 Initialize counter to zero
3
4 Prompt the user to enter the first grade
5 Input the first grade (possibly the sentinel)
6
7 While the user has not yet entered the sentinel
8 Add this grade into the running total
9 Add one to the grade counter
10 Prompt the user to enter the next grade
11 Input the next grade (possibly the sentinel)
12
13 If the counter is not equal to zero
14 Set the average to the total divided by the counter
15 Print the total of the grades for all students in the class
16 Print the class average
17 else
18 Print “No grades were entered”
controlled repetition.
19
# include <iostream>
Using namespace std;
int main ()
{
// declaration phase
int total;
int gradecounter;
int grade;
double average;
// initialization phase
total= 0;
gradecounter =0;
// processing phase
cout <<“ enter grade or -1 to quit:”;
cin >> grade;
20
// loop until value read from user
While (grade != -1)
{
total = total + grade;
gradecounter = gradecounter + 1;
cout <<“ enter grade or -1 to quit:”;
cin >> grade;
}
// termination phase
if ( gradecounter != 0)
{
average = total / gradecounter;
cout << “ \n total of all “ << gradecounter<< “total pf the grade =“<< average;
cout <<“\n class average is”<< average;
}
Else
cout << “ no grades were entered”;
Return 0;
}
21
Outline
22
Assignment 8
A college offers a course that prepares students for the state exam for
real estate brokers. Last year, ten of the students who completed this
course took the exam. The college wants to know how well its students
did on the exam. You have been asked to write a program to summarize
the results. You have been given a list of these 10 students. Next to each
name is written a 1 if the student passed the exam or a 2 if the student
failed.
Your program should analyze the results of the exam as follows:
1. Input each test result (i.e., a 1 or a 2). Display the prompting message
“Enter result” each time the program requests another test result.
2. Count the number of test results of each type.
3. Display a summary of the test results indicating the number of
students who passed and the number who failed.
4. If more than eight students passed the exam, print the message
“Raise tuition.”
23
1 Initialize passes to zero
2 Initialize failures to zero
3 Initialize student counter to one
4
5 While student counter is less than or equal to 10
6 Prompt the user to enter the next exam result
7 Input the next exam result
8
9 If the student passed
10 Add one to passes
11 Else
12 Add one to failures
13
14 Add one to student counter
15
16 Print the number of passes
17 Print the number of failures
18
19 If more than eight students passed
20 Print “Raise tuition”