Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 11 Multi-dimensional Arrays

Similar presentations


Presentation on theme: "Lecture 11 Multi-dimensional Arrays"— Presentation transcript:

1 Lecture 11 Multi-dimensional Arrays
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 Glenn Stevenson CSIS 113A MSJC
2D Array Depicted Glenn Stevenson CSIS 113A MSJC

4 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

5 Glenn Stevenson CSIS 113A MSJC
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 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

8 Glenn Stevenson CSIS 113A MSJC
Storage & Retrieval Glenn Stevenson CSIS 113A MSJC

9 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; } Glenn Stevenson CSIS 113A MSJC

10 Multi-Dimensional Arrays As Arguments
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]) Glenn Stevenson CSIS 113A MSJC

11 Glenn Stevenson CSIS 113A MSJC
Example Glenn Stevenson CSIS 113A MSJC

12 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

13 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;    } } Glenn Stevenson CSIS 113A MSJC


Download ppt "Lecture 11 Multi-dimensional Arrays"

Similar presentations


Ads by Google