



|
|
Byron's CSC212 Web Site
|
Class Notes
Wednesday November 1 , 2000
|
|
|
Arrays
Class Notes --
Wednesday November 1 , 2000
CSC212 - November 01, 2000
========================
Lecture Topic: ARRAYS
Associated with each array is a "public variable" called
"Length"
which stores the number of memory cells in the array.
___
a -> | | 0 a.length -> 3
|___|
| | 1
|___|
| | 2
|___|
Tasks:
Assume: a is an array of real integer numbers,
b is an array of Strings,
both have been instantiated.
1) Display all of the values in array a - one per line.
a.length yields length of cell
for (int x = 0; x < a.length; x++) // No () needed
{
IO.println ([x]);
}
___
x -> 0 a[0] | 0 |
|___|
x -> 1 a[1] | 1 |
|___|
x -> 2 a[2] | 2 |
|___|
or:
for (int x = 1 ; x <= a.length; x++)
{
IO.println(a[x-1]);
}
2) Add 10 to each ELEMENT (ELT) of array a
for (int x = 0; x < a.length; x++)
{
a[x] = a[x] + 10;
}
Ex: a[0] = a[0] + 10;
a[1] = a[1] + 10;
... ... ;
3) Assign the largest value in array a to the variable
"max" - which must be declared. Assume a has at
least one element.
int max = a[0];
for (int x = 1; x < a.length; x++)
{
if(a[x] > max)
{
max = a[x];
}
}
Array:
___
a[0] | 5 | ENVIRONMENT
|___|
a[1] |10 | max = 5 10 16 16 16
|___|
a[2] | 4 | x = 1 2(4) 3(16) 4(9)
|___|
a[3] |16 |
|___|
a[4] | 9 |
|___|
Hand Simulate previous code.
4) Make a copy of array a, call the copy ... c.
___ ___
a[0] | 5 | c[0] | 5 |
|___| |___|
a[1] |10 | c[1] |10 |
|___| |___|
a[2] | 4 | c[2] | 4 |
|___| |___|
NOTE - This WON't do:
___
# c = a; c[0] - | 5 |
# a[0] - |___|
# |10 |
# |___|
# | 4 |
# |___|
The Solution:
// Create array c
int c[];
c = new int [a.length];
for (int x = 0; x < a.length; x++)
{
c[x] = a[x];
}
5) Swap the first two values in Array a
___ ___
a[0] | 5 | c[0] | 8 |
|___| |___|
a[1] | 8 | c[1] | 5 |
|___| |___|
a[2] |10 | c[2] |10 |
|___| |___|
a[3] | 4 | c[3] | 4 |
|___| |___|
What do you think of this?
a[0] = a[1] WRONG!
a[1] = c[0]
The Standard Way: "Array Processing"
int temp = a[0];
a[0] = a[1];
a[1] = temp;
___ ___
a[0] | 5 | c[0] | 8 |
|___| |___|
a[1] | 8 | c[1] | 5 |
|___| |___|
a[2] |10 | c[2] |10 |
|___| |___|
a[3] | 4 | c[3] | 4 |
|___| |___|
temp | 0 | temp | 5 |
|___| |___|
Problem: Read several (at least one) positive integers
and display all of the integers which are
greater than their average value.
Example:
Input: 10 4 15 7 -1 //Average = 9
Output: 10 15
A solution which features the global procedural
application architectures. All mehods command style.
Step 1 - Main Method.
Step 2 - Declare Global Variables
Step 3 - Refine The Method
1) The main method
static public void main(String args)
{
readAndStoreTheNumbers() //method
computeAverage(); //method
displayTheNumbersAboveAverage() //method
}
2) The variables
//int a[];
static private int a[];
static private final int LIMIT=1000 // CONSTANT
static private double average;
static private int nrElements;
static private int number;
3) Refine the methods
static private void readAndStoreTheNumbers
// "void" returns no value
{
a = new int [LIMIT] //LIMIT is a constant
nrElements = 0;
number = IO.read_int();
while number > 0
{
a[nrElements] = nrElements + 1;
number = IO.read_int();
}
}
static private void computeTheAverage()
{
int sum = 0;
for (int x=0;x<nrElements;x++)
{
sum = sum + a[x];
}
average = (double) sum / (double) nrElements;
static private void displayTheNumbersAboveAverage
{
for (int x=0; x < nrElements; x = x++)
}
if (a[x] > average)
{
IO.println(a[x]);
}
}
}
// Demo: Displays 10 & 15
// "sum" is a local variable
// "nrElements = 4" is a Global Variable
// Environment is local to computeTheAverage
sum --> 0 10 14 19 36
x --> 0 1 2 3 4
Average:36 / 4 = 9.0
Hand Simulate Solution
Environment (Global Procedural)
[0] [1] [2] [3] [4] ... [n]
____________________________________
a | 10 | 4 | 15 | 7 | 15 | ...| n |
|____|____|____|____|____|____|____|
=========================================================
12345678911234567892123456789312345678941234567895123456
|
|
|