153k views
1 vote
g Given a 5 by 5 matrix of all positive values, write a program to find and display the greatest product of the 2 adjacent values in the vertical direction. For example, in the matrix below the greatest product of 2 adjacent values in vertical direction is 64 which is a product of mat(1,3) and mat(2,3).

User Zessx
by
4.5k points

1 Answer

4 votes

Answer:

Check the explanation

Step-by-step explanation:

% iterate through each column (outer loop) , then iterate rows-1 (inner loop)

%take product of adjacent rows

mat = input('enter 5 by 5 matrix : ');

[R,C] = size(mat);

max_prod =0;

for c =[1:C]

for r=[1:(R-1)]

temp = mat(r,c)*mat((r+1),c);

if max_prod<temp

max_prod=temp;

end

end

end

fprintf('Greatest product : %i\\', max_prod)

Kindly check the output in the attached image below.

g Given a 5 by 5 matrix of all positive values, write a program to find and display-example-1
User Xialvjun
by
4.7k points