7. Based on the above the answer, which of the following two function prototypes
ID: 3865433 • Letter: 7
Question
7. Based on the above the answer, which of the following two function prototypes makes sense to use for a function that will swap two values, the first or second?
void swap(int val1, int val2); //prototype #1
void swap(int& val1, int& val2); // prototype #2
8. Write a function definition as follows: it returns the C++ form of a Boolean value, its function identifier is anyTwoTheSame, it has three formal parameters which all are floating-point values, and the body of the function returns true if any two of the parameters are equal, otherwise it returns false. I recommend using short, simple identifiers for the three formal parameters.
9. (yes/no) In C++, is it legal to declare the variable int v; at the top of the body of a function and later declare char v; inside of a while loop body? (Book review question on scope.)
//example function body template
{
int v;
…
while(…)
{
char v;
…
}
…
}
Answer:
10-13. Consider the following function definition: void defaultParam(int num1, int num2 = 7, double z = 1.5){ int num3; num3 = static_cast<int>(z) + num2 – num1; cout << "num3 = " << num3 << endl;}What is the output of the following function calls?10. Case one: defaultParam(7);
Output:
11. Case two: defaultParam(-7, -8);
Output:
12. Case three: defaultParam(0, 1, -7.5);
Output:
13. Case four: defaultParam(4, -4, -1.3);
Output:
Explanation / Answer
7. Based on the above the answer, which of the following two function prototypes makes sense to use for a function that will swap two values, the first or second?
void swap(int val1, int val2); //prototype #1
void swap(int& val1, int& val2); // prototype #2
Ans) void swap(int& val1, int& val2); // prototype #2
Because swap function in prototype # 2 uses call by reference to swap two values which means after the function is completed both val1 and val2 variable values are swapped. Whereas in prototype # 1 it is passed by call by copy that means just the values are swapped inside the function but they will not take any impact outside of the function.
8. Write a function definition as follows: it returns the C++ form of a Boolean value, its function identifier is anyTwoTheSame, it has three formal parameters which all are floating-point values, and the body of the function returns true if any two of the parameters are equal, otherwise it returns false. I recommend using short, simple identifiers for the three formal parameters.
Ans)
bool anyTwoTheSame(float a, float b, float c)
{
if(a==b)
return true;
else if(b==c)
return true;
else if(c==a)
return true;
else
return false;
}
9. (yes/no) In C++, is it legal to declare the variable int v; at the top of the body of a function and later declare char v; inside of a while loop body? (Book review question on scope.)
//example function body template
{
int v;
…
while(…)
{
char v;
…
}
…
}
Answer: Yes
You can declare the same variable declared in main body inside a block as well with different data type. Below is an example program you can use to test.
Example program:-
#include <iostream>
using namespace std;
int main()
{
int v=10;
cout << v << endl;
while(true)
{
char v;
v = 'a';
cout << v << endl;
break;
}
return 0;
}
code output:
sh-4.2$ g++ -o main *.cpp
sh-4.2$ main
10
a
sh-4.2$
10-13. Consider the following function definition: void defaultParam(int num1, int num2 = 7, double z = 1.5){ int num3; num3 = static_cast<int>(z) + num2 – num1; cout << "num3 = " << num3 << endl;}What is the output of the following function calls?10. Case one: defaultParam(7);
Output:
Ans) 1
In the function,
num3 = static_cast<int>(z) + num2 - num1;
num3 = 1 + 7 - 7 = 1
because only one argument is passed which is 7 assigned to num1 and others default values are picked. So 1.5 become 1 after casting and num2 becomes 7, num1 becomes 7. Hence the answer 1.
11. Case two: defaultParam(-7, -8);
Output: 0
In the function,
num1 becomes -7 and num2 becomes -8
so, num3 = static_cast<int>(z) + num2 - num1;
num3 = 1 -8 - (-7) = 1 - 8 + 7 = -7 + 7 = 0. Hence the answer 0
12. Case three: defaultParam(0, 1, -7.5);
Output: -6
In the function num1 becomes 0, num2 becomes 1 and z becomes -7.5
so, num3 = static_cast<int>(z) + num2 - num1 = -7 + 1 - 0 = -6
13. Case four: defaultParam(4, -4, -1.3);
Output: -9
In the function num1 becomes 4, num2 becomes -4 and z becomes -1.3
so, num3 = static_cast<int>(z) + num2 - num1 = -1 -4 - 4 = -9
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.