0% found this document useful (0 votes)
16 views

Multi Dimensional Arrays

The document discusses multi-dimensional arrays in C language. It explains that C allows arrays with any number of dimensions to be defined. It provides an example of a 3x4 two-dimensional array and demonstrates how elements within the array can be accessed and referred to using subscript notation like M[i][j]. It also includes examples of common operations that can be performed on two-dimensional arrays like calculating the sum of elements, adding two arrays, finding the minimum value, and multiplying two arrays.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

Multi Dimensional Arrays

The document discusses multi-dimensional arrays in C language. It explains that C allows arrays with any number of dimensions to be defined. It provides an example of a 3x4 two-dimensional array and demonstrates how elements within the array can be accessed and referred to using subscript notation like M[i][j]. It also includes examples of common operations that can be performed on two-dimensional arrays like calculating the sum of elements, adding two arrays, finding the minimum value, and multiplying two arrays.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Multi-dimensional arrays

C language allows arrays of any dimension to be


defined.
A two-dimensional array arises in the case of a
matrix. Consider the 3 x 4 matrix
10

-3

17

32

20

If we call the preceding matrix as M, the notation


Mi, j refers to the element in the ith row and jth
column, where 1 <= i <= 3, and 1 <= j <= 4.
The notation M3, 2 refers to the value 20.
In C, we use almost similar notation to refer to
the elements of a 2-dimensional array.

1-1

Multi-dimensional arrays
In C, we use almost similar notation to refer to
the elements of a 2-dimensional array.

10

-3 17

32 20

In C, the notation equivalent to Mi, j is M[i][j].


Accordingly, M[2][1]refers to the value at row 2
and column 1, that is 20.
int M[3][4]; declares M to be a 2dimensional array consisting of 3 rows and 4
columns, for a total of 12 elements.

1-2

Multi-dimensional arrays
In C, we use almost similar notation to refer to
the elements of a 2-dimensional array.

10

-3 17

32 20

In C, the notation equivalent to Mi, j is M[i][j].


Accordingly, M[2][1]refers to the value at row 2
and column 1, that is 20.
int M[3][4]; declares M to be a 2dimensional array consisting of 3 rows and 4
columns, for a total of 12 elements.

1-3

2-dimensional arrays
#include <stdio.h>
int main (void) {
int sum, i, j;
int M [3][4] = {
{10, 5, -3, 17},
{ 9, 0, 0, 8},
{32, 20, 1, 0}
};
sum = 0;
for (i = 0; i < 3; ++i)
for (j = 0; j < 4; ++j)
sum = sum + M [i][j] ;
printf(The sum is = %i\n, sum);
return 0;
}
______________________________________
The sum is = 99
1-4

Adding two 2-dimensional arrays


#include <stdio.h>
int main (void) {
int i, j;
int M [3][4] = { {10, 5, -3, 17},
{ 9, 0, 0, 8},
{32, 20, 1, 0}
};
int N [3][4] = { {10, 5, -3, 17},
{ 9, 0, 0, 8},
{32, 20, 1, 0}
};
int SUM [3][4];
for (i = 0;
for (j =
SUM

i < 3; ++i)
0; j < 4; ++j)
[i][j] = M [i][j] +
N [i][j];

return 0;
}

1-5

Finding minimum value in a given 2dimensional array


#include <stdio.h>
int main (void) {
int minValue, i, j;
int M [3][4] = {
{10, 5, -3, 17},
{ 9, 0, 0, 8},
{32, 20, 1, 0},
};
minValue = 2147000000;

// MAXINT

for (i = 0; i < 3; ++i)


for (j = 0; j < 4; ++j)
if (M [i][j] < minValue)
minValue = M [i][j];
printf(Minimum value is = %i\n,
minValue);
return 0;
}
1-6

Multiplying two 2-dimensional arrays

int A[2][3] = {
{1, 3, 4},
{2, 0, 1}
};

int B[3][4] = {
{1, 2, 3, 1},
{2, 2, 2, 2},
{3, 2, 1, 4}
};

For example:
C [0,0] = (1 x 1) + (3 x 2) + (4 x 3) = 19
and
C [1,2] = (2 x 3) + (0 x 2) + (1 x 1) = 7

1-7

Multiplying two 2-dimensional arrays


#include <stdio.h>
int main (void) {
int i, j, k = 0;
int A[2][3] = { {1, 3, 4},
{2, 0, 1}
};
int B[3][4] =
{2,
{3,
};

{
2,
2,

{1, 2, 3, 1},
2, 2},
1, 4}

int C[2][4] = { {0, 0, 0, 0},


{0, 0, 0, 0}
};

for (i = 0; i < 2; i++)


for (j = 0; j < 4; j++)
for(k = 0; k < 3; k++)
C[i][j] += A[i][k] *
B[k][j];
return 0;
}
1-8

You might also like