More on Arithmetic Expressions

     

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)