Breaking

Friday 1 September 2017

Matrix - Sum of Edge Elements

                 Matrix - Sum of Edge Elements



The input elements of R*C matrix is passed as the input (R is the number of rows and C is the number of columns in the matrix. The program must print the sum S of the elements along the edge of the matrix.
Input Format:
The first line contains R and C separated by a space..
Next R lines contain C values each, with the values separated by a space.
Output Format:
The first line contains S.
Boundary Conditions:
2 <= R, C <= 100
1 <= Matrix Cell Value <= 1000
Example Input/Output 1:
Input:
5 3
1 2 3
4 5 6
7 8 9
5 5 5
2 2 2
Output:
48
Example Input/Output 2:
Input:
3 3
100 200 300
400 500 600
700 800 900
Output:
4000

Code:
#include <iostream>
using namespace std;
int main(int argc, char** argv)
{
int n,m,i,j,sum=0;
cin>>n>>m;
int arr[n][m];
for(i=0;i<n;i++)
{
    for(j=0;j<m;j++)
    {
        cin>>arr[i][j];
        if(i==0||i==n-1||j==0||j==m-1)
               sum+=arr[i][j];
    }
}
cout<<sum; 
}

Please do comment If u have any Queries!

1 comment:

  1. *FOR JAVA*

    import java.util.*;
    import java.io.*;
    class Matrix
    {
    public static void main(String[] args)
    {
    int r,c,sum=0;
    Scanner s=new Scanner(System.in);
    r=s.nextInt();
    c=s.nextInt();
    int a[][]=new int[r][c];
    for(int i=0;i<r;i++)
    {
    for(int j=0;j<c;j++)
    {
    a[i][j]=s.nextInt();
    if(i==0||i==r-1||j==c-1||j==0)
    sum+=a[i][j];
    }
    }
    System.out.println(sum);
    }
    }

    ReplyDelete

Like