1 / 13

CSIS113A

CSIS113A. Lecture 11 Multi-dimensional Arrays. Multi-dimensional Intro. Arrays can come in just about as many dimensions as you would like. My opinion is that if you have to go more than two dimensions, there may be a better way of doing things.

roana
Download Presentation

CSIS113A

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. CSIS113A Lecture 11 Multi-dimensional Arrays Glenn Stevenson CSIS 113A MSJC

  2. Multi-dimensional Intro • Arrays can come in just about as many dimensions as you would like. • My opinion is that if you have to go more than two dimensions, there may be a better way of doing things. • Think of a two dimension array as a grid or kind of like a bingo card that consists of rows and columns. • Like the old Battleship game • You declare a two dimensional array the same way you would a one dimensional array except that you have to add another set of brackets: Glenn Stevenson CSIS 113A MSJC

  3. 2D Array Depicted Glenn Stevenson CSIS 113A MSJC

  4. Rows & Columns • Think of the number in the first bracket as the number of rows and the number in the second bracket as the number of columns. Glenn Stevenson CSIS 113A MSJC

  5. 2D Arays & Memory • In memory, the elements are contiguous, like this: Glenn Stevenson CSIS 113A MSJC

  6. 2D Array Initialization Glenn Stevenson CSIS 113A MSJC

  7. Rules To Live By • You must supply the number of columns (the second subscript). • The two dimensional array must be enclosed in curly braces • Te closing curly brace surrounding the array must have a semicolon • Each row is surrounded by curly braces • Each value within the row must be comma separated • All rows are comma separated Glenn Stevenson CSIS 113A MSJC

  8. Storage & Retrieval Glenn Stevenson CSIS 113A MSJC

  9. #include <iostream>#include <ctime> using namespace std; int main(){int ar[5][5]; srand(time(0));   for(int row = 0; row < 5; row++)   {      for(int col = 0; col < 5; col++)      {         ar[row][col] = rand() % 101;      }   }   for(int row = 0; row < 5; row++)   {      for(int col = 0; col < 5; col++)      {         cout << ar[row][col] << "\t";      }      cout << endl;   }   return 0;} Glenn Stevenson CSIS 113A MSJC

  10. Passing 2D arrays to functions is a little different than passing single dimension arrays but, not much. When you call the function you still just pass the array name, and you are still passing the base memory address of the array (address of first element or array[0]) Multi-Dimensional Arrays As Arguments Glenn Stevenson CSIS 113A MSJC

  11. Example Glenn Stevenson CSIS 113A MSJC

  12. #include <iostream>#include <ctime> using namespace std; void fillArray(int ar[][5], int numRows);void printArray(const int ar[][5], int size); int main(){int array[5][5];    fillArray(array, 5);   printArray(array, 5);return 0;}void fillArray(int ar[][5], int numRows){   srand(time(0));   for(int row = 0; row < numRows; row++)   {      for(int col = 0; col < 5; col++)      {         ar[row][col] = rand() % 101;      }   }} Glenn Stevenson CSIS 113A MSJC

  13. void printArray(const int ar[][5], int numRows){   for(int row = 0; row < numRows; row++)   {      for(int col = 0; col < 5; col++)      {         cout << ar[row][col] << "\t";      }      cout << endl;   } } Glenn Stevenson CSIS 113A MSJC

More Related