adding only negative values in matrix.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include<stdio.h>
#include<conio.h>

int main()
{
  int i,j,a[10][10],sum,m,n;
  printf("\ninsert lines : ");
  scanf ("%d",&m);
  printf("\ninsert columns : ");
  scanf ("%d",&n);
  for(i=0;i<m;i++)
       for(j=0;j<n;j++)
        {
          printf("insert values : ",i,j);
          scanf("%d",&a[i][j]);
        }
  sum = 0;
  for(i=0;i<m;i++)
  for(j=0;j<n;j++)
    {
      if ( i < j )
      sum = sum + a[i][j];
    }
  printf("\nmatrix\n");
  for(i=0;i<3;i++)
  {
   printf("\n");
   for(j=0;j<3;j++)
   printf("%d\t",a[i][j]);
  }  
  printf("\nsum of above main diagonal values : %d",sum);
  getchar();
  getchar();
  return 0;
}


So I have this code but I can't figure out how to add only negative values in the matrix. So I insert for example 1 -2 -3
4 5 6
7 8 9
it gets 1 for the awnser but it should get -5.Also i can't seem to make it work using while instead of for loop.
Last edited on
1
2
   if ( i < j )
      sum = sum + a[i][j];
That's summing the upper triangle of the matrix.
 0 -2 -3
 0  0  6
 0  0  0


You need to test the value of the cell.
Topic archived. No new replies allowed.