Difference between revisions of "Working with Partially Filled Arrays"

From CompSciWiki
Jump to: navigation, search
Line 2: Line 2:
  
 
==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.
 
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.
Line 43: Line 45:
 
===How to Check for an Empty Array Value===
 
===How to Check for an Empty Array Value===
  
How you check for an empty array element depends on the type of values stored in the array and how you define an element.
+
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 23:46, 4 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 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.

   

{{{Body}}}

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 elements will be empty.

Types of Partially Filled Arrays

There are 3 types of partially filled arrays:

1. Linear Contiguous Block

//placeholder for picture of linear contiguous block

The filled elements in the partially filled array start at the first element, and additional filled elements come immediately after the previously filled element. However, the array is not completely full.

2. Non-Linear Contiguous Block

//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.

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

2. How to keep track of the number of elements in the array

How to Check for an Empty Array Value

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.



more text after the wicked boxenz

Checking for Empty Array Values

wahooo!

Example.jpg