Homework Quz 4-Extra Credit Extra Credit #Extra Credit (HW) Due: 11/1/2017 (No e
ID: 3607272 • Letter: H
Question
Homework Quz 4-Extra Credit Extra Credit #Extra Credit (HW) Due: 11/1/2017 (No excuse for late submission) 1. Write the MATLAB code using for or while loop to do the following: a. Sum of even-Indexed numbers of glven array X b. Sum of odd-indexed numbers given array X c. Sum of only even numbers of given array X d. Sum of only positive even numbers of given array X e. Sum of only negative even numbers of given array X f. Sum of only odd numbers of given array X Sum of only positive odd numbers of glven array X h. Sum of only negative odd numbers of given array X X = [-56] Reflect in ePortfoio Download View asExplanation / Answer
Ans a)
% Create sample data.
array = -5 : 6;
sumx2=0
k = 1;
while k <= length(array) %While loop to access array
evens=mod(k,2)==0;
if evens==1
sumx2=sumx2+array(k);
end
k = k+1; % Increment Counter
end
sumx2
Ans b)
% Create sample data.
array = -5 : 6;
sumx2=0
k = 1;
while k <= length(array) %While loop to access array
evens=mod(k,2)==0;
if evens==0
sumx2=sumx2+array(k);
end
k = k+1; % Increment Counter
end
sumx2
Ans c)
% Create sample data.
array = -5 : 6;
sumx2=0
k = 1;
while k <= length(array) %While loop to access array
evens=mod(array(k),2)==0;
if evens==1
sumx2=sumx2+array(k);
end
k = k+1; % Increment Counter
end
sumx2
Ans d)
% Create sample data.
array = -5 : 6;
sumx2=0
k = 1;
while k <= length(array) %While loop to access array
evens=(mod(array(k),2)==0&&array(k)>0);
if evens==1
sumx2=sumx2+array(k);
end
k = k+1; % Increment Counter
end
sumx2
Ans e)
% Create sample data.
array = -5 : 6;
sumx2=0
k = 1;
while k <= length(array) %While loop to access array
evens=(mod(array(k),2)==0&&array(k)<0);
if evens==1
sumx2=sumx2+array(k);
end
k = k+1; % Increment Counter
end
sumx2
Ans f)
% Create sample data.
array = -5 : 6;
sumx2=0
k = 1;
while k <= length(array) %While loop to access array
evens=(mod(array(k),2)==0);
if evens==0
sumx2=sumx2+array(k);
end
k = k+1; % Increment Counter
end
sumx2
Ans g)
% Create sample data.
array = -5 : 6;
sumx2=0
k = 1;
while k <= length(array) %While loop to access array
evens=(mod(array(k),2)!=0&&array(k)>0);
if evens==1
sumx2=sumx2+array(k);
end
k = k+1; % Increment Counter
end
sumx2
Ans h)
% Create sample data.
array = -5 : 6;
sumx2=0
k = 1;
while k <= length(array) %While loop to access array
evens=(mod(array(k),2)!=0&&array(k)<0);
if evens==1
sumx2=sumx2+array(k);
end
k = k+1; % Increment Counter
end
sumx2
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.