4-5. Two Dimensional Array-1
4-5. Two Dimensional Array-1
element [0][0].
Two-Dimensional Array
◼ Declaration of two-dimensional array:
int arr[3][3];
◼ Here, this example is a graphical or pictorial
representation of 2-D array.
0 1 2
Arr[0][0] Arr[0][1] Arr[0][2]
0
0 10 20 30
1 40 50 60
2 Dimensional Arrays
Initialization of two dimensional arrays
type array-name [row size] [col size ] ={list of values};
int table [2][3]={0,0,0,1,1,1};
→ initializes the elements of the first row to zero and the
second row to 1.
Initialization is always done row by row.
The above statement can be equivalently written as
int table [2][3]={{0,0,0},{1,1,1}};
OR in matrix form it can be written as
int table [2][3]= { {0,0,0},
{1,1,1} };
◼ Column-major implementation
Two-Dimensional Arrays:
Row-major Implementation
◼ Row-major implementation is a linearization
technique in which elements of array are read
from the keyboard row-wise that means the
complete first row is stored then the complete
second row is stored and so on.
◼ For example an array [3][3] is stored in the
memory as show bellow:
type array_name[s1][s2][s3]…..[sn];
Multi-Dimensional Array
Suppose 3-D array can be group of an array of arrays.
For example : int a[2][3][4];
Here 3-D array which is a collection of four 2-D arrays each
contain 2 rows and 3 column.
N-Dimensional Array
N-Dimensional Array
N-Dimensional Array
Multi-Dimensional Array
Multi-Dimensional Array
Multi-Dimensional Array
Multi-Dimensional Array
Multi-Dimensional Array
Exercise:
9/6/2022 38
Matrix Addition
for(i=0;i<n;i++)
printf("\t %d”,colsum[i]);
9/6/2022 CSE 1001 Department of CSE 41
Problems on
2 D A R R AY
for(i=0;i<n;i++)
for(j=0;j<n;j++)
if(i==j){
temp=arr[i][j];
arr[i][j]=arr[i][n-i-1];
arr[i][n-i-1]=temp;
int main(){ }
int i, j, temp, arr[4][4],n; printf("\nModified Matrix:\n“);
for(i=0;i<n;i++){
printf("\nEnter dimension: “); for(j=0;j<n;j++)
scanf(“%d”,&n); printf(" “);
Printf(“%d”,arr[i][j]);
printf(“\nEnter elements:\n"); printf("\n“);
for(i=0;i<n;i++) }
for(j=0;j<n;j++) return 0;
scanf(“%d”,&arr[i][j]); }
9/6/2022 CSE 1001 Department of CSE 45
4. Exchange the Rows and Columns of a ‘mxn’ matrix