Lec13 PDF

Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

Two-dimensional Arrays

• A two-dimensional array consists of both rows and


columns of elements. It is essentially a matrix.

• To declare a two-dimensional array, we merely use two


sets of square brackets.
• The first contains the number of rows
• The second contains the number of columns

Lecture 13 //Creates a 2D array with 3 rows and 4 columns


int values[3][4];
Arrays
CSE115: Programming Language I

Indices in 2D Arrays Using 2D Arrays


• Just like 1D arrays, once you have specified the index, you
• Assume that the two dimensional array called nums are just working with a single variable of the given data
is declared and looks like the following: type.
• Assignments and usage is still the same:
nums Col 0 Col 1 Col 2 Col 3
Row 0 8 16 9 52 sumRow0 = nums[0][0] + nums[0][1] + nums[0][2] +
nums[0][3];
Row 1 3 15 27 6
Row 2 14 25 2 10 //assigns 72 to cell at row 2, column 3
nums[2][3] = 72;

• To access the cell containing 6, we use nums[1][3],


that is, row 1, column 3 of the array nums.
Initializing 2D Arrays More on 2D Arrays
• You can use additional braces to indicate when rows start and end, • Initialization of 2D arrays is done in row order.
but you don’t have to do that.
int nums[3][4] = {{8,16,9,52},
{3,15,27,6}, • 2D arrays work well with (for) loops like 1D arrays.
{14,25,2,10} However, to access all elements, typically you will need
};
nested loops for 2D arrays. Can you see why?
• Or
int nums[3][4] = {8,16,9,52,
3,15,27,6,
14,25,2,10};
• Or (correct, but not as clear as the first two):
int nums[3][4] = {8,16,9,52,3,15,27,6,14,25,2,10};

Example (Display Array Values) Example (Array Values as User-input)


int main() int main()
{ {
int A[100][100], i, j, rows, columns;
int A[3][4] = { {11, 12, 13, 14}, printf("Number of rows: ");
{21, 22, 23, 24}, scanf("%d",&rows);
{31, 32, 33, 34} }; printf("Number of columns: ");
int i, j; scanf("%d",&columns);
for(i=0;i<rows;i++)
{
printf("Values in array A:\n"); for(j=0;j<columns;j++)
for(i=0;i<3;i++) {
{ printf("A[%d][%d]: ",i, j);
scanf("%d",&A[i][j]);
for(j=0;j<4;j++) }
{ }
printf("%10d ",A[i][j]); printf("Values in array A:\n");
} for(i=0;i<rows;i++)
{
printf("\n"); for(j=0;j<columns;j++)
} {
return 0; printf("%10d ",A[i][j]);
} }
printf("\n");
}
return 0;
}
Example (Add Two Matrices) 1/3 Example (Add Two Matrices) 2/3
int main() for(i=0;i<rowsA;i++)
{ {
int A[100][100], B[100][100], C[100][100]; for(j=0;j<columnsA;j++)
int i, j, rowsA, columnsA, rowsB, columnsB; {
printf("Number of rows in A: ");
printf("A[%d][%d]: ",i, j);
scanf("%d",&rowsA); scanf("%d",&A[i][j]);
printf("Number of columns in A: "); }
scanf("%d",&columnsA); }
printf("Number of rows in B: "); printf("\n");
scanf("%d",&rowsB); for(i=0;i<rowsB;i++)
printf("Number of columns in B: "); {
scanf("%d",&columnsB); for(j=0;j<columnsB;j++)
{
if(rowsA != rowsB || columnsA != columnsB) printf("B[%d][%d]: ",i, j);
{ scanf("%d",&B[i][j]);
printf("Invalid matrix dimensions\n"); }
return 0; }
}

Example (Add Two Matrices) 3/3 Example (Multiply Two Matrices)


for(i=0;i<rowsA;i++) • Multiply rows with columns.
{
for(j=0;j<columnsA;j++) • You can only multiply if the number of columns in
{
C[i][j] = A[i][j] + B[i][j]; the 1st matrix is equal to the number of rows in
}
} the 2nd matrix.
printf("Result:\n");  −8 2 
for(i=0;i<rowsA;i++)
{
−3 2 5   
for(j=0;j<columnsA;j++)  7 1 0 ×  1 5 
  
 0 −3
{
printf("%10d ",C[i][j]);
} They must match.
printf("\n");
}
return 0;
} Dimensions: 2x3 3x2
The dimensions of your answer.
Example (Multiply Two Matrices) Example (Multiply Two Matrices)
A B A B
2 −1 3 −9 2 2 −1 3 −9 2
× ×
3 4 5 7 −6 3 4 5 7 −6
2(3) + -1(5) 2(-9) + -1(7) 2(2) + -1(-6)
3(3) + 4(5) 3(-9) + 4(7) 3(2) + 4(-6)

