Answer to Question #341780 in C++ for LM.

Question #341780

Find the error(s) in each of the following program segments and explain how the error(s) can be

corrected:

(i) int function1()

{

cout << "Inside function function1 " <<

endl; int function2()

{

cout << "Inside function function1 " << endl;

}

}

(ii) int sum(int x, int y)

{

int result;

result = x +

y;

}

(iii) int computeProd(int n)

{

if (n == 0)

return 0;

else

}

n * computeProd(n – 1);

(iv) void aFunction(float a)

{

float a;

cout << a << endl;

}

(v) void theProduct()

{

int

a;

int

b;

int

c;

int result;

cout << “Enter three integers “ <<

endl; cin >> a >> b >> c;

result = a * b * c;

cout << “Result is “ << result <<

endl; return result;

(vi) float calculateSquare(float number)

{

return number * number;

}


1
Expert's answer
2022-05-18T15:27:17-0400
#include <iostream>

using namespace std;







int function2(){

  cout << "Inside function function1" << endl;

  return 0;  // function type INT, so it has to return integer value;

}

int function1(){

  cout << "Inside function function1 " <<endl;

  function2();  // do not nest functions inside functions

  return 0;    // function type INT, so it has to return integer value;

}




int sum(int x, int y){

  int result;

  result = x +y;

  return result; // return the result

}




int computeProd(int n){

  if (n == 0)

    return 1; // if you return 0, everything will be multiplied by 0

  else 

    return n * computeProd(n - 1);

}

void aFunction(float a){

//  float a;        already declared

  cout << a << endl;

}




int theProduct(){      // function type void returns nothing. You need to return int. 

  int a; int b; int c;

  int result;

  cout << "Enter three integers" <<endl;

  cin >> a >> b >> c;

  result = a * b * c;

   

  cout << "Result is"<< result << endl; 

  return result;

} 




float calculateSquare(float number){ //works just fine

  return number * number;

}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS