 Download Download Presentation An introduction to arrays, continued

An introduction to arrays, continued

Download Presentation An introduction to arrays, continued

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

1. An introduction to arrays,continued

2. Recall from last time… In the code that follows, we will simply deal with rooms numbered 0..N-1. Also, we’ll define a variable called occupied to make things easier to read. public static void main ( String args[] ) { //define number of rooms final int N = 100; //define hotel rooms boolean rooms[] = new boolean[ N ]; //define occupied (or not) final boolean occupied = true; //initialize to all unoccupied for (int i=0; i<N; i++) { rooms[i] = !occupied; } … }

3. Recall from last time… • How do we determine if a given room k is occupied?

4. Recall from last time… • How do we determine if a given room k is occupied? if (rooms[k] == occupied) System.out.println( “room “ + k + “ is occupied.” ); else System.out.println( “room “ + k + “ is not occupied.” );

5. Recall from last time… • How do we determine if a given room k is occupied? if (rooms[k] == occupied) System.out.println( “room “ + (k+1) + “ is occupied.” ); else System.out.println( “room “ + (k+1) + “ is not occupied.” ); A third alternative to the arrays-starting-at-0-but-people-starting-at-1 problem is to represent them internally as 0..N-1 and externally as 1..N by adding 1. But we’ll stick with 0..N-1 both inside and out in these examples.

6. Recall from last time… • How can we find an unoccupied room?

7. Recall from last time… • How can we find an unoccupied room? int where = -1; for (int i=0; i<N && where==-1; i++) { if (rooms[i] == !occupied) { where = i; } } if (where != -1) System.out.println( “room “ + where + “ is unoccupied.” ); else System.out.println( “Sorry. No vacancy.” );

8. How can we determine the percentage of rooms that are occupied? int count = 0; for (int i=0; i<N; i++) { if (rooms[i] == occupied) ++count; } double pcnt = ((double)count) / N * 100.0; System.out.println( pcnt + “% occupied.” );

9. Recall from last time… • 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? • Can you write this code?

10. Recall from last time… • 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? • Because we will spread the guest throughout the hotel • And because we will equally use each room. • Can you write this code?

11. Recall from last time… • 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. int where = -1; while (where==-1) { int k = r.nextInt( N ); if (rooms[k] == !occupied) where = k; }

12. Recall from last time… • 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. int where = -1; while (where==-1) { int k = r.nextInt( N ); if (rooms[k] == !occupied) where = k; } What happens when the hotel is full?

13. Recall from last time… • 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. int where = -1; for (int attempt=0; attempt<1000 && where==-1; attempt++) { int k = r.nextInt( N ); if (rooms[k] == !occupied) where = k; }

14. Recall from last time… • 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. int where = -1; for (int attempt=0; attempt<1000; attempt++) { int k = r.nextInt( N ); if (rooms[k] == !occupied) { where = k; break; } } Similar, but with a break.

15. Recall from last time… • 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. int where = -1; for (int attempt=0; attempt<1000 && where==-1; attempt++) { int k = r.nextInt( N ); if (rooms[k] == !occupied) where = k; } But there is still a small chance that the hotel has one room. Yet we were unlucky and didn’t find it. What can we do?

16. Recall from last time… • 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. int where = -1; for (int attempt=0; attempt<1000 && where==-1; attempt++) { int k = r.nextInt( N ); if (rooms[k] == !occupied) where = k; } for (int i=0; i<N && where==-1; i++) { if (rooms[i] == !occupied) where = i; } if (where != -1) … Same as before.

17. Recall from last time… • 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?

18. Recall from last time… • 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). int where = -1; for (int i=0; i<N && where==-1; i++) { if ( rooms[i] == !occupied && rooms[i+1] == !occupied && rooms[i+2] == !occupied ) { where = i; } } This works well except for one problem. What happens if the hotel is full except for the last room?

19. Recall from last time… • 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). int where = -1; for (int i=0; i<N && where==-1; i++) { if ( rooms[i] == !occupied && rooms[i+1] == !occupied && rooms[i+2] == !occupied ) { where = i; } } What happens if the hotel is full except for the last room - array bounds exception! How can we fix it?

20. Recall from last time… • 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). int where = -1; for (int i=0; i<N-2 && where==-1; i++) { if ( rooms[i] == !occupied && rooms[i+1] == !occupied && rooms[i+2] == !occupied ) { where = i; } } What happens if the hotel is full except for the last room - array bounds exception! Problem solved!

21. Recall from last time… • 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?

22. Use another array to indicate whether or not a room allows smoking or not. • What data type should we use? • How many do we need?

23. Use another array to indicate whether or not a room allows smoking or not. • What data type should we use? • boolean • How many do we need? • One for each room. Another array!

24. Use another array to indicate whether or not a room allows smoking or not. //define number of rooms final int N = 100; //define hotel rooms boolean rooms[] = new boolean[ N ]; boolean allowsSmoking[] = new boolean[ N ]; //define occupied (or not) final boolean occupied = true; //initialize to all unoccupied for (int i=0; i<N; i++) { rooms[i] = !occupied; allowsSmoking[i] = false; } //rooms 15..25 allow smoking for (int i=15; i<=25; i++) { allowsSmoking[i] = true; }

25. How can we modify this to find a free non-smoking room? //Code to randomly find a free room. //It determines the free room #. //It should yield -1 if no rooms are free. int where = -1; for (int attempt=0; attempt<1000 && where==-1; attempt++) { int k = r.nextInt( N ); if (rooms[k] == !occupied) where = k; } for (int i=0; i<N && where==-1; i++) { if (rooms[i] == !occupied) where = i; } if (where != -1) …

26. How can we modify this to find a free non-smoking room? //Code to randomly find a free room. //It determines the free room #. //It should yield -1 if no rooms are free. int where = -1; for (int attempt=0; attempt<1000 && where==-1; attempt++) { int k = r.nextInt( N ); if (rooms[k] == !occupied && !allowsSmoking[k]) where = k; } for (int i=0; i<N && where==-1; i++) { if (rooms[i] == !occupied && !allowsSmoking[i]) where = i; } if (where != -1) …

27. Use another array for wakeup calls. • What data type should we use? • Let’s make it an integer like military time. Ex. 700 (0700) is 7AM. 1330 is 1:30 PM.

28. Use another array for wakeup calls. • Let’s make it an integer like military time. Ex. 700 (0700) is 7AM. 1330 is 1:30 PM. //define number of rooms final int N = 100; //define hotel rooms boolean[] rooms = new boolean[ N ]; boolean[] allowsSmoking = new boolean[ N ]; int[] wakeupTime = new int[ N ]; //define occupied (or not) final boolean occupied = true; //initialize to all unoccupied for (int i=0; i<N; i++) { rooms[i] = !occupied; allowsSmoking[i] = false; wakeupTime[i] = -1; //indicating no wakup time yet }