This challenge activity uses a 3rd party app Though your act
This challenge activity uses a 3rd party app. Though your activity may be recorded, a refresh may be required to update the banner to the left. Write a for loop that sets each array element in bonus Scores to the sum of itself and the next element, except for the last element which stays the same. Be careful not to index beyond the last element Ex: If bonus Scores = [10. 20. 30. 40], then after the loop bonus Scores = [30. 50. 70. 40] The first element is 30 or 10 + 20. the second element is 50 or 20 + 30. and the third element is 70 or 30 + 40. The last element remains the same function bonus Scores = Combine Scores(number Scores, user Scores) %number Scores: Number of scores in array bonus Scores % bonus Scores: User defined array of test scores % Write a for loop that sets each array element in bonus Scores to %the sum of itself and the next element, except for the last element %which stays the same bonus Scores = user Scores; Combine Scores(4, [10, 20, 30, 40])
Solution
% matlab code
function bonusScores = CombineScores(numberScores, userScores)
% itearte over userScores till second last element
for i = 1:(numberScores-1)
% add current score and next score
userScores(i) = userScores(i) + userScores(i+1);
end
bonusScores = userScores;
end
bonusScores = CombineScores(4,[10,20,30,40]);
disp(bonusScores);
%output: 30 50 70 40
