Question

In: Computer Science

I have this matlab program, and need to turn it into a C++ program. Can anyone...

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)]);

Solutions

Expert Solution

#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;
}


Related Solutions

So, I have this Matlab program that I have made for a lab, and despite having...
So, I have this Matlab program that I have made for a lab, and despite having different frequencies, when I go to plot them, the graphs look exactly the same. Can someone tell me why that is? I also need to know how to determine the digital frequencies(rad/sec) and how many samples per period of both of the waves? Thank you Code: N = 100; % Total number of time domain samples in simulation. A1 = 1; % Amplitude of...
in c++ please In this program you are going to have several files to turn in...
in c++ please In this program you are going to have several files to turn in (NOT JUST ONE!!) hangman.h – this is your header file – I will give you a partially complete header file to start with. hangman.cpp – this is your source file that contains your main function functions.cpp – this is your source file that contains all your other functions wordBank.txt – this is the file with words for the game to use.  You should put 10...
Can anyone explain to me how I solve this, I need to prepare a journal entry...
Can anyone explain to me how I solve this, I need to prepare a journal entry for the given problem: on jan 1 a corporation issued $325,000, 5.5%, 9 year bonds when the market rate was 6%. Interest is to be paid annually on each Jan 1
Hi i need a c++ program that can convert charactor into numbers pseodocode user input their...
Hi i need a c++ program that can convert charactor into numbers pseodocode user input their name for example john every single charactor should have a value so it return correct result eg: j=2, o=1, h=7,n=2 and display these numbers if you may need any question to ask me, i will be very happy to answer them. Thanks! example project close but not accurate #include #include #include #include #include #include #include #include #define INPUT_SIZE 8 using namespace std; // Function...
If anyone can give a detailed, step by step analysis of the following problem I need...
If anyone can give a detailed, step by step analysis of the following problem I need to be able to do a problem LIKE this for a test Dec. 5, 2019. Thanks! Example Problem – 40 points. Use the printout to answer the following questions. 1. Suppose we want to develop a model to predict assessed value based on heating area. A sample of 15 single-family houses is selected in a particular community. The assessed value (in thousands of $...
Make a java program of Mickey I have the starter program but I need to add...
Make a java program of Mickey I have the starter program but I need to add eyes and a smile to it. import java.awt.Canvas; import java.awt.Color; import java.awt.Graphics; import java.awt.Rectangle; import javax.swing.JFrame; public class Mickey extends Canvas { public static void main(String[] args) { JFrame frame = new JFrame("Mickey Mouse"); Canvas canvas = new Mickey(); canvas.setSize(400, 400); canvas.setBackground(Color.white); frame.add(canvas); frame.pack(); frame.setVisible(true); } public void paint(Graphics g) { Rectangle bb = new Rectangle(100, 100, 200, 200); mickey(g, bb); } public void...
C++: (I need this program to output in a certain format I will post the "needed...
C++: (I need this program to output in a certain format I will post the "needed results" below) To make telephone numbers easier to remember, some companies use letters to show their telephone number. For example, using letters, the telephone number 438-5626 can be shown as GET LOAN. In some cases, to make a telephone number meaningful, companies might use more than seven letters. For example, 225-5466 can be displayed as CALL HOME, which uses eight letter. Write a program...
I want to create an image compression program with matlab use PCA. I have the code...
I want to create an image compression program with matlab use PCA. I have the code listed below. But this code is fail, the image is colorless, but still gray. Can you help me to fix my code. clc clear all picture = im2double(imread('picture1.jpg')); Red = picture(:,:,1); premean = mean(Red(:)); premax = max(Red(:)); premin = min(Red(:)); x = size(Red,1); y = size(Red,2); Z = ones(x,y)*premean; A = (Red - Z)*(1/premax - premin); B = cov(A); [veceig,eig,C] = pcacov(B); NewRed =...
I need specific codes for this C program assignment. Thank you! C program question: Write a...
I need specific codes for this C program assignment. Thank you! C program question: Write a small C program connect.c that: 1. Initializes an array id of N elements with the value of the index of the array. 2. Reads from the keyboard or the command line a set of two integer numbers (p and q) until it encounters EOF or CTL - D 3. Given the two numbers, your program should connect them by going through the array and...
I have to create a program in C++ where a user can enter as many numbers...
I have to create a program in C++ where a user can enter as many numbers as they want (they predetermine the number of values to be inputted) and then the program can echo that input back to the user and then determine if the numbers were even, odd, or a zero and it outputs how many of each were found. This is to be down with four void functions and no arrays. The program initializes the variables zero, odds,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT