1. Describe the difference between value parameters and reference parameters. De
ID: 3579346 • Letter: 1
Question
1. Describe the difference between value parameters and reference parameters. Describe this in terms of the way the compiler handles these arguments and in terms of how the parameters behave.
2. Write a C++ function that reads positive integers from the user until the user enters –1 to quit. Output the largest number, the smallest number, the average, and the number of odd numbers the user inputs.
3. What are the rules for valid identifier (variable / function) names:
4. Write a C++ statement to do each of the following. All are done to the same array.
a. Declare an integer array of size 36
b. Assign the fifth value in the array to be 17.
c. Assign the last value in the array to be 42.
d. Assign all values in the array to be equal to a value the user inputs.
5. Write a C++ function named processArray that takes an array of doubles as its first parameter, a double value as its second parameter, and the length of the array as the third parameter. The function should print all values in the array that are less than the second parameter.
6. What is the difference between value parameters and reference parameters? Include a description of the way the compiler handles the arguments and how the parameters behave.
7. Why are functions and important part of C++ programming?
8. Write a complete C++ function called surfaceArea that accepts one integer parameter representing the length of one side of a cube. The surfaceArea returns the surface area of the cube. For example, if the function is passed the value 5, it will return the value 150. You do not need to include any input or output statements in the surfaceArea function. You must write the function header.
Explanation / Answer
1)
When a variable is declared as reference, it becomes an alternative name for an existing variable. A variable can be declared as reference by putting ‘&’ in the declaration.
A reference (or pointer) allows called function to modify a local variable of the caller function. For example, consider the following example program where fun() is able to modify local variable x of main().
void fun(int &x) {
x = 20;
}
int main() {
int x = 10;
fun(x);
cout<<"New value of x is "<<x;
return 0;
}
Output:
New value of x is 20
Where the value parameters can not change due to any modification in called function.
void fun(int x) {
x = 20;
}
int main() {
int x = 10;
fun(x);
cout<<"New value of x is "<<x;
return 0;
}
Output:
New value of x is 10
If an argument is large, passing by reference (or pointer) is more efficient because only an address is really passed, not the entire object.
2)
void readUserInput(){
int count = 0;
int largest, smallest, sum = 0, oddCount = 0;
int input;
cout<<"Enter integer (-1 to stop): ";
cin>>input;
while(input != -1){
// read first number, so initialize all
if(count == 0){
largest = input;
smallest = input;
}
if(input % 2 == 1)
oddCount++;
count++;
sum = sum + input;
cout<<"Enter integer (-1 to stop): ";
cin>>input;
}
cout<<"Largest: "<<largest<<endl;
cout<<"Smallest: "<<smallest<<endl;
cout<<"Odd integers: "<<oddCount<<endl;
cout<<"Average: "<<(double)sum/(double)count<<endl;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.