0% found this document useful (0 votes)
15 views8 pages

Lab 3

The document contains code to write programs that: 1) create two 4x4 matrices, calculate their product matrix, and print it. 2) create a 5x5 matrix, calculate its transpose multiplied by the original, and check if it equals the identity matrix to determine if the original is orthogonal.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views8 pages

Lab 3

The document contains code to write programs that: 1) create two 4x4 matrices, calculate their product matrix, and print it. 2) create a 5x5 matrix, calculate its transpose multiplied by the original, and check if it equals the identity matrix to determine if the original is orthogonal.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

1. Write a program to create two matrices of size 4x4, calculate and print the product matrix.

CODE:

// write a program to create two matrices of size 4x4, calculate and


print the product matrix
#include <stdio.h>
#include <time.h>

void multiplication(int m, int n, int a[m][n], int b[m][n])


{
int result[m][n];
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
result[i][j] = 0;
}
}
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
for (int k = 0; k < n; k++)
{
result[i][j] += a[i][k] * b[k][j];
}
}
}
printf("product of two matrices A and B\n");
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
printf("%d ", result[i][j]);
}
printf("\n");
}
}

int main()
{
clock_t start=clock();
int m,n;
printf("Enter no of rows and columns of two matrices : ");
scanf("%d %d",&m,&n);
int mat1[m][n],mat2[m][n];
printf("Enter Matrix 1: \n");
for(int i=0;i<m;i++){
for(int j=0;j<m;j++){
scanf("%d",&mat1[i][j]);
}
}
printf("Enter Matrix 2: \n");
for(int i=0;i<m;i++){
for(int j=0;j<m;j++){
scanf("%d",&mat2[i][j]);
}
}

multiplication(m,n,mat1,mat2);

printf("\n Execution timme for this code is :


%f\n",(double)(clock()-start)/CLOCKS_PER_SEC);
return 0;
}
OUTPUT:
2. Write a program to create a matrix of size 5x5. Find whether it is orthogonal or not (i.e. AT*A
= I or not).
CODE:

// write a program to create a matrix of size 5x5. find whether it is


orthogonal or not.
#include <stdio.h>
#include <time.h>
void orthogonal(int m, int a[m][m])
{
int result[m][m];
for (int i = 0; i < m; i++)
{
for (int j = 0; j < m; j++)
{
result[i][j] = 0;
}
}
int b[m][m];
for (int i = 0; i < m; i++)
{
for (int j = 0; j < m; j++)
{
b[i][j] = a[j][i];
}
}
for (int i = 0; i < m; i++)
{
for (int j = 0; j < m; j++)
{
for (int k = 0; k < m; k++)
{
result[i][j] += a[i][k] * b[k][j];
}
}
}
printf("product of two matrices A and A' \n");
for (int i = 0; i < m; i++)
{
for (int j = 0; j < m; j++)
{
printf("%d ", result[i][j]);
}
printf("\n");
}
// declaring identity matrix
int identity[m][m];
for (int i = 0; i < m; i++)
{
for (int j = 0; j < m; j++)
{
if (i == j)
identity[i][j] = 1;
else
identity[i][j] = 0;
}
}

// comoparing A*A' with identity


for (int i = 0; i < m; i++)
{
for (int j = 0; j < m; j++)
{
if (result[i][j] != identity[i][j])
{
printf("matrix is not orthogonal");
return;
}
}
}
printf("matrix is orthogonal");
return;
}

int main()
{
clock_t start = clock();
int m, n;
printf("Enter no of rows and columns of the matrix : ");
scanf("%d %d", &m, &n);
int mat1[m][n];
printf("Enter Matrix : \n");
for (int i = 0; i < m; i++)
{
for (int j = 0; j < m; j++)
{
scanf("%d", &mat1[i][j]);
}
}

if (m != n)
{
printf("Matrix is not Orthogonal\n");
}
else
{
orthogonal(m, mat1);
}

printf("\n Execution timme for this code is : %f\n",


(double)(clock() - start) / CLOCKS_PER_SEC);
return 0;
}

OUTPUT:

You might also like