Respuesta :
Answer:
- #include <iostream>
- #include <cmath>
- using namespace std;
- struct data {
- int a;
- int b;
- double c;
- };
- data func2(){
- int a, b;
- double c;
- data d;
- cout<<"Input first number: ";
- cin>>a;
- cout<<"Input second number: ";
- cin>>b;
- if(a != 0 && b != 0){
- if(a >= b){
- c = pow(a, b);
- }else{
- c = pow(b, a);
- }
- }
- else if(a != 0 && b == 0){
- c = sqrt(abs(a));
- }
- else if(a == 0 && b != 0){
- c = sqrt(abs(b));
- }
- else{
- c = 0;
- }
- d.a = a;
- d.b = b;
- d.c = c;
- return d;
- }
- int main()
- {
- data d = func2();
- cout<<d.a<<" "<<d.b<<" "<<d.c<<"\n";
- return 0;
- }
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).