In: Computer Science
I have this matlab program, and need to turn it into a C++ program. Can anyone help me with this?
% Prompt the user for the values x and y
x = input ('Enter the x coefficient: ');
y = input ('Enter the y coefficient: ');
% Calculate the function f(x,y) based upon
% the signs of x and y.
if x >= 0
if y >= 0
fun = x + y;
else
fun = x + y^2;
end
else
if y >= 0
fun = x^2 + y;
else
fun = x^2 + y^2;
end
end
% Write the value of the function.
disp (['The value of the function is ' num2str(fun)]);
#include <iostream>
using namespace std;
int fun(int x,int y)
{
int func_value;
if(x>=0)
{
if(y>=0)
func_value= x + y;
else
func_value= x + y*y;
}
else
{
if(y>=0)
func_value= x*x + y;
else
func_value= x*x + y*y;
}
return func_value;
}
int main()
{
int x,y,returnValue;
cout<<"Enter the x coefficient : ";
cin>>x;
cout<<"Enter the y coefficient : ";
cin>>y;
returnValue=fun(x,y);
cout<<"The value of the function is :
"<<returnValue;
return 0;
}