Question
done in matlab write the code for all problems
ECE 72: Introduction to Electrical and Computer Engineering Tools 3 In-Class Assignment 9: Flow Control Due: 9/21/17 by 2:55 PM Complete the following tasks in this in-class assignment and submit a single publishecd PDF file to Blackboard before the end of class with each of the 3 problems as a new section, titling your sections each of the problem numbers. Make sure to include a title and enough comments to describe what is happening 1. Create a 4x4x4 sized matrix, with any values you like, then using a double nested for loop, calculate the sum of all of these elements Use the randi function to create a vector with 100 elements, ranging from 1-50, and create the variables a 1 and b 1. Using a while loop, and conditional statements, check each of the elements, and if the element is greater than 40, 2. 3 perform aatb, if the element is less than 10, perform bra x b, if the element is qual to 25, perform a=a+a, and otherwise perform a=a+1 and h=b print statement to print just the values of a and b after your loop. t1. Use the Perform problem 2 again, except use a switch statement instead of conditional statements to accomplish it 3.
Explanation / Answer
clear all; close all;
%Solution 1
B = repmat(5, [4 4 4]);
sum = 0;
for i=1:4
m=B(:,:,i);
for j=1:4
for k=1:5
sum = sum + m(i,j);
end
end
end
%Solution 2
ll = 1;
ul = 50;
r = (ul-ll).*rand(100,1) + ll;
a = 1;
b = 1;
dig = 1;
while(dig < 100)
numHere = r(dig);
if (numHere > 40)
a = a + b;
elseif (numHere < 10)
b = a.*b;
elseif (numHere == 25)
a = a + a
else
a = a + 1;
b = b + 1;
end
dig = dig + 1;
end
%Solution 3
ll = 1;
ul = 50;
r = (ul-ll).*rand(100,1) + ll;
a = 1;
b = 1;
dig = 1;
while(dig < 100)
numHere = r(dig);
switch(numHere)
case (numHere > 40)
a = a + b;
case (numHere < 10)
b = a.*b;
case (numHere == 25)
a = a + a
otherwise
a = a + 1;
b = b + 1;
end
dig = dig + 1;
end
a
b