Home > Software engineering >  C Program- How to find sum of middle elements in a matrix?
C Program- How to find sum of middle elements in a matrix?

Time:04-06

Given a 2-Dimensional Array, sum up all the numbers that are not on the edges of the 2-D array.

Example 2-D Array:

5 7 1 6
2 6 1 8
1 5 4 7
5 8 9 1
4 4 5 1

The numbers added should only be 6 1 5 4 8 9.

  int rows, cols,s1=0,s2=0;
    printf("Enter number of rows: ");
    scanf("%d", &rows);
    printf("Enter number of columns: ");
    scanf("%d", &cols);
    
    int matrix[rows][cols];
  
  printf("Elements:\n");
    for(int row = 0; row < rows; row  ){
        for(int col = 0; col < cols; col  ){
            scanf("%d", &matrix[row][col]);
        }
  }
  printf(Sum: %d", __);
......
  

CodePudding user response:

Write a function:

static int sum_middle(int rows, int cols, int matrix[rows][cols])
{
    int sum = 0;
    for (int r = 1; r < rows - 1; r  )
    {
        for (int c = 1; c < cols - 1; c  )
             sum  = matrix[r][c];
    }
    return sum;
}

Then call it after your input loops:

int sum = sum_middle(rows, cols, matrix);

CodePudding user response:

You could change your input functionality to calculate the sum as you go along:

    int sum = 0;
    for(int row = 0; row < rows; row  ){
        for(int col = 0; col < cols; col  ){
            scanf("%d", &matrix[row][col]);
            if(
               row && (rows > 2) && (row   1) != rows &&
               col && (cols > 2) && (col   1) != cols
            )
               sum  = matrix[row][col];
        }
    }

Or you could do this in a 2nd loop (see @JonathanLeffler's answer). This would allow you to do the rows and cols check only once instead of per iteration (i.e. if rows = INT_MAX and cols is 0, 1 or 2).

CodePudding user response:

Let me explain you how to do this in pseudo-code:

You need to sum all numbers which don't pass a certain criteria. There are two ways to do this:

  1. Sum all number which don't pass a certain criteria. This can be done if the criteria is not too difficult.
  2. Sum all numbers. Sum all numbers who pass a certain criteria and subtract that from the first sum.

Let's see if we can accomplish in the first way: we start by summing all numbers:

sum = 0
for i = 0 to a-1:
  for j = 0 to b-1:
    sum  = matrix[i,j];
  next j
next i

Now we need to add the criteria:

sum = 0
for i = 0 to a-1:
  for j = 0 to b-1:
    if not(criteria(i,j))
    then sum  = matrix[i,j];
  next j
next i

But what is the criteria: how to you say (i,j) belongs to an edge?
Well, that's quite simple: the edges mean that i equals either 0 or a, and j equals 0 or b:

So you write a function criteria(i,j) as follows:

boolean criteria(i,j):
  return ((i == 0) OR (i == a)) AND 
         ((j == 0) OR (j == b));

Or, as the criteria is so simple, you don't even need a function for this:

sum = 0
for i = 0 to a-1:
  for j = 0 to b-1:
    if not(((i == 0) OR (i == a)) AND 
           ((j == 0) OR (j == b)))
    then sum  = matrix[i,j];
  next j
next i

But as you might expect, there's an easier way to write the criteria, using not(x AND y) = not(x) OR not(y) and not (x OR y) = not(x) AND not(y), but this one I leave to you :-)

  • Related