1 / 45

Last of the basics

Last of the basics. Controlling output Overflow and underflow Standard function libraries Potential pitfalls of getting information with cin Type casting. Formatting Output. Escape Sequences t, <br> +others iomanip.h setw() setiosflags(…) setprecision(). <br> gives a blank line.

rumer
Télécharger la présentation

Last of the basics

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  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 +others • iomanip.h • setw() • setiosflags(…) • setprecision()

  3. \n gives a blank line 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 Name Total Grade Miyo 186 B Jake 211 A Syd 203 A_ \n gives a blank line

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

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

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

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

  8. Formatting Outputsetw(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”; ¨o33¨7132¨oHi 337132¨Hi fields expand

  9. x format result314.0 setw(10) setprecision(2) ¨ooo314.00 point counts 314.0 setw(7) setprecision(5) 314.00000 columns added Formatting Outputsetprecision(n) sets number of decimals 314.0setw(10) setprecision(5)¨314.00000

  10. Formatting Output example setiosflags(ios::fixed) displays 6 digits after the decimal #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; }

  11. Formatting Output ios::fixed 0.001235 1.234568 12.345679 1234.567890 12345.678900 + setprecision(2) 0.00 1.23 12.35 1234.57 12345.68 default 0.00123457 1.23457 12.3457 1234.57 12345.7

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

  13. Overflow and underflow • Overflow • 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.

  14. Example of integer overflow #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; }

  15. Example of integer underflow #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; }

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

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

  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 :

  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 :

  20. Trigonometric functionsdouble 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

  21. Inverse Trigonometric functionsdouble 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

  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)

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

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

  25. Type Coercion • The implicit (automatic) conversion of a value from one data type to another. someDouble = 42; is stored as 42.0someInt = 11.9; is stored as 11 *

  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.

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

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

  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;

  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

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

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

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

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

  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

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

  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

  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.

  39. Robustness • Program keeps working even with incorrect data.

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

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

  42. Extensibility • The program is easy to modify and extend functionality

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

  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

  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.

More Related