Before we start, did you try solving the workbook yourself?
If not, please click the link in the resources folder and follow the instructions for this workbook.
Welcome to Workbook 6.14, the last workbook of this section.
The first thing I'm going to do is copy this function into our class, because what it will do is return
a random number between zero and 99.
Now we get to write a function that prints the contents of an array.
So the function is called print to the array public static.
It's going to be void and it will expect a2d array that can store int elements called.
Well, I guess just a ray.
So the function uses a nested loop.
The inner loop prints each row in one line.
So four and I is equal to zero.
The outer loop is going to keep running so long as I is smaller than the number of rows inside of the
studio array plus plus array length for a two RD array represents the number of rows that this array
has, and each run of the outer loop is going to run the inner loop to completion.
So here we can say int j is equal to zero.
J is smaller then however many elements there are in row I so row I length.
J plus plus, and the inner loop runs for as many elements as there are in Row II, which means we can
use this print statement to index every single element J in Row II and print it all right.
After the inner loop runs to completion, print a new line.
Very simple.
All right.
And now we call print to dx array for the following array.
Just to test it out.
So here.
Bridge going to say print to the array array.
I'm not just going to run it.
I will visualize the runtime.
So we'll step inside, print to the array.
The array parameter, as you can see, has three rows and ten elements per row.
Which means every run of the outer loop is going to run the inner loop ten times.
So here we start at I equaling zero, which corresponds to row zero.
The inner loop will keep running so long as J is smaller than the number of elements inside of row zero.
So this inner loop is going to run ten times the first time it runs.
IE is zero.
J is zero, printing 48 I is zero, J is one printing 56, I is zero.
J is two once again also printing 56.
i0j is three this time printing 76 and the inner loop is going to keep running for every single element
inside of row zero.
After the inner loop runs to completion, it prints every single element in row zero.
And now we're ready to print a new line.
All right, Now, the outer loop runs a second time.
This time I equals one representing row one.
And once again, every run of the outer loop is going to run the inner loop to completion.
It's going to run so long as J is smaller than the number of elements inside of row one.
So the inner loop will run ten times.
Here we index element zero of row one.
Here we index element to one over oh one.
Here we index element two from row 173.
All right.
And this is just going to run seven more times.
When J equals nine will be the last time that this inner loop runs.
So the inner loop runs to completion, and then we print a new line so that we prepare for the next
run of our outer loop.
Now I equals two representing row two, and then we use the inner loop to print every single element
in row two.
And once the inner loop runs to completion, we print a new line and we are done.
Now.
A-plus plus increases.
AI to three.
Three is not smaller than the number of rows three.
So our outer loop breaks we are done.
Print two DX array is effectively capable of printing any two dx array that we pass in.
This is just beautiful.
So now we can move on.
Delete the array if you achieve the result.
Indeed we did.
Now create a 2D array with 100 rows and ten columns.
By columns I mean ten elements per row.
So we'll just set this equal to a new 2D array that stores ten rows.
Or was it 100 rows?
And ten columns, ten elements per row.
We'll just say Java Sea, random numbers dot Java, Java, random numbers.
It just prints 100 rows and ten elements per row.
Ten columns.
OC use a nested loop to populate the array with random numbers before calling print to the array.
All right, so what we'll do?
This will create a loop.
The outer loop starts and equaling zero.
The outer loop is going to keep running so long as I is smaller than the number of rows is smaller than
array length, which means the outer loop is going to run 100 times.
And every time the outer loop runs, we need to run the inner loop so that we can go through every single
element in that row.
So the inner loop will keep running so long as J is smaller than the current row where iterating through
is length.
J plus plus.
And so now here we can basically index every single row, every single element in each row, and set
that equal to a random number.
All right.
I'm only going to visualize execution for the first two rows, and then I will let the runtime continue
naturally.
All right.
So here we have an array with 100 rows and ten elements per row.
Obviously, we're not going to break point through all of this.
So first I equals zero corresponding to row zero.
Every time the outer loop runs, it runs the inner loop for as many elements as there are in the current
row.
So Jay starts off at zero.
Here we update the element at index zero zero to a random number.
Here we update the element zero one to a random number.
Here we update the element to zero two to a random number.
And this keeps on going.
The inner loop is going to run to completion.
All right, now I equals one corresponding to row one.
This inner loop is going to keep running so long as J is less than the number of elements in row one.
So J starts off at zero, element zero of row one gets updated to a random number, element to one of
row one.
And now the inner loop is going to update every single element inside of row one.
After we update all over oh one.
Now we move to row two.
I'm not going to do all of that.
That will take a long time.
So let's just press continue.
Let's remove this.
Let's remove these breakpoints.
Then press continue.
All right.
Here you can see we've updated every single row, every single element in that row.
Now it's just time to print it.
We've already demonstrated that print to the array can print any to the array that we pass in.
So we'll just step over this.
And beautiful.
As you can see.
We printed a 2D array of random numbers.
I hope you enjoyed this breakpoint session.