Finishing the Matrix

Today, we finished the assignment over a matrix (2-dimensional array) in Python by calculating the ranking points for the teams in the FIRST Tech Challenge El Dorado League of North Texas for the 2017/2018 season.

As you may recall from the previous post titled “Continuing the Matrix“, when there is a tie in the qualifying points, the 3 highest ranking points for each team are summed together to break the tie. The team with the higher sum wins the tie-break.

I am going to remove lines 147 to 157 from last class, which was a test print and am going to replace them with the following to calculate the sum of the 3 highest ranking points.

teams[0][13] = (teams[0][5] + teams[0][7] + teams[0][9] + teams[0][11]) - min(teams[0][5], teams[0][7], teams[0][9], teams[0][11])
teams[1][13] = (teams[1][5] + teams[1][7] + teams[1][9] + teams[1][11]) - min(teams[1][5], teams[1][7], teams[1][9], teams[1][11])
teams[2][13] = (teams[2][5] + teams[2][7] + teams[2][9] + teams[2][11]) - min(teams[2][5], teams[2][7], teams[2][9], teams[2][11])
teams[3][13] = (teams[3][5] + teams[3][7] + teams[3][9] + teams[3][11]) - min(teams[3][5], teams[3][7], teams[3][9], teams[3][11])
teams[4][13] = (teams[4][5] + teams[4][7] + teams[4][9] + teams[4][11]) - min(teams[4][5], teams[4][7], teams[4][9], teams[4][11])
teams[5][13] = (teams[5][5] + teams[5][7] + teams[5][9] + teams[5][11]) - min(teams[5][5], teams[5][7], teams[5][9], teams[5][11])
teams[6][13] = (teams[6][5] + teams[6][7] + teams[6][9] + teams[6][11]) - min(teams[6][5], teams[6][7], teams[6][9], teams[6][11])
teams[7][13] = (teams[7][5] + teams[7][7] + teams[7][9] + teams[7][11]) - min(teams[7][5], teams[7][7], teams[7][9], teams[7][11])
teams[8][13] = (teams[8][5] + teams[8][7] + teams[8][9] + teams[8][11]) - min(teams[8][5], teams[8][7], teams[8][9], teams[8][11])
teams[9][13] = (teams[9][5] + teams[9][7] + teams[9][9] + teams[9][11]) - min(teams[9][5], teams[9][7], teams[9][9], teams[9][11])
teams[10][13] = (teams[10][5] + teams[10][7] + teams[10][9] + teams[10][11]) - min(teams[10][5], teams[10][7], teams[10][9], teams[10][11])

Let’s take a look at what we have going on here by just taking a look at the first line in this code segment:

teams[0][13] = (teams[0][5] + teams[0][7] + teams[0][9] + teams[0][11]) - min(teams[0][5], teams[0][7], teams[0][9], teams[0][11])

As you can see, we start by saying that we are going to populate column with 13 with some data. That data is the value of columns 5, 7, 9, & 11 added together and then the smallest of those values subtracted from that sum.

Remember, only the top 3 scores are used in the calculation and it we add the 4 scores together and then subtract the smallest (min) from that total, we get the data we need.

We now need to display our data.

print(teams[0][0] + " - " + teams[0][1] + " - " + str(teams[0][12]) + " (" + str(teams[0][13]) + ")")
print(teams[1][0] + " - " + teams[1][1]  + " - " + str(teams[1][12]) + " (" + str(teams[1][13]) + ")")
print(teams[2][0] + " - " + teams[2][1]  + " - " + str(teams[2][12]) + " (" + str(teams[2][13]) + ")")
print(teams[3][0] + " - " + teams[3][1]  + " - " + str(teams[3][12]) + " (" + str(teams[3][13]) + ")")
print(teams[4][0] + " - " + teams[4][1]  + " - " + str(teams[4][12]) + " (" + str(teams[4][13]) + ")")
print(teams[5][0] + " - " + teams[5][1]  + " - " + str(teams[5][12]) + " (" + str(teams[5][13]) + ")")
print(teams[6][0] + " - " + teams[6][1]  + " - " + str(teams[6][12]) + " (" + str(teams[6][13]) + ")")
print(teams[7][0] + " - " + teams[7][1]  + " - " + str(teams[7][12]) + " (" + str(teams[7][13]) + ")")
print(teams[8][0] + " - " + teams[8][1]  + " - " + str(teams[8][12]) + " (" + str(teams[8][13]) + ")")
print(teams[9][0] + " - " + teams[9][1]  + " - " + str(teams[9][12]) + " (" + str(teams[9][13]) + ")")
print(teams[10][0] + " - " + teams[10][1]  + " - " + str(teams[10][12]) + " (" + str(teams[10][13]) + ")")

Let’s take a look at what we have going on here by just taking a look at the first line in this code segment:

print(teams[0][0] + " - " + teams[0][1] + " - " + str(teams[0][12]) + " (" + str(teams[0][13]) + ")")

Here, we are displaying a concatenation of the team number from column 0, with a space/hyphen/space, with the team name from column 1, with a space/hyphen/space, with the sum of the qualifying points for the team from column 12, with a space, with the sum of the three highest ranking points in parenthesis from column 13.

The output should look like the following:

Python 3.6.1 (default, Dec 2015, 13:05:11)
[GCC 4.8.2] on linux

127 - The Fighting Pickles - 23 (867)
5443 - Synergy - 18 (737)
9402 - Hive of Steel - 15 (739)
9403 - CyberSwarm - 29 (932)
10143 - Bits & Bots - 14 (763)
11085 - Mad Hackers - 16 (718)
11242 - ERROR 451 - 28 (812)
12645 - S.C.R.E.W. Ups - 19 (874)
12650 - Cannot Compute - 12 (673)
12992 - Vindicators - 6 (316)
13915 - Eagles Robotics - 14 (407)

Following is a complete overview of the code that was created over the 3 days of this lesson:

w = 14 #Width of matrix
h = 11 #Height of matrix
teams = [[0 for x in range(w)] for y in range(h)]
teams[0][0] = "127"
teams[1][0] = "5443"
teams[2][0] = "9402"
teams[3][0] = "9403"
teams[4][0] = "10143"
teams[5][0] = "11085"
teams[6][0] = "11242"
teams[7][0] = "12645"
teams[8][0] = "12650"
teams[9][0] = "12992"
teams[10][0] = "13915"
teams[0][1] = "The Fighting Pickles"
teams[1][1] = "Synergy"
teams[2][1] = "Hive of Steel"
teams[3][1] = "CyberSwarm"
teams[4][1] = "Bits & Bots"
teams[5][1] = "Mad Hackers"
teams[6][1] = "ERROR 451"
teams[7][1] = "S.C.R.E.W. Ups"
teams[8][1] = "Cannot Compute"
teams[9][1] = "Vindicators"
teams[10][1] = "Eagles Robotics"
teams[0][2] = "Ben Barber Innovation Academy"
teams[1][2] = "Harmony School of Innovation"
teams[2][2] = "Henderson Junior High School"
teams[3][2] = "Henderson Junior High School"
teams[4][2] = "Harmony School of Innovation"
teams[5][2] = "Mansfield High School"
teams[6][2] = "Ferris High School"
teams[7][2] = "Ferris High School"
teams[8][2] = "Ferris Junior High School"
teams[9][2] = "Italy High School"
teams[10][2] = "Faith Family Academy"
teams[0][3] = "Mansfield, TX"
teams[1][3] = "Ft. Worth, TX"
teams[2][3] = "Stephenville, TX"
teams[3][3] = "Stephenville, TX"
teams[4][3] = "Ft. Worth, TX"
teams[5][3] = "Mansfield, TX"
teams[6][3] = "Ferris, TX"
teams[7][3] = "Ferris, TX"
teams[8][3] = "Ferris, TX"
teams[9][3] = "Italy, TX"
teams[10][3] = "Waxahachie, TX"
teams[0][4] = 6
teams[1][4] = 0
teams[2][4] = 2
teams[3][4] = 6
teams[4][4] = 6
teams[5][4] = 4
teams[6][4] = 10
teams[7][4] = 6
teams[8][4] = 6
teams[9][4] = 4
teams[10][4] = 0
teams[0][5] = 210
teams[1][5] = 105
teams[2][5] = 132
teams[3][5] = 183
teams[4][5] = 212
teams[5][5] = 184
teams[6][5] = 220
teams[7][5] = 156
teams[8][5] = 160
teams[9][5] = 164
teams[10][5] = 0
teams[0][6] = 6
teams[1][6] = 4
teams[2][6] = 4
teams[3][6] = 10
teams[4][6] = 2
teams[5][6] = 0
teams[6][6] = 6
teams[7][6] = 4
teams[8][6] = 4
teams[9][6] = 0
teams[10][6] = 0
teams[0][7] = 257
teams[1][7] = 257
teams[2][7] = 220
teams[3][7] = 314
teams[4][7] = 205
teams[5][7] = 0
teams[6][7] = 238
teams[7][7] = 277
teams[8][7] = 224
teams[9][7] = 0
teams[10][7] = 0
teams[0][8] = 6
teams[1][8] = 6
teams[2][8] = 6
teams[3][8] = 6
teams[4][8] = 4
teams[5][8] = 6
teams[6][8] = 6
teams[7][8] = 6
teams[8][8] = 0
teams[9][8] = 2
teams[10][8] = 6
teams[0][9] = 188
teams[1][9] = 168
teams[2][9] = 156
teams[3][9] = 218
teams[4][9] = 160
teams[5][9] = 202
teams[6][9] = 244
teams[7][9] = 184
teams[8][9] = 156
teams[9][9] = 152
teams[10][9] = 140
teams[0][10] = 5
teams[1][10] = 8
teams[2][10] = 3
teams[3][10] = 7
teams[4][10] = 2
teams[5][10] = 6
teams[6][10] = 6
teams[7][10] = 3
teams[8][10] = 2
teams[9][10] = 0
teams[10][10] = 8
teams[0][11] = 400
teams[1][11] = 312
teams[2][11] = 363
teams[3][11] = 400
teams[4][11] = 346
teams[5][11] = 332
teams[6][11] = 330
teams[7][11] = 413
teams[8][11] = 289
teams[9][11] = 0
teams[10][11] = 267
teams[0][12] = teams[0][4] + teams[0][6] + teams[0][8] + teams [0][10]
teams[1][12] = teams[1][4] + teams[1][6] + teams[1][8] + teams [1][10]
teams[2][12] = teams[2][4] + teams[2][6] + teams[2][8] + teams [2][10]
teams[3][12] = teams[3][4] + teams[3][6] + teams[3][8] + teams [3][10]
teams[4][12] = teams[4][4] + teams[4][6] + teams[4][8] + teams [4][10]
teams[5][12] = teams[5][4] + teams[5][6] + teams[5][8] + teams [5][10]
teams[6][12] = teams[6][4] + teams[6][6] + teams[6][8] + teams [6][10]
teams[7][12] = teams[7][4] + teams[7][6] + teams[7][8] + teams [7][10]
teams[8][12] = teams[8][4] + teams[8][6] + teams[8][8] + teams [8][10]
teams[9][12] = teams[9][4] + teams[9][6] + teams[9][8] + teams [9][10]
teams[10][12] = teams[10][4] + teams[10][6] + teams[10][8] + teams [10][10]
teams[0][13] = (teams[0][5] + teams[0][7] + teams[0][9] + teams[0][11]) - min(teams[0][5], teams[0][7], teams[0][9], teams[0][11])
teams[1][13] = (teams[1][5] + teams[1][7] + teams[1][9] + teams[1][11]) - min(teams[1][5], teams[1][7], teams[1][9], teams[1][11])
teams[2][13] = (teams[2][5] + teams[2][7] + teams[2][9] + teams[2][11]) - min(teams[2][5], teams[2][7], teams[2][9], teams[2][11])
teams[3][13] = (teams[3][5] + teams[3][7] + teams[3][9] + teams[3][11]) - min(teams[3][5], teams[3][7], teams[3][9], teams[3][11])
teams[4][13] = (teams[4][5] + teams[4][7] + teams[4][9] + teams[4][11]) - min(teams[4][5], teams[4][7], teams[4][9], teams[4][11])
teams[5][13] = (teams[5][5] + teams[5][7] + teams[5][9] + teams[5][11]) - min(teams[5][5], teams[5][7], teams[5][9], teams[5][11])
teams[6][13] = (teams[6][5] + teams[6][7] + teams[6][9] + teams[6][11]) - min(teams[6][5], teams[6][7], teams[6][9], teams[6][11])
teams[7][13] = (teams[7][5] + teams[7][7] + teams[7][9] + teams[7][11]) - min(teams[7][5], teams[7][7], teams[7][9], teams[7][11])
teams[8][13] = (teams[8][5] + teams[8][7] + teams[8][9] + teams[8][11]) - min(teams[8][5], teams[8][7], teams[8][9], teams[8][11])
teams[9][13] = (teams[9][5] + teams[9][7] + teams[9][9] + teams[9][11]) - min(teams[9][5], teams[9][7], teams[9][9], teams[9][11])
teams[10][13] = (teams[10][5] + teams[10][7] + teams[10][9] + teams[10][11]) - min(teams[10][5], teams[10][7], teams[10][9], teams[10][11])
print(teams[0][0] + " - " + teams[0][1] + " - " + str(teams[0][12]) + " (" + str(teams[0][13]) + ")")
print(teams[1][0] + " - " + teams[1][1]  + " - " + str(teams[1][12]) + " (" + str(teams[1][13]) + ")")
print(teams[2][0] + " - " + teams[2][1]  + " - " + str(teams[2][12]) + " (" + str(teams[2][13]) + ")")
print(teams[3][0] + " - " + teams[3][1]  + " - " + str(teams[3][12]) + " (" + str(teams[3][13]) + ")")
print(teams[4][0] + " - " + teams[4][1]  + " - " + str(teams[4][12]) + " (" + str(teams[4][13]) + ")")
print(teams[5][0] + " - " + teams[5][1]  + " - " + str(teams[5][12]) + " (" + str(teams[5][13]) + ")")
print(teams[6][0] + " - " + teams[6][1]  + " - " + str(teams[6][12]) + " (" + str(teams[6][13]) + ")")
print(teams[7][0] + " - " + teams[7][1]  + " - " + str(teams[7][12]) + " (" + str(teams[7][13]) + ")")
print(teams[8][0] + " - " + teams[8][1]  + " - " + str(teams[8][12]) + " (" + str(teams[8][13]) + ")")
print(teams[9][0] + " - " + teams[9][1]  + " - " + str(teams[9][12]) + " (" + str(teams[9][13]) + ")")
print(teams[10][0] + " - " + teams[10][1]  + " - " + str(teams[10][12]) + " (" + str(teams[10][13]) + ")")

 

