You are on page 1of 6

Multidimensional data

Arrays
An array is collection of values, with every value is accessed
by an index, i.e.,

int pfmarks[50]; // makes an array of 50 ints


String cityname[12]; // makes an array of 12 strings

6th element of each array is accessed as pfmarks[5] and


cityname[5] respectively. There is no 20th element in cityname
(as its size is 12) and the same of pfmarks is accessed as
pfmarks[19].

This type of arrays are one dimensional arrays or simply


arrays.
2 dimensional arrays
int pfmarks[50][3]; // makes an array of 150(50x3) ints

This time, pfmarks can be considered as marks of 50 students


and in 3 categories mid, final and sessional.

String cityname[4][12];
// makes an array of 48(4x12) strings
Rectangular data
A two
dimensional
array is an array
of arrays, i.e.,
every row or
column is itself
an array.

Generally
processed with
nested loops.
Rectangular data
const int ROWS=3, COLS=2;
double rda[ROWS][COLS];

cout << "Enter six values for 3X2 matrix" << endl;
for(int r=0; r<ROWS; r++)
for(int c=0; c<COLS; c++)
cin >> rda[r][c];

for(int r=0; r<ROWS; r++)


{ for(int c=0; c<COLS; c++)
for(int c=0; c<COLS; c++) {
cout << rda[r][c] << " "; for(int r=0; r<ROWS; r++)
cout << endl; cout << rda[r][c] << " ";
cout << endl;
}
}
Multidimensional data
Triangular data
int ia[S]; [i]th element is at ith location in linear array

float fa[S1][S2]; [i1][i2]th element is at (i1*S2+i2)th location in


linear array

bool ba[S1][S2][S3]; [i1][i2][i3]th element is at


(i1*S2*S3+i2*S3+i3)th location in linear array

Course ca[S1][S2][S3][S4]; [i1][i2][i3][i4]th element is at


(i1*S2*S3*S4+i2*S3*S4+i3*S4+i4)th location in linear array

Generalize it
What about triangular data

You might also like