numeric types, expressions, and output robert reaves

16
Numeric Types, Expressions, and Output ROBERT REAVES

Upload: geraldine-short

Post on 17-Dec-2015

250 views

Category:

Documents


4 download

TRANSCRIPT

Page 1: Numeric Types, Expressions, and Output ROBERT REAVES

Numeric Types, Expressions, and OutputROBERT REAVES

Page 2: 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.

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

Page 3: Numeric Types, Expressions, and Output ROBERT REAVES

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

Page 4: Numeric Types, Expressions, and Output ROBERT REAVES

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

Page 5: Numeric Types, Expressions, and Output ROBERT REAVES

What is a value-returning function?

A function that returns a single value to its caller and is invoked from within an expression.

Page 6: Numeric Types, Expressions, and Output ROBERT REAVES

Void Functions

void myFunc( . . .) {

.

.

}

Notice how it begins with the word void instead of a data type like int or 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?

Page 7: Numeric Types, Expressions, and Output ROBERT REAVES

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

Page 8: Numeric Types, Expressions, and Output ROBERT REAVES

Header Files

endl, fixed, and showpoint are including inside the iostream header file to perform I/O. #include <iostream>

setw and setprecision are inside the iomanip header file. #include <iomanip>

Page 9: Numeric Types, Expressions, and Output ROBERT REAVES

Setw

setw means “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.

Page 10: Numeric Types, Expressions, and Output ROBERT REAVES

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.

Page 11: Numeric Types, Expressions, and Output ROBERT REAVES

Showpoint

showpoint forces 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?

Page 12: Numeric Types, Expressions, and Output ROBERT REAVES

setprecision

setprecision specifies the desired number of decimal places. REMAINS IN EFFECT for all subsequent output.

Should use setprecision and the fixed together to get correct results.

Page 13: Numeric Types, Expressions, and Output ROBERT REAVES

Additional String Operations

.at()

Included instead to the cctype header file; #include <cctype>

toupper()

tolower()

Page 14: Numeric Types, Expressions, and Output ROBERT REAVES

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

Page 15: Numeric Types, Expressions, and Output ROBERT REAVES

toupper

toupper(ch) returns the uppercase equivalent of ch, if ch is a lowercase character; ch, otherwise.

Ex. toupper(‘r’) returns ‘R’

toupper(‘R’) returns ‘R’

Page 16: Numeric Types, Expressions, and Output ROBERT REAVES

tolower

tolower(ch) returns the lowercase equivalent of ch, if ch is an uppercase letter; ch, otherwise.

Ex. tolower(‘R’) returns ‘r’

tolower(‘r’) returns ‘r’