KEMBAR78
Intro To Computer Programming Lecture 4 | PDF | C++ | Integer (Computer Science)
0% found this document useful (0 votes)
4 views96 pages

Intro To Computer Programming Lecture 4

Uploaded by

cudarun
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views96 pages

Intro To Computer Programming Lecture 4

Uploaded by

cudarun
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 96

Introduction to Computer

Programing
ECEG-1052
Lecture 04

1 / 17
Recap
• Variables: definition, naming,
declaration, initialization
• Data types in C++
• Operators in C++: definition,
types
• Expressions: definition, types,
precedence of operators in
expressions
• Characters and strings
• Type casting ( data type coercion)
2 / 17
1 // basic structure of C++ program

2 // A first program in C++


Comments
3 #include <iostream> Written between /* and */ or following a //
4 using namespace std; Improve program readability and do not cause the
5 int main() computer to perform any action

6 {
preprocessor directive
7 cout << "Welcome to C++!\n"; Do not use ; at end of #include statement
8 Message
C++ to thecontain
programs C++ preprocessor
one or more functions,
9 return 0; // indicate that exactly
Lines
program one of which
beginning
ended must bepreprocessor
with # are
successfully main directives
10 }
Parenthesis
#include used to indicate a function
<iostream> tells the preprocessor
to include
int means the
thatcontents of the file
main "returns" an integer value.
<iostream>,
which
More in includes input/output operations (such as
functions.
Welcome to C++! printing to the screen).
Prints the string of characters contained between the
return is one a way toquotation
exit a marks.
function. Left brace { begins the body of every function
The entire line, including cout, the << operator, the
return 0, in this case,string
means"Welcome
that and a right brace } ends it
to C++!\n" and the semicolon
the program terminated normally.
(;), is called a statement.

All statements must end with a semicolon.


Including header files and library functions

programmer chooses what to include in the executable

Header files declare the functions to be linked in from libraries…

#include <iostream> read & write functions

#include <math> sqrt, log, sin, abs, etc

#include “D:\andy.h” My stuff that I use frequently


and don’t want to recreate

If header is in “” the filename must be full (or relative);


if in <> pre-processor searches in pre-defined folders
Preprocessor Directives
• C++ has a small number of operations
• Many functions and symbols needed to run a C++
program are provided as collection of libraries
• Every library has a name and is referred to by a
header file
• Preprocessor directives are commands supplied to
the preprocessor
• All preprocessor commands begin with #
• No semicolon at the end of these commands

5
namespace and Using cin
and cout in a Program
• cin and cout are declared in the header file
iostream, but within std namespace
• To use cin and cout in a program, use the
following two statements:
#include <iostream>
using namespace std;

6
A Simple Program: Printing a Line of
Text
• std::cout
• standard output stream object
• “connected” to the screen
• <<
• stream insertion operator
• value to the right of the operator (right operand) inserted into
output stream (which is connected to the screen)
cout << " Welcome to C++!\n"

•\
• escape character
• indicates that a “special” character is to be output
A Simple Program: Printing a Line of
Text (II)
Escape Sequence Description

\n Newline. Position the screen cursor to the


beginning of the next line.
\t Horizontal tab. Move the screen cursor to the next
tab stop.
\r Carriage return. Position the screen cursor to the
beginning of the current line; do not advance to the
next line.
\a Alert. Sound the system bell.
\\ Backslash. Used to print a backslash character.
\" Double quote. Used to print a double quote
character.

• There are multiple ways to print text


• Following are more examples
1

2 // Printing a line with multiple statements

3 #include <iostream>

4 using namespace std;

5 int main()

6 {

7 cout << "Welcome ";

8 cout << "to C++!\n";

10 return 0; // indicate that program ended successfully

11 }

Welcome to C++!

Unless new line '\n' is specified, the text continues


on the same line.
1

2 // Printing multiple lines with a single statement

3 #include <iostream>

4 using namespace std;

5 int main()

6 {

7 cout << "Welcome\nto\n\nC++!\n";

9 return 0; // indicate that program ended successfully

10 }

Welcome
to

C++!

Multiple lines can be printed with one


statement
Class exercise
• Write a program in C++ to display the following message
“Time is’t the main thing. It’s the only thing” Miles Davis.
1 // sample code to show students how scape symbol can be used

2 // write program to display quote in C++

3 #include <iostream>
using namespace std;
4

5 int main()

6 {

7 cout << “\” Time is’t the main thing. It’s the only thing\” Miles Davis. \n";

9 return 0; // indicate that program ended successfully

10 }

11 / 17
Another Simple Program: Adding Two
Integers (II)

• >> (stream extraction operator)


• When we use cin, it waits for user to input a value and stores the
value in the variable to the right of the operator.
• user types the expected input, then presses the Enter (Return) key
to send the data to the computer
• Example:
int myVariable;
cin >> myVariable;
• waits for user input, then stores input in myVariable
1
2 // Addition program
3 #include <iostream>
4 using namespace std;
5 int main()
6 {
7 int integer1, integer2, sum; // declaration
8
9 cout << "Enter first integer\n"; // prompt
Notice how cin is used to get user input.
10 cin >> integer1; // read an integer
11 cout << "Enter second integer\n"; // prompt
12 cin >> integer2; // read an integer
13 sum = integer1 + integer2; // assignment of sum
14 cout << "Sum is " << sum << endl; // print sum
15
16 return 0;
endl flushes the buffer and prints a
// indicate that program ended successfully
newline.
17 }

Enter first integer Variables can be output using cout << variableName
45
Enter second integer
72
Sum is 117
Example Program – Use of cout to prompt user to enter some data

#include <iostream>
#include <string>
using namespace std;
int main()
{
string name;
cout << "What is your name? ";
cin >> name;

cout << "Hello there, " << name;


return 0;
}
Example Program – Use of cout to prompt user to enter some data

#include <iostream>
#include <string>
using namespace std;

int main() {
string fullName;
cout << "Type your full name: ";
getline (cin, fullName);
cout << "Your name is: " << fullName;
return 0;
}
Types of Errors

• 3 types of Errors

• Syntax or compile time error


• Logical error or semantic error
• Run-time error
Syntax error – Omitting Semicolons
Oh No!

1 #include <iostream>
2
3 using namespace std;
4
5 int main()
6 {
7 cout << "Hello, World!" << endl
8 return 0;
9 }
Issues due to case changing or dropping characters:

cot << "Hello World!" << endl;

• This will cause a syntax error


Logical error:

cout << "Hollo, World!" << endl;

• Logic errors or run-time errors are errors in a program that


compiles (the syntax is correct), but executes without
performing the intended action.
Syntax error – Using Undefined Variables

You must define a variable before you use it for the first time.

double can_volume = 12 * liter_factor;


double liter_factor = 0.0296;
Using Uninitialized Variables

Is it a syntax or Logical error?

int bottles; // Forgot to initialize


int bottle_volume = bottles * 2;

What value would be output from the following statement?

cout << bottle_volume << endl;


// Result is
unpredictable
Logical Error – Unintended Integer Division

cout << "Please enter your last three test scores: ";
int s1;
int s2;
int s3;
cin >> s1 >> s2 >> s3;
double average = (s1 + s2 + s3) / 3;
cout << "Your average score is " << average << endl;

What do you think is the problem in


this code fragment?
Syntax Error – Unbalanced Parentheses

Consider the expression

What is wrong with it?

