Breaking

Tuesday, 19 September 2017

Matrix Row Sum

                             Matrix Row Sum
skillrack matrix row sum

Given a R*C matrix (R - rows and C- Columns), print the sum of the values in each row as the output.
Input Format:
First line will contain R and C separated by a space.
Next R lines will contain C values, each separated by a space.
Output Format:
R lines representing the sum of elements of rows 1 to R.
Boundary Conditions:
2 <= R, C <= 50
Example Input/Output 1:
Input:
4 4
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
Output:
10
26
42
58
Code:

#include <iostream>

using namespace std;

int main(int argc, char** argv)
{
    int r,c,i,j,sum=0;
cin>>r>>c;
int arr;
for(i=0;i<r;i++)
{
    for(j=0;j<c;j++)
    {
        cin>>arr;
        sum+=arr;
    }
    cout<<sum<<"\n";
    sum=0;
}
}

Please do comment If u have any Queries!

5 comments:

  1. ::::PYTHON 3 :::

    dim = list(map(int, input().split()))
    for i in range(0,dim[0]):
    a = list(map(int, input().split()))
    print(sum(a))

    ReplyDelete
  2. #include

    using namespace std;

    int main(int argc, char** argv)
    {
    int r,c,t;
    cin>>r>>c;
    for(int i=0;i>t;
    sum+=t;
    }
    cout<<sum<<"\n";
    }
    }

    ReplyDelete
  3. #include

    using namespace std;

    int main(int argc, char** argv)
    {
    int r,c,t;
    cin>>r>>c;
    for(int i=0;i>t;
    sum+=t;
    }
    cout<<sum<<"\n";
    }
    }

    ReplyDelete
  4. #include

    using namespace std;

    int main(int argc, char** argv)
    {
    int r,c,t;
    cin>>r>>c;
    for(int i=0;i<r;i++){
    int sum=0;
    for(int j=0;j<c;j++){
    sum+=t;
    }
    cout<<sum<<"\n";
    }
    }

    ReplyDelete
  5. import java.util.*;
    public class Hello {

    public static void main(String[] args) {
    //Your Code Here
    Scanner sc=new Scanner(System.in);
    int a=sc.nextInt();
    int b=sc.nextInt();
    int arr[][]=new int[a][b];
    //int sum=0;
    for(int i=0;i<a;i++)
    {
    for(int j=0;j<b;j++)
    {
    arr[i][j]=sc.nextInt();
    }
    }
    for(int i=0;i<a;i++)
    {
    int sum=0;
    for(int j=0;j<b;j++)
    {
    sum+=arr[i][j];
    }
    System.out.println(sum);
    }
    //System.out.println("krce");

    }
    }

    ReplyDelete

Like