question

Narasimha-5266 avatar image
0 Votes"
Narasimha-5266 asked JeanineZhang-MSFT answered

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

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

Thanks,
Santosh.

c++
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

JeanineZhang-MSFT avatar image
0 Votes"
JeanineZhang-MSFT answered

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.


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

rupeshshukla-4529 avatar image
1 Vote"
rupeshshukla-4529 answered

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

Thanks

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.