More on Arithmetic Expressions

• Precedence rules

- In what order will the arithmetic operations be performed in the following expression ?

avg = x + y / 2.0;

- Is x + y calculated first or is y / 2.0 calculated first ?

- The following rules apply

* Highest precedence: ()
* Next precedence: * / %
* Lowest precedence: + -

- y / 2.0 will be calculated first !!!

- If we want x + y to be calculated first, we must enclosed it in ()

avg = (x + y) / 2.0;

- When there are multiple arithmetic operators with the same precedence, they are carried out from left to right

x + y - 2 implies (x + y) - 2

<Table from page 95>

• Type coercion

- Implicit data conversion performed by the compiler when several different data types have been mixed in the same expression (see previous set of notes !!)

float someFloat, anotherFloat;
int someInt, anotherInt;

someFloat = 12.0;
someInt= 4;
someFloat = 3 * someInt + 2;
someInt = 5.2 / someFloat - anotherFloat;

- Warning: conversion from a higher level data type to a lower level data type (e.g., float -> int) might cause loss of information (i.e., errors)

- Data coercion involving functions

* Data conversion takes also place when we call a function and there is a mismatch in the data type of the arguments of the function and the values passed to the function

* When an expression is passed to a function, data conversion will take place if data types have been mixed in the expression followed by another conversion if there is a mismatch in the data type of the arguments and the data type of the value of the expression

* Values returned by functions will also be converted to a different data type if there is a mixing of data types

#include <iostream.h>

int compute_sum(int, int);

int main()
{
int x, y, sumInt;
float w, z, sumFloat, avg;

x=11;
y=20;
sumInt=compute_sum(x,y); // sumInt will be equal to 31

w=11.2;
z=20.8;
sumInt=compute_sum(w,z); // sumInt will be equal to 31 again !!

sumInt=compute_sum(x+w, y+z); // x will converted to float, then x+w will be coverted to int
// y will converted to float and then y+z will be coverted to int
// sum will be equal to 62

sumFloat=compute_sum(x,y); // sumFloat will be equal to 31.0 now !!

avg=compute_sum(x,y) / 2.0; // avg will be equal to 15.5

return 0;
}

int compute_sum(int num1, int num2)
{
int result;

result = num1 + num2;

return result;
}

• Type casting

- Explicit data type conversion which is performed by using the cast operators

someFloat = float(3 * someInt + 2);

someInt = int(5.2 / someFloat - anotherFloat);

someInt=1;
anotherInt=2;
someFloat = float(someInt) / float(anotherInt);
(or someFloat = float(someInt) / anotherInt;)

- We can also use type casting to convert the data type of the values passed to a function

sumInt=compute(int(w),int(z));

sumInt=compute(int(x+w), int(y+z));

Library functions

- Prewritten functions that are part of every C++ system and are available for use by any program

#include <iostream.h>
#include <math.h> // for sqrt() and fabs()

main()
{
float alpha, beta;

beta = -20.2;
alpha = sqrt(7.3 + fabs(beta)); // function call inside another function call !!
cout << "alpha=" << alpha << endl;
}

- If you call a library function from your program, you must place an #include command on the top of your program with the appropriate file (header file) which contains information about that function (see Appendix C)

<Table from page 103>

Void functions

- There are functions which do not return any value to the caller program (void-returning functions)

- The keyword void must be used before the name of a void-returning function to avoid warning messages from the compiler !!

#include <iostream.h>

void main()
{
cout << "Hello World" << endl;
}

- Void-returning functions cannot appear in an expression (because they return no value !!)

- With void functions, the function call is a separate, stand-alone statement

#include <iostream.h>

void print_message(); // function declaration

void main()
{
void print_message(); // a separate statement
}

void print_message() // function definition
{
cout << "Hello World" << endl;
}

Formatting the output

- Control the way the output appears visually on the screen or on a printer

- To format the output, various manipulators can be used (e.g., endl)

- To use a manipulator in our program, the statement #include <iomanip.h> must be included in the top of the program (note: endl is defined in iostream.h)

- Creating blank lines and inserting blanks within a line (read pages 105-108)

- The setw manipulator

* Determines the number of columns (field width) that the output will occupy
* The total number of columns is expanded if the specified field width is too narrow
* Applies only to the very next item printed !!!

ans=33;
num=7132;

<Table from page 110>

- Decimal versus scientific notation

* Include the following statement in your program before you use cout

cout.setf(ios::fixed, ios::floatfield);

- The decimal point is not printed if the number is a whole number

someFloat=95.0;
cout << f2someFloat; will print 95 !!! (not 95.0)

- Forcing the computer to print the decimal point

* Include the following statement in your program before you use cout

cout.setf(ios::showpoint);

- The setprecision manipulator

* Determines the number of decimal places to be displayed
* Remains in effect for all subsequent output (until you change it again)
* The total number of columns is expanded if the specified field width is too narrow

<Table from page 112>

Recommended exercises

- All Quick Check excersices (pages 123-125)

- 2 (page 125), 4, 5 (page 126), 7 (page 127)