PDE - Setting Up and Accessing Arrays Part 1 (Lesson)
Setting Up and Accessing Arrays Part 1
How to Access Your Array
You may access an array in several different ways. By access initially we are setting up the array. Another access is when we need to get information from the array itself.
Individually as shown on the Create page which is obviously slow for the programmer to put together. Using this method each item is accessed individually as needed with a separate piece or line of code.
Updating individual items of an array, one here or there, is always individual: example array nbrSense would update position 5 using the following code: nbrSense[4] = 17 would change the value in nbrSense at actual position 5 (computer access [4]) to the value of 17.
An array could be set up and loaded with values one at a time using the concept above. This however is not as efficient as using a loop as demonstrated later on the page.
Example: nbrSense[ ] = {0, 0, 0, 0, 0, 0} nbrSense.length = 6 (the actual number of items in the array), the computer indices are [0] - [5] for accessing the items, and the actual position in the array using the counting numbers is 1 - 6. Remember the computer is 0 relative and we count 1 relative.
nbrSense{0} = 5
nbrSense{1} = 14
nbrSense{2} = 6
nbrSense{3} = 1
nbrSense{4} = 10
nbrSense{5} = 7
Below is a manual load using random numbers. This would work much simpler with less room for error using a load with a count or while statement.
The above load would work much simpler with less room for error using a load with a count or while statement which create loops with shortened code statements and definitely less room for error. Let's look how they would be structured to load data to the dataset nbrSense. Again, this assumes that the array data structure has been set up but is unloaded, or must be reloaded with new data.
Note:
- The use of cnt or index. Does the word matter or could "i" be used?
- The use of the arrayName.length? What does this tell the program to do?
- Does the cnt or index always have to go up by 1?
- What are the possible values returned by the next random method used in this example and why?
- What are the index or cnt values in a 6 item array data structure?
- Why is up to or < used with the loop?
Further Notes and Answers to the Above Questions
- The word for the index does not matter. What matters is that in both of these loops you must control the reading of the array data structure and increment the index to get to the next item.
- The variable name for the length of the array, in this case nbrSense.length, is created for you when you do the original array creation. This is the actual length, the number of data items in the array. Once the nbrSense.length length value is reached, the loop will no longer operate. If you try to access an item outside the array length, you will get an "out-of-bounds" error.
- No, the +1 is used if access to each of the items in the array is required. Using +2 for example would only choose the even items in the array, or starting with one and then +2 would only choose the odd array data items, skipping the first array data value.
- The nextRandom pull that is done here includes 0 but does not include 100, do the random numbers can be any whole number (integer) that is between 0 and 99.
- If you added 6 items to the array, the length of the array would be 6. So what are the positions, index or cnt values, for an array of 6 data positions? 0, 1, 2, 3, 4. 5 Ah.... this is arrayName.length - 1. In this case nbrSense.length - 1.
- The < or up to is used so that the loop does not access position number 6. This will cause an out of bounds error or exception and more than likely your program will crash. Backup is important so that you can recover 1 save back that was working in case the error is not recoverable that you as the programmer made. This will be a run time error (logic error), not a syntax error that a compiler might alert you to earlier.
Notice the efficiency of code using the loop. Additionally, using the length to control the number of times the loop is executed allows for adding more into the array if needed, by increasing only increasing the number of positions in the original default array. Unlike the original array load which used the specific index hard-coded for each line, using the length allows for a line of code to change with a size change. the individual load method needs a line of code for each item added to the size of the array. So if an array goes from size 6 to 20, the programmer would need to add 14 lines of code to get those positions loaded with values. The loop method only needed to change the initial size of the array and the loop accommodates for the new number, by checking the length variable.
Example: The loop options above allow for less code and greater efficiency for the program, programmer, as well as reliability of code as there is only one place to change if there is an issue or change needed during the pass through the array. For example: updating all values by 10 would work much quicker and effectively through through the array with a loop whereas a single load statement would need to have 10 statements to update all of the numbers.
Each of the options above will be shown in the pages following in the various problems. Arrays are read typically with a loop so that less code and better code efficiency is available versus reading each item separately with an independent statement. However, there is a place for individual updates of a data structure. For example, you deposit $100 in your bank account. The bank only wants to update your account and not everyone's so your account number is needed to make sure that your account is located and updated only.
Use of an index to read the array is needed for the count and the while, whereas the for loops can traverse the array themselves. Use of the index notation in the for each loops will allow for specific identification and processing as needed by a programmer. The for each loops automatically use the arrayName.length to exit the loop.
Input from a user could be loaded to an array using all 3 of these methods depending on the program situation and needs. Of course, with larger sets of data to be loaded, a file upload maybe used with the loops structures. This is beyond the scope of this course.
IMAGES CREATED BY GAVS.