Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Is sum of every row and column in matrix same

I have a problem. I need to write programe that checks if every row has same sum , and if every collumn has same sum. Example:

 3 3 3 
 3 3 3

In this example output should be "YES" for rows and "YES" for collumns. Another example:

 4 5 6
 6 4 5

In this examle output should be "YES" for rows because sum of 1st and 2nd row is the same (15), and it should be "NO" for collumns because sum is not the same (10,9,11).

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I have made code that checks first row and first collumn than it compares if they are same as the other ones. I have made it to check if its not but i dont know how to check it if it is , i mean how to ouput "YES" for both cases.

THIS IS MY CODE:

#include <stdio.h>

int main() {
  int mat[100][100];
  int n, m;
  int i, j;
  int sumak = 0;
  int sumar = 0;
  int sumarp = 0;
  int sumakp = 0;
  do {
    printf("Unesite brojeve M i N: ");
    scanf("%d %d", &m, &n);
  } while (m < 0 || m > 100 || n < 0 || n > 100);

  printf("Unesite clanove matricee: ");
  for (i = 0; i < m; i++) {
    for (j = 0; j < n; j++) {
      scanf("%d", &mat[i][j]);
    }
  }
  // suma of first row
  for (i = 0; i < 1; i++) {
    for (j = 0; j < n; j++) {
      sumarp = sumarp + mat[i][j];
    }
    sumarp = sumarp + mat[i][j];
  }
  // sum of all rows
  for (i = 1; i < m; i++) {
    for (j = 0; j < n; j++) {
      sumar = sumar + mat[i][j];
    }
    if (sumarp != sumar) {
      printf("NE");
    } else {
      sumar = 0;
      continue;
    }
  }

  // sum of the first collumn

  for (j = 0; j<1; j++) {
    for (i = 0;  i< m;i++ ) {
      sumakp = sumakp + mat[i][j];
    }
    sumakp = sumakp + mat[i][j];
  }

 

  // sum of every collumn

  for (j = 1; j < n; j++) {
    for (i= 0; i < m; i++) {
      sumak = sumak + mat[i][j];
    }
    if (sumakp == sumak) {
        sumak=0;
      continue;
    } 
    if(sumakp!=sumak)
    {
        printf("NE");
        return 0;
    }
    else{
        printf("DA");
    } 
  }



}

So if someone can explain me how to do the rest of it.

Thank you!

>Solution :

Use a variable to indicate whether any of the rows or columns didn’t match. Then check it at the end of the loop.

Here’s how to do it for rows. Columns are similar, just switch the order of the i and j loops.

int all_matched = 1;
for (int i = 0; i < m; i++) {
    int sumar = 0;
    for (int j = 0; j < n; j++) {
        sumar += mat[i][j];
    }
    if (sumar != sumarp) {
        all_matched = 0;
        break;
    }
}
if (all_matched) {
    printf("EQ\n");
} else {
    printf("NE\n");
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading