1 / 16

Numeric Types, Expressions, and Output

Numeric Types, Expressions, and Output. Robert Reaves. Compound Arithmetic Expressions. Arithmetic expressions can be made up of many constants, variables, operators, and parentheses. Precedence Level (highest  lowest) Unary +, Unary - *, /, % +, - ( … ) are always evaluated first.

ariane
Download Presentation

Numeric Types, Expressions, and Output

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. Numeric Types, Expressions, and Output Robert Reaves

  2. Compound Arithmetic Expressions • Arithmetic expressions can be made up of many constants, variables, operators, and parentheses. • Precedence Level (highest  lowest) • Unary +, Unary - • *, /, % • +, - • (…) are always evaluated first. • Ex. • 10 / 2 * 3 = 15 • 10 % 3 – 4 / 2 = -1 • 5.0 * 2.0 / 4.0 * 2.0 = 5.0 • 5.0 * 2.0 / (4.0 * 2.0) =1.25

  3. Type Coercion and Type Casting • Type Coercion is the implicit (automatic) conversion of a value from one data type to another. • Ex. • float someFloat = 12; • Computer inserts extra machine language instructions to convert to 12.0. • Type Casting is the explicit conversion of a value from one data type to another, also called type conversion. • A C++ cast operation consists of a data type name and then, within parentheses, the expression to be converted. • Ex. • float someFloat = float(3 * someInt * 2);

  4. Arithmetic Expressions • Possible to mix data types within an expression, this is called mixed type expression or mixed mode expression. • Whenever an integer value and floating-point value are joined by an operator, implicit type coercion occurs: • Integer value is temporarily coerced to a floating-point value • Operation is performed • Result is a floating-point value

  5. What is a value-returning function? • A function that returns a single value to its caller and is invoked from within an expression.

  6. Void Functions • void myFunc( . . .) { . . } • Notice how it begins with the word void instead of a data type like intor float. • Void Function( (procedure) is a function that does not return a function value to its caller and is invoked as a separate statement. • What do you mean by a separate statement?

  7. Formatting Output • What this means is to control how output appears visually on the screen or on a printout. • The C++ standard library supplies many manipulators, but we will look at only five of them: • endl • setw • fixed • showpoint • setprecision

  8. Header Files • endl, fixed, and showpoint are including inside the iostreamheader file to perform I/O. • #include <iostream> • setw and setprecision are inside the iomanipheader file. • #include <iomanip>

  9. Setw • setwmeans “set width”, it lets us control how many character positions the next data item should occupy when it is output. • Cout << setw(4) << “Hi” << endl; • Output (_ means blank) _ _ Hi • If number of characters to output is less then that amount of characters in the output the field will automatically expand to fit the output.

  10. Fixed • What happens when we use floating-point values and setw? • Take the value 4.85, it takes four output positions to print this to the screen. • Another problem with float-point values is that large values are printed in Scientific notation. • 123456789.5 may print as 1.23457e+08 on some systems. • Can use fixed to force all subsequent floating-point output to appear in decimal form instead of scientific notation. • cout << fixed << 3.8 * x; • However, if the number is whole number it will not print as a floating-point number.

  11. Showpoint • showpointforces decimal points to be displayed in subsequent floating-point output, even for whole numbers. • cout << showpoint << someFloat; • What if we want just two decimal places?

  12. setprecision • setprecisionspecifies the desired number of decimal places. • REMAINS IN EFFECT for all subsequent output. • Should use setprecisionand the fixed together to get correct results.

  13. Additional String Operations • .at() • Included instead to the cctypeheader file; • #include <cctype> • toupper() • tolower()

  14. At function • At function allows for individual character access in a string. • str1.at(pos); • pos being the character position you want to access. • This returns the character at that location within the string. • string str1 = “Robert”; • char letter = str1.at(0); • The variable letter now is holding the character ‘R’;

  15. toupper • toupper(ch) returns the uppercase equivalent of ch, if chis a lowercase character; ch, otherwise. • Ex. • toupper(‘r’) returns ‘R’ • toupper(‘R’) returns ‘R’

  16. tolower • tolower(ch) returns the lowercase equivalent of ch, if chis an uppercase letter; ch, otherwise. • Ex. • tolower(‘R’) returns ‘r’ • tolower(‘r’) returns ‘r’

More Related