+ All Categories
Home > Documents > 1 Last of the basics Controlling output Overflow and underflow Standard function libraries Potential...

1 Last of the basics Controlling output Overflow and underflow Standard function libraries Potential...

Date post: 31-Dec-2015
Category:
Upload: rhoda-cox
View: 219 times
Download: 1 times
Share this document with a friend
Popular Tags:
45
1 Last of the basics Controlling output Overflow and underflow Standard function libraries Potential pitfalls of getting information with cin Type casting
Transcript
Page 1: 1 Last of the basics Controlling output Overflow and underflow Standard function libraries Potential pitfalls of getting information with cin Type casting.

1

Last of the basics

Controlling outputOverflow and underflowStandard function librariesPotential pitfalls of getting information with cinType casting

Page 2: 1 Last of the basics Controlling output Overflow and underflow Standard function libraries Potential pitfalls of getting information with cin Type casting.

2

Formatting Output

Escape Sequences \t, \n +othersiomanip.h

setw() setiosflags(…) setprecision()

Page 3: 1 Last of the basics Controlling output Overflow and underflow Standard function libraries Potential pitfalls of getting information with cin Type casting.

3

Escape SequencesA simple tablecout << “Name\tTotal\tGrade\n”;cout << “Miyo\t186\t B\n”;cout << “\n Jake\t211\t A\n”;cout << “Syd\t203\t A\n”;

OUTPUTName Total GradeMiyo 186 B

Jake 211 ASyd 203 A_

\n gives a blank line

\n gives a blank line

Page 4: 1 Last of the basics Controlling output Overflow and underflow Standard function libraries Potential pitfalls of getting information with cin Type casting.

4

Escape Sequences

\ Changes the meaning of the character that follows it.

\” means take quotes literally

cout << “Al \”Scarface\” Capone”;

displays Al ”Scarface” Capone

Page 5: 1 Last of the basics Controlling output Overflow and underflow Standard function libraries Potential pitfalls of getting information with cin Type casting.

5

Escape Sequence Combinationscout << “\n Al\n\”Scarface\”\nCapone”displaysAl“Scarface”Capone

Page 6: 1 Last of the basics Controlling output Overflow and underflow Standard function libraries Potential pitfalls of getting information with cin Type casting.

6

Formatting Output

iomanip.h contains the objects which have special effects on the iostream.

*

#include <iostream.h>#include <iomanip.h>

Page 7: 1 Last of the basics Controlling output Overflow and underflow Standard function libraries Potential pitfalls of getting information with cin Type casting.

7

setw(n) how many columns for datasetprecision(n) sets number of decimalssetiosflags(ios::fixed) displays 6 digits after the decimal

Formatting Output

iomanip.h contains the objects which havespecial effects on the iostream...

Page 8: 1 Last of the basics Controlling output Overflow and underflow Standard function libraries Potential pitfalls of getting information with cin Type casting.

8

Formatting Output

setw(n) how many columns for data(ans = 33, num = 7132)

cout << setw(4) << ans cout << setw(1) << ans << setw(5) << num << setw(3) << num << setw(4) << “Hi”; << setw(3) << “Hi”;

fields expand

337132Hi 337132Hi

Page 9: 1 Last of the basics Controlling output Overflow and underflow Standard function libraries Potential pitfalls of getting information with cin Type casting.

9

Formatting Output

setprecision(n) sets number of decimals

x format result314.0 setw(10)

setprecision(2)314.00

point counts

314.0 setw(10)setprecision(5) 314.00000

columns added

314.0 setw(7)setprecision(5) 314.00000

Page 10: 1 Last of the basics Controlling output Overflow and underflow Standard function libraries Potential pitfalls of getting information with cin Type casting.

10

Formatting Output example

#include<iostream.h>#include<iomanip.h>

int main() {double v = 0.00123456789;double w = 1.23456789;double x = 12.3456789;double y = 1234.56789;double z = 12345.6789;cout <<v<< “\n”<<w<< “\n”<<x<< “\n”<<y<< “\n”<<z<<”\n\n”;cout << setiosflags(ios::fixed);cout <<v<< “\n”<<w<< “\n”<<x<< “\n”<<y<< “\n”<<z<<”\n\n”;cout << setprecision(2);cout <<v<< “\n”<<w<< “\n”<<x<< “\n”<<y<< “\n”<<z<<”\n\n”;return 0;

}

