Tuesday, June 19, 2012

Homework 7: Definitions

1. Write the definition of a function  oneMore , which receives an integer parameter and returns an integer that is one more than the value of the parameter. So if the parameter's value is 7, the function returns the value 8. If the parameter's value happens to be 44, the functions returns the value 45.


int oneMore ( int x ) { return x + 1 ; }

2. Write the definition of a function  oneLess , which receives an integer parameter and returns an integer that is one less than the value of the parameter. So if the parameter's value is 7, the function returns the value 6. If the parameter's value happens to be 44, the functions returns the value 43.

int oneLess ( int x ) { return x - 1 ; }

3. Write the definition of a function  half , which receives an integer parameter and returns an integer that is half the value of the parameter. (Use integer division!) So if the parameter's value is 7, the function returns the value 3. If the parameter's value happens to be 44, the functions returns the value 22.

int half ( int x ) { return x / 2 ; }

4. Write the definition of a function  square , which receives an integer parameter and returns the square of the value of the parameter. So if the parameter's value is 7, the function returns the value 49. If the parameter's value happens to be 25, the functions returns the value 625. If the value of the parameter is 0, the function returns 0.

int square ( int x ) { return x * x ; }

5. Write the definition of a function  absoluteValue , that receives an integer parameter and returns the absolute value of the parameter's value. So, if the parameter's value is 7 or 803 or 141 the function returns 7, 803 or 141 respectively. But if the parameter's value is -22 or -57, the function returns 22 or 57 (same magnitude but a positive instead of a negative). And if the parameter's value is 0, the function returns 0.

int absoluteValue ( int x ) { return abs ( x ) ; }

6. Write the definition of a function  signOf , that receives an integer parameter and returns a -1 if the parameter is negative, returns 0 if the parameter is 0 and returns 1 if the parameter is positive. So, if the parameter's value is 7 or 803 or 141 the function returns 1. But if the parameter's value is -22 or -57, the function returns -1. And if the parameter's value is 0, the function returns 0.

int signOf ( int x ) { if ( x < 0 ) { return - 1; } else if ( x == 0 ) { return 0; } else { return 1; } }

7. Write the definition of a function  isPositive , that receives an integer parameter and returns true if the parameter is positive, and false otherwise. So, if the parameter's value is 7 or 803 or 141 the function returns true. But if the parameter's value is -22 or -57, or 0, the function returns false.

bool isPositive ( int x ) { if ( x > 0 ) { return true; } else { return false ; } }

8. Write the definition of a function  isSenior , that receives an integer parameter and returns true if the parameter's value is greater or equal to 65, and false otherwise. So, if the parameter's value is 7 or 64 or 12 the function returns false. But if the parameter's value is 69 or 83 or 65 the function returns true.

bool isSenior ( int x ) { if ( x >= 65 ) { return true; } else { return false; } }

9. Write the definition of a function  isEven , that receives an integer parameter and returns true if the parameter's value is even, and false otherwise. So, if the parameter's value is 7 or 93 or 11 the function returns false. But if the parameter's value is 44 or 126 or 7778 the function returns true.

bool isEven ( int x ) { if ( x % 2 == 0 ) { return true ; } else { return false ; } }

10. Write the definition of a function  twice , that receives an integer parameter and returns an integer that is twice the value of that parameter. 

int twice ( int x ) { return 2 * x ; }

11. Write the definition of a function  add , which receives two integer parameters and returns their sum. 

int add ( int x , int y ) { return x + y ; }

12. Write the definition of a function  max that has three  int parameters and returns the largest. 

int max ( int a , int b, int c ) { return a < b ? ( b < c ? c : b ) : ( a < c ? c : a ); }

13. Write the definition of a function  powerTo , which receives two parameters. The first is a  double and the second is an  int . The function returns a  double . If the second parameter is negative, the function returns 0. Otherwise, it returns the value of the first parameter raised to the power of the second. 

double powerTo ( double x , int n ) { double prod = 1.0; if ( n < 0 ) return 0.0; while ( n > 0 ) { prod *= x; --n; } return prod; }

No comments:

Post a Comment