0% found this document useful (0 votes)
9 views4 pages

2dArrays_homework

The document contains a set of programming tasks focused on 2D arrays, including traversing, summing elements, and manipulating matrices. It provides specific challenges such as printing a matrix, calculating row sums, finding the largest number, transposing a matrix, summing diagonal elements, and creating a Tic-Tac-Toe board. Each task includes example inputs and expected outputs to guide the implementation.

Uploaded by

Ridm D
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)
9 views4 pages

2dArrays_homework

The document contains a set of programming tasks focused on 2D arrays, including traversing, summing elements, and manipulating matrices. It provides specific challenges such as printing a matrix, calculating row sums, finding the largest number, transposing a matrix, summing diagonal elements, and creating a Tic-Tac-Toe board. Each task includes example inputs and expected outputs to guide the implementation.

Uploaded by

Ridm D
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
You are on page 1/ 4

Homework: 2D Arrays Practice Questions

1. 2D Array Traversal
✏️ Write a Python or Java program to complete the following tasks.
a) Print a 2D Array in Matrix Format
i) Write a program that prints the following 2D array as a matrix:
ii) create a function that takes in any 2D array and prints it in a matrix format
Given array:

1 2 3
4 5 6
7 8 9

Expected Output:

1 2 3
4 5 6
7 8 9

b) Sum of All Elements in a 2D Array


i) Write a program to find the sum of all elements in a 3×3 matrix.
ii) create a function that takes in any 2D array and does the above
Example Input:

matrix = [[2, 4, 6], [1, 3, 5], [7, 8, 9]]

Expected Output:

Sum of all elements: 45


2. Working with Rows and Columns
📝 Solve these programming problems:
a) Sum of Each Row in a Matrix
i) Write a program to compute the sum of each row in a 3×3 matrix.
ii) Create a function that takes in any 2D array and does the above
Example Input:

matrix = [[3, 6, 9], [2, 4, 8], [1, 5, 7]]

Expected Output:

Row 1 sum = 18
Row 2 sum = 14
Row 3 sum = 13

b) Find the Largest Number in a 2D Array


i) Write a program that finds and prints the largest number in a 2D array.
ii) Create a function that takes in any 2D array and does the above
Example Input:

matrix = [[11, 25, 9], [42, 8, 17], [33, 19, 21]]

Expected Output:

The largest number is: 42

3. Special Challenges
🎯 Challenge your understanding with these problems:
(if you can do these i would be confident in your knowledge of loops, arrays and Decomposition)
a) Transpose of a Matrix
Write a program to find the transpose of a given 3×3 matrix.
(The transpose is obtained by swapping rows and columns.)

Example Input:

1 2 3
4 5 6
7 8 9

Expected Output (Transpose):

1 4 7
2 5 8
3 6 9

b) Diagonal Sum of a Matrix


Write a program to find the sum of the diagonal elements in a 3×3 matrix.

Example Input:

1 2 3
4 5 6
7 8 9

Expected Output:

Sum of main diagonal: 15 (1 + 5 + 9)

c) Tic-Tac-Toe Board Representation


Write a program that initializes a 3×3 Tic-Tac-Toe board using a 2D array and allows a player to
enter X or O in a specific row and column.

Example Input:
Enter row (0-2): 1
Enter column (0-2): 2
Enter value (X/O): X

Expected Output:

_ _ _
_ _ X
_ _ _

You might also like