Arrays Week 9
Arrays Week 9
Lecture 9
Week 9
Arrays
Last Week
Functions
Nested Function Calls
Global and Local Variables to Functions
This Week
Arrays
Declaration
Initialization
Indexing
Passing
Recap
Consider the following code
int test(int a, int b){
return a*b-b+a;
}
Trace out the output of the following function
call
print(test(test(test(2,1),test(1,2)),test(test(3,3),3)));
137
luckyNumbers = {80,6,12,2001,25,17,2011};
favMovies = {brave heart, gladiator"};
fascinatingValues = {3.14159, 42, 1.618};
lowerCaseVowels ={'a','e','i','o','u};
Need of Arrays
Without Arrays
int x1 = 5;
int x2 = 10;
//AND SO ON !!
int x500 = 2500;
With Arrays
int[] x = new int[500];
for(int i=0; i<x.length; i++)
x[i] = 5*(i+1);
6
Array Declaration
Syntax
dataType[ ] arrayName;
Examples
int[ ] ages;
float[ ] salaryList;
Array Instantiation
You need to allocate memory for the items in
the array before you can use it.
arrayName = new dataType[size];
Examples
ages = new int[5];
wages = new double[24];
Instantiation Implications
int[ ] ages;
ages = new int[5];
ages
ages[0] = 0
ages[1] = 0
ages[2] = 0
ages[3] = 0
ages[4] = 0
Examples
int[ ] ages= new int[50];
double[ ] wages = new double[24];
10
Sample Questions
a.
b.
c.
d.
Declaration
int[] ages;
Instantiation
ages = new int[5];
Declare, instantiate arrays for following requirements.
Draw the memory diagram.
ages
10 integers
5 floating point values
8 characters
4 booleans
ages[0] = 0
ages[1] = 0
ages[2] = 0
ages[3] = 0
ages[4] = 0
11
b. 5 floats:
c. 8 chars:
d. 4 booleans:
12
Example,
int [ ] ageList = new int [6];
println(ageList.length); //6
13
//visit arr[i]
}
15
Initialization by Iteration
16
Explicit Initialization
When you already know the values you want
inside the array, and the number of items is
not too large
dataType[] arrayName = {val1, val2, };
Example
int[] gradeBounds = {50, 65, 75, 85};
char [ ] lowerCaseVowels = {'a,'e,'i,'o,'u'};
float[ ] marathonDistances = {21.0975, 42.195};
17
Display by Iteration
18
19
Class Exercise
Class Exercise
21
Problem Solving
Create an array that contains 100 integers
such that the
first item is
0
second item is 5
third item is
0
fourth item is 5
And so on
22
Problem solving
First, creating an array to hold 100 integers
int[] arr = new int[100];
arr[0] should be 0
arr[1] should be 5
arr[2] should be 0
arr[3] should be 5
arr[4] should be 0
arr[5] should be 5
23
Problem solving
even indexed values are 0, odd indexed values are 5
declare and allocate memory for array arr to store 100
ints
for each item indexed by i (i = 0 to arr.length-1)
if i is even
arr[i] = 0
else
arr[i] = 5
end of if
end of for
24
Problem solving
even indexed values are 0, odd indexed values are 5
int[] arr = new int[100];
for(int i = 0; i < arr.length; i++) {
if ( i % 2 == 0 ) {
arr[i] = 0;
else {
arr[i] = 5;
}
}
25
Class Exercise
Assuming the following array
int[ ] nums = {5,4,2,7,6,8,5,2,8,14};
Write a code to square each number in array
((i.e., multiply each by itself)
for (int i = 0; i < nums.length; i++) {
nums[i] = nums[i]*nums[i];
}
Class Exercise
Assuming the following array
int[ ] nums = {5,4,2,7,6,8,5,2,8,14};
Write a code to calculate the sum of all the
numbers.
int sum = 0;
for (int i = 0; i < nums.length; i++) {
sum += nums[i];
}
Class Exercise
Assuming the following array
int[ ] nums = {5,4,2,7,6,8,5,2,8,14};
Write a code add a random number between
zero and 10 to each number.
for (int i = 0; i < nums.length; i++) {
nums[i] += int(random(10));
}
Class Exercise
Assuming the following array
int[ ] nums = {5,4,2,7,6,8,5,2,8,14};
Write a code to add to each number the
number that follows in the array. Skip the last
value in the array.
for (int i = 0; i < nums.length-1; i++) {
nums[i] += nums[i+1];
}
call
definition
30
31
A and B being
arrays
A = 10
10
20
B = 10
A,B
30
32
float[] x, y;
x = new float[4];
float[] x;
float y;
You need to declare each array individually as:
float[] x;
float[] y;
33