Download - CSIS113A
![Page 1: CSIS113A](https://reader035.vdocuments.site/reader035/viewer/2022062723/56813de5550346895da7bccb/html5/thumbnails/1.jpg)
CSIS113A
Lecture 11
Multi-dimensional Arrays
Glenn Stevenson CSIS 113A MSJC
![Page 2: CSIS113A](https://reader035.vdocuments.site/reader035/viewer/2022062723/56813de5550346895da7bccb/html5/thumbnails/2.jpg)
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
![Page 3: CSIS113A](https://reader035.vdocuments.site/reader035/viewer/2022062723/56813de5550346895da7bccb/html5/thumbnails/3.jpg)
2D Array Depicted
Glenn Stevenson CSIS 113A MSJC
![Page 4: CSIS113A](https://reader035.vdocuments.site/reader035/viewer/2022062723/56813de5550346895da7bccb/html5/thumbnails/4.jpg)
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
![Page 5: CSIS113A](https://reader035.vdocuments.site/reader035/viewer/2022062723/56813de5550346895da7bccb/html5/thumbnails/5.jpg)
2D Arays & Memory
• In memory, the elements are contiguous, like this:
Glenn Stevenson CSIS 113A MSJC
![Page 6: CSIS113A](https://reader035.vdocuments.site/reader035/viewer/2022062723/56813de5550346895da7bccb/html5/thumbnails/6.jpg)
2D Array Initialization
Glenn Stevenson CSIS 113A MSJC
![Page 7: CSIS113A](https://reader035.vdocuments.site/reader035/viewer/2022062723/56813de5550346895da7bccb/html5/thumbnails/7.jpg)
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
![Page 8: CSIS113A](https://reader035.vdocuments.site/reader035/viewer/2022062723/56813de5550346895da7bccb/html5/thumbnails/8.jpg)
Storage & Retrieval
Glenn Stevenson CSIS 113A MSJC
![Page 9: CSIS113A](https://reader035.vdocuments.site/reader035/viewer/2022062723/56813de5550346895da7bccb/html5/thumbnails/9.jpg)
Glenn Stevenson CSIS 113A MSJC
#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;}
![Page 10: CSIS113A](https://reader035.vdocuments.site/reader035/viewer/2022062723/56813de5550346895da7bccb/html5/thumbnails/10.jpg)
Multi-Dimensional Arrays As Arguments
Glenn Stevenson CSIS 113A MSJC
• 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])
![Page 11: CSIS113A](https://reader035.vdocuments.site/reader035/viewer/2022062723/56813de5550346895da7bccb/html5/thumbnails/11.jpg)
Example
Glenn Stevenson CSIS 113A MSJC
![Page 12: CSIS113A](https://reader035.vdocuments.site/reader035/viewer/2022062723/56813de5550346895da7bccb/html5/thumbnails/12.jpg)
Glenn Stevenson CSIS 113A MSJC
#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; } }}
![Page 13: CSIS113A](https://reader035.vdocuments.site/reader035/viewer/2022062723/56813de5550346895da7bccb/html5/thumbnails/13.jpg)
Glenn Stevenson CSIS 113A MSJC
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; }}