Given an input matrix mat. Find and return sum of elements of each row of the matrix.
| Input Parameters |
Expected outputs |
mat = {
{3,1,5,6},
{9,3,5,0},
{1,1,7,3}};
|
Sum of rows :
[0] = 15
[1] = 17
[2] = 12
|
mat = {
{-4,1,5,6},
{9,3,5,-6},
{1,0,-0,3}};
|
Sum of rows :
[0] = 8
[1] = 11
[2] = 4
|
| The java method should accept following input parameters: mat (integer matrix).
|
| Declare int variable sumRows to hold sum of a particular row.
|
| Declare int array arrSumRows of size equal to number of rows of mat. Elements of arrSumRows will hold the sums of individual rows of mat.
|
| 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:
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:
Increment sumRows by mat[rIdx][cIdx] in each iteration.
Once inner loop completes, sumRows has the sum of current row of mat. Store value of sumRows in arrSumRows[rIdx].
|
| After both above loops are completed, arrSumRows contains the sum of individual rows of mat. Return arrSumRows.
|
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 MdArrayRowSum {
public int[] matRowSum(int[][] mat) throws Exception{
int sumRow;
int[] arrSumRows=new int[mat.length];
for (int rIdx=0; rIdx < mat.length; rIdx++) {
sumRow=0;
for (int cIdx=0; cIdx < mat[rIdx].length; cIdx++) {
sumRow += mat[rIdx][cIdx];
}
arrSumRows[rIdx]=sumRow;
}
return arrSumRows;
}
public static void main(String[] args) {
MdArrayRowSum ap=new MdArrayRowSum();
int[] retArr;
try {
int[][] arr = { {3,1,5,6},
{9,3,5,0},
{1,1,7,3}
};
print2dArray(arr, "The 2d Array");
retArr=ap.matRowSum(arr);
printArray(retArr, "Sum of rows");
int[][] arr1 = { {-4,1,5,6},
{9,3,5,-6},
{1,0,-0,3}
};
print2dArray(arr1, "The 2d Array");
retArr=ap.matRowSum(arr1);
printArray(retArr, "Sum of rows");
}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 printArray(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+" : \n");
int arrayIndex=0;
for (arrayIndex=0; arrayIndex < intArray.length; arrayIndex++) {
System.out.print("["+arrayIndex+"] = "+intArray[arrayIndex]+"\n");
}
System.out.println();
System.out.println("**********************************************************************");
System.out.println();
Thread.sleep(2000);
return;
}
}