an introduction to arrays. problem: a hotel reservation system say we have a hotel of 100 rooms and...

28
An introduction to arrays

Upload: ricardo-phinney

Post on 15-Jan-2016

215 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: An introduction to arrays. Problem: a hotel reservation system  Say we have a hotel of 100 rooms and we need to know if the rooms are occupied or not

An introduction to arrays

Page 2: An introduction to arrays. Problem: a hotel reservation system  Say we have a hotel of 100 rooms and we need to know if the rooms are occupied or not

Problem: a hotel reservation system

Say we have a hotel of 100 rooms and we need to know if the rooms are occupied or not.

What data type should we use to indicate whether or not a particular room is occupied?

Page 3: An introduction to arrays. Problem: a hotel reservation system  Say we have a hotel of 100 rooms and we need to know if the rooms are occupied or not

One solution:

Let’s declare 100 boolean variables (one for each room).//false will indicate that the room is not// occupiedboolean room1 = false;boolean room2 = false;…boolean room100 = false;

Page 4: An introduction to arrays. Problem: a hotel reservation system  Say we have a hotel of 100 rooms and we need to know if the rooms are occupied or not

How can we determine if a particular room is occupied?

Page 5: An introduction to arrays. Problem: a hotel reservation system  Say we have a hotel of 100 rooms and we need to know if the rooms are occupied or not

How do we determine if a particular room is occupied (w/ an if)?

int which = in.nextInt();if (which==1) {

if (room1) System.out.println( “ occupied” );else System.out.println( “ not occupied” );

} else if (which==2) {if (room2) System.out.println( “ occupied” );else System.out.println( “ not occupied” );

} else if (which==3) {…

} else if (which==100) {if (room100) System.out.println( “ occupied” );else System.out.println( “ not occupied” );

}

Page 6: An introduction to arrays. Problem: a hotel reservation system  Say we have a hotel of 100 rooms and we need to know if the rooms are occupied or not

Is a switch any better?

Page 7: An introduction to arrays. Problem: a hotel reservation system  Say we have a hotel of 100 rooms and we need to know if the rooms are occupied or not

How do we determine if a particular room is occupied (w/ a switch)?

int which = in.nextInt();switch (which) {

case 1:if (room1) System.out.println( “ occupied” );else System.out.println( “ not occupied”

);break;

case 2:if (room2) System.out.println( “ occupied” );else System.out.println( “ not occupied”

);break;

case 3:…case 100:

if (room100) System.out.println( “ occupied” );else System.out.println( “ not occupied”

);break;

}

Page 8: An introduction to arrays. Problem: a hotel reservation system  Say we have a hotel of 100 rooms and we need to know if the rooms are occupied or not

What if we want to determine the # of rooms that are occupied?

Page 9: An introduction to arrays. Problem: a hotel reservation system  Say we have a hotel of 100 rooms and we need to know if the rooms are occupied or not

What if we want to determine the # of rooms that are occupied?

int count = 0;

if (room1) ++count;if (room2) ++count;…if (room100) ++count;

System.out.println( count+ " rooms are occupied." );

Page 10: An introduction to arrays. Problem: a hotel reservation system  Say we have a hotel of 100 rooms and we need to know if the rooms are occupied or not

More hotel problems: What if we add more rooms to our hotel? What if we want to see if we have a block

(say 3) of adjacent rooms available?

Our current approach quickly becomes unwieldy!

So let’s introduce something new (arrays) to help us.

Page 11: An introduction to arrays. Problem: a hotel reservation system  Say we have a hotel of 100 rooms and we need to know if the rooms are occupied or not

Recall scalars and vectors from math.

Scalar – one number Vectors – list of numbers

Vector X = < 1, -2, 52 > Or more generally, X = < x1, x2, x3 >

subscript

Page 12: An introduction to arrays. Problem: a hotel reservation system  Say we have a hotel of 100 rooms and we need to know if the rooms are occupied or not

Arrays

List of the same type of things. Subscript (numbering) starts with 0. How do we declare an array?

int x[] = new int [ 100 ];

How do we refer to a particular int?x[0] = 1; //first onex[1] = 7;…x[99] = 52; //last one

Page 13: An introduction to arrays. Problem: a hotel reservation system  Say we have a hotel of 100 rooms and we need to know if the rooms are occupied or not

Hint:

Remember that an array of 100 elements is subscripted by 0..99.

Page 14: An introduction to arrays. Problem: a hotel reservation system  Say we have a hotel of 100 rooms and we need to know if the rooms are occupied or not

What happens if I try the following?

int ray[] = new int [100]; // ok

System.out.println( ray[-1] ); // ?

int k = ray[100]; // ?

Page 15: An introduction to arrays. Problem: a hotel reservation system  Say we have a hotel of 100 rooms and we need to know if the rooms are occupied or not

What happens if I try the following?

