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 will be displayed by the statements below?
``` char s1[8] = "petunia", s2[9] = "marigold"; char tmp1[10], tmp2[20]; strcpy(tmp2, s1); strcat(tmp2, s2); strncpy(tmp1, &tmp2[5], 6); tmp1[6] = '\0'; printf("b%s\n", tmp1); ``` a. iamari b. biamari c. oldpet d. boldpet e. none of the above
After you finish entering text in vim insert mode, you can press the ____ key to change back to command mode.
A. ESC B. Back C. Shift D. Del