Continuing the Matrix

Today, we continued the matrix (2-dimensional array) in Python in Computer Science 1. We added an additional 88 data points to the existing 44 we started with. This brings the total number of points to 132.

See the post entitled “Starting a Matrix in Python” for the first part of the program code.

Now, we are going to remove lines 48 through 58 of the code to continue entering data.

teams[0][4] = 6
teams[1][4] = 0
teams[2][4] = 2
teams[3][4] = 6
teams[4][4] = 6
teams[5][4] = 4
teams[6][4] = 10
teams[7][4] = 6
teams[8][4] = 6
teams[9][4] = 4
teams[10][4] = 0
teams[0][5] = 210
teams[1][5] = 105
teams[2][5] = 132
teams[3][5] = 183
teams[4][5] = 212
teams[5][5] = 184
teams[6][5] = 220
teams[7][5] = 156
teams[8][5] = 160
teams[9][5] = 164
teams[10][5] = 0

Here, we are adding the scores for each team from the first league meet of the 2017/2018 season as new columns 4 and 5 to the array. You can see these are being entered as integers as we will be doing some math with them later.

teams[0][6] = 6
teams[1][6] = 4
teams[2][6] = 4
teams[3][6] = 10
teams[4][6] = 2
teams[5][6] = 0
teams[6][6] = 6
teams[7][6] = 4
teams[8][6] = 4
teams[9][6] = 0
teams[10][6] = 0
teams[0][7] = 257
teams[1][7] = 257
teams[2][7] = 220
teams[3][7] = 314
teams[4][7] = 205
teams[5][7] = 0
teams[6][7] = 238
teams[7][7] = 277
teams[8][7] = 224
teams[9][7] = 0
teams[10][7] = 0

