Presentation is loading. Please wait.

Presentation is loading. Please wait.

Fundamental Programming

Similar presentations


Presentation on theme: "Fundamental Programming"— Presentation transcript:

1 310201 Fundamental Programming
Introduction to Multidimensional Arrays

2 Introduction to two-dimensional arrays
There are many problems where the data being processed can be naturally organised as a table. A two dimensional array provides a way to build such a software model.

3 A 2D Array <Type of array> <Array Name> [<num rows>] [<num columns>] Example : float Sales_Array [3] [12] // Hold 3 years of monthly sales data.

4 A 2D Array Example to hold character for a computer screen :
const int NumRows = 24, NumColumns = 80; char Screen1 [NumRows] [NumColumns]; // Table : rows 0-23 columns 0-79

5 Getting to elements The variable Screen1 is a two-dimensional array in which the notation Screen1[0][0] is the element at row 1 and column 1 Screen1[22][60] is the element at row 23 and column 61 cout << Screen1[12][12] // display element stored row 13 col 13 Screen[23][79] = ‘*’; // store element at row 24 col 80

6 Getting to elements Accessing all elements of the table using nested loops for (int row = 0; row < NumRows; row++) { for (int col = 0; col < NumColumns; col++) cout << Screen1[row][col]; // display all columns cout << endl; // skip a line to display next row }

7 Passing 2D arrays to functions
An example : int main() { char DisplayScreen [24] [80]; Initialise(DisplayScreen); }

8 Passing 2D arrays to functions
void Initialise(Screen [ ] [ ]) // Initialise the Screen Array – set all values to spaces. { for (int row = 0; row < NumRows; row++) for (int col = 0; col < NumColumns; col++) Screen[row][col] = ‘ ‘; }

9 Initialising 2 D Arrays For example const int ROWS = 4, COLS = 3;
float TempTable[ROWS][COLS] = {17.0}; or float TempTable[ROWS][COLS] = { {65.5, 68.3, 62.0}, // Row 1 {68.8, 68.9, 64.5}, // Row {70.4, 69.4, 66.3}, // Row 3 {68.8, 68.9, 64.5}, // Row 4 } ; // using double braces

10 3D Arrays - Example So far we have looked at 2D arrays or tables now we will investigate arrays with a greater number of dimensions. i.e. have more than 2 indices

11 3D Arrays - Example To define a 3D array which holds prices for ice-creams: const int Flavors = 12, Sizes = 3, Conetypes = 2; float Prices[Flavors][Sizes][Conetypes];

12 3D Arrays - Example To address individual elements
cout << Prices[8][2][1]; // price of flavor 9, size 3, conetype 2. Price[10][0][1] = 4.75; // price of flavor 11, size 1, conetype 2.

13 3D Arrays – Example (cont …)
If you wish to loop through higher dimensional arrays displaying or setting all elements, then you require a loops for each dimension. For 2D arrays, you need 2 nested loops.

14 3D Arrays – Example (cont …)
For 3D arrays, you need 3 nested loops: for(int f = 0; f < Flavors; f++) for(int s = 0; s < Sizes; s++) for(int c = 0; c < Conetypes; c++) Price[f][s][c] = 5.50; // loop thru’ 12*3*2 (72) times

15 3D Arrays – Example (cont …)
It is easy to visualize the array in three dimensions: Size Flavor Conetype

16 Greater than 3D arrays You may declare and use greater than 3D arrays, but these are hard to visualise at first. In business, you may well be dealing with 7 dimension, 10 dimension, or higher arrays. Just think as each of the elements of the array as having more than three attributes which describes it uniquely.

17 Example : 4D Array For example, a company may wish to track its actual and forecast sales by year, month, region, and product, as follows : Years , 1998, 1999, 2000, 2001, 2002 Months - Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec Regions - NSW, Qld, Vic, Tas, SA, WA, NT Products - Toaster, Fridge, Jug, Washing_Machine int actual_sales[6][12][7][4]; int forecast_sales[6][12][7][4];

18 Example : 4D Array (cont ….)
Using a 4D array, it would be possible to store the sales details by year, month, region, and product. So, for example, to record the actual number of sales of toasters in Qld in Dec 2000, we would do so as follows : Actual_Sales [3][11][1][0] = 1500;

19 Example : 4D Array (cont ….)
To set a Forecast Sales for Toasters in Qld in Dec 2001, we may increase the 2000 value by 10% as follows : Forecast_Sales [4][11][1][0] = Actual_Sales [3][11][1][0] * 1.10;

20 Example : 4D Array (cont ….)
In fact, we could set all 2001 Forecast values to be the 2000 Actuals increased by 10% as follows : for(int month = 0; month < 12; month++) for(int region = 0; region < 7 ; region++) for(int prod = 0; prod < 4; prod++) Forecast_Sales [4][month][region][prod] = Actual_Sales [3][month][region][prod] * 1.10;

21 Things to watch out for and be aware of :
The order of dimensions is very important – you must always list the array’s dimensions in the same order in which they were originally defined ! If you define an array eg Int Whatever[10][8][6] then valid subscripts are 0-9, 0-7, and 0-5 respectively!!!

22 Example : 4D Array (cont ….)
A diagram of the 4D array may help to visualize it : Regions Years Months Products

23 5D Arrays Using a 5D array, it would be possible to store the sales details by year, month, region, product, and salesperson.

24 6D Arrays Using a 6D array, we could store numbers of students by
Mode of study (eg Internal, External), Campus (eg Bundy, Rocky, Melbourne, Sydney), Gender (eg M, F), Program (eg BIT, BMM, BAT, BComms), Year of enrolment (eg 1999, 2000, 2001), Background (eg School leaver, TAFE, Mature age)

25 Summary Multidimensional arrays are just extensions of the ideas introduced when discussing one-dimensional arrays!


Download ppt "Fundamental Programming"

Similar presentations


Ads by Google