Anwser the questions using the code below: template <typename T> bool compare (T
ID: 3838631 • Letter: A
Question
Anwser the questions using the code below:
template <typename T>
bool compare (T a[ ], T b[ ], int size)
{ for (int k = 0; k<size; k++)
if (a[k] != b[k]) return false;
return true; }
6. (3) What is the best PRE comment for this function?
a. PRE: none
b. PRE: T must be a numeric data type
c. PRE: Type T must support the != operator
d. PRE: Type T must support the != and the [ ] operators.
7. (3) What is the best POST comment for this function?
a. POST: return true if the two arrays a and b contain different values; else false
b. POST: return true if the two arrays a and b both contain the same one and only one value
c. POST: return true if the two arrays a and b contain the same items in any order; else false
d. POST: return true if the two arrays a and b contain the same items in the same order; else false
8. (3) The main function below calls the compare template function. A version of template function compare instantiating T with data type int is created and used.
int main ( )
{ int a[4] = { 10, 20, 30 };
int b[4] = { 10, 40, 50 };
bool b = compare (a, b, 4);
… }
a. True b. False
9. (3) A template function is often stored in a header file as it is not able to be compiled.
a. True b. False
Explanation / Answer
6. ( a.) PRE: none
Because template itself is used to manage or operate upon different types of data i.e. different data types. So we dont need to define as particular datatype or particular operations as it supports all.
7. a. POST: return true if the two arrays a and b contain different values; else false
Based on the template functionality defined in the template it checks for inequality and returns true.
8. a. True.
As the template main functionality is that only. It is used by classes that needs to operate on many types.
9. b. False
The files included in the header will always be compiled.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.