Menu Close

Calculate the sum of boundary elements of a matrix

Advika bought a cadbaury gems pocket for his friends. She opened the pocket and arranged the gems colour wise. After finishing the arrangement it looked like a matrix. 

Now She wanted to calculate sum of boundary elements of a matrix. 
If you help Advika, she will share chocolates with you.

Input:
The first input has two numbers r and c respectively.
Then r rows with c columns follow

Output:
print the single value representing sum of boundary elements of a Matrix.
#include <stdio.h>
int main()
{
  int r,c;
  int arr[100][100],sum = 0,i,j;
  scanf("%d %d",&r,&c);
  for(i = 0;i < r;i++){
    for(j=0;j < c;j++){
      scanf("%d",&arr[i][j]);
    }
  }
  for(i=0;i<r;i++){
    for(j=0;j<c;j++){
      if(i==0 || j==0 || i==r-1 || j==c-1){
        sum+=arr[i][j];
      }
    }
  }
  //printf("Sum: ");
  printf("%d",sum);
return 0;
}

INPUT_1:
3  3
1  2  3
4  5  6
7  8  9

OUTPUT:
40


INPUT_2:
2  3
1  2  3
4  5  4

OUTPUT:
19



INPUT_3:
3  3
100  200  300
50  26  80
600  120  45

OUTPUT:
Sum: 1495


ILLUSTRATION:

EXECUTED USING GCC

Morae! Q