Calculate Primary and Secondary Diagonal

Write an algorithm that calculates all the elements of rows and columns of a square matrix and calculate the total of primary and secondary diagonal.

Questions by wiseboy

Showing Answers 1 - 16 of 16 Answers

int sum=0;
For (int i=0;i<Line;i++)
    For(int j=0;j<COL;j++)
    {
         //Primary diagonal
         if(i==j )sum += A[i][j];
         //Secondary diagonal
         if(i==(COL-j))sum += A[i][j];
     }

  Was this answer useful?  Yes

Ashima

  • Aug 9th, 2012
 

Assuming the matrix size is row, col and i represents the row and j represents the col.
Get the primary Diagonal where i==j and secondary diagonal where i==j-1.
Below is the code for that.

Code
  1. for (int i = 0; i < row; i++)

  2.             {

  3.                 int j = 0;

  4.                 while (j < col)

  5.                 {

  6.                     //Primary diagonal

  7.                     if (i == j) sum += A[i][j];

  8.                     j++;

  9.                     //Secondary diagonal

  10.                     if (i == (col - j)) sum += A[i][j - 1];

  11.                 }

  12.             }

JosephVP

  • Jul 24th, 2014
 

As it is a Square Matrix, we can do with single Loop, Please follow the below algorithm.

Code
  1. private static void findSumOfPrimaryAndSecondaryDiagonal(int[][] sortedMatrix, int rowStartPos, int columnStartPos){

  2.                 int sumValuePrimary = 0;

  3.                 int sumValueSecondaryRight= 0;

  4.                 int sumValueSecondaryLeft= 0;

  5.                 for(int i=0;i<=rowStartPos;i++){

  6.                         sumValuePrimary += sortedMatrix[i][i];

  7.                         if(i!=rowStartPos){

  8.                                 sumValueSecondaryRight+=sortedMatrix[i][i+1];

  9.                         }

  10.                         if(i!=0){

  11.                                 sumValueSecondaryLeft+=sortedMatrix[i][i-1];

  12.                         }

  13.                 }

  14.                 System.out.println("Primary Total Is : "+sumValuePrimary+", Secondary Left Total Is : "+sumValueSecondaryLeft+", Secondary Right Total Is : "+sumValueSecondaryRight);

  15.         }



  Was this answer useful?  Yes

hema

  • Jan 3rd, 2015
 

Code
  1. package testCode;

  2.  

  3. public class SquareMatrix {

  4.        

  5.  

  6.         public static void main(String[] args) {

  7.         int [][] squarematrix =  {

  8.                       { 1,1,1,10},

  9.                       { 1,10,-1,2},

  10.                       { 1,-2,1,2},

  11.                       { 1,1,1,-2}

  12.                     };

  13.         int primryDiagonaSum = 0, secondaryDiagonalSum = 0;

  14.         int size = squarematrix.length -1;

  15.         for(int i = 0; i<squarematrix.length; i++){

  16.                 primryDiagonaSum += squarematrix[i][i];

  17.                 secondaryDiagonalSum+= squarematrix[i][size-i];

  18.         }

  19.         System.out.println(" Primary diagonal sum =" +primryDiagonaSum);

  20.         System.out.println(" Secondary diagonal sum =" + secondaryDiagonalSum);

  21.         }

  22.         }

  23.  

  Was this answer useful?  Yes

hema

  • Jan 3rd, 2015
 

Tested Java code

Code
  1. package testCode;

  2.  

  3. public class SquareMatrix {

  4.         public static void main(String[] args) {

  5.         int [][] squarematrix =  {

  6.                       { 1,1,1,10},

  7.                       { 1,10,-1,2},

  8.                       { 1,-2,1,2},

  9.                       { 1,1,1,-2}

  10.                     };

  11.         int primryDiagonaSum = 0, secondaryDiagonalSum = 0;

  12.         int size = squarematrix.length -1;

  13.         for(int i = 0; i<squarematrix.length; i++){

  14.                 primryDiagonaSum += squarematrix[i][i];

  15.                 secondaryDiagonalSum+= squarematrix[i][size-i];

  16.         }

  17.         System.out.println(" Primary diagonal sum =" +primryDiagonaSum);

  18.         System.out.println(" Secondary diagonal sum =" + secondaryDiagonalSum);

  19.         }

  20.         }

  21.  

  Was this answer useful?  Yes

Kavya oleti

  • Jan 22nd, 2017
 

If it is a square matrix order n
Primary diagonal
if(i==j)
{
sum+=a[i][j];
}
Secondary diagonal
if(i==(n-1)-j)
{
sum+=a[i][j];
}

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions