Is there any way to find sum of diagonals for every submatrix in matrix by using c language.

Narasimha 61 Reputation points
2021-07-23T05:58:41.097+00:00

Iam trying to find sum of diagonals for every sub matrix in matrix by using c. Can anyone please help me

Thanks,
Santosh.

Developer technologies | C++
0 comments No comments
{count} votes

Accepted answer
  1. Jeanine Zhang-MSFT 11,356 Reputation points Microsoft External Staff
    2021-07-26T07:31:37.033+00:00

    Hi,

    First you could try to use for loop to iterate every cell present in the matrix. Conditions inside the for loops ((rows < i) and (columns < j)) will ensure the program compiler, not to exceed the Matrix limit. Otherwise, the matrix will overflow. And we could use the scanf statement inside the for loop will store the user entered values in every individual array element.

    In the next for loop, we could find sum of diagonal elements of a matrix.

    Here is my code:

    for(rows = 0; rows < i; rows++)    
    {    
    	for(columns = 0;columns < j;columns++)    
    {  
    	scanf("%d", &a[rows][columns]);    
    }  
    }    
          
    for(rows = 0; rows < i; rows++)    
    {    
    	Sum = Sum + a[rows][rows];    
    }    
     
    printf("\n The Sum of Diagonal Elements of a Matrix =  %d", Sum );    
    

    Best Regards,

    Jeanine


    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. rupesh shukla 16 Reputation points
    2021-07-23T16:23:55.827+00:00

    This forum is only for Question and Answer not for any home work or assignment .

    Thanks

    1 person found this answer helpful.
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.