Вы находитесь на странице: 1из 37

Introduction to C++ programming

Shaobai Kan
chapter 4 (Continue...)

Outline

Outline

Exercise

Outline

Outline

Exercise

Introduction to C++ programming p. 3/30

Example
Exercise 1. Write a program that input students grades, calculate average grades, and print the average grades for each section.
Section 1. 86 67 54 28 75 64

93 30

100 34

53 76 67

Section 2. 66 67 54 44 67 29 72 46 54

85 34 99

C++ code for exercise 1

# include <iostream> using std::cout; using std::endl; using std::cin; int main( )

{
int total = 0 ; int counter = 0 ; int grade, average ; bug-free cout << Enter the grade or -1 to quit: ; cin >> grade ; while ( grade != -1 ) // -1 is the sentinel value { total = total + grade ; counter ++ ; cout << Enter the grade or -1 to quit ; cin >> grade ; }

C++ code for exercise 1

if (counter != 0) { average = total / counter; cout << Class average is << average << endl; } else cout << No grade were entered << endl;

return 0; }

Introduction to C++ programming p. 7/30

oat/double variable

name, e.g. a

oat v.s. double

oat & double variables. - common. Both hold the oating-point numbers, e.g. 3.141, -2.1, 1.0.

oat v.s. double

oat & double variables. - common. Both hold the oating-point numbers, e.g. 3.141, -2.1, 1.0. - difference.
oat. single-precision oating-point numbers

range: 3.4 1038 3.4 1038

double. double-precision oating-point numbers

range: 1.7 10308 1.7 10308 - double is preferred to oat.

Introduction to C++ programming p. 9/30

Exercise
Exercise 2.20. Write a program that reads in the radius of a circle as an integer and prints the circles diameter, circumference and area. Use the constant value 3.14159 for .

Introduction to C++ programming p. 10/30

Exercise
Exercise 2.20. Write a program that reads in the radius of a circle as an integer and prints the circles diameter, circumference and area. Use the constant value 3.14159 for . Formula. diameter = 2 radius circumf erence = 2 radius area = radius2

Source code for exercise 2.20

# include <iostream> using std::cout; using std::endl; using std::cin; int main( )

{
int r ; // r- radius cout << Enter the radius: ; cin >> r ; cout << diameter: << 2 * r << endl; cout << circumference: << 2 * 3.14159 * r << endl; cout << area: << 3.14159 * r * r << endl; return 0;

std::

# include <iostream> using namespace std; int main( )

{
int r ; double d, c, a ; // d - diameter, c - circumference, a - area

cout << Enter the radius: ; cin >> r ; d=2*r; c = 2 * 3.14159 * r ; a = 3.14159 * r * r ; cout << diameter: << d << endl; cout << circumference: << c << endl; cout << area: << a << endl; return 0;

Introduction to C++ programming p. 13/30

Stream manipulator
Question. How to manipulate the output form of a oating-point number? e.g. rounded to the nearest hundredth?

Introduction to C++ programming p. 14/30

Stream manipulator
Question. How to manipulate the output form of a oating-point number? e.g. rounded to the nearest hundredth?

Output form of numbers

form of numbers.

oating-point form (xed) e.g. 12.98, 0.0001 Scientic form e.g. 1e 4, 2.6e + 5

# include <iostream> # include <iomanip> using namespace std; int main( )

{
int r ; double d, c, a ; // d - diameter, c - circumference, a - area

cout << Enter the radius: ; cin >> r ; d=2*r; c = 2 * 3.14159 * r ; a = 3.14159 * r * r ;
manipulators

cout << fixed << setprecision(2) ; cout << diameter: << d << endl; cout << circumference: << c << endl; cout << area: << a << endl; return 0;

Output

int, oat and double

int, oat & double variables.
int. hold integer values

range: 2147483684 2147483647

oat. hold real values

range: 3.4 1038 3.4 1038

double. hold real values

Introduction to C++ programming p. 18/30

Exercise
Exercise 3.34 (a). Write a program that reads a nonnegative integer and computes and prints its factorial.

Introduction to C++ programming p. 19/30

Exercise
Exercise 3.34 (a). Write a program that reads a nonnegative integer and computes and prints its factorial. Denition. n factorial n! = n (n 1) (n 2) . . . 2 1 e.g. 5! = 5 4 3 2 1 = 120

Incorrect source code for exercise 3.34 (a)

Incorrect source code # include <iostream> using namespace std; int main( )

{
int n; int factorial =1; cout << Enter the integer (n): ; cin >> n ; for ( int i = n ; i >= 1 ; i --) factorial = factorial * i; cout << n!= << factorial << endl; return 0;

// factorial *= i

Correct source code for exercise 3.34 (a)

Correct source code # include <iostream> # include <iomanip> using namespace std; int main( )

{
double variable

int n; double factorial =1; cout << Enter the integer (n): ; cin >> n ; for ( int i = n ; i >= 1 ; i --) factorial = factorial * i;

// factorial *= i

integer output

cout << fixed << setprecision(0) ; cout << n!= << factorial << endl; return 0;

Introduction to C++ programming p. 24/30

switch statement

syntax of switch statement switch ( expression ) { case expression1: executable statement; break; case expression2: executable statement; break; . . . case expressionN: executable statement; break; default : executable statement; break; }

Introduction to C++ programming p. 25/30

Example
Example. Write a program that reads in a grade value and performs the following actions: A: print "90-100" B: print "80-89" C: print "70-79" D: print "60-69" F: print "0-59" other character: print "error"

other A B C D F

Print 90-100

Print 80-89

Print 70-79

Print 60-69

Print 0-59

Print error

C++ source code for example 1

# include <iostream> using namespace std; int main( ) { char grade; cout << Enter the grade (A - Z): ; cin >> grade ; switch ( grade ) { case A : cout << 90-100\n ; break; case B : cout << 80-89\n ; break; case C : cout << 70-79\n ; break; case D : cout << 60-69\n ; break; case F : cout << 0-60\n ; break; default: cout << error ; break; } return 0; }

Introduction to C++ programming p. 28/30

Exercise 2
Exercise 2. Write a program (using switch statement)
that input a students grade (0-100) from the keyboard and prints the corresponding scale (e.g. A, B, C, D, F) according to the following rule
90-100 80-89 70-79 60-69 0-59

A B C D F
Introduction to C++ programming p. 29/30

Homework:
Read Sec. 4.6 Exercise 2 (in this slide)