Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

6.54 (Find the Error) Determine whether the following program segments contain e

ID: 3731586 • Letter: 6

Question

6.54 (Find the Error) Determine whether the following program segments contain errors. For each error, explain how it can be corrected. [Note: For a particular program segment, it’s possible that no errors are present in the segment.]

a) template < class A >

int sum( int num1, int num2, int num3 )

{

        return num1 + num2 + num3;

}

b) void printResults( int x, int y )

{

        cout << “The sum is” << x + y << ‘ ’;

        return x + y;

}

c) template < A >

A product( A num1, A num2, A num3 )

{

        return num1 * num2 * num3;

}

d) double cube( int );

int cube( int );

Explanation / Answer

A)

template < class A >

int sum( int num1, int num2, int num3 )

{

        return num1 + num2 + num3;

}

There is not any error in the segment.

B)

void printResults( int x, int y )

{

        cout << “The sum is” << x + y << ‘ ’;

        return x + y;

}

Here error is that function returning x+y that is integer but in function signature is it void.

So need to change return type of function.

int printResults( int x, int y )

{

        cout << “The sum is” << x + y << ‘ ’;

        return x + y;

}

C)

template < A >

A product( A num1, A num2, A num3 )

{

        return num1 * num2 * num3;

}

There is error when declaring template class is missing in the declaration. So corrected code is as below:

template <class A >

A product( A num1, A num2, A num3 )

{

        return num1 * num2 * num3;

}

D)

double cube( int );

int cube( int );

Here the error is both function having the same type of input argument. So need to change input type for one function.

Correct code is

double cube( double );

int cube( int );