Ruby program to calculate the sum of matrix elements

We saw a lot of information in the previous article. We are going to know about this very clearly in the article Ruby program to calculate the sum of matrix elements. I think you will like this article very much. Let’s go into the article.

Ruby program to calculate the sum of matrix elements

Ruby program to calculate the sum of matrix elements

# Ruby program to calculate the sum 
# of matrix elements

TwoDArr = Array.new(2){Array.new(2)}
sum = 0;

printf "Enter elements of MATRIX:\n";
i = 0;
while (i < 2) 
  j = 0;
  while (j < 2) 
    printf "ELEMENT [%d][%d]: ", i, j;
    TwoDArr[i][j] =  gets.chomp.to_i;
    j = j + 1;
  end
  i = i + 1;
end

printf "MATRIX:\n";
i = 0;
while (i < 2)
  j = 0;
  while (j < 2) 
    sum = sum +TwoDArr[i][j];
    print TwoDArr[i][j]," ";
    j = j + 1;
  end
  i = i + 1;
  print "\n";
end

printf "Sum of matrix elements: %d\n",sum;
Enter elements of MATRIX:
ELEMENT [0][0]: 1
ELEMENT [0][1]: 2
ELEMENT [1][0]: 3
ELEMENT [1][1]: 4
MATRIX:
1 2 
3 4
Sum of matrix elements: 10

Final Words

Ruby program to calculate the sum of matrix elements This article is your favorite topic because it is your favorite. And I will meet you in the next article.

Hi, I'm Ranjith a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coding Deekshi. Here, I post about programming to help developers.

Share on:

Leave a Comment