Sie sind auf Seite 1von 19

2D Arrays

Two Dimensional Arrays


 A two dimensional array has two subscripts/indexes.
 The first subscript refers to the row, and the second,
to the column.
• Syntax:
Data Type arrayName [ row size ][ column size ];
Where type can be any valid C data type
and arrayName will be a valid C identifier.
A two-dimensional array can be considered as a table
which will have ‘n’ number of rows and ‘n’ number of
columns.
• A two-dimensional array a, which contains
three rows and four columns can be shown as
follows −
Accessing Two-Dimensional Array
Elements
• An element in a two-dimensional array is
accessed by using the subscripts, i.e., row
index and column index of the array.
• For example −int val = a[2][3];
• Ie., 2nd row, 3rd column

ARRAYS
If we assign initial string values for the 2D array it will look
something like the following,
char Name[6][10] = {"Mr. Bean", "Mr. Bush", "Nicole",
"Kidman", "Arnold", "Jodie"};

 The array with 6 strings, each with maximum 9 characters long.


 If depicted in rows and columns as contiguous arrangement in
the memory.
Computations on 2D arrays

6
Max in 2D
• Find the maximum of (positive)
int matrix[3][4]

int max =0; 0 1 2 3


0 0 1 0 2
for (i=0; i<3; i++)
for (j=0; j<4; j++) 1 1 2 4 3
if (matrix[i][j] > max) 2 0 1 3 1
max = matrix[i][j];

7
Find a value in 2D

• Find the number of times x appears in int matrix[3][4]

int count = 0; 0 1 2 3
0 0 1 0 2
for (i=0; i<3; i++)
for (j=0; j<4; j++) 1 -1 2 4 3
if (matrix[i][j] == x) 2 0 -1 3 1
count = count + 1;

8
Matrix sum

• Compute the addition of two


matrices
0 1 2 3 0 1 2 3 0 1 2 3
0 0 1 0 2 0 3 -1 3 1 0 3 0 3 3
-1 2 4 3 + 1 1 4 2 0 = 1 0 6 6 3
1
2 0 -1 3 1 2 2 1 1 3 2 2 0 4 4

9
solution
int matrix1[3][4],
matrix2[3][4],
sum[3][4];
// initialize matrix1 and matrix2

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


for (j=0; j<4; j++)
sum[i][j]= matrix1[i][j]+matrix2[i][j];

10
Matrix Addition
Matrix Subtraction
• //Add preprocessor file
• #include<stdio.h>
• #include<conio.h>
• void main()
• {
• int i,j,a[10][10],b[10][10],c[10][10];
• // clear screen;
• clrscr();
• printf(“enter elements of first matrix: ”);
• for(i=0;i<3;i++)
• {
• for{j=0;j<3;j++)
• {
• scanf(“%d”,&a[i][j]);
• }
• }
• printf(“enter elements of second matrx:”);
• for(i=0;i<3;i++)
• {
• for(j=0;j<3;j++)
• {
• scanf(“%d”,&b[i][j]);
• }
• }
• for(i=0;i<3;i++)
• {
• for(i=0;i<3;i++)
• {
• c[i][j]=a[i][j]-b[i][j];
• }
• }
• printf(“\n subtraction of matrices is: \n”);
• for(i=0;i<3;i++)
• {
• for(j=o;j<3;j++)
• {
• // Print the Result
• printf(“%d\t”,c[i][j]);
• }
• }
• printf(“\n”);
• }
• getch();
• }
Matrix multiplication
double a[2][2], b[2][2], c[2][2];
• Find c = a * b;
• a= 1 2
4 5

• b= 4 5
6 7

c[0][0] = a[0][0]*b[0][0] + a[0][1]*b[1][0]


c[0][1] = a[0][0]*b[0][1]+ a[0][1]*b[1][1]
c[1][0] = a[1][0]*b[0][0] + a[1][1]*b[1][0]
c[1][1] = a[1][0]*b[0][1] + a[1][1]*b[1][1]
16
// Multiplication

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


for(j=0; j < 3; j++) {
c[i][j] = 0;
for(k=0; k < 3; k++) {
c[i][j] = c[i][j] + a[i][k] * b[k][j];
}
}
}

18
Transpose
void transpose(int a[NROWS][NCOLS],
int b[NCOLS][NROWS])
{
a /* Declare Variables. */
1 5 3 int i, j;
4 2 6
/* Transfer values to the
transpose matrix. */
b for(i=0; i<NROWS; i++) {
1 4 for(j=0; j<NCOLS; j++) {
b[j][i] = a[i][j];
5 2 }
}
3 6 return;
}

19

Das könnte Ihnen auch gefallen