Showing posts with label Matrix. Show all posts
Showing posts with label Matrix. Show all posts

Saturday, June 20, 2020

Matrix Addition

Given two matrices. Print the sum of two matrices.

Input Format:

Enter the row and column of a matrix Enter the matrix

Output Format:

Addition of a matrix

Sample Input:

2

2

1

2

3

4

1

2

3

4

Sample Output:

2 4

6 8

SOLUTION:

#include<iostream>

using namespace std;

int main(){

int r, c, mat1[100][100], mat2[100][100], sum[100][100], i, j;

cin >> r >> c;

//1st matrix

for(i=0; i<r; ++i){

for(j=0; j<c; ++j){

cin >> mat1[i][j];

}

}

//2nd matrix

for(i=0; i<r; ++i){

for(j=0; j<c; ++j){

cin >> mat2[i][j];

}

}

// Adding Two matrices

for(i=0;i<r;++i)

for(j=0;j<c;++j){

sum[i][j]=mat1[i][j]+mat2[i][j];

}

 // print the result

for(i=0;i<r;++i)

for(j=0;j<c;++j){

cout <<sum[i][j] << " ";

if(j==c-1){

cout << "\n";

}

}

return 0;

}


Maximum element in the matrix

Raju is the maths teacher in high secondary school and provided mark sheet to students.In this class room, students are arranged in the form of rows and columns. Raju needs to find the highest mark in this class. Help him to find out.

INPUT FORMAT:

The input consists of (m*n+2) integers.

The first integer corresponds to m, the number of rows in the matrix and the second integer corresponds to n, the number of columns in the matrix.

The remaining integers correspond to the elements in the matrix.

The elements are read in row-wise order, the first row first, then the second row and so on.

Assume that the maximum value of m and n is 10.

OUTPUT FORMAT:

Refer to the sample output for details.

SAMPLE INPUT & OUTPUT:

3

2

4 5

6 9

0 3

The maximum element is 9

SOLUTION:

#include<iostream>

using namespace std;

int main()

{

  int r, c;

  cin>>r>>c;

  int a[r][c];

  for(int i = 0; i < r; i++){

    for(int j = 0; j < c; j++){

      cin>>a[i][j];

    }

  }

  int temp = a[0][0];

  for(int i = 0; i < r; i++){

    for(int j = 0; j < c; j++){

      if(temp < a[i][j])

        temp = a[i][j];

    }

  }

  cout<<"The maximum element is "<<temp;

}

horizontal ads