 # Answered! Background: The two basic concept used for all programming are (1) loops and (2) branches. These two concepts…

Background: The two basic concept used for all programming are (1) loops and (2) branches. These two concepts are used in most computer programs. More importantly, they are the only two concepts that you will need to implement the algorithms for this class. Branching refers to a choice of the blocks of statements to evaluate, and loops implies that some repetitive tasks are to be performed. Computers are fast and best suited to problems with a lot of repetitive tasks. If your program does not contain loops and branches, then it is not saving you much labor or time. The absolute function is an example of branching:

function y = absolute(x)

Don't use plagiarized sources. Get Your Custom Essay on
Answered! Background: The two basic concept used for all programming are (1) loops and (2) branches. These two concepts…
GET AN ESSAY WRITTEN FOR YOU FROM AS LOW AS \$13/PAGE

% input parameter

% x = input number

% output parameter

% y = absolute value of x

if x < 0 % if x is negative, do the following (one branch)

y = -x;

else % otherwise do the following (the other branch)

y = x;

end

end % end of function

An example of loop is the sum of an array. Let x = [1 2 3 4 5] be an array of numbers from 1 to 5.

function sum = mySum(x)

% input parameter

% x = input array of numbers

% output parameter

% y = the sum of the value of elements in x

sum = 0;

for i = 1:end

sum = sum + x(i);

end

end % end of function

It is important to note that the equal sign means assign. sum = sum + x(i)means add x(i)to sum and assign this value back to the memory space with the name of sum. It is not the mathematical meaning of equal, which would make the statement incorrect, unless x(i)is always 0. The right hand side sum in sum + x(i)must have a value for the addition to work, and the subscript i must have a value to identify which element of x is to be used. The statement sum = 0 is needed so that when i = 1 (the first loop) the right hand side sum has a value of 0. Subsequent loops when i = 2, 3, etc. sum will have a value from the previous loop. sum is called the accumulator. The combination of an accumulator and an initial value before a loop are common in programming. Please review the above to fully understand how this works.

Assignment: Write a short MATLAB programs to obtain the product of the numbers in an array, but ignore the first two negative numbers. The product should include the subsequent negative numbers, if any. Return the product as positive, regardless of the sign of the actual product.

IMPORTANT NOTE: Your programs must be silent—i.e., they should not echo any information to the screen other than the messages specified in the problem statement. This includes any MATLAB-

generated warning or error messages as well as intermediate results of your own program’s calculations.

Your function statement should look like this

function prod = posProd(x)

% input parameter

% x = input array of positive and negative numbers

% output parameter

% y = the product of the positive elements in x

Your input array may contain some negative elements. The first two are to be ignored. For example, the input may be x = [1 2 -3 -4 -5 2]. Your result should be |1 ×2×−5×2|=20. The first two negative numbers have been dropped in the calculation of the product. Write your program to accept x with any length, and x containing any number of negative numbers in any order. You may use the functions in the examples above, but you may not use any built-in functions.

The following code answers the question. It makes use of the absolute() function shown as example. Please don’t forget to rate the answer if it helped. Thank you very much.

Here are the steps …

Step 1: Save the following absolute function from example in a file absolute.m

function y = absolute(x)

% input parameter

% x = input number

% output parameter

% y = absolute value of x

if x < 0 % if x is negative, do the following (one branch)

y = -x;

else % otherwise do the following (the other branch)

y = x;

end

end % end of function

Step 2: Save the following function posProd () in a file named posProd.m

function prod = posProd(x)
% input parameter
% x = input array of positive and negative numbers
% output parameter
% y = the product of the positive elements in x
negCount = 0;
prod = 1;
for p = 1 : numel(x)
if(x(p) < 0 && negCount < 2) %check if element is -ve and we counted less than 2 -ve numbers
negCount++;
else
prod = prod * absolute(x(p)); %always multiply by absolute value so product is +ve
end
end
end

Step 3: now you can use the function we have created as shown below

x = [1 2 -3 -4 -5 2];

po = posProd(x)