In: Computer Science
the question should be done on Matlab.
There is a vector defined as H = [-88, 45, 75, 24, 14, 19, -11, 9, -5, -3, 12, 19 ,56, -4, 5,
17, -6, -8, -9, 6, 15]
Write a program as a script file that triples the elements that are positive and a divisible by 3 or 5, and, raises to the power of 2 the elements that are negative but greater than -11.
Code:
H = [-88, 45, 75, 24, 14, 19, -11, 9, -5, -3, 12, 19 ,56,...
-4, 5,17, -6, -8, -9, 6, 15];
n = size(H); % [ 1 x ] where x = no. of elements in H
n = n(2); % n= x
for i = 1:n % loop through elements of H
if(H(i)>0) % if element is greater than 0
% and divisible by 3 or 5
if((mod(H(i),3)==0)||(mod(H(i),5)==0))
% then triple it
H(i)=H(i)*3;
end
elseif(H(i)<0) % if element is less than 0
% if greater than -11
if(H(i)>-11)
% raise it to the power of 2
H(i)=2^(H(i));
end
end
end
disp(H);
Screenshot:
Code output
==================
Comments have been added by the side of each line to help you understand better.
Please upvote.
For any query comment.