setiosflags(ios::fixed) displays 6 digits after the decimal

Page 11: 1 Last of the basics Controlling output Overflow and underflow Standard function libraries Potential pitfalls of getting information with cin Type casting.

Formatting Output

default

0.00123457 1.2345712.34571234.5712345.7

ios::fixed

0.001235

1.234568

12.345679

1234.567890

12345.678900

+ setprecision(2)

0.00

1.23

12.35

1234.57

12345.68

Page 12: 1 Last of the basics Controlling output Overflow and underflow Standard function libraries Potential pitfalls of getting information with cin Type casting.

Formatting Output

For decimal alignment use:For decimal alignment use:

ios::fixed and setprecision( ) sitckysitcky

setw( ) needed each timeneeded each time

cout << setiosflags(ios::fixed) << setprecision(n);cout << setiosflags(ios::fixed) << setprecision(n);

cout << “- -” << setw(n) << var1 << setw(n) << “- -”;cout << “- -” << setw(n) << var1 << setw(n) << “- -”;

cout << “- -\t” << setw(n) << var1 << setw(n) <<var2;cout << “- -\t” << setw(n) << var1 << setw(n) <<var2;

Page 13: 1 Last of the basics Controlling output Overflow and underflow Standard function libraries Potential pitfalls of getting information with cin Type casting.

13

Overflow and underflowOverflow means that the result is too large to

be represented in an objects type.

Underflow Result is too small to be represented

by object.

Demonstrate with example Chris using debugger.

Page 14: 1 Last of the basics Controlling output Overflow and underflow Standard function libraries Potential pitfalls of getting information with cin Type casting.

14

#include <iostream.h>#include <math.h>int main() {

int x = 1000;cout << x << endl;x = x*x;cout << x << endl;x = x*x;cout << x << endl;x = x*x;cout << x << endl;x = x*x;cout << x << endl;x = x*x;return 0;

}

Example of integer overflow

Page 15: 1 Last of the basics Controlling output Overflow and underflow Standard function libraries Potential pitfalls of getting information with cin Type casting.

15

#include <iostream.h>#include <math.h>int main() {

float x = 1.0;cout << x << endl;x = x/1e10;cout << x << endl;x = x/1e10;cout << x << endl;x = x/1e10;cout << x << endl;x = x/1e10;cout << x << endl;x = x/1e10;cout << x << endl;return 0;

}

Example of integer underflow

Page 16: 1 Last of the basics Controlling output Overflow and underflow Standard function libraries Potential pitfalls of getting information with cin Type casting.

16

Math Library FunctionsPre-written functions for math'sPortabilityNeeds #include <math.h><Return type> FunctionName(param list)You need to know

What their identifiers (names) are? What is their purpose? Power, sqrt, sin, cos, exp etc Data type of return value and parameters How they blow up?

Use help F1 for usage guide

Page 17: 1 Last of the basics Controlling output Overflow and underflow Standard function libraries Potential pitfalls of getting information with cin Type casting.

17

double sqrt(double n)Calculates and returns square root of n. Warning! n must be positive when in doubt

use with fabs(n) -> sqrt(fabs(n)); Can use any number data (will promote) Normal usage :double question = 45.35, double answer; answer = sqrt( question ); :

Page 18: 1 Last of the basics Controlling output Overflow and underflow Standard function libraries Potential pitfalls of getting information with cin Type casting.

18

double pow(double n, double b)

Calculates and returns n to the power b. Warning! Can overflow or underflow! Can blow up (if n = 0.0 and b < 0) Can use any number data (will promote) Normal usage:double question = 3.0, double answer; answer = pow(question , 4.0); //raise to power 4:

Page 19: 1 Last of the basics Controlling output Overflow and underflow Standard function libraries Potential pitfalls of getting information with cin Type casting.

19

double fabs(double n)Calculates the absolute value of the floating-point argument. Will promote integers Normal usage:double question = -3.0, double answer; answer = fabs(question ); //answer = 3.0:

Page 20: 1 Last of the basics Controlling output Overflow and underflow Standard function libraries Potential pitfalls of getting information with cin Type casting.

20

Trigonometric functionsdouble cos(double n), double sin(double n), double tan(double n)

n is in radians360 degrees equals 2 radiansNeed to convert angles90 degrees = /2 radians45 degrees = /4 radiansn degrees = n* /180 radians

Page 21: 1 Last of the basics Controlling output Overflow and underflow Standard function libraries Potential pitfalls of getting information with cin Type casting.

21

Inverse Trigonometric functions double acos(double n)double asin(double n)double atan(double n)

n is strictly between –1 and 1 Returns angle in radiansIf you need to convert angles to degrees radians = 180 degrees /2 radians = 90 degreesn radians = n*180/ degrees

Page 22: 1 Last of the basics Controlling output Overflow and underflow Standard function libraries Potential pitfalls of getting information with cin Type casting.

22

double log10(double n)double log(double n)double exp(double n)

log10(n) returns the logarithm to base 10 Log10(20) = 1.3013 Pow(10,1.3013) = 20

log(1) = 0; natural logarithm (statistics, radioactive decay population) exp(0) = 1; inverse (ex where e = 2.7182)

Page 23: 1 Last of the basics Controlling output Overflow and underflow Standard function libraries Potential pitfalls of getting information with cin Type casting.

23

Math Library Functions

nested functions

sqrt( pow ( fabs (-4), 3) ) =sqrt( pow ( 4.0 , 3) ) =sqrt( 64.0 ) = 8.0

Page 24: 1 Last of the basics Controlling output Overflow and underflow Standard function libraries Potential pitfalls of getting information with cin Type casting.

24

Math Function Example

Mathematical notation5.5 e .02(year-1900)

C++ notation5.5 * exp(0.02*(year-1990))

Page 25: 1 Last of the basics Controlling output Overflow and underflow Standard function libraries Potential pitfalls of getting information with cin Type casting.

25

Type Coercion

The implicit (automatic) conversion of a value from one data type to another.

someDouble = 42; is stored as 42.0

someInt = 11.9; is stored as 11

*

Page 26: 1 Last of the basics Controlling output Overflow and underflow Standard function libraries Potential pitfalls of getting information with cin Type casting.

26

Type CastingAutomatic typecasts mentioned before In arithmetic expressions In passing parameters to library functions

Can be made explicit Stop some of those nagging warning messages To deliberately remove fractional information

Syntax: data_type (expression)

int (5.34 * 1.68)

int (8.9712)This returns a value of 8.

Page 27: 1 Last of the basics Controlling output Overflow and underflow Standard function libraries Potential pitfalls of getting information with cin Type casting.

27

Type Casting with assignments

someInt = someDouble - 8.2;

someInt = int(someDouble - 8.2);

These are identical statements.These are identical statements.

Page 28: 1 Last of the basics Controlling output Overflow and underflow Standard function libraries Potential pitfalls of getting information with cin Type casting.

28

Pitfalls of getting information into the computer

cin >> my_num;

The keyboard entry is stored into variable called my_num.

Page 29: 1 Last of the basics Controlling output Overflow and underflow Standard function libraries Potential pitfalls of getting information with cin Type casting.

29

cin chains

Syntax: cin >> var1 >> var2 >> ...White space or illegible character is used as a terminator. White space can be an newline.Input continues from point of terminationcin >> first >> last >> my_num;

Page 30: 1 Last of the basics Controlling output Overflow and underflow Standard function libraries Potential pitfalls of getting information with cin Type casting.

30

cin chain example

int num1, num2, num3;double average;

cout << "Enter three integer numbers: ";cin >> num1 >> num2 >> num3;

average = (num1 + num2 + num3) / 3.0;

cout << "The average is " << average;cout << '\n';

Needed to force floating

Point calculation

Page 31: 1 Last of the basics Controlling output Overflow and underflow Standard function libraries Potential pitfalls of getting information with cin Type casting.

31

cin ExampleOutput:

