Presentation is loading. Please wait.

Presentation is loading. Please wait.

Numeric Types, Expressions, and Output ROBERT REAVES.

Similar presentations


Presentation on theme: "Numeric Types, Expressions, and Output ROBERT REAVES."— 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 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?

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 iostream header file to perform I/O.  #include  setw and setprecision are inside the iomanip header file.  #include

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

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  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?

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

13 Additional String Operations .at()  Included instead to the cctype header file;  #include  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 ch is 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 ch is an uppercase letter; ch, otherwise.  Ex.  tolower(‘R’) returns ‘r’  tolower(‘r’) returns ‘r’


Download ppt "Numeric Types, Expressions, and Output ROBERT REAVES."

Similar presentations


Ads by Google