Docsity

MULTI DIMENSIONAL ARRAYS C++, Slides of C programming

Partial preview of the text

Download MULTI DIMENSIONAL ARRAYS C++ and more Slides C programming in PDF only on Docsity!

Multidimensional Arrays

2

Introduction

⚫ Data is often available in tabular form ⚫ Tabular data can be represented using arrays ⚫ Matrix is an example of tabular data and is often represented as a 2-dimensional array ⚫ After the formation of a two dimensional matrix you can then perform mathematical operations on the array.

4 C++ Array Representation

2D Arrays

The elements of a 2-dimensional array a declared as: Syntax: int a[3][4]; may be shown as a table a[0][0] a[0][1] a[0][2] a[0][3] a[1][0] a[1][1] a[1][2] a[1][3] a[2][0] a[2][1] a[2][2] a[2][3]

Columns of a 2D Array

a[0][0] a[0][1] a[0][2] a[0][3] a[1][0] a[1][1] a[1][2] a[1][3] a[2][0] a[2][1] a[2][2] a[2][3] Col- 0 Col- 1 Col- 2 Col- 3

Initialization Examples

8 int A[3][4] = { {8, 2, 6, 5}, //row 0 {6, 3, 1 ,0}, //row 1 {8, 7, 9, 6} }; //row 2 int A[3][4] = { 8, 2, 6, 5,6, 3, 1 ,0,8, 7, 9, 6}; double distance[][4] = { { 44, 72, 96, 46 }, { 6, 68, 34, 62} }; int array3[ 2 ][ 3 ] = { { 1, 2 }, { 4 } }; 1 2 0 4 0 0 44 72 96 46 6 68 34 62

Exercise

⚫ Write a program to print the diagonal entries of a square matrix. 10

Brief solution

int a[3][3]; for(int i=0;i<=2;i++) { cout<<a[i][i]<<endl; } 11