0% found this document useful (0 votes)
5 views

week 6 assignment 1 sol.

The document provides solutions for various programming assignments related to 2D arrays in C++. It includes code examples for initializing a matrix, adding two matrices, summing elements in a specified rectangle, finding the largest element in a matrix, identifying the row with the maximum sum, and displaying elements from the middle row and column of a square matrix. Each solution is accompanied by input and output examples to illustrate functionality.

Uploaded by

prakashgyan1067
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

week 6 assignment 1 sol.

The document provides solutions for various programming assignments related to 2D arrays in C++. It includes code examples for initializing a matrix, adding two matrices, summing elements in a specified rectangle, finding the largest element in a matrix, identifying the row with the maximum sum, and displaying elements from the middle row and column of a square matrix. Each solution is accompanied by input and output examples to illustrate functionality.

Uploaded by

prakashgyan1067
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Assignment Solutions | 2D Arrays - 1 | Week 6

Write a program to store 10 at every index of a 2D matrix with 5 rows and 5 columns.

Solution :

#include<iostream>
using namespace std;

int main(){
int matrix[5][5];

for(int i=0;i<5;i++){
for(int j=0;j<5;j++){
matrix[i][j] = 10;
}
}

for(int i=0;i<5;i++){
for(int j=0;j<5;j++){
cout << matrix[i][j] << " ";
}
cout<<endl;
}
}

Write a program to add two matrices and save the result in one of the given matrices.

Input 1:

123

456

789

458

008

120
Output 1:

5 7 11

4 5 14

8 10 9

Solution :

#include<iostream>
using namespace std;

int main(){

int n , m;
cout << "Enter the number of rows : ";
cin >> n;

cout << "Enter the number of columns : ";


cin >> m;

int a[n][m];
cout << "Enter the first matrix : "<<endl;
for(int i = 0 ; i < n ; i++){
for(int j = 0 ; j < m ; j++){
cin >> a[i][j];
}
}

int b[n][m];
cout << "Enter the second matrix : "<<endl;
for(int i = 0 ; i < n ; i++){
Q3: Given a matrix ‘A’ of dimension n x m and 2 coordinates (l1, r1) and (l2, r2). Return the sum of the
rectangle from (l1,r1) to (l2, r2).

Input 1:

1 2 -3 4

0 0 -4 2

1 -1 2 3

-4 -5 -7 0

l1 = 1, r1 = 2 , l2 = 3 , r2 = 3

Output 1: -4

Input 2:

1 2 -3 4

0 0 -4 2

1 -1 2 3

-4 -5 -7 0

l1 = 1, r1 = 0 , l2 = 0 , r2 = 3

Output 1: 2

Solution :
#include<iostream>
using namespace std;

int main(){

int n,m;
cout << "Enter the number of rows : ";
cin >> n;
cout << "Enter the number of columns : ";
cin >> m;

int a[n][m];
cout << "Enter the matrix element : ";
for(int i = 0 ; i < n ; i++){
for(int j = 0 ; j < m ; j++){
cin >> a[i][j];
}
}

int l1 , l2 , r1 , r2;

cout << "Enter the value of l1 coordinate : ";


cin >> l1;

Q4: Write a C++ program to find the largest element of a given 2D array of integers.

Input 1:

1346

2457

3568
4679

Output 1: 9

Solution :

#include<iostream>
using namespace std;

int main(){

int n , m;
cout << "Enter the number of rows : ";
cin >> n;

cout << "Enter the number of columns : ";


cin >> m;

int a[n][n];
cout << "Enter the matrix elements : ";
for(int i = 0 ; i < n ; i++){
for(int j = 0 ; j < m ; j++){
cin >> a[i][j];
}
}

int maximum = -1000000;

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

Q5: Write a program to print the row number having the maximum sum in a given matrix.

Input 1:

1357

3478

1 4 12 3

Output 1: 2

Explanation : The 2nd row has the maximum sum i.e. 1+4+12+3 = 20

Solution :
#include<iostream>
using namespace std;

int main(){

int n , m;
cout << "Enter the number of rows : ";
cin >> n;

cout << "Enter the number of columns : ";


cin >> m;

int a[n][n];
cout << "Enter the matrix elements : ";
for(int i = 0 ; i < n ; i++){
for(int j = 0 ; j < m ; j++){
cin >> a[i][j];
}
}

int maximum = -1000000;


int rowNumber = -1;

Q6: Write a function which accepts a 2D array of integers and its size as arguments and displays the
elements of middle row and the elements of middle column.

[Assuming the 2D Array to be a square matrix with odd dimensions i.e. 3x3, 5x5, 7x7 etc...]

Input 1:

12345

34567

76543

87654

1 2 37 8 0

Output 1:
3
5

7 6 5 4 3

6
37

Solution :

#include<iostream>
using namespace std;

int main(){

int n;
cout << "Enter the number of rows : ";
cin >> n;

int a[n][n];
cout << "Enter the matrix elements : ";
for(int i = 0 ; i < n ; i++){
for(int j = 0 ; j < n ; j++){
cin >> a[i][j];
}
}

cout << "The elements of the middle row and middle column are as follows :
"<<endl;
int i,j;
for(i = 0 ; i < n ; i++){
for(int j = 0 ; j < n ; j++){
if(i == n/2 or j == n/2)cout<<a[i][j]<<" ";

You might also like