(-(b * b - 4 * a * c) / (2 * a)
Syntax Error – un-initialization of constants

How many errors are there?


#include <iostream>
using namespace std;
int main()
{
double const PI;
float width, area = 100 * PI;
float const e = 2.71828;
e= area;

return 0;
}
How you fix it?
Basics of Control Structures in
Programing
• Sequential execution
• Statements executed in order
• Transfer of control
• Next statement executed not next one in sequence
• 3 control structures
• Sequence structure
• Programs executed sequentially by default
• Selection structures
• if, if/else, switch
• Repetition structures
• while, do/while, for
• Design Issues:

• What is the form and type of the control


expression?
• What is the selectable segment form (single
statement, statement sequence, compound
statement)?
• How should the meaning of nested selectors be
specified?
Selection or decision control
structures in C++
Single-Way Selection

• One-way “if” statement


• Syntax:
if (boolean_expr)
statement;
• Problem: can select only a single statement
Two-Way Selection

• “if-then-else” statement
• syntax:
if (boolean_expr)
statement;
else
statement;
• The statements could be single or compound
if Selection Structure Example
• Example code fragment using C++
If student’s grade is greater than or equal to 60
Print “Passed”

if ( grade >= 60 )
cout << "Passed";
if Selection Structure Example
contd…

• Flowchart of pseudocode statement

A decision can be made


on any expression.
true zero - false
grade >= print “Passed”
60 nonzero - true
Example:
false 3 - 4 is true

What will be executed if it is false?


The if/else Selection Structure

• if/else
• A different action is performed when condition is true
and when condition is false
• Psuedocode
if student’s grade is greater than or equal to 60
print “Passed”
else
print “Failed”
• C++ code fragment
if ( grade >= 60 )
cout << "Passed";
else
cout << "Failed";

32 / 17
if/else Selection Structure

• Code could be written:


cout << ( grade >= 60 ? “Passed” : “Failed” );

Condition Value if true Value if false

false true
grade >= 60

print “Failed” print “Passed”


Nested if/else Selection Structure
• Nested if/else structures
• One inside another, test for multiple cases
• Once condition met, other statements skipped
if student’s grade is greater than or equal to 90
Print “A”
else
if student’s grade is greater than or equal to 80
Print “B”
else
if student’s grade is greater than or equal to 70
Print “C”
else
if student’s grade is greater than or equal to 60
Print “D”
else
Print “F”
Nested if/else Selection
Structure
• Example
if ( grade >= 90 ) // 90 and above
cout << "A";
else if ( grade >= 80 ) // 80-89
cout << "B";
else if ( grade >= 70 ) // 70-79
cout << "C";
else if ( grade >= 60 ) // 60-69
cout << "D";
else // less than 60
cout << "F";
Compound Vs Single statement

• Compound statement: Set of statements within a pair of braces

if ( grade >= 60 )
cout << "Passed.\n";
else {
cout << "Failed.\n";
cout << "You must take this course
again.\n";
}
• Without braces,
cout << "You must take this course again.\
n";
always executed

• Block
• Set of statements within braces
Nested if statement

An if-else statement is included with another if or else statement


Statement 1

statement1

expression1 true if (expression1)


if (expression2)
false expression2 true
statement2a

Statement 2b Statement 2a
else
statement2b

statement3
Statement 3

37
Nested if statement

• With which “if” the “else” part is associated?

if (a==1) if (a==1)
if (b==2) if (b==2)
cout << “***\n”; cout << “***\
else n”;
cout << “###\n”; else
cout << “###\
n”;

38
More Example on Nested-if

• An if statement can be nested within another if statement


if(mark>=70 && mark <=100) //get an A grade but
can be A-, A or A+
{
if (mark>90)
cout << "You get grade A+.\
n";
else if (mark>80)
cout << "You get grade A.\
n";
else
cout << You get grade A-.\
n";
}
39
else if ……
What is the output?

int x=5; int x=5;


if (x!=5); if (x!=5)
x=3; x=3;
cout << x; cout << x;
/*output is =???/
/*output is = ???*/

40
The switch Multiple-Selection Structure
• switch
• Useful when variable or expression is tested for
multiple values
• Consists of a series of case labels and an optional
default case
• break is (almost always) necessary
switch (expression) {

case val1:

statement if (expression == val1)


break; statement
else if (expression==val2)
case val2: statement
statement ….
break;
….
case valn: else if (expression== valn)
statement statement
else
break; statement
default:
statement

break;
}
The switch Statement

•The expression of a switch statement


must result in an integral type, meaning
an integer (byte, short, int, long)
or a char
•It cannot be a boolean value or a
floating point value (float or double)

5-43
flowchart

true
case a case a action(s) break

false

true
case b case b action(s) break
false

.
.
.

true
case z case z action(s) break
false

default action(s)
Example
#include <iostream>
using namespace std;
void main(){
int x=0;
switch (x){
case 0:
cout << “Zero”;
x=1;
break; /* no braces is needed */
case 1:
cout << “One”;
break;
case 2:
cout << “Two”;
break;
default:
cout << “Greater than two”;
} //end switch
}

45
1 // example code to demonstrate swich statement
2 // Counting letter grades
3 #include <iostream>
4 using namespace std;
5 int main()
6 {
7 int grade, // one grade
8 aCount = 0, // number of A's
9 bCount = 0, // number of B's
10 cCount = 0, // number of C's
11 dCount = 0, // number of D's
12 fCount = 0; // number of F's
13
14 cout << "Enter the letter grades." << endl
15 << "Enter the EOF character to end input." << endl;
16
17 while ( ( grade = cin.get() ) != EOF ) {
18
Notice how the case statement is used
19 switch ( grade ) { // switch nested in while
20 case 'A': // grade was uppercase A
21 case 'a': // or lowercase a
22 ++aCount;
23 break; // necessary to exit switch

24 case 'B': // grade was uppercase B


25 case 'b': // or lowercase b
26 ++bCount;

27 break;
28 case 'C': // grade was uppercase C
29 case 'c': // or lowercase c
30 ++cCount;
31 case 'D': // grade was uppercase D
32 case 'd': // or lowercase d break causes switch to end and
33 ++dCount; the program continues with the first
34 break; statement after the switch
35 structure.
case 'F': // grade was uppercase F
36 case 'f': // or lowercase f
37 ++fCount;
38 break;
39 case '\n': // ignore newlines,
40 case '\t': // tabs,
41 Notice the default
case ' ': // and spaces in input statement.
42 break;
43 default: // catch all other characters
44 cout << "Incorrect letter grade entered."
45 << " Enter a new grade." << endl;
46 break; // optional
47 }
48 }
49 cout << "\n\nTotals for each letter grade are:"
50 << "\nA: " << aCount
51 << "\nB: " << bCount
52 << "\nC: " << cCount
53 << "\nD: " << dCount
54 << "\nF: " << fCount << endl;
55 return 0;
Enter the letter grades.
Enter the EOF character to end input.
a
B
c
C
A
d
f
C
E
Incorrect letter grade entered. Enter a new grade.
D
A
b

Totals for each letter grade are:

Program Output
A: 3
B: 2
C: 3
D: 2
F: 1
Repetition structures (LOOPs)

while, do/while, for

49 / 17
The while Loop
• The general form of the while statement is:
while (expression)
statement;
while is a reserved word
• Statement can be simple or compound
• Expression acts as a decision maker and is usually a
logical expression
• Statement is called the body of the loop
• The parentheses are part of the syntax

50
The while Loop
(continued)
• Expression provides an entry condition

• Statement executes if the expression initially


evaluates to true

• Loop condition is then reevaluated

• Statement continues to execute until the expression


is no longer true

51
The while Loop (continued)

• Infinite loop: continues to execute endlessly

• Can be avoided by including statements in the loop


body that assure exit condition will eventually be
false

52
Counter-Controlled while Loops
• If you know exactly how many pieces of data need
to be read, the while loop becomes a counter-
controlled loop

• Example

56
Sentinel-Controlled while
Loops
• Sentinel variable is tested in the condition and
loop ends when sentinel is encountered

57
Flag-Controlled while
Loops
• A flag-controlled while loop uses a bool variable to
control the loop
• The flag-controlled while loop takes the form:

58
Infinite Loops
• The body of a while loop eventually must make the
condition false
• If not, it is called an infinite loop, which will execute
until the user interrupts the program
• This is a common logical error
• You should always double check the logic of a program
to ensure that your loops will terminate normally

5-59
Infinite Loops
• An example of an infinite loop:
int count = 1;
while (count <= 25)
{
cot<<count;
count = count - 1;
}

This loop will continue executing until interrupted by


the user or until an underflow error occurs

5-60
Nested While Loops
• How many times will the string "Here" be printed?

count1 = 1;
while (count1 <= 10)
{
count2 = 1;
while (count2 <= 20)
{
cout<<"Here \n";
count2++;
}
count1++;
}

5-61
The for Loop

• The general form of the for statement is:


for (initial statement; loop condition; update statement)

statement;
• The initial statement, loop condition, and update
statement are called for loop control statements

62
The for loop executes as follows:
1. The initial statement executes.
2. The loop condition is evaluated. If the loop condition
evaluates to true
i. Execute the for loop statement.
ii. Execute the update statement (the third expression
in the parentheses).
3. Repeat Step 2 until the loop condition evaluates to
false.

The initial statement usually initializes a variable (called the for


loop control, or for indexed, variable).
In C++, for is a reserved word.
The for Loop (comments)
• A semicolon at the end of the for statement (just before
the body of the loop) is a semantic error. In this case, the
action of the for loop is empty.
• In the for statement, if the loop condition is omitted, it is
assumed to be true.
• In a for statement, you can omit all three statements—
initial statement, loop condition, and update statement.
The following is a legal for loop:
• for (;;)
cout << "Hello" << endl;

67
Multiple variable
initialization
• Initialization and increment
• For multiple variables, use comma-separated lists
for (int i = 0, j = 0; j + i <= 10; j++, i++)
cout << j + i << endl;

68
The do…while Loop
• The general form of a do...while statement is:
do
statement;
while (expression);
• The statement executes first, and then the
expression is evaluated
• If the expression evaluates to true, the statement
executes again
• As long as the expression in a do...while
statement is true, the statement executes

70
The do…while Loop
(continued)
• To avoid an infinite loop, the loop body must
contain a statement that makes the expression
false
• The statement can be simple or compound
• If compound, it must be in braces
• do...while loop has an exit condition and
always iterates at least once (unlike for and
while)

71
break & continue Statements

• break and continue alter the flow of control

• When the break statement executes in a


repetition structure, it immediately exits

• The break statement can be used in while,


for, and do...while loops

75
break & continue Statements (continued)

• The break statement is used to exit early from a


loop

• After the break statement executes, the program


continues with the first statement after the structure

• The use of a break statement in a loop can


eliminate the use of certain (flag) variables
• If we have nested loops, the break statement
causes an exit from the innermost enclosing loop or
switch statement

76
Break example

• Break Logic
• Execution of a break ; statement at any location in a
loop-structure causes immediate exit from the loop-
structure

• for ( k = 0 ; k < 10 ; k++ ) {


if ( k == 5 ) break ;
cout<<k;
}

• Produces output : 0, 1, 2, 3, 4
break & continue
Statements (continued)
• continue is used in while, for, and do…
while structures

• When executed in a loop


• It skips remaining statements and proceeds with the
next iteration of the loop

78
Continue Example
• Continue Logic
• Execution of a continue ; statement at any location
in a loop-structure causes execution to continue at
the beginning of the loop structure (at the next loop
iteration) while skipping the remaining statements.

• for ( k = 0 ; k < 5 ; k++ ) {


if ( k == 3 ) continue ;
cout<<k ;
}

• Produces output : 0, 1, 2, 4
Nested for loop
• Suppose we want to create the following pattern
*
**
***
****
*****
• In the first line, we want to print one star, in the
second line two stars and so on

80
Nested Control Structures (continued)

• Since five lines are to be printed, we start with the


following for statement
for (i = 1; i <= 5 ; i++)
• The value of i in the first iteration is 1, in the
second iteration it is 2, and so on
• Can use the value of i as limit condition in another
for loop nested within this loop to control the
number of starts in a line

81
Nested Control Structures
(continued)
• The syntax is:
for (i = 1; i <= 5 ; i++)
{
for (j = 1; j <= i;
j++)
cout << "*";
cout << endl;
}

82
Nested Control Structures
(continued)
• What pattern does the code produce if we replace
the first for statement with the following?
for (i = 5; i >= 1; i--)
• Answer:
*****
****
***
**
*

83
Nested Control Structures
(continued)
• What if you want to display?
*
* *
* * *
* * * *
* * * * *

84
Example code on control structure

• Example
A class of ten students took a quiz. The
grades (integers in the range 0 to 100) for
this quiz are available to you. Determine the
class average on the quiz.

85
// Class average program with counter-controlled repetition.
#include <iostream>
using namespace std;

const int MAX_GRADES = 10;


// function main begins program execution
int main()
{
int total; // sum of grades input by user
int gradeCounter; // number of grade to be entered next
int grade; // grade value
int average; // average of grades

// initialization phase
total = 0; // initialize total
gradeCounter = 0; // initialize loop counter

86
// processing phase fig02_07.cpp
(2 of 2)

while ( gradeCounter < MAX_GRADES ) { // loop MAX_GRADES times


cout << "Enter grade: "; // prompt for input
cin >> grade; // read grade from user
total = total + grade; // add grade to total
gradeCounter = gradeCounter + 1; // increment counter
}

// termination phase The counter gets incremented


each time the loop executes.
average = total / MAX_GRADES; // integer division
Eventually, the counter causes the
loop to end.
// display result
cout << "Class average is " << average << endl;

return 0; // indicate program ended successfully

} // end function main

87
Enter grade: 98
Enter grade: 76
Enter grade: 71
Enter grade: 87
Enter grade: 83
Enter grade: 90
Enter grade: 57
Enter grade: 79
Enter grade: 82
Enter grade: 94
Class average is 81

88
more example on control strucutres

• Suppose problem becomes:


Develop a class-averaging program that will process an
arbitrary number of grades each time the program is run
• Unknown number of students
• How will program know when to end?
• Sentinel value
• Indicates “end of data entry”
• Loop ends when sentinel input
• Sentinel chosen so it cannot be confused with regular input
• -1 in this case or something else

89
// Class average program with sentinel-controlled repetition.
#include <iomanip>
using namespace std;

#include <iostream> // parameterized stream manipulators

const int SENTINEL = -1;


// function main begins program execution
int main()
{
int total; // sum of grades
int gradeCounter; // number of grades entered
int grade; // grade value

double average; // number with decimal point for average

// initialization phase
total = 0; // initialize total
90
gradeCounter = 0; // initialize loop counter
// processing phase
// get first grade from user
cout << "Enter grade, “ << SENTINEL << “ to end: "; // prompt for input
cin >> grade; // read grade from user

// loop until sentinel value read from user


while ( grade != SENTINEL ) {
total = total + grade; // add grade to total
gradeCounter = gradeCounter + 1; // increment counter

cout << "Enter grade, “ << SENTINEL << ” to end: "; // prompt for input
cin >> grade; // read next grade

} // end while
// termination phase
// if user entered at least one grade ...
if ( gradeCounter != 0 ) {
// calculate average of all grades entered
average = ( double )( total ) / gradeCounter;
91
// display average with two digits of precision
cout << "Class average is " << setprecision( 2 )<< average << endl;

} // end if part of if/else

else // if no grades were entered, output appropriate message


cout << "No grades were entered" << endl;

return 0; // indicate program ended successfully


setprecision(2)prints two digits past
decimal point (rounded to fit precision).
} // end function main
Programs that use this must include
<iomanip>
Enter grade, -1 to end: 75
Enter grade, -1 to end: 94
Enter grade, -1 to end: 97
Enter grade, -1 to end: 88
Enter grade, -1 to end: 70
Enter grade, -1 to end: 64
Enter grade, -1 to end: 83
Enter grade, -1 to end: 89
Enter grade, -1 to end: -1
92
Class average is 82.50
Nested Control Structures
• Problem statement
A college has a list of test results (1 = pass, 2 = fail) for 10
students. Write a program that analyzes the results. If more
than 8 students pass, print "Raise Tuition".
• Notice that
• Program processes 10 results
• Fixed number, use counter-controlled loop
• Two counters can be used
• One counts number that passed
• Another counts number that fail
• Each test result is 1 or 2
• If not 1, assume 2

93
// Analysis of examination results.
#include <iostream>
using namespace std;
const int MAX_STUDENTS = 10; const int MIN_PASSES = 8;
const int PASS = 1; const int FAIL = 2;
// function main begins program execution
int main()
{
// initialize variables in declarations
int passes = 0; // number of passes
int failures = 0; // number of failures
int studentCounter = 0; // student counter
int result; // one exam result

// process 10 students using counter-controlled loop


while ( studentCounter <= MAX_STUDENTS ) {

// prompt user for input and obtain value from user


cout << "Enter result (“ << PASS << ” = pass, “ << FAIL << ” = fail): ";
cin >> result;
94
// if result 1, increment passes; if/else nested in while
if ( result == PASS ) // if/else nested in while
passes = passes + 1;

else // if result not 1, increment failures


failures = failures + 1;

// increment studentCounter so loop eventually terminates


studentCounter = studentCounter + 1;

} // end while

// termination phase; display number of passes and failures


cout << "Passed " << passes << endl;
cout << "Failed " << failures << endl;

// if more than eight students passed, print "raise tuition"


if ( passes > MIN_PASSES )
cout << "Raise tuition " << endl;
return 0; // successful termination
95
} // end function main
Enter result (1 = pass, 2 = fail): 1
Enter result (1 = pass, 2 = fail): 2
Enter result (1 = pass, 2 = fail): 2
Enter result (1 = pass, 2 = fail): 1
Enter result (1 = pass, 2 = fail): 1
Enter result (1 = pass, 2 = fail): 1
Enter result (1 = pass, 2 = fail): 2
Enter result (1 = pass, 2 = fail): 1
Enter result (1 = pass, 2 = fail): 1
Enter result (1 = pass, 2 = fail): 2
Passed 6
Failed 4

Enter result (1 = pass, 2 = fail): 1


Enter result (1 = pass, 2 = fail): 1
Enter result (1 = pass, 2 = fail): 1
Enter result (1 = pass, 2 = fail): 1
Enter result (1 = pass, 2 = fail): 2
Enter result (1 = pass, 2 = fail): 1
Enter result (1 = pass, 2 = fail): 1
Enter result (1 = pass, 2 = fail): 1
Enter result (1 = pass, 2 = fail): 1
Enter result (1 = pass, 2 = fail): 1
Passed 9
Failed 1 96
Raise tuition

You might also like