Given an input matrix mat. Find and return sum of elements of each column of the matrix.
| Input Parameters |
Expected outputs |
mat = {
{7,1,4,1},
{9,9,3,0},
{2,1,4,3}};
|
Sum of columns : 18,11,11,4
|
mat= {
{3,1,-5,6},
{-9,3,2,7},
{-1,0,0,9}}
|
Sum of columns : -7,4,-3,22
|
| The java method should accept following input parameters: mat (integer matrix).
|
| Initialize int variable sumCols to 0.
|
| Initialize int array arrSumCols with size mat[0].length to hold the sum of the elements of columns.
|
| Iterate through mat using a for loop, using variable cIdx as loop counter. Loop variable cIdx will be initialized with value 0 and will iterate through all the matrix’s columns one by one till cIdx < mat[0].length:
Within above loop, iterate through mat using a for loop, using variable rIdx as loop counter. Loop variable rIdx will be initialized with value 0 and will iterate through all the matrix’s columns one by one till rIdx < mat.length:
Increment arrSumCols by mat[rIdx][cIdx] in each iteration.
|
| When above for loops are completed arrSumCols will have sum of each column of the matrix.
|
| Reference code is given below. For sake of brevity, code for validation for the method’s input parameters is not provided. To ensure that the code works properly, ensure input parameters are valid before calling the method.
|
Below fully running code can be copied and run on Eclipse or other Java IDEs. Refer the classname in code below. If the class name below is "A", save the code below to a file named A.java before running it.
Be sure to try your own test cases to enhance your understanding !
You can also tweak the code to optimize or add enhancements and custom features.
public class MdArrayColumnSum {
public int[] matArrayColumnSum(int[][] mat) throws Exception{
int sumCol;
int[] arrSumCols=new int[mat[0].length];
for (int cIdx=0; cIdx < mat[0].length; cIdx++) {
sumCol=0;
for (int rIdx=0; rIdx < mat.length; rIdx++) {
sumCol += mat[rIdx][cIdx];
}
arrSumCols[cIdx]=sumCol;
}
return arrSumCols;
}
public static void main(String[] args) {
MdArrayColumnSum ap=new MdArrayColumnSum();
int[] retArr;
try {
int[][] arr = { {7,1,4,1},
{9,9,3,0},
{2,1,4,3}
};
print2dArray(arr, "The 2d Array");
retArr=ap.matArrayColumnSum(arr);
printArraySummary(retArr, "Sum of columns");
int[][] arr1 = { {3,1,-5,6},
{-9,3,2,7},
{-1,0,0,9}
};
print2dArray(arr1, "The 2d Array");
retArr=ap.matArrayColumnSum(arr1);
printArraySummary(retArr, "Sum of Columns");
}catch (Exception exception) {
System.out.print("Exception,"+ exception);
exception.printStackTrace();
}
}
public static void print2dArray(int[][] intArray, String label) throws Exception {
// Case 1: The input Array is null !!
if (intArray == null) { System.out.println("\n\n Input 2d Array was null !! \n"); return; }
// Case 2: Print input Array by index (first to last)
System.out.println();
System.out.println("*********************************************************************");
System.out.print(label+" : \n");
int rIdx=0; int cIdx=0;
for (rIdx=0; rIdx < intArray.length; rIdx++) {
for (cIdx=0; cIdx < intArray[rIdx].length; cIdx++) {
System.out.print(intArray[rIdx][cIdx]);
if (cIdx < intArray[rIdx].length) System.out.print(",");
}
System.out.print("\n");
}
System.out.println();
System.out.println("**********************************************************************");
System.out.println();
Thread.sleep(2000);
return;
}
public static void printArraySummary(int[] intArray, String label) throws Exception {
// Case 1: The input Array is null !!
if (intArray == null) { System.out.println("\n\n Input Array was null !! \n"); return; }
// Case 2: Print input Array by index (first to last)
System.out.println();
System.out.println("************************************************************************");
System.out.print(label+" : ");
int arrayIndex=0;
for (arrayIndex=0; arrayIndex< intArray.length; arrayIndex++) {
System.out.print(intArray[arrayIndex]);
if (arrayIndex< intArray.length-1) {System.out.print(",");}
}
System.out.println();
System.out.println("*************************************************************************");
System.out.println();
Thread.sleep(2000);
return;
}
}