Sunday, March 15, 2009

Useful functions


In this section, we will use functions can can be quite useful, especially for mathematical operations. These functions are not available in the standard input out library (stdio). Therefore other libraries must be included in the preprocessor directive to be able to use special functions. E.g. math.h, ctype.h, stdlib.h and windows.h

Lets look at the math.h library functions first, and it contains useful functions such as
  • sqrt()
  • pow()
  • ceil()
  • floor()
These function can help a lot when performing mathematical operations.

sqrt()
This function allows you to perform square root operation.
Function syntax:
sqrt(int x);
where int x is an integer variable and returns the square root of the variable x.
Example:
y= sqrt(25);//returns the value of 5 and stores it into variable y

OR
x=25;
y=sqrt(x);//returns the value of 5 and stores it into variable y.

If you wondered is there any other functions that might do cube roots, or fourth roots or nth root, then the answer is on the next function.

pow()
This function, as its name suggests, computes power functions.
Function syntax:
pow(base_number, powerof_n);
Where base_number is the number you want to compute the power, and the powerof_n is the number of the power.

Example:
5 cube would be pow(5, 3);

Lets say you want to compute the 7th root of 5, then we can simply write:

pow(5, (1/7));

since 5 to the power of 1/7 is the same as the 7th root of 5.