We are now adding the scores from each team from the second league meet of the 2017/2018 season as new columns 6 and 7 to the array.

teams[0][8] = 6
teams[1][8] = 6
teams[2][8] = 6
teams[3][8] = 6
teams[4][8] = 4
teams[5][8] = 6
teams[6][8] = 6
teams[7][8] = 6
teams[8][8] = 0
teams[9][8] = 2
teams[10][8] = 6
teams[0][9] = 188
teams[1][9] = 168
teams[2][9] = 156
teams[3][9] = 218
teams[4][9] = 160
teams[5][9] = 202
teams[6][9] = 244
teams[7][9] = 184
teams[8][9] = 156
teams[9][9] = 152
teams[10][9] = 140

We are now adding the scores from each teams from the third league meet of the 2017/2018 season as new columns 8 and 9 to the array.

teams[0][10] = 5
teams[1][10] = 8
teams[2][10] = 3
teams[3][10] = 7
teams[4][10] = 2
teams[5][10] = 6
teams[6][10] = 6
teams[7][10] = 3
teams[8][10] = 2
teams[9][10] = 0
teams[10][10] = 8
teams[0][11] = 400
teams[1][11] = 312
teams[2][11] = 363
teams[3][11] = 400
teams[4][11] = 346
teams[5][11] = 332
teams[6][11] = 330
teams[7][11] = 413
teams[8][11] = 289
teams[9][11] = 0
teams[10][11] = 267

We now add the results from the league tournament final qualifying rounds of the 2017/2018 season as new columns 10 and 11 to the array.

Now, what we need to do is calculate the total qualifying points, which determines placement. This is done by calculating the sum of all qualifying points for each team individually. This was stored in columns 4, 6, 8, & 10 in the array.

In the event of a tie in the qualifying points, we take the sum of the 3 highest ranking points scores for each team. The team with the higher ranking points wins the tie-break. The ranking points are stored in columns 5, 7, 9, & 11 in the array.

Calculating the qualifying points for each team is somewhat easy and can be done using simple arithmetic functions.

teams[0][12] = teams[0][4] + teams[0][6] + teams[0][8] + teams [0][10]
teams[1][12] = teams[1][4] + teams[1][6] + teams[1][8] + teams [1][10]
teams[2][12] = teams[2][4] + teams[2][6] + teams[2][8] + teams [2][10]
teams[3][12] = teams[3][4] + teams[3][6] + teams[3][8] + teams [3][10]
teams[4][12] = teams[4][4] + teams[4][6] + teams[4][8] + teams [4][10]
teams[5][12] = teams[5][4] + teams[5][6] + teams[5][8] + teams [5][10]
teams[6][12] = teams[6][4] + teams[6][6] + teams[6][8] + teams [6][10]
teams[7][12] = teams[7][4] + teams[7][6] + teams[7][8] + teams [7][10]
teams[8][12] = teams[8][4] + teams[8][6] + teams[8][8] + teams [8][10]
teams[9][12] = teams[9][4] + teams[9][6] + teams[9][8] + teams [9][10]
teams[10][12] = teams[10][4] + teams[10][6] + teams[10][8] + teams [10][10]

As you can see, we have added column 12 to the array and it holds the sum of columns 4, 6, 8, & 10 for the given row. This is the total qualifying points.

We can add a “test-print” using the following:

print(teams[0][0] + " - " + str(teams[0][12]))
print(teams[1][0] + " - " + str(teams[1][12]))
print(teams[2][0] + " - " + str(teams[2][12]))
print(teams[3][0] + " - " + str(teams[3][12]))
print(teams[4][0] + " - " + str(teams[4][12]))
print(teams[5][0] + " - " + str(teams[5][12]))
print(teams[6][0] + " - " + str(teams[6][12]))
print(teams[7][0] + " - " + str(teams[7][12]))
print(teams[8][0] + " - " + str(teams[8][12]))
print(teams[9][0] + " - " + str(teams[9][12]))
print(teams[10][0] + " - " + str(teams[10][12]))

