Comp4
Basic Matrix Operations¶
We will see how to use arrays for 1D, 2D matrices and then doing matrix operations.
%%writefile mat_unit.c
#include<stdio.h>
void main()
{
// Write a unit matrix of given dimension
int dim;
printf("Enter the size of the matrix\n");
scanf("%d",&dim);
int a[dim][dim],i,j;
for(i=0;i<dim;i++) {
for(j=0;j<dim;j++) {
if(i==j) a[i][j]=1;
else a[i][j]=0;
}
}
for(i=0;i<dim;i++) {
for(j=0;j<dim;j++) {
printf("%d\t",a[i][j]);
}
printf("\n");
}
}
Writing mat_unit.c
!gcc mat_unit.c
!./a.out
Enter the size of the matrix 2 1 0 0 1
%%writefile mat_addition.c
#include<stdio.h>
int main() {
int a[10][10], b[10][10], s[10][10], i, j,r,c;
int sum = 0;
label: printf("Enter the Number of Rows ");
scanf("%d",&r);
printf("Enter the Number of columns ");
scanf("%d",&c);
printf("\nEnter First Matrix : ");
for (i = 0; i < r; i++) {
for (j = 0; j < c ; j++) {
scanf("%d", &a[i][j]);
}
}
printf("\nEnter Second Matrix:");
for (i = 0; i<r; i++) {
for (j = 0; j<c; j++) {
scanf("%d", &b[i][j]);
}
}
printf("The First Matrix is: \n");
for (i=0; i<r; i++) {
for (j= 0; j<c; j++) {
printf("%d ", a[i][j]);
}
printf("\n");
}
printf("The Second Matrix is : \n");
for (i = 0; i<r; i++) {
for (j = 0; j<c; j++) {
printf(" %d ", b[i][j]);
}
printf("\n");
}
//Addition
for (i=0; i<r; i++) {
for (j=0; j<c; j++) s[i][j]= a[i][j]+b[i][j];
}
printf("\nAdition Of Two Matrices :\n");
for (i=0;i<r;i++) {
for (j= 0;j<c;j++) {
printf("%d \t",s[i][j]);
}
printf("\n");
}
return (0);
}
Overwriting mat_addition.c
!gcc mat_addition.c
!./a.out
Enter the Number of Rows 2 Enter the Number of columns 2 Enter First Matrix : 1 2 3 4 Enter Second Matrix:4 5 6 7 The First Matrix is: 1 2 3 4 The Second Matrix is : 4 5 6 7 Adition Of Two Matrices : 5 7 9 11
Passing arrays to functions¶
In the above example of matrix addition, if we want to avoid writing codes to print matrices several times, we may put them inside functions. But C copies in its arguments. For arrays, C copies in the address of the array. In C an array without using a pointer can not be passed. Arrays are always passed by pointer. The array label itself decays into a pointer to the first byte(element) automatically. Passing an array to a function allows the function to directly access and modify the original array.
So we will make use of pointer in the program. Stil, we have a limit of size of the matrix. We take sufficiently large matrix in the code for any future requirements of execution. Defining the size of a matrix during execution is only possible using advanced memory management, dynamic memory allocation concepts.
Here is a good web link for clear understanding.
%%writefile mat_mul.c
#include<stdio.h>
// Function to read a matrix using array notation
void readMatrix(int matrix[][10], int rows, int cols) {
printf("Enter the elements of the matrix:\n");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
printf("Enter element at [%d][%d]: ", i, j);
scanf("%d", &matrix[i][j]);
}
}
}
// Function to print the matrix (for verification)
void printMatrix(int matrix[][10], int rows, int cols) {
printf("\nThe matrix is:\n");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
printf("%d\t", matrix[i][j]);
}
printf("\n");
}
}
// Function for Matrix Multiplication
int multMatrix (int m1[][10], int r1, int c1, int m2[][10], int r2, int c2, int m3[][10]) {
int sum;
for (int i=0; i<=r1; i++) {
for (int j=0; j<=c2; j++) {
sum=0;
for (int k=0; k<c1; k++) sum += m1[i][k]*m2[k][j];
m3[i][j] = sum;
}
}
}
int main()
{
int a[10][10], b[10][10], c[10][10], r1,r2,c1,c2;
label: printf("Enter the Number of Rows of the First matrix:");
scanf("%d",&r1);
printf("Enter the Number of columns of the First matrix:");
scanf("%d",&c1);
// Call the function to read the matrix
readMatrix(a, r1, c1);
printf("Enter the Number of Rows of the second matrix:");
scanf("%d",&r2);
printf("Enter the Number of columns of the Second matrix:");
scanf("%d",&c2);
// Call the function to read the matrix
readMatrix(b, r2, c2);
if (c1!=r2) {
printf("Matrix Multiplication not possible \n Enter correct dimension ");
goto label;
}
printf("The First Matrix is: \n");
printMatrix(a, r1, c1);
printf("The Second Matrix is : \n");
printMatrix(b, r2, c2);
printf("\n Multiplication Of Two Matrices : \n");
multMatrix(a, r1, c1, b, r2, c2, c);
printMatrix(c, r1, c2);
return (0);
}
Overwriting mat_mul.c
!gcc mat_mul.c
!./a.out
Enter the Number of Rows of the First matrix:2 Enter the Number of columns of the First matrix:2 Enter the elements of the matrix: Enter element at [0][0]: 1 Enter element at [0][1]: 2 Enter element at [1][0]: 3 Enter element at [1][1]: 4 Enter the Number of Rows of the second matrix:2 Enter the Number of columns of the Second matrix:2 Enter the elements of the matrix: Enter element at [0][0]: 5 Enter element at [0][1]: 6 Enter element at [1][0]: 7 Enter element at [1][1]: 8 The First Matrix is: The matrix is: 1 2 3 4 The Second Matrix is : The matrix is: 5 6 7 8 Multiplication Of Two Matrices : The matrix is: 19 22 43 50
Comments
Post a Comment