### Order Description

Week 3 Assignments: In programming, when we mention the word “Array”, many students cringe because it sounds scary and they don’t understand it. But it’s really nothing to worry about and actually quite simple to understand. For example, take a file cabinet

Let’s say this open drawer has 12 folders in it. We can name this anything but since we’re going to expand on this in a moment let’s call it FC, for file cabinet. Dim FC(12) as integer We have just declared an array called FC that has 12 memory locations in it. Because it’s declared as an integer, we would be storing a numeric value in these 12 locations. For example, if we wanted to store 12 months’ worth of sales data (some amount for each month) this would be a good way to do so. FC(1) = 3,498,234 FC(2) = 4,585,122 FC(3) = 2,688,782 FC(4) = 4,999,122 FC(5) = 6,498,234 FC(6) = 5,585,122 FC(7) = 1,498,234 FC(8) = 8,585,122 FC(9) = 2,226,234 FC(10) = 4,666,781 FC(11) = 3,123,234 FC(12) = 5,444,689

Now, if we wanted to know the sales for July we would simply address FC(7) which would give us 1,498,234

We can also make this a little more user friendly by defining the names of the months.

Dim January as integer = 1 Dim February as integer = 2 And so on through December.

Now we can ask for FC(July) which again would give us 1,498,234

Let’s say these sales figures belong to a store, and let’s also say that this company has a number of stores. We have this file cabinet which has 4 drawers and we just so happen to have 4 regions, East, West, North and South and each has 12 months of sales data. All we need to do is add a level (another dimension) to our array.

Dim FC(4,12) as integer.

The 4 represent the 4 regions, East, West, North and South. Again for simplicity we could assign a name/value to these

Dim East as integer = 1 Dim West as integer = 2 And so on.

Now, if we want to see the sales for the West region in August we could do one of the following.

FC(2,8) ‘address west region for august FC(West,August) ‘same as above

Of course we would probably be displaying this in a list box, or msgbox and would have to convert that data from integer to string.

Moving on, let’s say this company has 6 divisions that sell distinct products. 6 file cabinets could be used to house these 6 divisions, each with their 4 regions and each with 12 months of sales data.

Dim FC(6,4,12)

We now have a 3-dimensional array. Perhaps these 6 divisions have names like Tires, Wheels, Parts, Services, Brakes, Mufflers. Again these names can be used instead of numbers 1-6 to make things more readable:

Dim Tires as integer = 1 Dim Wheels as integer = 2 And so on

How about a 4-dimensional array?

From the above it sounds like we have an automotive company, let’s say Firestone which owns a number of stores, like Firestone, Tires Plus, Bridgestone, Dayton, Wheel Works, TruckPM, etc. Perhaps they would like to drill down into these, so here we go with our 4th dimension.

FC(7,6,4,12)

Again, we can address the first dimension by name:

Dim Firestone as Integer = 1 Dim TiresPlus as Integer = 2 And so on

Note that this 4-dimensional array takes 2016 memory locations (7x6x4x12). You can go much larger with each dimension and you can have more than 5, but as you increase your memory availability decreases so keep this in mind.

Assignment:

Create an Xojo program that will load a 2-dimensional array having dimensions of 4,12

HINT: dim FC(4,12) as integer, use two for loops to load the array values.

Double hint: for x = 1 to 4 for z = 1 to 12 FC(x,z) = z next z next x

Create a Word document. Run your program and put a break point at the FC(x,z)=z line above.

Provide a screen shot of the results in the break point at 2,6 and 3,4

Extra credit. Do the same thing in Java (see Java example below). Arrays in Java In Java it is good to work with Arrays when we know a fixed size of the Array. If we do not know the size of the Array we use an ArrayList (we will get to it later).

String[] carOwners ;

Other examples: byte[] anArrayOfBytes; short[] anArrayOfShorts; long[] anArrayOfLongs; float[] anArrayOfFloats; double[] anArrayOfDoubles; boolean[] anArrayOfBooleans; char[] anArrayOfChars; String[] anArrayOfStrings;

Reference = https://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html

Here we declared the Array type (String), the name (carOwners). There are two ways to initialize the Array. The decision of which way to use will depend on what information is given.

String[] carOwners = {"John Smith", "john Doe"};

In this example we already know the content of the Array and we can add to the list if needs to be.

The second way to initialize an Array is by declaring the type, the name, and allocating memory. If the memory is not allocated, we will get an error.

String[] carOwners = new String;

To initialize this Array we need to do it one element at a time. First, we should decide which index we want initialize and declared it. Keep note that indexes start from 0. We previously allocated 2 indexes for the Array, so the indexes that it contains are 0 and 1.

// Initialize first element. carOwners = “John Smith”; // Initialize Second element. carOwners = “john Doe”;

We have successfully initialized the Array.

Processing an Array with Loops Populating the Array We can process a complete Array in one pass through with a For-Each loop. The For-Each loop is used in Arrays and Collections. It goes through each element with less code. The for each loop set up: For ( <: > )

In this example we declare the data type and and variable name of what would be contained the Array. Then we just process each one. The scanner class is used to take input from the user. The scanner will know that the allocated memory for the Array and after it is pressed enter that amount of times it will stop.

Now we have a populated Array that was based on user input. If we want to print the contents of the Array in one pass through, we use the for-each loop.

Again we declare a single element of the Array , in this situation is a String. Then we process each element.

Multidimensional Array In Java a Multidimensional Array is an Array whose components are themselves Arrays. In this section we will talk about a Multidimensional Array. Multidimensional Arrays are good when representing programs that model board games like checkers, chess, and tic-tac-toe. Basically, we can use Multidimensional Arrays to model any type of mapping one component to the next. Reference - https://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html Declaring Multidimensional Arrays Just like the simple Arrays, the Multidimensional Arrays have two different ways to get declared and initialized. We can see in the following example that a Multidimensional Arrays is an Array Arrays. String [] [] carOwnerNames = { // rows // 0 1 2 {"Mr. ", "Mrs. ", "Ms. "}, //column 0 {"Smith", "Doe", "Perez"} //column 1 }; This Multidimensional Array is not populated. If we wanted to map the title to the names, we can pass the right positions of the indexes to map a title to a last name. So, in the above example, “Mr.” is found at carOwnerNames   and “Smith” is found in carOwnerNames  . To be able to print the Mr. Smith we would do this: System.out.println(carOwnerNames   + carOwnerNames  ); Notice that the space between the title and the last name is already included in the declaration in column 0.

The second way to declare a Multidimensional Array is declaring the data type, variable name, and allocate the memory. String [] [] carOwnerNames = new String  ; // 3 rows 3 columns for memory allocation. We added one more column to this Array. We will use the third column to represent states. Now we have titles, names, and states. We have an initialized the Array, but no data in it. We will take the input of a user to populate it. Note – An Array with these dimensions would have been a 3D Array in Xojo, but in Java is still called a Multidimensional Array. There are different ways to populate a Multidimensional Array. In this example we populate one row at a time. On the first For-Loop we start in the first row and pass through the row until is done the move to the next column. Whether the Arrays is a  or  or a , they are all processed the same way.

Printing the Array Now that we have a populated Array we can print it. Like we did before, we will use a For-each loop to pass through the Array and print out the results.