Computer Science, asked by pratishmoses, 9 months ago

Write a programin Java to input numbers in a 4 X 5 matrix. Display the numbers in a tabular form. Also display the sum of elements of each row.

Answers

Answered by dhruvrshete
1

Algorithm

Declare and initialize a two-dimensional array a.

Calculate the number of rows and columns present in the array a and store it in variables rows and cols respectively.

Maintain two variables sumRow and sumCol to store the sum of elements in the specific row and the sum of elements in specific column respectively.

To calculate the sum of elements in each row:

Two loops will be used to traverse the array where the outer loop selects a row, and the inner loop represents the columns present in the matrix a.

Calculate the sum by adding elements present in a row.

Display sumRow.

Repeat this for each row.

To calculate the sum of elements in each column:

Two loops will be used to traverse the array where the outer loop select a column, and the inner loop represents the rows present in the matrix a.

Calculate the sum by adding elements present in a column.

Display sumCol.

Repeat this for each column.

Solution

Python

#Initialize matrix a  

a = [    

       [1, 2, 3],  

       [4, 5, 6],  

       [7, 8, 9]  

   ];  

   

#Calculates number of rows and columns present in given matrix  

rows = len(a);  

cols = len(a[0]);  

   

#Calculates sum of each row of given matrix  

for i in range(0, rows):  

   sumRow = 0;  

   for j in range(0, cols):  

       sumRow = sumRow + a[i][j];  

   print("Sum of " + str(i+1) +" row: " + str(sumRow));  

   

#Calculates sum of each column of given matrix  

for i in range(0, rows):  

   sumCol = 0;  

   for j in range(0, cols):  

       sumCol = sumCol + a[j][i];  

   print("Sum of " + str(i+1) +" column: " + str(sumCol));  

Output:

Sum of 1 row: 6

Sum of 2 row: 15

Sum of 3 row: 24

Sum of 1 column: 12

Sum of 2 column: 15

Sum of 3 column: 18

C

#include <stdio.h>  

   

int main()  

{  

   int rows, cols, sumRow, sumCol;  

 

     

   //Initialize matrix a  

   int a[][3] = {      

                   {1, 2, 3},  

                   {4, 5, 6},  

                   {7, 8, 9}  

               };  

     

   //Calculates number of rows and columns present in given matrix  

   rows = (sizeof(a)/sizeof(a[0]));  

   cols = (sizeof(a)/sizeof(a[0][0]))/rows;  

     

   //Calculates sum of each row of given matrix  

   for(int i = 0; i < rows; i++){  

       sumRow = 0;  

       for(int j = 0; j < cols; j++){  

         sumRow = sumRow + a[i][j];  

       }  

       printf("Sum of %d row: %d\n", (i+1), sumRow);  

   }  

     

   //Calculates sum of each column of given matrix  

   for(int i = 0; i < cols; i++){  

       sumCol = 0;  

       for(int j = 0; j < rows; j++){  

         sumCol = sumCol + a[j][i];  

       }  

       printf("Sum of %d column: %d\n", (i+1), sumCol);  

   }  

         

   return 0;  

}  

Output:

Sum of 1 row: 6

Sum of 2 row: 15

Sum of 3 row: 24

Sum of 1 column: 12

Sum of 2 column: 15

Sum of 3 column: 18

JAVA

public class SumofRowColumn  

{  

   public static void main(String[] args) {  

       int rows, cols, sumRow, sumCol;  

         

       //Initialize matrix a  

       int a[][] = {      

                       {1, 2, 3},  

                       {4, 5, 6},  

                       {7, 8, 9}  

                   };  

           

         //Calculates number of rows and columns present in given matrix  

         rows = a.length;  

       cols = a[0].length;  

         

       //Calculates sum of each row of given matrix  

       for(int i = 0; i < rows; i++){  

           sumRow = 0;  

           for(int j = 0; j < cols; j++){  

             sumRow = sumRow + a[i][j];  

           }  

           System.out.println("Sum of " + (i+1) +" row: " + sumRow);  

       }  

         

       //Calculates sum of each column of given matrix  

       for(int i = 0; i < cols; i++){  

           sumCol = 0;  

           for(int j = 0; j < rows; j++){  

             sumCol = sumCol + a[j][i];  

           }  

           System.out.println("Sum of " + (i+1) +" column: " + sumCol);  

       }  

   }  

}  

Output:

Sum of 1 row: 6

Sum of 2 row: 15

Sum of 3 row: 24

Sum of 1 column: 12

Sum of 2 column: 15

Sum of 3 column: 18

C#

using System;  

                     

public class SumofRowColumn  

{  

   public static void Main()  

   {  

       int rows, cols, sumRow, sumCol;  

         

       //Initialize matrix a  

       int[,] a = {      

                       {1, 2, 3},  

                       {4, 5, 6},  

                       {7, 8, 9}  

                  };  

           

         //Calculates number of rows and columns present in given matrix  

         rows = a.GetLength(0);  

       cols = a.GetLength(1);  

         

       //Calculates sum of each row of given matrix  

       for(int i = 0; i < rows; i++){  

           sumRow = 0;  

           for(int j = 0; j < cols; j++){  

             sumRow = sumRow + a[i,j];  

           }  

           Console.WriteLine("Sum of " + (i+1) +" row: " + sumRow);  

       }  

         

       //Calculates sum of each column of given matrix  

       for(int i = 0; i < cols; i++){  

           sumCol = 0;  

           for(int j = 0; j < rows; j++){  

             sumCol = sumCol + a[j,i];  

           }  

           Console.WriteLine("Sum of " + (i+1) +" column: " + sumCol);  

       }  

   }  

}  

Output:

Sum of 1 row: 6

Sum of 2 row: 15

Sum of 3 row: 24

Sum of 1 column: 12

Sum of 2 column: 15

Sum of 3 column: 18

PHP

<!DOCTYPE html>  

<html>  

<body>  

<?php  

//Initialize matrix a  

$a = array(      

            array(1, 2, 3),  

            array(4, 5, 6),  

            array(7, 8, 9)  

          );  

   

//Calculates number of rows and columns present in given matrix  

$rows = count($a);  

$cols = count($a[0]);  

   

//Calculates sum of each row of given matrix  

for($i = 0; $i < $rows; $i++){  

   $sumRow = 0;  

   for($j = 0; $j < $cols; $j++){  

     $sumRow = $sumRow + $a[$i][$j];  

   }  

   print("Sum of " . ($i+1) ." row: " . $sumRow);  

   print("<br>");  

}  

   

//Calculates sum of each column of given matrix  

for($i = 0; $i < $cols; $i++){  

   $sumCol = 0;  

   for($j = 0; $j < $rows; $j++){  

     $sumCol = $sumCol + $a[$j][$i];  

   }  

   print("Sum of " . ($i+1) . " column: " . $sumCol);  

   print("<br>");  

}  

?>  

</body>  

</html>  

Output:

Sum of 1 row: 6

Sum of 2 row: 15

Sum of 3 row: 24

Sum of 1 column: 12

Sum of 2 column: 15

Sum of 3 column: 18

Similar questions