0% found this document useful (0 votes)
3 views6 pages

Questions With Solutions Two Diminarray

The document contains a series of C++ programming questions and solutions related to 2D arrays. It includes tasks such as inputting and printing a matrix, calculating sums of elements, finding maximum values, transposing matrices, checking for identity matrices, and printing diagonal elements. Each question is followed by a complete code solution demonstrating the required functionality.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views6 pages

Questions With Solutions Two Diminarray

The document contains a series of C++ programming questions and solutions related to 2D arrays. It includes tasks such as inputting and printing a matrix, calculating sums of elements, finding maximum values, transposing matrices, checking for identity matrices, and printing diagonal elements. Each question is followed by a complete code solution demonstrating the required functionality.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Questions with Solutions (2D Arrays in C++)

Question 1: Input and Print a Matrix

Write a program to input and print a 2D array.

Solution:

#include <iostream>

using namespace std;

int main() {

int rows = 2, cols = 3;

int arr[2][3];

cout << "Enter elements:\n";

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

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

cin >> arr[i][j];

cout << "Matrix:\n";

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

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

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

cout << endl;

return 0;

}
Question 2: Sum of All Elements

Write a program to find the sum of all elements in a 2D array.

Solution:

#include <iostream>

using namespace std;

int main() {

int arr[2][2] = {{1, 2}, {3, 4}};

int sum = 0;

for (int i = 0; i < 2; i++)

for (int j = 0; j < 2; j++)

sum += arr[i][j];

cout << "Sum = " << sum;

return 0;

Question 3: Find Maximum Element

Write a program to find the largest element in a 2D array.

Solution:

#include <iostream>

using namespace std;

int main() {

int arr[2][2] = {{1, 9}, {3, 4}};

int max = arr[0][0];

for (int i = 0; i < 2; i++)

for (int j = 0; j < 2; j++)


if (arr[i][j] > max)

max = arr[i][j];

cout << "Max = " << max;

return 0;

Question 4: Sum of Each Row

Write a program to calculate the sum of each row in a 2D array.

Solution:

#include <iostream>

using namespace std;

int main() {

int arr[2][3] = {{1, 2, 3}, {4, 5, 6}};

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

int sum = 0;

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

sum += arr[i][j];

cout << "Sum of row " << i << " = " << sum << endl;

return 0;

Question 5: Sum of Each Column

Write a program to calculate the sum of each column.

Solution:
#include <iostream>

using namespace std;

int main() {

int arr[2][3] = {{1, 2, 3}, {4, 5, 6}};

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

int sum = 0;

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

sum += arr[i][j];

cout << "Sum of column " << j << " = " << sum << endl;

return 0;

Question 6: Transpose of a Matrix

Write a program to find the transpose of a matrix.

Solution:

#include <iostream>

using namespace std;

int main() {

int arr[2][3] = {{1, 2, 3}, {4, 5, 6}};

cout << "Transpose:\n";

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

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

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

}
cout << endl;

return 0;

Question 7: Check Identity Matrix

Write a program to check if a matrix is an identity matrix.

Solution:

#include <iostream>

using namespace std;

int main() {

int arr[3][3] = {

{1, 0, 0},

{0, 1, 0},

{0, 0, 1}

};

bool isIdentity = true;

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

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

if (i == j && arr[i][j] != 1)

isIdentity = false;

if (i != j && arr[i][j] != 0)

isIdentity = false;

if (isIdentity)
cout << "Identity Matrix";

else

cout << "Not Identity Matrix";

return 0;

Question 8: Diagonal Elements

Print the main diagonal elements of a square matrix.

Solution:

#include <iostream>

using namespace std;

int main() {

int arr[3][3] = {

{1, 2, 3},

{4, 5, 6},

{7, 8, 9}

};

cout << "Main diagonal: ";

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

cout << arr[i][i] << " ";

return 0;

You might also like