Find the error(s) in each of the following program segments, and explain how the error(s) can be corrected
a) ```
int g()
{
cout << "Inside function g" << endl;
int h()
{
cout << "Inside function h" << endl;
}
}
```
b) ```
int sum( int x, int y )
{
int result;
result = x + y;
}
```
c) ```
int sum( int n )
{
if ( n == 0 )
return 0;
else
n + sum( n - 1 );
}
```
d) ```
void f( double a );
{
float a;
cout << a << endl;
}
```
e) ```
void product()
{
int a;
int b;
int c;
int result;
cout << "Enter three integers: ";
cin >> a >> b >> c;
result = a * b * c;
cout << "Result is " << result;
return result;
}
```
a) Error: Function h is defined in function g.
Correction: Move the definition of h out of the definition of g.
b) Error: The function is supposed to return an integer, but does not.
Correction: Delete variable result and place the following statement in the function:
return x + y;
c) Error: The result of n + sum( n - 1 ) is not returned; sum returns an improper result.
Correction: Rewrite the statement in the else clause as
return n + sum( n - 1 );
d) Errors: Semicolon after the right parenthesis that encloses the parameter list, and redefining the parameter a in the function definition.
Corrections: Delete the semicolon after the right parenthesis of the parameter list, and delete the declaration float a;.
e) Error: The function returns a value when it isn’t supposed to.
Correction: Eliminate the return statement or change the return type.
You might also like to view...
What is a method of the String class that lets you search through the characters in a string?
What will be an ideal response?
____________________ are a collection of scopes that contain sets of non-consecutive IP addresses that can be assigned to a single network.
Fill in the blank(s) with the appropriate word(s).
Used to authenticate individuals (e.g., by requiring digitally signed and encrypted email in order to connect to a corporate network via a VPN), software (Windows can require that device drivers be digitally signed), or server applications (many web servers are digitally signed).
What will be an ideal response?
Broken cables cause intermittent problems.
Answer the following statement true (T) or false (F)