Consider the provided C++ code in the main.cpp file: The function func2 has three parameters of type int, int, and double, say a, b, and c, respectively. Write the definition of func2 so that its action is as follows: Prompt the user to input two integers and store the numbers in a and b, respectively. If both of the numbers are nonzero: If a >= b, the value assigned to c is a to the power b, that is, aᵇ. If a < b, the value assigned to c is b to the power a, that is, bᵃ. If a is nonzero and b is zero, the value assigned to c is the square root of the absolute value of a. If b is nonzero and a is zero, the value assigned to c is the square root of the absolute value of b. Otherwise, the value assigned to c is 0. The values of a, b, and c are passed back to the calling environment. After completing the definition of the func2 and writing its function prototype, run your program.

Respuesta :

Answer:

  1. #include <iostream>
  2. #include <cmath>
  3. using namespace std;
  4. struct data {
  5.    int a;
  6.    int b;
  7.    double c;
  8. };
  9. data func2(){
  10.    
  11.    int a, b;
  12.    double c;
  13.    data d;
  14.    
  15.    cout<<"Input first number: ";
  16.    cin>>a;
  17.    cout<<"Input second number: ";
  18.    cin>>b;
  19.    
  20.    if(a != 0 && b != 0){
  21.        if(a >= b){
  22.            c = pow(a, b);
  23.        }else{
  24.            c = pow(b, a);
  25.        }
  26.    }
  27.    else if(a != 0 && b == 0){
  28.        c = sqrt(abs(a));
  29.    }
  30.    else if(a == 0 && b != 0){
  31.        c = sqrt(abs(b));
  32.    }
  33.    else{
  34.        c = 0;
  35.    }
  36.    
  37.    d.a = a;
  38.    d.b = b;
  39.    d.c = c;
  40.    return d;
  41. }
  42. int main()
  43. {
  44.    data d = func2();
  45.    cout<<d.a<<" "<<d.b<<" "<<d.c<<"\n";
  46.    
  47.    return 0;
  48. }

Explanation:

Since the func2 is expected to return a, b and c and therefore we create a struct data to hold the return values (Line 6 - 10);

Next, we create the func2 that will return a struct data (Line 12).

Declare three required variables, a, b and c (Line 14 -16) and prompt user to input a and b values (Line 18 - 21).

Next, create nested if else condition that fulfill all the condition requirements as stated in the question (Line 23 - 38) and calculate the value a and b and then assign the result to c accordingly. Please note, we use c++ math library methods such as pow, abs and sqrt to get value from calculation that involves power (Line 25, Line 27), absolute (Line 31, Line 34) and square root (Line 31, Line 34).

At last, set the value a, b and c to the struct d and return d as output (Line 43).

ACCESS MORE