1 / 19

More arrays

More arrays. Two dimensional arrays. But our hotel reservation system has a limitation. What if our hotel has more than one floor and each floor can have hundreds of rooms?. Maybe matrices from mathematics can help us. Arrays (so far) are one dimensional. Matrices are two dimensional.

drushing
Download Presentation

More arrays

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. More arrays Two dimensional arrays.

  2. But our hotel reservation system has a limitation. What if our hotel has more than one floor and each floor can have hundreds of rooms?

  3. Maybe matrices from mathematics can help us. • Arrays (so far) are one dimensional. • Matrices are two dimensional. (from wikipedia)

  4. Maybe matrices from mathematics can help us. • So the row can be the floor of the hotel and the column can be the particular room # on that floor!

  5. Introducing 2D arrays.

  6. 2D arrays • Recall: Declare (and allocate) a 1D array int N = 100; //number of rooms (numbered 0..99 (N-1)) boolean rooms[] = new boolean [ N ]; • Declare (and allocate) a 2D array int Floors = 10; //number of floors (numbered 0..9) int Rooms = 100; //rooms on a floor (numbered 0..99) boolean rooms2D[][] = new boolean[ Floors ][ Rooms ];

  7. 2D arrays • Recall: Initialize a 1D array for (int i=0; i<N; i++) rooms[i] = false; //initially unoccupied • Initialize a 2D array int Floors = 10; //number of floors int Rooms = 100; //rooms on a floor boolean rooms2D[][] = new boolean[ Floors ][ Rooms ]; Looks like we’ll need a for loop for the floor and another for loop for the rooms. But first, how would we initialize all of the rooms on floor 0?

  8. 2D arrays • Recall: Initialize a 1D array for (int i=0; i<N; i++) rooms[i] = false; //initially unoccupied • Initialize a 2D array int Floors = 10; //number of floors int Rooms = 100; //rooms on a floor boolean rooms2D[][] = new boolean[ Floors ][ Rooms ]; But first, how would we initialize all of the rooms on floor 0? for (intrm=0; rm<Rooms; rm++) rooms2D[0][rm] = false;

  9. 2D arrays But first, how would we initialize all of the rooms on floor 0? for (intrm=0; rm<Rooms; rm++) rooms2D[0][rm] = false; Or… int f = 0; for (intrm=0; rm<Rooms; rm++) rooms2D[f][rm] = false; Can we initialize the next floor?

  10. 2D arrays Can we initialize the next floor? int f = 0; for (intrm=0; rm<Rooms; rm++) rooms2D[f][rm] = false; f = 1; for (intrm=0; rm<Rooms; rm++) rooms2D[f][rm] = false; How about the next?

  11. 2D arrays How about the next? int f = 0; for (intrm=0; rm<Rooms; rm++) rooms2D[f][rm] = false; f = 1; for (intrm=0; rm<Rooms; rm++) rooms2D[f][rm] = false; f = 2; for (intrm=0; rm<Rooms; rm++) rooms2D[f][rm] = false; … So f starts at 0 and then increments to the next floor and so on.

  12. 2D arrays Initialization with nested for loops. for (int f=0; f<Floors; f++) { for (intrm=0; rm<Rooms; rm++) { rooms2D[f][rm] = false; } }

  13. Recall from our 1D hotel reservation system… //function that determines how many rooms // are occupied in our hotel static intoccupiedCount ( boolean rooms[], int N ) { int count = 0; for (int i=0; i<N; i++) { if (rooms[i]) ++count; } return count; } Now, how can we make a similar function that works for our 2D hotel?

  14. Recall from our 1D hotel reservation system… //function that determines how many rooms // are occupied in our 2D hotel static intoccupiedCount ( boolean rooms2d[][], int Floors, int Rooms ) { int count = 0; for (int i=0; i<N; i++) { if (rooms[i]) ++count; } return count; } This still needs “adjustment.”

  15. Recall from our 1D hotel reservation system… //function that determines how many rooms // are occupied in our 2D hotel static intoccupiedCount ( boolean rooms2d[][], int Floors, int Rooms ) { int count = 0; int f = 0; for (int i=0; i<Rooms; i++) { if (rooms[f][i]) ++count; } return count; } This is better but it still needs “adjustment.”

  16. Recall from our 1D hotel reservation system… //function that determines how many rooms // are occupied in our 2D hotel static intoccupiedCount ( boolean rooms2d[][], int Floors, int Rooms ) { int count = 0; for (int f=0; f<Floors; f++) { for (int i=0; i<Rooms; i++) { if (rooms[f][i]) ++count; } } return count; } Done!

  17. Back to matrices from mathematics. • In linear algebra, the trace of an n-by-n square matrix A is defined to be the sum of the elements on the main diagonal (the diagonal from the upper left to the lower right) of A, i.e., (from wikipedia)

  18. Practice with 2D arrays • Calculate the trace of a 2D array. • Calculate the sum of all of the elements in a 2D array; similarly, calculate the average. • Consider a 2D array of char for tic-tac-toe. Check for a horizontal win. • Calculate the sum of a particular row in a 2D array. • Calculate the sum of a particular column in a 2D array. • Calculate means (averages) for 1-4 above. Calculate mean for 3x3.

  19. Recap • primitive values • reference values • 1D arrays and 2D arrays • arrays and functions

More Related