\[ \begin{align}\begin{aligned}\newcommand\blank{~\underline{\hspace{1.2cm}}~}\\% Bold symbols (vectors) \newcommand\bs[1]{\mathbf{#1}}\\% Differential \newcommand\dd[2][]{\mathrm{d}^{#1}{#2}} % use as \dd, \dd{x}, or \dd[2]{x}\\% Poor man's siunitx \newcommand\unit[1]{\mathrm{#1}} \newcommand\num[1]{#1} \newcommand\qty[2]{#1~\unit{#2}}\\\newcommand\per{/} \newcommand\squared{{}^2} \newcommand\cubed{{}^3} % % Scale \newcommand\milli{\unit{m}} \newcommand\centi{\unit{c}} \newcommand\kilo{\unit{k}} \newcommand\mega{\unit{M}} % % Percent \newcommand\percent{\unit{{\kern-4mu}\%}} % % Angle \newcommand\radian{\unit{rad}} \newcommand\degree{\unit{{\kern-4mu}^\circ}} % % Time \newcommand\second{\unit{s}} \newcommand\s{\second} \newcommand\minute{\unit{min}} \newcommand\hour{\unit{h}} % % Distance \newcommand\meter{\unit{m}} \newcommand\m{\meter} \newcommand\inch{\unit{in}} \newcommand\foot{\unit{ft}} % % Force \newcommand\newton{\unit{N}} \newcommand\kip{\unit{kip}} % kilopound in "freedom" units - edit made by Sri % % Mass \newcommand\gram{\unit{g}} \newcommand\g{\gram} \newcommand\kilogram{\unit{kg}} \newcommand\kg{\kilogram} \newcommand\grain{\unit{grain}} \newcommand\ounce{\unit{oz}} \newcommand\pound{\unit{lbs}} % % Temperature \newcommand\kelvin{\unit{K}} \newcommand\K{\kelvin} \newcommand\celsius{\unit{{\kern-4mu}^\circ C}} \newcommand\C{\celsius} \newcommand\fahrenheit{\unit{{\kern-4mu}^\circ F}} \newcommand\F{\fahrenheit} % % Area \newcommand\sqft{\unit{sq\,\foot}} % square foot % % Volume \newcommand\liter{\unit{L}} \newcommand\gallon{\unit{gal}} % % Frequency \newcommand\hertz{\unit{Hz}} \newcommand\rpm{\unit{rpm}} % % Voltage \newcommand\volt{\unit{V}} \newcommand\V{\volt} \newcommand\millivolt{\milli\volt} \newcommand\mV{\milli\volt} \newcommand\kilovolt{\kilo\volt} \newcommand\kV{\kilo\volt} % % Current \newcommand\ampere{\unit{A}} \newcommand\A{\ampere} \newcommand\milliampereA{\milli\ampere} \newcommand\mA{\milli\ampere} \newcommand\kiloampereA{\kilo\ampere} \newcommand\kA{\kilo\ampere} % % Resistance \newcommand\ohm{\Omega} \newcommand\milliohm{\milli\ohm} \newcommand\kiloohm{\kilo\ohm} % correct SI spelling \newcommand\kilohm{\kilo\ohm} % "American" spelling used in siunitx \newcommand\megaohm{\mega\ohm} % correct SI spelling \newcommand\megohm{\mega\ohm} % "American" spelling used in siunitx % % Capacitance \newcommand\farad{\unit{F}} \newcommand\F{\farad} \newcommand\microfarad{\micro\farad} \newcommand\muF{\micro\farad} % % Inductance \newcommand\henry{\unit{H}} \newcommand\H{\henry} \newcommand\millihenry{\milli\henry} \newcommand\mH{\milli\henry} % % Power \newcommand\watt{\unit{W}} \newcommand\W{\watt} \newcommand\milliwatt{\milli\watt} \newcommand\mW{\milli\watt} \newcommand\kilowatt{\kilo\watt} \newcommand\kW{\kilo\watt} % % Energy \newcommand\joule{\unit{J}} \newcommand\J{\joule} % % Composite units % % Torque \newcommand\ozin{\unit{\ounce}\,\unit{in}} \newcommand\newtonmeter{\unit{\newton\,\meter}} % % Pressure \newcommand\psf{\unit{psf}} % pounds per square foot \newcommand\pcf{\unit{pcf}} % pounds per cubic foot \newcommand\pascal{\unit{Pa}} \newcommand\Pa{\pascal} \newcommand\ksi{\unit{ksi}} % kilopound per square inch \newcommand\bar{\unit{bar}} \end{aligned}\end{align} \]

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.

  1. Create three User Defined Functions (UDFs) in your main script:

    1. 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 to build_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.

    2. The second UDF, named traverse_with_for, should take in a matrix, X, as its only argument, and use nested for loops to traverse the matrix in row-major order, printing the value of each element.

    3. The third UDF, named traverse_with_while, should take in a matrix, X, and a stopping number as its arguments. It should then use nested while loops 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 is X[2][1] = 10. We recommend that you draw out the matrix on paper to help you visualize how to access each element using loops.

  2. In your main function, ask the user to enter the number of rows and columns in the first matrix. Then use your build_matrix UDF to create the matrix, and pass it to your traverse_with_for UDF 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 your build_matrix UDF again to create the second matrix, and pass it along with the stopping number to your traverse_with_while UDF to print its elements until the stopping number is reached.

  3. 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.

Table 13.3 Test Cases#

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

Table 13.4 Deliverables#

Deliverables

Description

py3_pre_0_username.py

Your completed Python code.