Apr 28, 2026 | 515 words | 5 min read
13.1.2. Task 0#
Learning Objectives#
Practice writing while loops and for loops in Python.
Introduction#
Matrices can be traversed in either row-major or column-major order: in row-major order,
we visit all elements in a row before moving to the next row, while in column-major
order, we visit all elements in a column before moving to the next column. In this
task, we will build matrices and traverse them in row-major order using both
while and for loops. You can read more about row-major and
column-major order on
Wikipedia.
Before beginning, be sure to familiarize yourself with the pre-class materials.
Task Instructions#
Start by making a copy of the ENGR133_Python_Template.py Python template and renaming it
as py3_pre_0_username.py. Make sure
to fill out all header information, including a short description of the code.
Create three User Defined Functions (UDFs) in your main script:
The first UDF, named
build_matrix, should create a return a 2D matrix (list of lists) of specified dimensions containing integers in numerical order starting from \(1\). This function should take the number of rows and columns of the desired matrix as its only arguments. The values of the matrix should be filled in row-major order (left to right, then top to bottom). For example, a call tobuild_matrix(3, 4)should return a representation of the following matrix:\[\begin{split}\begin{bmatrix} 1 & 2 & 3 & 4 \\ 5 & 6 & 7 & 8 \\ 9 & 10 & 11 & 12 \end{bmatrix}\end{split}\]Note
Do not use any external libraries such as NumPy, which we will explore later.
The second UDF, named
traverse_with_for, should take in a matrix,X, as its only argument, and use nestedforloops to traverse the matrix in row-major order, printing the value of each element.The third UDF, named
traverse_with_while, should take in a matrix,X, and a stopping number as its arguments. It should then use nestedwhileloops to traverse the matrix in row-major order, printing the value of each element until it reaches the stopping number. Do not print the stopping number or any numbers that come after it in the traversal.
Hint
Recall that python uses zero-based indexing, so the first element of the matrix can be accessed with
X[0][0]and the second element in the third row isX[2][1] = 10. We recommend that you draw out the matrix on paper to help you visualize how to access each element using loops.In your
mainfunction, ask the user to enter the number of rows and columns in the first matrix. Then use yourbuild_matrixUDF to create the matrix, and pass it to yourtraverse_with_forUDF to print all its elements. Next, ask the user to enter the number of rows and columns of the second matrix, as well as a stopping number. Use your Call yourbuild_matrixUDF again to create the second matrix, and pass it along with the stopping number to yourtraverse_with_whileUDF to print its elements until the stopping number is reached.Save the file as py3_pre_0_username.py and turn in the assignment on Gradescope.
Sample Output#
Use the values in Table 13.3 below to test your program.
Case |
Rows |
Columns |
Rows |
Columns |
Stop |
|---|---|---|---|---|---|
1 |
3 |
2 |
3 |
4 |
6 |
2 |
2 |
5 |
4 |
3 |
10 |
Ensure your program’s output matches the provided samples exactly. This includes all characters, white space, and punctuation. In the samples, user input is highlighted like this for clarity, but your program should not highlight user input in this way.
Case 1 Sample Output
$ python3 py3_pre_0_username.py Enter Matrix Dimensions Enter rows: 3 Enter columns: 2 FOR loop traversal: X[0,0] = 1 X[0,1] = 2 X[1,0] = 3 X[1,1] = 4 X[2,0] = 5 X[2,1] = 6
Enter Matrix Dimensions Enter rows: 3 Enter columns: 4 Enter Stop Value: 6 WHILE loop traversal: X[0,0] = 1 X[0,1] = 2 X[0,2] = 3 X[0,3] = 4 X[1,0] = 5
Case 2 Sample Output
$ python3 py3_pre_0_username.py Enter Matrix Dimensions Enter rows: 2 Enter columns: 5 FOR loop traversal: X[0,0] = 1 X[0,1] = 2 X[0,2] = 3 X[0,3] = 4 X[0,4] = 5 X[1,0] = 6 X[1,1] = 7 X[1,2] = 8 X[1,3] = 9 X[1,4] = 10
Enter Matrix Dimensions Enter rows: 4 Enter columns: 3 Enter Stop Value: 10 WHILE loop traversal: X[0,0] = 1 X[0,1] = 2 X[0,2] = 3 X[1,0] = 4 X[1,1] = 5 X[1,2] = 6 X[2,0] = 7 X[2,1] = 8 X[2,2] = 9
Deliverables |
Description |
|---|---|
py3_pre_0_username.py |
Your completed Python code. |