Difference between revisions of "Working with Partially Filled Arrays"

From CompSciWiki
Jump to: navigation, search
Line 1: Line 1:
{{Template:1010Topic|Chapter_TOC=[[More With Arrays]] > [[Working with Partially Filled Arrays]]|Introduction=When working with arrays, you will come across situations where some element in an array do not have data. These arrays are said to be 'partially filled'. This section will tell you why partially filled arrays occur, types of partially filled arrays, and how to handle partially filled arrays.|Overview=Overview goes here.}}
+
{{Template:1010Topic|Chapter_TOC=[[More With Arrays]] > [[Working with Partially Filled Arrays]]|Introduction=When working with arrays, you will come across situations where some array indexes do not have useful values. These arrays are said to be 'partially filled'. This section will tell you why partially filled arrays occur, what a partially filled array looks like, and how to handle partially filled arrays.|Overview=Working with partially filled arrays requires that you have defined what an empty index is, and that you increment/decrement a counter whenever you insert/remove values from the array.}}
  
 
==Why Partially Filled Arrays Occur==
 
==Why Partially Filled Arrays Occur==
  
//change elements to indexes? maybe makes more sense
+
Partially filled arrays occur because once an array size it set, the array size cannot be changed. Therefore, we generally pick an array size that is large enough to hold the ''potential'' number of values that need to be stored. For example, if you usually need to store 20 values in an array, but sometimes you might need to store 40, then you need to make an array of size 40. However, this means that on average, half the array indexes will be empty.
  
Partially filled arrays occur because once an array size it set, the array size cannot be changed. Therefore, we generally pick an array size that is large enough to hold the ''potential'' number of values that need to be stored.
+
Other reasons why partially filled arrays occur include cases where array indexes are only filled with values once certain conditions are met. Whenever there are filled indexes mixed with empty indexes, the array will be partially filled. For example, say you wanted to make a program to keep track of which numbers from 0 to 9 a user has typed in. You could create an array of size 10, and set the corresponding array index for each number when the number is typed in. So for example, if the user has typed in 3, 7, and 9, then only the array indexes at 3, 7 and 9 will be filled and the array will be partially filled.
  
For example, if you usually need to store 20 values in an array, but sometimes you might need to store 40, then you need to make an array of size 40. However, this means that on average, half the array elements will be empty.
+
==What a Partially Filled Array Looks Like==
  
==Types of Partially Filled Arrays==
+
Figure 1 below shows the most complex type of filled index layout for a partially filled array. While there are other ways to fill the indexes which would still make the array a partially filled array, Figure 1 represents the most general case you will need to handle.
  
There are 3 types of partially filled arrays:
+
//picture haer
  
'''1. Linear Contiguous Block'''
 
  
//placeholder for picture of linear contiguous block
+
==Handling Partially Filled Arrays==
  
The filled elements in the partially filled array start at the first element, and additional filled elements come immediately
+
There are a few different ways to handle partially filled arrays depending on the layout of the filled indexes. However, given that you will need to handle the most general case (see Figure 1) at some point anyway, that is the only case you need to know how to solve. Thankfully, handling the general case is not that difficult, but it does require knowledge of two things:
after the previously filled element. However, the array is not completely full.
+
  
'''2. Non-Linear Contiguous Block'''
+
1. How you want/need to define an array index to be empty, based on the type of variable that gets stored in the array.
 
+
//placeholder for picture of non-linear contiguous block
+
 
+
The filled elements in the partially filled array look very similar to the linear contiguous block, except that the contiguous block of filled elements does not start at the first element. However, the filled elements still form a solid block of values in the array.
+
 
+
 
+
'''3. Non-Contiguous Block'''
+
 
+
//placeholder for picture of non-contiguous block
+
 
+
The filled elements in the partially filled array can be in any location, and there is no guarantee of when an array element will be filled.
+
 
+
==Handling Partially Filled Arrays==
+
  
There are a few different ways we could handle a partially filled array depending on the type of partially filled array we're dealing with. However, in some programs, array elements are constantly changing. A partially filled array with a linear-contiguous block of values one moment may have a non-contiguous block of values the next.  
+
2. How to keep track of the number of filled indexes in the array.
  
Given that the array could change, we need a solution to handle all cases. This involves knowing how to do two things:
 
  
1. How to check if an array value is empty
+
===Defining an Empty Index===
  
2. How to keep track of the number of elements in the array
+
The types of arrays you need to know about right now are arrays of string variables, and arrays of primitive variables (int, double, char, boolean).
  
===How to Check for an Empty Array Value===
+
Recall that with string variables, a string can either be set to null, or to the empty string:
  
How you check for an empty array element depends on the type of values stored in the array and how you define an element to be empty.
 
  
For example, suppose your program keeps a partially filled array of ints. If an array is filled, then it will have a value from 1 to 100.
 
  
  

Revision as of 18:27, 5 December 2007

COMP 1010 Home > More With Arrays > Working with Partially Filled Arrays


Introduction

When working with arrays, you will come across situations where some array indexes do not have useful values. These arrays are said to be 'partially filled'. This section will tell you why partially filled arrays occur, what a partially filled array looks like, and how to handle partially filled arrays.

   

{{{Body}}}

Why Partially Filled Arrays Occur

Partially filled arrays occur because once an array size it set, the array size cannot be changed. Therefore, we generally pick an array size that is large enough to hold the potential number of values that need to be stored. For example, if you usually need to store 20 values in an array, but sometimes you might need to store 40, then you need to make an array of size 40. However, this means that on average, half the array indexes will be empty.

Other reasons why partially filled arrays occur include cases where array indexes are only filled with values once certain conditions are met. Whenever there are filled indexes mixed with empty indexes, the array will be partially filled. For example, say you wanted to make a program to keep track of which numbers from 0 to 9 a user has typed in. You could create an array of size 10, and set the corresponding array index for each number when the number is typed in. So for example, if the user has typed in 3, 7, and 9, then only the array indexes at 3, 7 and 9 will be filled and the array will be partially filled.

What a Partially Filled Array Looks Like

Figure 1 below shows the most complex type of filled index layout for a partially filled array. While there are other ways to fill the indexes which would still make the array a partially filled array, Figure 1 represents the most general case you will need to handle.

//picture haer


Handling Partially Filled Arrays

There are a few different ways to handle partially filled arrays depending on the layout of the filled indexes. However, given that you will need to handle the most general case (see Figure 1) at some point anyway, that is the only case you need to know how to solve. Thankfully, handling the general case is not that difficult, but it does require knowledge of two things:

1. How you want/need to define an array index to be empty, based on the type of variable that gets stored in the array.

2. How to keep track of the number of filled indexes in the array.


Defining an Empty Index

The types of arrays you need to know about right now are arrays of string variables, and arrays of primitive variables (int, double, char, boolean).

Recall that with string variables, a string can either be set to null, or to the empty string:




more text after the wicked boxenz

Checking for Empty Array Values

wahooo!

Example.jpg