Vous êtes sur la page 1sur 34

ITEC102- SIBT

Lecture 9
Week 9
Arrays

Last Week
Functions
Nested Function Calls
Global and Local Variables to Functions

This Week
Arrays
Declaration
Initialization
Indexing
Passing

Practice and Exercises

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

Need for Arrays


To store a set of values of the same data type

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

Declaration and Instantiation


dataType[] arrayName = new dataType[size];

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

Solutions Sample Questions


a. 10 integers:

int[ ] intArr = new int[10];

b. 5 floats:

float[ ] floatArr = new float[5];

c. 8 chars:

char[ ] charArr = new char[8];

d. 4 booleans:

boolean[ ] boolArr = new boolean[4];

12

Getting number of items


arrayName.length

Example,
int [ ] ageList = new int [6];
println(ageList.length); //6

13

Accessing Array Items


float[ ] arr = new float[5];

Access item at index 0 arr[0]


Access item at index 1 arr[1]
Access item at index 2 arr[2]
Access item at index 3 arr[3]
Access item at index 4 arr[4]
--------------------------Access item at index i arr[i]

for i from 0 to arr.length - 1


14

Accessing Array Items


//for each item of the array
for(int i=0; i<arr.length; i++) {

//visit arr[i]
}

15

Initialization by Iteration

float[] arr = new float[30];


//for each item of the array
for(int i=0; i<arr.length; i++) {
arr[i] = random(10);
}

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

//for each item of the array


for(int i=0; i<arr.length; i++) {
text(arr[i], 50, 40 + 30*i);
}
/* where the first value is displayed at (50,40), second
value at (50,70), third value at (50,100), and so on
*/

18

Operating on Array Items


float[] GPAs = new float[10];
float total= 0, avgGPA;
for(int i=0; i<GPAs.length; i++) {
total += GPAs[i];
}
avgGPA = total/GPAs.length;

19

Class Exercise

int[] arr = new int[10];


for(int i=0; i<arr.length; i++)
arr[i] = i+1;
//What are the array items values?

for(int i=0; i<arr.length; i++)


arr[i] = 5*I;
//What about now?

for(int i=arr.length-1; i>=0; i--)


arr[i] = 20-i*2;
//And now?
20

Class Exercise

int[] arr = new int[10];


for(int i=0; i<arr.length; i++)
arr[i] = i+1;
//1, 2, 3, , 10

for(int i=0; i<arr.length; i++)


arr[i] = 5*i;
//0, 5, 10, , 45

for(int i=arr.length-1; i>=0; i--)


arr[i] = 20-i*2;
//20, 18, 16, , 2

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];
}

Passing array to functions

call

int[] diceOutcomes= new int[10];


initialize(diceOutcomes,1,6);
for(int i=0; i<diceOutcomes.length; i++)
println(diceOutcomes[i]);
//WILL BE in range 1 to 6

definition

void initialize(int[] a, int low, int high) {


for(int i=0; i<a.length; i++)
a[i] = (int) random(low, high+1);
}

30

Passing Array to Functions


So, when you pass an array (say A) to a
function as array (B), and modify B, A is
modified too.
This is, because, unlike individual
variables, a duplicate of an array is NOT
made and the formal parameter (B) is
physically the same as A

31

Passing Array to Functions


A and B being
individual variables

A and B being
arrays

A = 10

10
20

B = 10

A,B

30

32

Declaring Multiple Arrays

float[] x, y;

x = new float[4];

y = new float[4]; //Error


Because the declaration code is parsed as:

float[] x;

float y;
You need to declare each array individually as:

float[] x;

float[] y;

33

Vous aimerez peut-être aussi