Assignment: 2D Arrays

Assignment: 2D Arrays - Basics of C++ - Software Development

MCQs (Multiple Choice Questions)

Q.1. What is a 2D array?
(a) An array with 2 elements
(b) An array with 2 dimensions
(c) An array with 2 data types
(d) An array with 2 indexes

Ans. (b)

Q.2. How do you declare a 2D array in C++?
(a) int array[2][2];
(b) int array(2)(2);
(c) int array{2, 2};
(d) int array[2, 2];

Ans. (a)

Q.3. What is the result of adding two 2D arrays element-wise?
(a) Concatenation of the arrays
(b) Matrix multiplication
(d) Subtraction of corresponding elements

Ans. (c)

Q.4. Which loop structure is commonly used to iterate over a 2D array?
(a) for loop
(b) while loop
(c) do-while loop
(d) foreach loop

Ans. (a)

Q.5. Which of the following is true about a 2D array in C++?
(a) The size of a 2D array must be known at compile time.
(b) A 2D array can have a different number of columns for each row.
(c) A 2D array can only store integers.
(d) A 2D array can only have two rows and two columns.

Ans. (b)

Fill in the Blanks

Q.1. A 2D array can be thought of as an array of __________.

Ans. arrays

Q.2. To declare a 2D array in C++, we specify the number of _________ and _________.

Ans. rows, columns

Q.3. The elements of a 2D array are accessed using _______ and _______ indices.

Ans. row, column

Q.4. The size of a 2D array with n rows and m columns is ________.

Ans. n * m

Q.5. To initialize a 2D array during declaration, we can use ________ initialization.

Ans. brace

True or False

Q.1. A 2D array can be jagged, meaning different rows can have a different number of elements.

Ans. True

Q.2. The index of the first row in a 2D array is 1.

Ans. False

Q.3. A 2D array can store elements of different data types.

Ans. True

Q.4. The number of elements in a 2D array with n rows and m columns is n * m.

Ans. True

Q.5. A 2D array can be resized dynamically during runtime.

Ans. False

Hands-On Questions

Q.1. Write a C++ program to add two 2D arrays and store the result in a third 2D array. Display the resulting array.

#include <iostream>

const int ROWS = 2;

const int COLS = 2;

void addArrays(int arr1[ROWS][COLS], int arr2[ROWS][COLS], int result[ROWS][COLS])

{

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

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

result[i][j] = arr1[i][j] + arr2[i][j];

}

}

}

void displayArray(int arr[ROWS][COLS])

{

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

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

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

}

std::cout << std::endl;

}

}

int main()

{

int arr1[ROWS][COLS] = {{1, 2}, {3, 4}};

int arr2[ROWS][COLS] = {{5, 6}, {7, 8}};

int result[ROWS][COLS] = {};

std::cout << "Resultant Array:\n";

displayArray(result);

return 0;

}

Q.2. Write a C++ program to find the maximum element in a given 2D array and display its value.

#include <iostream>

const int ROWS = 3;

const int COLS = 4;

int findMaxElement(int arr[ROWS][COLS])

{

int maxElement = arr[0][0];

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

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

if (arr[i][j] > maxElement) {

maxElement = arr[i][j];

}

}

}

return maxElement;

}

int main()

{

int arr[ROWS][COLS] = {

{4, 2, 7, 1},

{6, 9, 5, 3},

{8, 0, 2, 4}

};

int maxElement = findMaxElement(arr);

std::cout << "Maximum element: " << maxElement << std::endl;

return 0;

}

Q.3. Write a C++ program to transpose a given 2D array. Display the original and transposed arrays.

#include <iostream>

const int ROWS = 3;

const int COLS = 4;

void transposeArray(int arr[ROWS][COLS], int transposed[COLS][ROWS])

{

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

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

transposed[j][i] = arr[i][j];

}

}

}

void displayArray(int arr[ROWS][COLS])

{

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

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

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

}

std::cout << std::endl;

}

}

int main()

{

int arr[ROWS][COLS] = {

{1, 2, 3, 4},

{5, 6, 7, 8},

{9, 10, 11, 12}

};

int transposed[COLS][ROWS] = {};

std::cout << "Original Array:\n";

displayArray(arr);

transposeArray(arr, transposed);

std::cout << "\nTransposed Array:\n";

displayArray(transposed);

return 0;

}

Q.4. Write a C++ program to check if a given 2D array is symmetric or not. A matrix is said to be symmetric if it is equal to its transpose.

#include <iostream>

const int SIZE = 3;

bool isSymmetric(int arr[SIZE][SIZE])

{

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

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

if (arr[i][j] != arr[j][i]) {

return false;

}

}

}

return true;

}

int main()

{

int symmetricArray[SIZE][SIZE] = {

{1, 2, 3},

{2, 4, 5},

{3, 5, 6}

};

int nonSymmetricArray[SIZE][SIZE] = {

{1, 2, 3},

{4, 5, 6},

{7, 8, 9}

};

if (isSymmetric(symmetricArray)) {

std::cout << "The array is symmetric.\n";

} else {

std::cout << "The array is not symmetric.\n";

}

if (isSymmetric(nonSymmetricArray)) {

std::cout << "The array is symmetric.\n";

} else {

std::cout << "The array is not symmetric.\n";

}

return 0;

}

Q.5. Write a C++ program to find the sum of each row and each column in a given 2D array. Display the sums.

#include <iostream>

const int ROWS = 3;

const int COLS = 4;

void calculateSums(int arr[ROWS][COLS])

{

int rowSums[ROWS] = {};

int colSums[COLS] = {};

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

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

rowSums[i] += arr[i][j];

colSums[j] += arr[i][j];

}

}

std::cout << "Row sums:\n";

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

std::cout << "Row " << i + 1 << ": " << rowSums[i] << std::endl;

}

std::cout << "\nColumn sums:\n";

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

std::cout << "Column " << j + 1 << ": " << colSums[j] << std::endl;

}

}

int main()

{

int arr[ROWS][COLS] = {

{1, 2, 3, 4},

{5, 6, 7, 8},

{9, 10, 11, 12}

};

calculateSums(arr);

return 0;

}

The document Assignment: 2D Arrays | Basics of C++ - Software Development is a part of the Software Development Course Basics of C++.
All you need of Software Development at this link: Software Development

Basics of C++

70 videos|45 docs|15 tests

Basics of C++

70 videos|45 docs|15 tests

Up next

 Explore Courses for Software Development exam

Top Courses for Software Development

Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev
Track your progress, build streaks, highlight & save important lessons and more!
Related Searches

,

,

,

,

,

,

,

,

,

,

,

,

,

,

,

,

,

,

,

,

,

;