Exam 3 Programming
Write the prototype for the function printGreater with two int variables
void printGreater(int,int);
Write the definition of a function named printStars, which has no parameters and doesn't return anything. The function prints 10 asterisks on a single line to screen.
void printStars() { cout << "**********"; } Or void printStars() { for(int count =1;count<=10;count++) cout << "*"; }
Write the definition of a function named swapper that is passed two int variables . The function returns nothing but exchanges the values of the two variables passed to it. So, if the parameters num1 and num2 have the values 5 and 8 respectively, then after the execution of the function the values of num1 and num2 will be 8 and 5 respectively. You have to write the function definition only.
void swapper(int &num1, int &num2 ) { int temp; temp = num1; num1 = num2; num2 = temp; }
A / an ____ variable is defined within a function and is not accessible outside the function.
local
What is the output of the following program? #include <iostream> using namespace std; void showInts(int, int); void changeInts(int, int); int main(){ int first = 1, second = 2; showInts(first, second); changeInts(first, second); showInts(first, second); return 0; } void showInts(int first, int second){ cout << first << " " << second << endl; } void changeInts(int aNumber, int anotherNumber){ int first = 3, second = 4; anotherNumber = second * anotherNumber; aNumber = first * aNumber; }
1 2 1 2
What is the output of the following program? #include <iostream> using namespace std; void showInts(int, int); void changeInts(int, int &); int main(){ int first = 1, second = 2; showInts(first, second); changeInts(first, second); showInts(first, second); return 0; } void showInts(int first, int second){ cout << first << " " << second << endl; } void changeInts(int aNumber, int & anotherNumber){ int first = 3, second = 4; anotherNumber = second * anotherNumber; aNumber = first * aNumber; }
1 2 3 8
Which line in the following program contains a call to the showDub function? 1 #include <iostream> 2 using namespace std; 3 4 void showDub(int); 5 6 int main( ) 7 { 8 int x = 2; 9 10 showDub(x); 11 cout << x << endl; 12 return 0; 13 } 14 15 void showDub(int num) 16 { 17 cout << (num * 2) << endl; 18 }
10
Which line in the following program contains (or begins) the declaration for the showDub function? 1 #include <iostream> 2 using namespace std; 3 4 void showDub(int); 5 6 int main( ) 7 { 8 int x = 2; 9 10 showDub(x); 11 cout << x << endl; 12 return 0; 13 } 14 15 void showDub(int num) 16 { 17 cout << (num * 2) << endl; 18 }
15
What is the output of the following program? # include <iostream> using namespace std; void doSomething(int&); int main( ) { int x = 2; cout << x << endl; doSomething(x); cout << x << endl; return 0; } void doSomething(int& num) { num = 0; }
2 0
What is the output of the following program? # include <iostream> using namespace std; void doSomething(int); int main( ) { int x = 2; cout << x << endl; doSomething(x); cout << x << endl; return 0; } void doSomething(int num) { num = 0; }
2 2
What is the output of the following program? # include <iostream> using namespace std; void doSomething(int); int main( ) { int x = 2; cout << x << endl; doSomething(x); cout << x << endl; return 0; } void doSomething(int num) { num = num * 2; }
2 2
Consider the function prototype int myFunction(double, double, double, double); How many parameters does this function have?
4
What is the output produced by this program? #include <iostream> using namespace std; void showDub(int); int main(){ int x = 2; showDub(x); return 0; } void showDub(int x){ cout << ( x * 2 ) << endl; }
4
Which line in the following program contains the prototype for the showDub function? 1 #include <iostream> 2 using namespace std; 3 4 void showDub(int); 5 6 int main( ) 7 { 8 int x = 2; 9 10 showDub(x); 11 cout << x << endl; 12 return 0; 13 } 14 15 void showDub(int num) 16 { 17 cout << (num * 2) << endl; 18 }
4
What is the output of the following program? # include <iostream> using namespace std; int getValue(int); int main( ) { int x = 2; cout << getValue(x) << endl; return 0; } int getValue(int num) { return num*3; }
6
A function ________ contains the statements that make up the function.
body
Write the definition of a function named checkOdd, that receives an integer parameter and returns true if the parameter's value is odd, and false otherwise.
bool checkOdd(int num) { return num % 2==0; } Or bool checkOdd(int num) { if (num % 2==0) return false; else return true; }
Write the prototype for a function, called isEven, that accepts a parameters of type int and returns a bool.
bool isEven(int);
A function is executed when it is:
called
These types of arguments are passed to parameters automatically if no argument is provided in the function call.
default
displayName is a function that accepts no parameters and returns no value. Write a statement that calls the function displayName.
displayName();
It is a good programming practice to ________ your functions by writing comments that describe what they do.
document
Look at the following function prototype. int myFunction(double); What is the data type of the function's parameter?
double
Write the prototype for a function, called calculateAverage, that accepts five parameters of type double and returns a double.
double calculateAverage(double, double, double, double, double);
Write the definition of a function named keepAverage that receives a double parameter and returns the average value that has been passed to it so far.
double keepAverage(double num) { static double total = 0.0; static int count =0; count++; total += num; return total/count; }
Write a statement that declares a prototype for a function named product, which has receives two parameters. The first parameter is a double and the second is an int. The function returns a double value.
double product(double,int);
This function causes a program to terminate, regardless of which function or control mechanism is executing.
exit()
A function's return data type must be the same as the function's parameter(s).
false
A named collection of statements that performs a specific task is a
function
Write a statement that calls the function named product and pass to it the variables payRate and hours( payRate is a double variable and hours is an int variable). The value returned by the function should be stored in the double variable grosspay. All these 3 variables - payRate, hours and grossPay have already been declared and initialized.
grossPay = product(payRate,hours);
If a function does not have a prototype, default arguments may be specified in the function ________.
header
Write the prototype for a function, called addTwo, that returns an int and accepts two int parameters.
int addTwo(int, int);
Given the integer variables north, south, west, and east , write code that assigns the smallest of these variables to another integer variable min. These variables should receive the values passed to a function named findLowest and the function should return the lowest value. Assume that all the variables( except for min) have already been declared and assigned values.
int findLowest(int north, int south, int west, int east) { min = north; if ( south < min) min = south; if ( west < min) min = west; if ( east < min) min=east; return min; }
Write the definition of a function named keepTrack that receives no parameters and returns 1 the first time it is invoked, returns 2 the next time it is invoked, then 3 and so on. In other words, the function returns a count of the number of times it was invoked/called.
int keepTrack() { static int count = 0; count ++; return count; }
Write the definition of a function named square, which receives an integer parameter and returns the square of the value of the parameter.
int square (int num) { return num * num; }
What are the two parts of a function signature?
parameter list and function name
printGreater is a function that accepts two int parameters and returns no value. Two int variables, score1 and score2, have already been declared and initialized. Write a statement that calls printGreater, passing it score1 and score2.
printGreater(score1,score2);
A function ________ eliminates the need to place a function definition before all calls to the function.
prototype
This statement causes a function to end.
return
Arguments are passed to a function in the order in which they appear in the function call.
true
Function prototypes are terminated with a semicolon.
true
One reason for using functions is to break programs into manageable units, or modules.
true
You may use the exit( ) function to terminate a program, regardless of which control mechanism is executing.
true
Write a function named keepAdding that adds 1 to its integer reference parameter. The function returns nothing.
void addOne(int &num) { num++; }
Write a statement that declares the prototype for the displayName() function
void displayName();
Write the definition of the function named printGreater described in Question 4. It receives two int parameters and returns nothing. The function prints on screen the greater value of the two parameters.
void printGreater(int num1, int num2) { if (num1 > num2 ) cout << num1 <<endl; else cout << num2 << endl; }