You can see that we are displaying the team number from column 0, concatenated with a space a hyphen and space, which is then concatenated with the values from column 12 cast as a string.

The output looks similar to the following:

Python 3.6.1 (default, Dec 2015, 13:05:11)
[GCC 4.8.2] on linux

127 - 23
5443 - 18
9402 - 15
9403 - 29
10143 - 14
11085 - 16
11242 - 28
12645 - 19
12650 - 12
12992 - 6
13915 - 14

Next class, we will take a look at how to calculate the tie-breaker. As you can see, it will be needed as there is a tie between FTC 10143 and FTC 13915.

Starting a Matrix in Python

I started working with a 2-dimensional array in Python with my students today. We first covered a basic table on paper with the letters of the alphabet populating the cells so they could read index coordinates.

We then moved to the problem. On the first day of this assignment, we worked to create an 11 X 4 table that holds information of the teams that formed the FIRST Tech Challenge El Dorado League of North Texas for the 2017/2018 Relic Recovery season.

26-February-2018 – Matrix Day 1 Data – Sheet1

This is a visual representation of the data we worked with today.

w = 4
h = 11
teams = [[0 for x in range(w)] for y in range(h)]

Here, we declared an integer variable named “w” and another named “h” and assigned the values 4 and 11 respectively. This represents the width and height of the array we are building.

On line 3, we create the array named teams and using a nested for loop, create the array that is 4 columns wide and 11 rows tall. The table is currently populated with zeros.

teams[0][0] = "127"
teams[1][0] = "5443"
teams[2][0] = "9402"
teams[3][0] = "9403"
teams[4][0] = "10143"
teams[5][0] = "11085"
teams[6][0] = "11242"
teams[7][0] = "12645"
teams[8][0] = "12650"
teams[9][0] = "12992"
teams[10][0] = "13915"

These lines are populating column 0 (first column) of the array with the team numbers of the 11 different FTC teams in the league. Please note that it only goes up to 10 since we start counting at 0.

teams[0][1] = "The Fighting Pickles"
teams[1][1] = "Synergy"
teams[2][1] = "Hive of Steel"
teams[3][1] = "CyberSwarm"
teams[4][1] = "Bit & Bots"
teams[5][1] = "Mad Hackers"
teams[6][1] = "ERROR 451"
teams[7][1] = "S.C.R.E.W. Ups"
teams[8][1] = "Cannot Compute"
teams[9][1] = "Vindicators"
teams[10][1] = "Eagles Robotics"

These lines are populating column 1 (second column) of the array with the team names.

teams[0][2] = "Ben Barber Innovation Academy"
teams[1][2] = "Harmony School of Innovation"
teams[2][2] = "Henderson Junior High School"
teams[3][2] = "Henderson Junior High School"
teams[4][2] = "Harmony School of Innovation"
teams[5][2] = "Mansfield High School"
teams[6][2] = "Ferris High School"
teams[7][2] = "Ferris High School"
teams[8][2] = "Ferris Junior High School"
teams[9][2] = "Italy High School"
teams[10}[2] = "Faith Family Academy"

These lines are populating column 2 (third column) of the array with the schools each team represents.

teams[0][3] = "Mansfield, TX"
teams[1][3] = "Ft. Worth, TX"
teams[2][3] = "Stephenville, TX"
teams[3][3] = "Stephenville, TX"
teams[4][3] = "Ft. Worth, TX"
teams[5][3] = "Mansfield, TX"
teams[6][3] = "Ferris, TX"
teams[7][3] = "Ferris, TX"
teams[8][3] = "Ferris, TX"
teams[9][3] = "Italy, TX"
teams[10][3] = "Waxahachie, TX"

These lines are populating column 3 (fourth column) of the array with the hometowns of each team.

print(teams[0])
print(teams[1])
print(teams[2])
print(teams[3])
print(teams[4])
print(teams[5])
print(teams[6])
print(teams[7])
print(teams[8])
print(teams[9])
print(teams[10])

These lines will print each of the 11 rows on a separate line. There is no formatting, so the output will look similar to the following:

Python 3.6.1 (default, Dec 2015, 13:05:11)
[GCC 4.8.2] on linux
['127', 'The Fighting Pickles', 'Ben Barber Innovation Academy', 'Mansfield, TX']
['5443', 'Synergy', 'Harmony School of Innovation', 'Ft. Worth, TX']
['9402', 'Hive of Steel', 'Henderson Junior High School', 'Stephenville, TX']
['9403', 'CyberSwarm', 'Henderson Junior High School', 'Stephenville, TX']
['10143', 'Bits & Bots', 'Harmony School of Innovation', 'Ft. Worth, TX']
['11085', 'Mad Hackers', 'Mansfield High School', 'Mansfield, TX']
['11242', 'ERROR 451', 'Ferris High School', 'Ferris, TX']
['12645', 'S.C.R.E.W. Ups', 'Ferris High School', 'Ferris, TX']
['12650', 'Cannot Compute', 'Ferris Junior High School', 'Ferris, TX']
['12992', 'Vindicators', 'Italy High School', 'Italy, TX']
['13915', 'Eagles Robotics', 'Faith Family Academy', 'Waxahachie, TX']

In the next class, we’ll discuss how to display an individual index and use concatenation to format a better output. We’ll then move into adding additional data to the existing array for each team that will involve basic math.

2-Dimensional Arrays in Java

Today, we started to cover 2-dimensional arrays in Java. I decided to start with something very easy:

2-D Array Visual
2-D Array Visual

We have an array with 2 rows and 3 columns. Like all things in Java, we start counting our indices at 0.

As such, the value of [0][0] is Vanilla and [1][0] is Ice Cream. Note that the first number in the reference points to the row and the second number in the reference points to the column.

import java.util.*;
public class TwoDArrays {
    public static void main(String[] args){
        String[][] myBigArray = new String [][] {
            {"Vanilla ", "Chocolate ", "Strawberry "},
            {"Ice Cream", "Cookie", "Candy"}
        };
        System.out.println(myBigArray[0][0] + myBigArray[1][0]);
        System.out.println(myBigArray[0][1] + myBigArray[1][0]);
        System.out.println(myBigArray[0][2] + myBigArray[1][0]);
        
        System.out.println(myBigArray[0][0] + myBigArray[1][1]);
        System.out.println(myBigArray[0][1] + myBigArray[1][1]);
        System.out.println(myBigArray[0][2] + myBigArray[1][1]);
        
        System.out.println(myBigArray[0][0] + myBigArray[1][2]);
        System.out.println(myBigArray[0][1] + myBigArray[1][2]);
        System.out.println(myBigArray[0][2] + myBigArray[1][2]);
    }
}

Line 4 is where we created the 2-dimensional array named “myBigArray”.

Lines 5 and 6 are where we populated the array. Note that line 5 is the first row and line 6 is the second row.

Lines 8 through 18 are where we are outputting text that is “fed” by the 2-D array.

Line 8 concatenates [0][0] with [1][0] which is Vanilla and Ice Cream.

Line 9 concatenates [0][1] with [1][0] which is Chocolate and Ice Cream.

Line 10 concatenates [0][2] with [1][0] which is Strawberry and Ice Cream.

Line 12 concatenates [0][0] with [1][1] which is Vanilla and Cookie.

Line 13 concatenates [0][1] with [1][1] which is Chocolate and Cookie.

Line 14 concatenates [0][2] with [1][1] which is Strawberry and Cookie.

Line 16 concatenates [0][0] with [1][2] which is Vanilla and Candy.

Line 17 concatenates [0][1] with [1][2] which is Chocolate and Candy.

Line 18 concatenates [0][2] with [1][2] which is Strawberry and Candy.

Arrays with Keyboard Interactivity

Today, we discussed how to create an array with keyboard interactivity. The user will be asked to enter how many numbers they will be entering and then they will be asked to enter numbers separated by a space.

//Program Name: Arrays with Keyboard Input
//Programmer Name: Eric Evans, M.Ed.
//Programmer Organization: Ferris High School
//Program Date: Spring 2017

import java.util.*;
import java.lang.*;
import java.io.*;

public class arrayKeys {
  public static void main(String args[]){
    Scanner count = new Scanner(System.in);
    System.out.print("How Many Numbers Do You Want to Enter?: ");
    int arraySize = count.nextInt();
    int myArray[] = new int[arraySize];
    Scanner nums = new Scanner(System.in);
    System.out.println("Enter Your " + arraySize + " numbers each separated by a space and press enter when done.");
    for(int counter = 0; counter < arraySize; counter++){
      myArray[counter] = nums.nextInt();
    }
  System.out.println(Arrays.toString(myArray));
  }
}

Following the header information (lines 1 through 4) and the imports (lines 6 through 8), we get to the actual application code.

On line 12, we declare a scanner object named “count” which will receive input from the keyboard.

On line 13, we ask the user to enter the number of items they will be entering. Lines 14 assigns the value that is entered as an integer variable named “arraySize”.

Line 15 creates an empty array named “myArray” which is initialized as the size of the “arraySize” variable declared on line 14.

Line 16 declares a second scanner object named “nums” which will receive the integers to the recorded from the keyboard.

On line 17, we ask the user to enter the integers they want recorded separated by a space.

Line 18 opens a for loop. The loop initialization creates an integer variable named “counter” with the value of 0. The loop condition is to run while the variable “counter” is less than the variable “arraySize”, which was declared on line 14. Each pass through the loop increments the value of the variable “counter” by 1.

Each pass through the loop executes line 19, which assigns the next integer in the sequence of numbers from the “nums” scanner to “myArray” in the index (position) declared by the “counter” variable.

Finally, on line 21, we output the contents of “myArray” as a string.

Coding Bat – Arrays Cluster 1

Today, while I was attending meetings, I assigned my students to complete 4 Coding Bat logic exercises.

CODING BAT – COMMONEND

This exercise requires that the program return TRUE if the first integers or last integers of two given arrays are the same, otherwise, FALSE will be returned.

Here is the code that you start with:

public boolean commonEnd(int[] a, int[] b) {

}

We start with two integer arrays named “a” and “b”.

