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

Introduction to C++ programming

Shaobai Kan
chapter 4 (Continue...)

Introduction to C++ programming p. 1/30

Outline

Introduction to C++ programming p. 2/30

Outline

Exercise

Introduction to C++ programming p. 2/30

Outline

Exercise oat & double variables

Introduction to C++ programming p. 2/30

Outline

Exercise oat & double variables switch statement

Introduction to C++ programming p. 2/30

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

Introduction to C++ programming p. 4/30

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

Introduction to C++ programming p. 5/30

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. 6/30

oat / double type variables

Introduction to C++ programming p. 7/30

oat/double variable

name, e.g. a

float / double type, e.g. 1.45

location, e.g. 103

Introduction to C++ programming p. 8/30

oat v.s. double


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

Introduction to C++ programming p. 9/30

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

Introduction to C++ programming p. 10/30

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;

Introduction to C++ programming p. 11/30

Updated version of source code for exercise 2.20

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. 12/30

Output for exercise 2.20

Enter the radius: 3 diameter: 6 circumference: 18.8495 area: 28.2743

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?

Stream manipulator - xed nonparameterized manipulator - setprecision( ) parameterized manipulator

Introduction to C++ programming p. 14/30

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

Introduction to C++ programming p. 15/30

Updated version of source code for exercise 2.20

header file

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

Introduction to C++ programming p. 16/30

Output

Enter the radius: 3 diameter: 6.00 circumference: 18.85 area: 28.27

Introduction to C++ programming p. 17/30

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

range: 1.7 10308 1.7 10308

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

Introduction to C++ programming p. 19/30

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

Introduction to C++ programming p. 20/30

Output for exercise 3.34 (a)

Enter the integer (n): 4 n!=24

Introduction to C++ programming p. 21/30

Output for exercise 3.34 (a)

Enter the integer (n): 44 n!=0

Introduction to C++ programming p. 22/30

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. 23/30

switch multiple-selection statement

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"

Introduction to C++ programming p. 26/30

UML activity diagram for example 1

Print grade

grade

other A B C D F

Print 90-100

Print 80-89

Print 70-79

Print 60-69

Print 0-59

Print error

Introduction to C++ programming p. 27/30

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)

Introduction to C++ programming p. 30/30