Math library and system library built-in functions

Functions come in two varieties. They can be defined by the user or built in as part of the compiler package. As we have seen, user-defined functions have to be declared at the top of the file. Built-in functions, however, are declared in header files using the #include directive at the top of the program file, e.g. for common mathematical calculations we include the file cmath with the #include <cmath> directive which contains the function prototypes for the mathematical functions in the cmath library

Mathematical functions

Math library functions allow the programmer to perform a number of common mathematical calculations:

Function Description
sqrt(x) square root
sin(x) trigonometric sine of x (in radians)
cos(x) trigonometric cosine of x (in radians)
tan(x) trigonometric tangent of x (in radians)
exp(x) exponential function
log(x) natural logarithm of x (base e)
log10(x) logarithm of x to base 10
fabs(x) absolute value (unsigned)
ceil(x) rounds x up to nearest integer
floor(x) rounds x down to nearest integer
pow(x,y) x raised to power y

Random numbers

Other header files which contain the function prototypes of commonly used functions include cstdlib and time. These contain functions for generating random numbers and for manipulating time and dates respectively.

The function random() randomly generates an integer between 0 and the maximum value which can be stored as an integer. Every time the function is called:

   randomNumber = random();

a different number will be assigned to the variable randomNumber. Each number is supposed to have an equal chance of being chosen each time the function is called. The details of how the function achieves this will not be discussed here.

Before a random number generator is used for the first time it must be initialised by giving it a number called the seed. Each seed will result in a different sequence of numbers. The function srandom() is used to provide a seed to initialise the random number generator, random(). It must be called with an arbitrary integer parameter (i.e. the seed) which can be conveniently generated by using the value returned by the system clock function time() with the actual parameter NULL. This returns the calendar time in seconds, converted to an integer value. The following call, which is usually used only once, can be used to initialise the random number generator:

   srandom(time(NULL));

The following program uses these system functions and defines a function to simulate rolling a six-sided die. The die is rolled N times (using a for loop) and the proportion of times the value 6 appears as the outcome is calculated.

(Double click on the icon with the file name RollDice.cc. Compile and run the program.)

// RollDice.cc
// Program to simulate rolling a die with 6 faces N times.
// Output is generated by random number generator and converted to range.
// Fraction of times the number 6 appears is calculated.

#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

int RollDie();

int main()
{
   int i, outcome, N=0, count_six=0, count_other=0;
   float fraction_six, fraction_other;

    // Initialise random number generator with value of system time.
    srandom(time(NULL));

    // Get user input in correct range.
    while(N<1 || N>1000)
    {
       cout << "Input the number of experiments (1-1000): ";
       cin >> N;
    }

    // Perform N experiments. 
    // Call RollDie() N times and record number of sixes.
    for(i=0; i< N; i++)
    {
       outcome = RollDie();
       cout << outcome << endl;
       if(outcome==6) count_six++;
       else count_other++;
    }

    //Integer variables must be converted (cast) for correct division
    fraction_six = static_cast<float>(count_six)/N;
    fraction_other = static_cast<float>(count_other)/N;

    // Output results
    cout << "Fraction of outcomes in which 6 was rolled: " 
         << fraction_six << endl;
    cout << "Fraction of outcomes in which other numbers were rolled: " 
         << fraction_other << endl;
    return 0;
}

// Function to simulate rolling a single 6-sided die. 

// Function takes no arguments but returns an integer.
// Each call will randomly return a different integer between 1 and 6.

int RollDie() 
{
   int randomNumber, die;

   randomNumber = random();
   die = 1 + (randomNumber % 6);
   return die;
}
Back to top