450 likes | 560 Views
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.
E N D
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, \n +others • iomanip.h • setw() • setiosflags(…) • setprecision()
\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
Escape Sequences \ Changes the meaning of the characterthat follows it. \” means take quotes literally cout << “Al \”Scarface\” Capone”; displays Al ”Scarface” Capone
Escape Sequence Combinations cout << “\n Al\n\”Scarface\”\nCapone”displays Al“Scarface”Capone
Formatting Output • iomanip.h contains the objects which have special effects on the iostream. #include <iostream.h>#include <iomanip.h> *
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
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
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
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; }
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
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;
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.
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; }
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; }
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
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 ); :
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 :
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 :
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
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
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)
Math Library Functions nested functions sqrt( pow( fabs (-4), 3) ) = sqrt( pow( 4.0 , 3) ) = sqrt( 64.0 ) = 8.0
Math Function Example Mathematical notation 5.5 e .02(year-1900) C++ notation 5.5 * exp(0.02*(year-1990))
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 *
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.
Type Casting with assignments • someInt = someDouble - 8.2; • someInt = int(someDouble - 8.2); • These are identical statements.
Pitfalls of getting information into the computer cin >> my_num; The keyboard entry is stored into variable called my_num.
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;
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
cin Example Output: 3 5 5 The average is 4.33333 OR Output: 3 5 5 The average is 4.33333
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’;
cin promotion Automatic promotion Output: Enter the radius of a circle: 14 The circum ... circle of radius 14 is 87.9648
cin cautionary example int num1, double num2 num3; cout <<"Enter three numbers: "; cin >> num1 >> num2 >> num3; cout << num1 << ‘\t’<< num2 << ‘\t’ << num3 << endl;
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
#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; }
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
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.
Robustness • Program keeps working even with incorrect data.
Efficiency • The program produces its result in a time efficient manner. • The program produces its result in a memory efficient manner
Programming in the large • Can the program be written using teams of programmers?
Extensibility • The program is easy to modify and extend functionality
Reusability • It is easy to reuse existing code, both within the current project or for a new project.
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
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.