Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "1 Last of the basics Controlling output Overflow and underflow Standard function libraries Potential pitfalls of getting information with cin Type casting."— Presentation transcript:

1

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

3 2 Formatting Output Escape Sequences \t, \n +others iomanip.h  setw()  setiosflags(…)  setprecision()

4 3 Escape Sequences A simple table cout << “Name\tTotal\tGrade\n”; cout << “Miyo\t186\t B\n”; cout << “\n Jake\t211\t A\n”; cout << “Syd\t203\t A\n”; OUTPUT NameTotalGrade Miyo186 B Jake211 A Syd203 A _ \n gives a blank line

5 4 Escape Sequences \ Changes the meaning of the character that follows it. \” means take quotes literally cout << “Al \”Scarface\” Capone”; displays Al ”Scarface” Capone

6 5 Escape Sequence Combinations cout << “\n Al\n\”Scarface\”\nCapone” displays Al “Scarface” Capone

7 6 Formatting Output iomanip.h contains the objects which have special effects on the iostream. * #include #include

8 7 setw(n) how many columns for data setprecision(n) sets number of decimals setiosflags(ios::fixed) displays 6 digits after the decimal Formatting Output iomanip.h contains the objects which have special effects on the iostream...

9 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  33  7132  Hi 337132  Hi

10 9 Formatting Output setprecision(n)sets number of decimals x format result 314.0setw(10) setprecision(2)  314.00 point counts 314.0setw(10) setprecision(5)  314.00000 columns added 314.0setw(7) setprecision(5) 314.00000

11 10 Formatting Output example #include 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

12 Formatting Output default 0.00123457 1.23457 12.3457 1234.57 12345.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

13 Formatting Output For decimal alignment use: sitcky ios::fixed and setprecision( ) sitcky needed each time setw( )needed each time cout << setiosflags(ios::fixed) << setprecision(n);  cout << “- -” << setw(n) << var1 << setw(n) << “- -”; cout << “- -\t” << setw(n) << var1 << setw(n) <<var2;

14 13 Overflow and underflow Overflow means that the r esult 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.

15 14 #include 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

16 15 #include 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

17 16 Math Library Functions Pre-written functions for math's Portability Needs #include 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

18 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 ); :

19 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 :

20 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 :

21 20 Trigonometric functions double cos(double n), double sin(double n), double tan(double n) n is in radians 360 degrees equals 2  radians Need to convert angles 90 degrees =  /2 radians 45 degrees =  /4 radians n degrees = n*  /180 radians

22 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 radians If you need to convert angles to degrees  radians = 180 degrees  /2 radians = 90 degrees n radians = n*180/  degrees

23 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 (e x where e = 2.7182)

24 23 Math Library Functions nested functions sqrt( pow ( fabs (-4), 3) ) = sqrt( pow ( 4.0, 3) ) = sqrt( 64.0 ) = 8.0

25 24 Math Function Example Mathematical notation 5.5 e.02(year-1900) C++ notation 5.5 * exp(0.02*(year-1990))

26 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 *

27 26 Type Casting Automatic 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.

28 27 Type Casting with assignments someInt = someDouble - 8.2; someInt = int(someDouble - 8.2); These are identical statements.

29 28 Pitfalls of getting information into the computer cin >> my_num; The keyboard entry is stored into variable called my_num.

30 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 termination cin >> first >> last >> my_num;

31 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

32 31 cin Example Output: 3 5 5 The average is 4.33333 OR Output: 3 5 The average is 4.33333

33 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’;

34 33 cin promotion Automatic promotion Output: Enter the radius of a circle: 14 The circum... circle of radius 14 is 87.9648

35 34 cin cautionary example int num1, double num2 num3; cout <<"Enter three numbers: "; cin >> num1 >> num2 >> num3; cout << num1 << ‘\t’<< num2 << ‘\t’ << num3 << endl;

36 35 cin Example 3 Output: Enter three numbers :12.8 9.9 8.9 120.8 9.9 8.9 is still waiting to be processed! Do next example Chris

37 36 #include 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; }

38 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

39 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.

40 39 Robustness Program keeps working even with incorrect data.

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

42 41 Programming in the large Can the program be written using teams of programmers?

43 42 Extensibility The program is easy to modify and extend functionality

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

45 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

46 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.


Download ppt "1 Last of the basics Controlling output Overflow and underflow Standard function libraries Potential pitfalls of getting information with cin Type casting."

Similar presentations


Ads by Google