Lecture 10
Week 10
Arrays
Last Week
n
Arrays
n
Declaration
Instantiation
This Week
n
Strings
Output ?
void setup(){
int x = 10;
funct1(x);
println("In Setup "+x);
}
void funct1(int x){
x+=2;
println("In Function"+x);
}
E.g.
n
What if
n
Student
Assessment 1
Assessment 2 Assessment 3
Student 1
89
97
65
Student 2
66
100
34
Student 3
78
93
65
Syntax
n
Example
n
10
Example
n
n
11
We need to have 2 loops, one for rows and one for columns.
The outer loop can be used for rows and inner loop can be
used for columns.
12
13
Explicit Initialization
n
3, 5},
5, 7},
7, 11},
11, 13}};
14
Traversing Multidimensional
Arrays
int[][] myArray = new int[5][6];
for(int i =0; i < 5; i++)
for(int j = 0; j < 6; j++)
myArray[i][j] = i+j;
for(int i =0; i < 5 ; i++){
for(int j = 0; j < 6; j++){
print(myArray[i][j]+" ");
}
println();
}
Output ?
15
Exercise
n
1 2
2 4
3 6
4 8
5 10
3
6
9
12
15
4
8
12
16
20
5
10
15
20
25
6
12
18
24
30
16
Exercise
n
1 2
2 4
3 6
4 8
5 10
3
6
9
12
15
x[3][3]
x[4][2]
x[5][6]
4
8
12
16
20
5
10
15
20
25
6
12
18
24
30
17
Exercise
n
18
Exercise
n
Write a function named equals that when passed two 2dimensional arrays of 3X3, returns true if both arrays are
equal and false otherwise.
Arrays are equal when all the elements of both arrays are
same.
10,20,30
77,39,45
89,44,23
is equals to
10,20,30
77,39,45
89,44,23
19