public boolean commonEnd(int[] a, int[] b) {
  int arrayALength = a.length;
  int arrayBLength = b.length;
  int lastA = a[arrayALength - 1];
  int lastB = b[arrayBLength - 1];
  int firstA = a[0];
  int firstB = b[0];
  if(firstA == firstB || lastA == lastB){
    return true;
  }
  return false;
}

The solution above starts by calculating the length of each array (lines 2 and 3). Remember, whenever you are asked to do anything with the “end” or “last” items, you will almost always need to calculate the length.

On lines 4 & 5, we are establishing variables to hold the last index of each array and lines 6 & 7 are holding the first index of each array.

Finally, lines 8 through 10 are a conditional statement comparing the first indices of each array and the last indices of each array. If either of them are equal, a TRUE is returned. However, if neither is TRUE, then FALSE is returned.

CODING BAT – MAKELAST

This exercise requires that the program calculate the length of a given array and create a new array that is double that length populated with zeros with the exception of the last index. The last index is to populated with the last index of the first array.

Here is the code that you start with:

public int[] makeLast(int[] nums) {

}

We start with a single integer variable named “nums”.

public int[] makeLast(int[] nums) {
 int arrayLength = nums.length;
 int arrayFinalLength = arrayLength * 2;
 int arrayLast = nums[arrayLength - 1];
 int[] num = new int[arrayFinalLength];
 num[arrayLength - 1] = arrayLast;
 return num;
}

As always, since we are asked to do some operation with the “last” or “end”, we must calculate the length of the array. This is done on line 2.

On line 3, we created a variable to hold the length of the new array, which is double the length of the original array.

Line 4 is where we capture the last index of the original array.

On line 5, we create a new integer array named “num” which has a size assigned by the variable that was created on line 3.

On line 6, we replace the last index of the new array with the value of the variable created on line 4.

Finally, on line 7, we return the new array.

CODING BAT – MAKEPI

This exercise requires that the program return the first 3 digits of PI in an array.

Here is the code that you start with:

public int[] makePi() {

}

We start with just the basic class declaration and no variables.

public int[] makePi() {
  int pi[] = {3,1,4};
  return pi;
}

The solution above starts by creating a new integer array named “pi” which is assigned the integers of 3, 1, and 4 on line 2.

On line 3, the array is returned.

Many students struggle to make this problem more challenging than it needs to be. Some try to take the Math.PI constant and populate the array one index at a time. However, this is not necessary and is not outlined as a requirement in the problem.

 

Starting with Arrays

Today, we started working with basic arrays in class. Before we got into them, we compared them to stacks. We discussed some of the applicable uses of stacks and how they are ideal for a queue where records must remain in the order they were received.

After the discussion reviewing stacks, I then compared them to a basic array. We discussed that while we can select and manipulate any index within an array, the array is a finite size. We discussed advantages and disadvantages of the two data structures. We then moved into practicing with some code.

//Program Name: Starting with Arrays
//Programmer Name: Eric Evans, M.Ed.
//Programmer Organization: Ferris High School
//Program Date: Spring 2017

import java.util.*;

public class arrays1 {
 public static void main(String args[]){
 int myFirstArray[] = {100, 25, 63, 3, 12, 50, 85};
 
 //Print the Array
 for(int i=0; i<myFirstArray.length; i++){
   System.out.print(myFirstArray[i] + " ");
 }
 
 //Alternate Way to Print the Array
 System.out.println("");
 for(int everyElement: myFirstArray){
   System.out.print(everyElement + " ");
 }
 
 //Adding the Items in the Array
 System.out.println("");
 int total = 0;
 for(int i=0; i<myFirstArray.length; i++){
   total += myFirstArray[i];
 }
 System.out.println("Total is " + total);
 
 //Sorting the Items in the Array
 Arrays.sort(myFirstArray);
 for(int everyElement: myFirstArray){
   System.out.print(everyElement + " ");
 }
 
 //Locate the Largest Item in the Array
 System.out.println("");
 int max = myFirstArray[0];
   for (int i=1;i<myFirstArray.length; i++){
     if (myFirstArray[i] > max)max = myFirstArray[i];
   }
 System.out.println("Max is " + max);
 }
}

We started the notes by creating the array (line 10). We also discussed the difference between:

int[] myFirstArray = {100, 25, 63, 3, 12, 50, 85};

and

int myFirstArray[] = {100, 25, 63, 3, 12, 50, 85};

The first method is the preferred method for JAVA. The second method was added to the syntax to accommodate programmers who had worked in C++.

Lines 13 through 15 focus on how to display the contents of the array using the for loop..

On line 13, we have a for loop that will start iterating (counting) at 0 using the variable “i” to do the counting. The loop will continue to run until “i” is no longer less than the length of the array. After the loop is successfully run, “i” is incremented by 1.

On line 14, we have what will be executed in each iteration of the loop. The instruction says to print myFirstArray[i].

Remember, the first time through the loop, i = 0. So, line 14 will print index 0 of myFirstArray, which is the first record in the array. The second time through the loop, i = 1. At this point, it will print index 1 of myFirstArray, which is the second record in the array. This process will continue until the end of the array is reached.

Lines 19 through 21 focus on how to display the contents of the array using the foreach loop.

On line 19, we start with a for loop but the conditions are completely different. We declare an integer variable named “everyElement” and set it to have the value received from myFirstArray.

