Chapter 6 C++
Assume a function addOne that accepts a single integer reference parameter and returns nothing. Call the function, passing it the variable i which has already been declared.
addOne (i) ;
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; } }
Write a statement that declares a prototype for a function powerTo, which has two parameters. The first is a double and the second is an int. The function returns a double.
double powerTo(double, int);
Write the header for a function addOne that accepts a single integer reference parameter and returns nothing. Name the parameter x.
void addOne(int &x)
Write the definition of a function named maxmin that is passed four int arguments. The function returns nothing but stores the larger of the first two arguments in the third argument it receives and the the smaller of the first two arguments in its fourth argument. So, if you invoke maxmin(3,7,x,y), upon return x will have the value 7 and y will have the value 3.
void maxmin(int iVal1, int iVal2, int& x, int& y){ if (iVal1 > iVal2) { x = iVal1; y = iVal2; }else { x = iVal2; y = iVal1; } }
Write a statement that declares a prototype for a function printTodaysDate, which has no parameters and doesn't return anything.
void printTodaysDate();
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; } }
toThePowerOf is a function that accepts two int parameters and returns the value of the first parameter raised to the power of the second. An int variable cubeSide has already been declared and initialized. Another int variable, cubeVolume, has already been declared. Write a statement that calls toThePowerOf to compute the value of cubeSide raised to the power of 3, and store this value in cubeVolume.
cubeVolume = toThePowerOf(cubeSide, 3);
add is a function that accepts two int parameters and returns their sum. Two int variables, euroSales and asiaSales, have already been declared and initialized. Another int variable, eurasiaSales, has already been declared. Write a statement that calls add to compute the sum of euroSales and asiaSales and store this value in eurasiaSales.
eurasiaSales = add(euroSales, asiaSales);
Given the integer variables x, y, and z, write a fragment of code that assigns the smallest of x, y, and z to another integer variable min. Assume that all the variables have already been declared and that x, y, and z have been assigned values).
if(x < y){ if(x<z){ min = x; }else{ min = z; } }else{ if(y<z){ min = y; }else{ min = z; } }
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;}
Write a statement that declares a prototype for a function add, which has two int parameters and returns an int.
int add (int, int);
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 value) { return value/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 amount) { return amount - 1; }
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 ; }
Write a function addOne that adds 1 to its integer reference parameter. The function returns nothing.
void addOne (int&x){ x++;}
Write a statement that declares a prototype for a function twice, which has an int parameter and returns an int.
int twice(int);
printErrorDescription is a function that accepts one int parameter and returns no value. Write a statement that calls the function printErrorDescription, passing it the value 14.
printErrorDescription(14);
printLarger is a function that accepts two int parameters and returns no value. Two int variables, sales1 and sales2, have already been declared and initialized. Write a statement that calls printLarger, passing it sales1 and sales2.
printLarger(sales1, sales2);
printTodaysDate is a function that accepts no parameters and returns no value. Write a statement that calls printTodaysDate.
printTodaysDate();
Write the definition of a function printAttitude, which has an int parameter and returns nothing. The function prints a message to standard output depending on the value of its parameter. If the parameter equals 1, the function prints disagree If the parameter equals 2, the function prints no opinion If the parameter equals 3, the function prints agree In the case of other values, the function does nothing. Each message is printed on a line by itself.
void printAttitude(int input) { switch(input) { case 1: cout<<"disagree\n"; break; case 2: cout<<"no opinion\n"; break; case 3: cout<<"agree\n"; break; } }
Write the definition of a function printDottedLine, which has no parameters and doesn't return anything. The function prints to standard output a single line (terminated by a new line character) consisting of 5 periods.
void printDottedLine() { cout << ".....\n"; }
Write a statement that declares a prototype for a function printErrorDescription, which has an int parameter and returns nothing.
void printErrorDescription(int);
Write the definition of a function printGrade, which has a char parameter and returns nothing. The function prints on a line by itself the message string Grade: followed by the char parameter (printed as a character) to standard output. Don't forget to put a new line character at the end of your line. Thus, if the value of the parameter is 'A', the function prints out Grade: A
void printGrade(char grade) { cout << "Grade: " << grade << "\n"; }
Write a statement that declares a prototype for a function printLarger, which has two int parameters and returns no value.
void printLarger(int,int);