The correct answers are highlighted in red.
A function calling statement must supply arguments that
Which of the following appear in a function header?
How does a function report its answer back to its calling (or boss) function?
A function prototype does not have to __________.
What will be displayed by the following code?
int main() { int num = 3; fn(num); cout << num; return 0; } int fn( int n ) { n++; }
When the body of a function begins to execute what is true of its arguments?
A variable called score is declared in main(). main() calls a function UpdateScore to update the score. UpdateScore also has a variable called score. When a value is added to score in UpdateScore, the value of score in main() is also changed.
Prototype statements are placed before int main().
The function header and the calling statement must contain the name and the data type of the arguments.
The statement int main() means that main is a function that takes no arguments, and returns an integer.
What is the subscript of the last element in the array Table if it is defined as follows?
float Table[30];
Which of the following statements (a-d) about Table is true?
float Table[] = { 0.9, 0.8, 0.7, 0.6 };
Which of the following "chunks" of C++ code will print the contents of an array called ar that has been defined to hold 25 integer values and has been initialized with 25 integer values?
int i;
cout << ar[];
for( i = 0; i == 25; i++ ) cout << ar[i] << " ";
for( i = 0; i < 25; i++ ) cout << ar[i] << " ";
for( i = 0; i < 25; i++ ); cout << ar[i] << " ";
The following is a valid array definition.
int ar[];
What will be displayed by the following code?
int main() { int num[5] = { 2, 4, 6, 8, 10 }; int i; fn(num); for( i = 0; i < 5; i++ ) cout << num[i] << ' '; return 0; } int fn( int n[] ) { n[3] = n[3] + 1; }
We call the pieces of information that are passed to a function, which it then uses to do its task a/an argument.
A function may return 0 or 1 result(s). It may take 0 to many arguments.
List two differences between a function prototype and a function header:
function prototype requires a semi-colon
function header requires argument names
Function prototypes are located before int main().
If a function does not return anything, then the return type on the function prototype and function header is void.
Consider the following program:
int main() int abc(int m) float xyz(float v, int k) { { { int a = 3, b; int i, j = 1; int i; float x = 2.0, y; float w = 1.0; b = abc(a); for(i=1; i<=m; i++) for(i=1; i<=k; i++, w *= v); y = xyz(x, a); j = j * i; return 0; } return j; return (w); } }
When the program is completed, the value of b is 6 and the value of y is 8.
Complete the following sentence: If you declare an array to hold N values, legal subscripts run from 0 to N - 1.
Complete the following sentence: the unsubscripted name of an array is the address of the array.
Write a function that takes a real number of ounces (representing the weight of a letter) and returns an integer number of cents which represents the cost of mailing the letter. The rules for calculating the cost are as follows:
int ouncesToCents( float ounces ) { if( ounces <= 1 ) return 33; else if( ounces <= 1.5 ) return 40; else if( ounces <= 2 ) return 50; else return (int) (30 * ounces); }
Write a function (call it getAns()) that prompts the user to enter a single letter from "a" to "d". It will return that letter to the calling function. It will not return anything but "a" through "d". If the user enters anything else, it will display an error message and ask the user to re-enter until they enter a value between "a" through "d".
string getAns() { string answer; cout << "Enter a letter from a to d: "; cin >> answer; while( answer != "a" && answer != "b" && answer != "c" && answer != "d" ) { cout << "Invalid answer. Try again: "; cin >> answer; } return answer; }
Write a single statement that will both call getAns() and display the value returned.
cout << getAns();
Write a function called GetIntegerInRange() that gets an integer value from the user that is between 0 and an upperbound. It takes 1 argument:
It returns an integer: the value that is between 0 and the upperbound.
The function should prompt the user to enter a value between 0 and the passed in high value. As long as the user enters an an invalid value (i.e. smaller then 0 or greater then the high value), display an error message and have the user try again. Once the user has entered a valid value, it should be returned.
Example: assuming the user passes in the value 120. The output should closely resemble:
Enter a number (0 to quit) between 0 and 120:
NOTE: You may assume that the passed in high value is greater than 0.
int getIntegerInRange( int upperBound ) { int userVal; cout << "Enter a number (0 to quit) between 0 and " << upperBound << ": "; cin >> userVal; while( userVal < 0 || userVal > upperBound ) { cout << "Invalid value. Try again: "; cin >> userVal; } return userVal; }
Write a COMPLETE program that uses GetIntegerInRange() to accept temperatures from the user in the range of 0 to 120. The value of 0 serves as an exit signal.
After all the temperatures have been entered, the program should display the following with brief labels:
You may assume that at least one temperature above 32 is entered and that one temperature below 32 is entered.
#include <iostream> #include <iomanip> using namespace std; int getIntegerInRange( int ); int main() { int temp, above32cnt = 0, below32cnt = 0, above32sum = 0, below32sum = 0; float above32Avg, below32Avg; temp = getIntegerInRange( 120 ); while( temp != 0 ) { if( temp > 32 ) { above32sum += temp; above32cnt++; } else //note: this will include temperatures equal to 32 { below32sum += temp; below2cnt++; } temp = getIntegerInRange( 120 ); } above32avg = (float) above32sum / above32cnt; below32avg = (float) below32sum / below32cnt; cout << "The average of the temperatures above 32 is " << above32avg << endl << "The number of temperatures above 32 is " << above32cnt << endl << endl << "The average of the temperatures below 32 is " << below32avg << endl << "The number of temperatures below 32 is " << below32cnt << endl; return 0; }
Assume an array called AR contains 45 integers. Write a loop to print out the values of every other integer starting from subscript 0.
int sub; for( sub = 0; sub < 45; sub += 2 ) { cout << AR[sub] << endl; }
Assume an array called AR contains 10 integers. Write code to swap the very first and the very last values in the array.
int temp = AR[0]; AR[0] = AR[9]; AR[9] = temp;
Declare an array to hold 500 integers. In the declaration statement, initialize the array so the very first value is 2, the second is 4, and all the rest are 0.
int AR[500] = { 2, 4 };
Write a function to cube the first N elements in an array of doubles. The function should be passed the array and the number of elements to be cubed. It returns nothing.
void cubeAr( double ar[], int N ) { int sub; for( sub = 0; sub < N; sub ++ ) { ar[sub] = ar[sub] * ar[sub] * ar[sub]; } }
Write a function to return the sum of the first N elements in an array of doubles. The function can assume N >= 0. The function should be passed the array and the number of elements to be added together.
double sumAr( double ar[], int N ) { int sub; double sum = 0; for( sub = 0; sub < N; sub ++ ) { sum += ar[sub]; } return sum; }