Line 20 prints the value of the variable “everyElement”.

The foreach loop runs through until each and every record has been touched.

Lines 25 through 29 discuss how to calculate the sum of the array.

On line 25, we declare an integer variable named “total” with an initial value of 0.

On line 26, we create a for loop identical to the one used on line 13. This will allow us to traverse the entire array.

On line 27, we take the variable “total” and add the currently selected index to it.

  • On the first pass, we have 0 + 100 = 100
  • On the second pass, we have 100 + 25 = 125
  • On the third pass, we have 125 + 63 = 188
  • On the fourth pass, we have 188 + 3 = 191
  • On the fifth pass, we have 191 + 12 = 203
  • On the sixth pass, we have 203 + 50 = 253
  • On the seventh and final pass, we have 253 + 85 = 338

This section could have been solved also using a foreach loop as outlined below:

int total = 0;
for(int everyElement: myFirstArray){
    total += everyElement;
}
System.out.println("Total is " + total);

Lines 32 through 35 cover how to sort an array.

Line 32 uses the sort function of the Arrays sub-library of the java.util library to sort the array. The sort function was imported on line 6.

Line 33 is a foreach loop (identical to the one seen on line 19.

Line 34 prints the array as it did on line 20.

This section could have been solved also using a traditional for loop as outlined below:

Arrays.sort(myFirstArray);
for(int i=0; i<myFirstArray.length; i++){
  System.out.print(myFirstArray[i] + " ");
}

Finally, in lines 39 to 43, we analyze how to locate the largest item in the array.

We start by declaring an integer variable named “max” and assign it the value of index 0 of the array. Since the array was just sorted on line 32, index 0 is now 3. As such, the variable “max” now has a value of 3.

Line 40 uses a slightly modified for loop as it starts iterating at 1 and not 0 as we did on line 13 and on line 26.

Line 41 uses a conditional if statement to check if the current iterated index of the array is greater than the variable “max”. If the statement is TRUE, the variable “max” is assigned the value of the iterated index. If is it FALSE, the variable “max” remains with its currently assigned value. This process repeats until the entire array is traversed.

As has been the case through all of this, you could use a foreach loop on this section of the problem as well. Here is what that code would look like with a foreach loop:

int max = myFirstArray[0];
  for (int everyElement: myFirstArray){
   if (everyElement > max)max = everyElement;
  }
System.out.println("Max is " + max);

Here is the code using traditional for loops:

//Program Name: Starting with Arrays
//Programmer Name: Eric Evans, M.Ed.
//Programmer Organization: Ferris High School
//Program Date: Spring 2017

import java.util.*;

public class arrays1 {
 public static void main(String args[]){
 int myFirstArray[] = {100, 25, 63, 3, 12, 50, 85};
 
 //Print the Array with for Loop
 for(int i=0; i<myFirstArray.length; i++){
   System.out.print(myFirstArray[i] + " ");
 }
 
 //Print the Array with foreach Loop
 System.out.println("");
 for(int everyElement: myFirstArray){
   System.out.print(everyElement + " ");
 }
 
 //Adding the Items in the Array
 System.out.println("");
 int total = 0;
 for(int i=0; i<myFirstArray.length; i++){
   total += myFirstArray[i];
 }
 System.out.println("Total is " + total);
 
 //Sorting the Items in the Array
 Arrays.sort(myFirstArray);
 for(int i=0; i<myFirstArray.length; i++){
   System.out.print(myFirstArray[i] + " ");
 }
 
 //Locate the Largest Item in the Array
 System.out.println("");
 int max = myFirstArray[0];
   for (int i=1;i<myFirstArray.length; i++){
     if (myFirstArray[i] > max)max = myFirstArray[i];
   }
 System.out.println("Max is " + max);
 }
}

Here is the code use foreach loops:

//Program Name: Starting with Arrays
//Programmer Name: Eric Evans, M.Ed.
//Programmer Organization: Ferris High School
//Program Date: Spring 2017

import java.util.*;

public class arrays1 {
 public static void main(String args[]){
 int myFirstArray[] = {100, 25, 63, 3, 12, 50, 85};
 
 //Print the Array with for Loop
 for(int i=0; i<myFirstArray.length; i++){
   System.out.print(myFirstArray[i] + " ");
 }
 
 //Print the Array with foreach Loop
 System.out.println("");
 for(int everyElement: myFirstArray){
   System.out.print(everyElement + " ");
 }
 
 //Adding the Items in the Array
 System.out.println("");
 int total = 0;
 for(int everyElement: myFirstArray){
   total += everyElement;
 }
 System.out.println("Total is " + total);
 
 //Sorting the Items in the Array
 Arrays.sort(myFirstArray);
 for(int everyElement: myFirstArray){
   System.out.print(everyElement + " ");
 }
 
 //Locate the Largest Item in the Array
 System.out.println("");
 int max = myFirstArray[0];
   for (int everyElement: myFirstArray){
    if (everyElement > max)max = everyElement;
   }
 System.out.println("Max is " + max);
 }
}

As you can see, both result in the same number of lines of code. It would ultimately be programmer’s preference and customer request for which loop structure would be used.