67.9k views
1 vote
(50 points) Write a programthat adds the first and last elements of an integer array and stored as the first element of the output array, adds the second and second-to-last elements and stored as the second number, and so on. If the array hasan odd number of integers, leave the central integer inthe original arrayunchanged.

1 Answer

6 votes

Answer:

A is the input array

B = [] ; %B is initially an empty array

for i = 1:length(A)/2 %iterates over A until the midpoint of A

B(i) = A(i) + A(end + 1 - i); %This adds the numbers from the first half and %second half of A, and stores in B

end

disp(B)

User Tmj
by
5.8k points