1 −25 10 
29 1 −18

C

Example (Multiply Two Matrices) Example (Multiply Two Matrices)


A B A Col 0 ACol 1 B BCol 0 Col 1 Col 2
2 −1 3 −9 2 Row 0 22 −1
-1 3
Row 0−9 3 2 -9 2
× Row 1 33 44
×
3 4 5 7 −6 Row 1 7
5 5 −6 7 -6

2(3) + -1(5) 2(-9) + -1(7) 2(2) + -1(-6) 2(3) + -1(5) 2(-9) + -1(7) 2(2) + -1(-6)
3(3) + 4(5) 3(-9) + 4(7) 3(2) + 4(-6) 3(3) + 4(5) 3(-9) + 4(7) 3(2) + 4(-6)

sum = 0;
1 −25 10  for(k=0;k<columnsA;k++) 1 −25 10 
29 1 −18
sum += A[0][k]*B[k][0];
29 1 −18
 C[0][0] = sum; 
C C
Example (Multiply Two Matrices) Example (Multiply Two Matrices)
A B A Col 0 ACol 1 B BCol 0 Col 1 Col 2
2 −1 3 −9 2 Row 0 22 −1
-1 3
Row 0−9 3 2 -9 2
× Row 1 33 44
× Row 1 5
3 4 5 7 −6 5 7 −6 7 -6

2(3) + -1(5) 2(-9) + -1(7) 2(2) + -1(-6) 2(3) + -1(5) 2(-9) + -1(7) 2(2) + -1(-6)
3(3) + 4(5) 3(-9) + 4(7) 3(2) + 4(-6) 3(3) + 4(5) 3(-9) + 4(7) 3(2) + 4(-6)

1 −25 10  for(j=0;j<columnsB;j++)
1 −25 10 
29 1 −18
{
29 1 −18
 sum = 0;
for(k=0;k<columnsA;k++) 
C sum += A[0][k]*B[k][j];
C[0][j] = sum;
C
}

Example (Multiply Two Matrices) Example (Multiply Two Matrices)


A B A Col 0 ACol 1 B BCol 0 Col 1 Col 2
2 −1 3 −9 2 Row 0 22 −1
-1 3
Row 0−9 3 2 -9 2
× Row 1 33 44
×
3 4 5 7 −6 Row 1 7
5 5 −6 7 -6

2(3) + -1(5) 2(-9) + -1(7) 2(2) + -1(-6) 2(3) + -1(5) 2(-9) + -1(7) 2(2) + -1(-6)
3(3) + 4(5) 3(-9) + 4(7) 3(2) + 4(-6) 3(3) + 4(5) 3(-9) + 4(7) 3(2) + 4(-6)

for(i=0;i<rowsA;i++)
1 −25 10  {
for(j=0;j<columnsB;j++)
1 −25 10 
29 1 −18 { 29 1 −18
 sum = 0; 
for(k=0;k<columnsA;k++)
C sum += A[i][k]*B[k][j];
C
C[i][j] = sum;
}
}
Example (Multiply Two Matrices) 1/3 Example (Multiply Two Matrices) 2/3
int main() for(i=0;i<rowsA;i++)
{ {
int A[100][100], B[100][100], C[100][100]; for(j=0;j<columnsA;j++)
int i, j, k, rowsA, columnsA, rowsB, columnsB;
int sum; {
printf("A[%d][%d]: ",i, j);
printf("Number of rows in A: "); scanf("%d",&A[i][j]);
scanf("%d",&rowsA); }
printf("Number of columns in A: "); }
scanf("%d",&columnsA); printf("\n");
printf("Number of rows in B: ");
scanf("%d",&rowsB); for(i=0;i<rowsB;i++)
printf("Number of columns in B: "); {
scanf("%d",&columnsB); for(j=0;j<columnsB;j++)
if(columnsA != rowsB)
{
{ printf("B[%d][%d]: ",i, j);
printf("Invalid matrix dimensions\n"); scanf("%d",&B[i][j]);
return 0; }
} }

Example (Multiply Two Matrices) 3/3


for(i=0;i<rowsA;i++)
{
for(j=0;j<columnsB;j++)
{
sum = 0;
for(k=0;k<columnsA;k++)
sum += A[i][k]*B[k][j];
C[i][j] = sum;
}
}
printf("Result:\n");
for(i=0;i<rowsA;i++)
{
for(j=0;j<columnsB;j++)
{
printf("%10d ",C[i][j]);
}
printf("\n");
}
return 0;
}

You might also like