3 5 5The average is 4.33333

OR

Output:

355The average is 4.33333

Page 32: 1 Last of the basics Controlling output Overflow and underflow Standard function libraries Potential pitfalls of getting information with cin Type casting.

32

cin Example 2 automatic promotion

double radius, circumference;const double pi = 3.1416;

cout << "Enter the radius of a circle: ";cin >> radius;

circumference = 2 * pi * radius;cout << "The circumference of a circle of radius " << radius << " is " << circumference <<‘\n’;

Page 33: 1 Last of the basics Controlling output Overflow and underflow Standard function libraries Potential pitfalls of getting information with cin Type casting.

33

cin promotion

Automatic promotionOutput:

Enter the radius of a circle: 14The circum ... circle of radius 14 is

87.9648

Page 34: 1 Last of the basics Controlling output Overflow and underflow Standard function libraries Potential pitfalls of getting information with cin Type casting.

34

cin cautionary example

int num1,double num2 num3;

cout <<"Enter three numbers: ";cin >> num1 >> num2 >> num3;

cout << num1 << ‘\t’<< num2 << ‘\t’ << num3 << endl;

Page 35: 1 Last of the basics Controlling output Overflow and underflow Standard function libraries Potential pitfalls of getting information with cin Type casting.

35

cin Example 3

Output:

Enter three numbers :12.8 9.9 8.9

12 0.8 9.9

8.9 is still waiting to be processed!

Do next example Chris

Page 36: 1 Last of the basics Controlling output Overflow and underflow Standard function libraries Potential pitfalls of getting information with cin Type casting.

36

#include <iostream.h>

int main() {

int i=1;

int num1;

float num2, num3, num4;

cout << "enter three numbers : ";

cin >> num1 >> num2 >> num3;

cout << num1 << '\t' << num2 << '\t'<< num3 << endl;

cin >> num4;

cout << num4 << endl;

return 0;

}

Page 37: 1 Last of the basics Controlling output Overflow and underflow Standard function libraries Potential pitfalls of getting information with cin Type casting.

37

Measures of program quality

Minimum criteria Program should work.

Besides simply working, a program quality can be measured by how whether it is, Clear Robust Efficient Programming in the large Reusable Extensible

Page 38: 1 Last of the basics Controlling output Overflow and underflow Standard function libraries Potential pitfalls of getting information with cin Type casting.

38

Clarity

From programmers point of view (good documentation)From a users point of view, program clearly identifies what the inputs are, and what exactly the outputs are.

Page 39: 1 Last of the basics Controlling output Overflow and underflow Standard function libraries Potential pitfalls of getting information with cin Type casting.

39

Robustness

Program keeps working even with incorrect data.

Page 40: 1 Last of the basics Controlling output Overflow and underflow Standard function libraries Potential pitfalls of getting information with cin Type casting.

40

Efficiency

The program produces its result in a time efficient manner.The program produces its result in a memory efficient manner

Page 41: 1 Last of the basics Controlling output Overflow and underflow Standard function libraries Potential pitfalls of getting information with cin Type casting.

41

Programming in the large

Can the program be written using teams of programmers?

Page 42: 1 Last of the basics Controlling output Overflow and underflow Standard function libraries Potential pitfalls of getting information with cin Type casting.

42

Extensibility

The program is easy to modify and extend functionality

Page 43: 1 Last of the basics Controlling output Overflow and underflow Standard function libraries Potential pitfalls of getting information with cin Type casting.

43

Reusability

It is easy to reuse existing code, both within the current project or for a new project.

Page 44: 1 Last of the basics Controlling output Overflow and underflow Standard function libraries Potential pitfalls of getting information with cin Type casting.

44

Procedural vs. OO programming

Procedural programming capable of Clarity Robustness Efficiency Programming in the large

Less well suited for Extensibility Reusability

OO programming designed with Extensibility and Reusability in mind

Page 45: 1 Last of the basics Controlling output Overflow and underflow Standard function libraries Potential pitfalls of getting information with cin Type casting.

45

Learning Object Oriented Technologies

Can take many years to learn all.We will be covering first stage on Object oriented programming if we have time.


Recommended