oregonstate instructure.com m/courses, 1621988/quizzes/2372423/take/questionsv47
ID: 3787670 • Letter: O
Question
oregonstate instructure.com m/courses, 1621988/quizzes/2372423/take/questionsv47123674 Show partial work. Circle the final answer if it isn't clear. You don't need to call clear/clc/clf. If you can't write the MATLAB code, at least write out the pseudo code. Comments are not required, but you may use them to clarify what you were trying to do Unless explicitly stated otherwise stated, assume "calculate" means write a MATLAB program to calculate the answer: do not calculate by hand. Output means what shows up in the command window D Question 14 2 pts I've split this problem up into 3 steps. Redd the whole problem before starting; if you're confused by later steps, just do the frst ones for partial credit. Do NOTassume that you know the number of elements in the arrays or the values in them. You are given an array x and an array y, both with the same number of elements Write a script that prints out the sum of x and y for every element. l.e.. if x was 1 2 5 l and y was 17 1 5 then it would print out 8 then 3 then 10. Do NOT assume that x and y are those specific arrays. Assume x and y are already defined above this line and add your code after this line HTML Editor Ea ll TT TT. Font Sizes Para Pa sExplanation / Answer
Code for the first part
x = [ 1 2 5]; % The x vector it can be any vector of any size
y = [7 1 5 ]; % The y vector it can be any vector of any size
Sx = sum(x); % sum of all elements in the x vector
Sy = sum(y); % sum of all elements in the y vector
MOD = 10; % variable to seperate the ones and tens positions in sum
while(Sx)% loop to seperate the ones and tens in Sx
dp = mod(Sx,MOD);% performing Sx%10 Sx%100 etc
disp(dp); % Displaying the result
Sx = Sx-dp; % Substracting ones from Sx
MOD = MOD*10; % changing MOD value to find tens in Sx
end
MOD = 10;
while(Sy)% loop to seperate the ones and tens in Sy
dp = mod(Sy,MOD);% performing Sy%10 Sy%100 etc
disp(dp);% Displaying the result
Sy = Sy-dp;% Substracting ones from Sy
MOD = MOD*10; % changing MOD value to find tens in Sy
end
Output
>> array_addition
9
3
10
Code for second part
x = [ 1 2 -5]; % The x vector it can be any vector of any size
y = [7 1 5 ]; % The y vector it can be any vector of any size
Sx = sum(x); % sum of all elements in the x vector
Sy = sum(y); % sum of all elements in the y vector
MOD = 10; % variable to seperate the ones and tens positions in sum
if Sx > 0 % Checking Sx is positive or not
while(Sx)% loop to seperate the ones and tens in Sx
dp = mod(Sx,MOD);% performing Sx%10 Sx%100 etc
disp(dp); % Displaying the result
Sx = Sx-dp; % Substracting ones from Sx
MOD = MOD*10; % changing MOD value to find tens in Sx
end
MOD = 10;
end
if Sy >0% Checking Sy is positive or not
while(Sy)% loop to seperate the ones and tens in Sy
dp = mod(Sy,MOD);% performing Sy%10 Sy%100 etc
disp(dp);% Displaying the result
Sy = Sy-dp;% Substracting ones from Sy
MOD = MOD*10; % changing MOD value to find tens in Sy
end
end
OUTPUT
>> array_addition
3
10
(Pls note that I changed the x variable so that the sum is less than zero. Hence its result is printed)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.