Beruflich Dokumente
Kultur Dokumente
6
Max in 2D
• Find the maximum of (positive)
int matrix[3][4]
7
Find a value in 2D
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
9
solution
int matrix1[3][4],
matrix2[3][4],
sum[3][4];
// initialize matrix1 and matrix2
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
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