2d Array

You might also like

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

Computer Programming Techniques

CSE 295
2D Array

Rifat Rahman
https://sites.google.com/view/rifatrahman007
2D Array

Array Declaration:
Data_type var_name[row_size][column_size];

Column
0 1 2 3 4…

0
1
Row 2
3
4
.
. 2
2D Array(cont.)

int var[5][4];
var[0][0] = 7;
var[2][1] = 3;
0 1 2 3

0 7
1
2 3
3
4

3
2D Array(cont.)
#include<stdio.h>
void main()
{ int row = 3, col = 3, mat[10][10], i, j;
0 1 2 3……

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


for(j=0 ; j<col ; j++) 1
scanf(“ %d”, &mat[i][j]); 2
3
for(i=0 ; i<row ; i++) 4
{ for(j=0 ; j<col ; j++) .
printf(“ %d”, mat[i][j]); .
.
printf(“\n”); .
} .
4
}
2D Array(cont.)
#include<stdio.h>
void main()
{ int row, col, mat[10][10], i, j;
0 1 2 3……
scanf(“ %d %d”, &row, &col);
for(i=0 ; i<row ; i++) 0
for(j=0 ; j<col ; j++) 1
scanf(“ %d”, &mat[i][j]); 2
3
for(i=0 ; i<row ; i++) 4
{ for(j=0 ; j<col ; j++) .
printf(“ %d”, mat[i][j]); .
.
printf(“\n”); .
} .
5
}
2D Array - Matrix Addition

0 1 2 3 0 1 2 3
0 1 10 15 17 0 9 5 5 3
1 12 18 8 11 1 8 2 2 4
2
3
7
9
2
5
24
29
21
24
+ 2 3 3 1 4
3 6 5 1 1
4 12 10 32 11 3 5 3 4
4
Matrix 1 0 1 2 3
Matrix 2
0 10 15 20 20
1 20 20 10 15
2 10 5 25 25
3 15 10 30 25
4 15 15 35 15 Sum 6
2D Array - Matrix Addition(cont.)
#include<stdio.h>
void main()
{ int row, col, mat1[10][10], mat2[10][10], sum[10][10], i, j;
scanf(“ %d %d”, &row, &col);

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


for(j=0 ; j<col ; j++) 1st Matrix Input
scanf(“ %d”, &mat1[i][j]);

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


for(j=0 ; j<col ; j++) 2nd Matrix Input
scanf(“ %d”, &mat2[i][j]);

7
Continue….
2D Array - Matrix Addition(cont.)
for(i=0 ; i<row ; i++)
{ Matrix 1 and Matrix 2
for(j=0 ; j<col ; j++) Addition
sum[i][j] = mat1[i][j] + mat2[i][j];
}

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


{
for(j=0 ; j<col ; j++) Matrix Output
printf(“%d ”, sum[i][j]);
printf(“\n”);
}
}
8
2D Array as Parameter
#include<stdio.h> void main()
{ int i,j, row, col, mat[10][10];
void func(int x[][10], int r, int c) scanf(“%d %d”, &row, &col);
{ int i,j;
for(i=0 ; i<r ; i++) for(i=0 ; i<row ; i++)
{ for(j=0 ; j<col ; j++)
for(j=0 ; j<c ; j++) scanf(“%d ”, &mat[i][j]);
printf(“%d ”, x[i][j]);
printf(“\n”); func(mat, row, col);
} }
}

9
Exercise 1

Write a program that takes a 3x3 matrix as input and


calculate the determinant of the matrix.

1 2 3
4 5 6
7 8 9

Determinant = 0

10
Exercise 2
Write a program that takes an nxn matrix as input and
print the diagonal value of the matrix.

0 1 2 3
0 4 3 2 1

1 1 2 3 4

2 4 5 6 7

3 7 8 9 0

4 2 6 0

11
Exercise 3
Write a program that takes an mxn matrix as input and
print the sum of each column of the matrix.

0 1 2 3 4
0 4 3 2 1 1

1 1 2 3 4 2
2 4 5 6 7 1
3 7 8 9 0 2

16 18 20 12 6

12
Exercise 3 - Soln

13
Exercise 4
Write a program that takes an nxn matrix as input,
transpose the matrix and print the matrix.

1 2 3 4 1 9 5 13
9 10 11 12 2 10 6 14
5 6 7 8 3 11 7 15
13 14 15 16 4 12 8 16

14
2D character array
#include<stdio.h>
void main()
{ int i;
char name[10][100]; 0 1 2 3 4. . .

0 A b c \0
for(i=0 ; i<10 ; i++) D e \0
1
gets(name[i]); M n o \0
2
3 P q r s \0
for(i=0 ; i<10 ; i++) .
.
puts(name[i]);
}
15
Example
Write a program that takes an integer number n as input and
then takes n words as input. After that it takes an integer
number m as input and then takes m words from user. For each
of m words you have to find whether it is in the list of previously
taken n words. You can assume n < 20 and maximum length of a
word can be 20. All words will be in lowercase.
Sample Input: Sample Output:
4 No
aesthetic Yes
curious
amorous
fascination
2
rose
amorous
16

You might also like