int ray[] = new int [100];

System.out.println( ray[-1] );

int k = ray[100];

Boom!

Page 16: An introduction to arrays. Problem: a hotel reservation system  Say we have a hotel of 100 rooms and we need to know if the rooms are occupied or not

How long (how many elements) is an array?

Ask it!

int howLongIsMyArray = a.length;

(Reminds one of .length() and Strings.)

Page 17: An introduction to arrays. Problem: a hotel reservation system  Say we have a hotel of 100 rooms and we need to know if the rooms are occupied or not

Back to the room reservation problem (now using arrays).

It becomes much easier!

How can we use arrays?

Page 18: An introduction to arrays. Problem: a hotel reservation system  Say we have a hotel of 100 rooms and we need to know if the rooms are occupied or not

Back to the room reservation problem (now using arrays).

It becomes much easier!

final int N = 100; //# of roomsboolean rooms[] = new boolean[ N ];//init all rooms to unoccupied

How?

Page 19: An introduction to arrays. Problem: a hotel reservation system  Say we have a hotel of 100 rooms and we need to know if the rooms are occupied or not

Back to the room reservation problem (now using arrays).

It becomes much easier!

final int N = 100; //# of roomsboolean rooms[] = new boolean[ N ];//init all rooms to unoccupiedfor (int i=0; i<N; i++) {

rooms[i] = false;}

Page 20: An introduction to arrays. Problem: a hotel reservation system  Say we have a hotel of 100 rooms and we need to know if the rooms are occupied or not

Back to the room reservation problem (now using arrays).

It becomes much easier!

final int N = 100; //# of roomsboolean rooms[] = new boolean[ N ];//init all rooms to unoccupiedfor (int i=0; i<N; i++) {

rooms[i] = false;}

What happens is we use <= instead of <?

Page 21: An introduction to arrays. Problem: a hotel reservation system  Say we have a hotel of 100 rooms and we need to know if the rooms are occupied or not

Back to the room reservation problem (now using arrays).

It becomes much easier!

final int N = 100; //# of roomsboolean rooms[] = new boolean[ N ];//init all rooms to unoccupiedfor (int i=0; i<N; i++) {

rooms[i] = false;}

I keep forgetting if false means occupied or not.

Can you suggest a change to make it clearer?

Page 22: An introduction to arrays. Problem: a hotel reservation system  Say we have a hotel of 100 rooms and we need to know if the rooms are occupied or not

Back to the room reservation problem (now using arrays).

It becomes much easier!

final int N = 100; //# of roomsboolean rooms[] = new boolean[ N ];final boolean occupied = true;//init all rooms to unoccupiedfor (int i=0; i<N; i++) {

rooms[i] = false;}

I keep forgetting if false means occupied or not.

Can you suggest a change to make it clearer?

Here’s a hint.

Page 23: An introduction to arrays. Problem: a hotel reservation system  Say we have a hotel of 100 rooms and we need to know if the rooms are occupied or not

Back to the room reservation problem (now using arrays).

It becomes much easier!

final int N = 100; //# of roomsboolean rooms[] = new boolean[ N ];final boolean occupied = true;//init all rooms to unoccupiedfor (int i=0; i<N; i++) {

rooms[i] = !occupied;}

I keep forgetting if false means occupied or not.

Can you suggest a change to make it clearer?

Here’s a hint.

Page 24: An introduction to arrays. Problem: a hotel reservation system  Say we have a hotel of 100 rooms and we need to know if the rooms are occupied or not

Interesting code to write:

Write a piece of code to find a free room. It should determine the free room #. (It should yield -1 if no rooms are free.)

Can you write this code?

Page 25: An introduction to arrays. Problem: a hotel reservation system  Say we have a hotel of 100 rooms and we need to know if the rooms are occupied or not

Interesting code to write:

Write a piece of code to randomly find a free room. It should determine the free room #. It should yield -1 if no rooms are free.

Why is this a better approach in practice (in the “real world”)?

Can you write this code?

Page 26: An introduction to arrays. Problem: a hotel reservation system  Say we have a hotel of 100 rooms and we need to know if the rooms are occupied or not

Interesting code to write:

Write a piece of code to count the total number of free rooms. Or conversely, write a piece of code to

count the number of rooms in use.

Can you write this code?

Page 27: An introduction to arrays. Problem: a hotel reservation system  Say we have a hotel of 100 rooms and we need to know if the rooms are occupied or not

Interesting code to write:

Write a piece of code to find a block of 3 free rooms.

It should determine the lowest room number of the 3 free rooms (otherwise, -1).

Can you write this code?

Page 28: An introduction to arrays. Problem: a hotel reservation system  Say we have a hotel of 100 rooms and we need to know if the rooms are occupied or not

Interesting code to write:

Use another array for wakeup calls.

Use another array to indicate whether or not a room allows smoking or not.

Can you write this code?