csis113a
DESCRIPTION
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. - PowerPoint PPT PresentationTRANSCRIPT
CSIS113A
Lecture 11
Multi-dimensional Arrays
Glenn Stevenson CSIS 113A MSJC
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
2D Array Depicted
Glenn Stevenson CSIS 113A MSJC
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
2D Arays & Memory
• In memory, the elements are contiguous, like this:
Glenn Stevenson CSIS 113A MSJC
2D Array Initialization
Glenn Stevenson CSIS 113A MSJC
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
Storage & Retrieval
Glenn Stevenson CSIS 113A MSJC
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;}
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])
Example
Glenn Stevenson CSIS 113A MSJC
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; } }}
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; }}