Ticker

6/recent/ticker-posts

CPNM LAB PROGRAM-12

 

12. Write a function which will invert a matrix.

FLOW CHART
Algorithm: 
Step – 1 : Start 
Step – 2 : Declare array a[10][10], variables i, j, n 
Step – 3 : Print enter order of matrix 
Step – 4 : Initialize i to zero 
Step – 5 : Check whether i < n if true go to next Step and execute from Step–9 otherwise increment i by 1 
Step – 6 : Initialize j to zero 
Step – 7 : Check whether j < n if true execute Step–8 and Step–9 otherwise increment i by 1and go to Step–5 
 Step – 8 : Read elements into an array 
Step – 9 : Increment j by 1 and go to Step–7 
Step – 10 : Call function inverse(a, n) 
Step – 11 : Declare variables I, j, k array b[10][10] 
Step – 12 : Initialize i to zero 
Step – 13 : Check whether i < n if true execute from Step–14 to Step–19 otherwise go to Step–20 Step – 14 : Initialize j to zero 
Step – 15 : Check whether j < n if true execute Step–16 and Step–17 otherwise go to Step–18 
Step – 16 : Calculate b[i][j] = 0.0 
Step – 17 : Increment j by 1 and go to Step–13 
Step – 18 : b[i][i] = 1.0 
Step – 19 : Increment i by 1 and go to Step–13 
Step – 20 : Initialize k =0 
Step – 21 : Check whether k < n if true execute from Step–22 to Step–31 otherwise go to Step–30 Step – 22 : Initialize i to zero 
Step – 23 : Check whether i < n if true executes from Step–24 to Step–31 otherwise go to Step–21 by increment k by 1 
Step – 24 : Check whether i == k if true go to Step–21 otherwise go to Step–25 
Step – 25 : Calculate r = a[i][k] / a[k][k] 
Step – 26 : Initialize j to zero 
Step – 27 : Check whether j is less than if true execute from Step–28 to Step–30 otherwise go to Step–23 by i + 1 
 Step – 28 : Calculate a[i][j] -=r * a[k][j] 
 Step – 29 : Calculate a[i][j] -=r * b[k][j] 
Step – 30 : Increment j by 1 and go to Step–27 
Step – 31 : Initialize i to zero 
Step – 32 : Check whether i < n if true execute from Step–33 to Step–36 otherwise go to Step–37 Step – 33 : Initialize j to zero 
Step – 34 : Check whether j < n if true execute Step–35 and Step–36 otherwise go to Step–32 by incrementing i by 1 
Step – 35 : Calculate b[i][j] = a[i][j] / a[i][i] 
Step – 36 : Increment j by 1 and go to Step–34 
 Step – 37 : Initialize i to zero 
Step – 38 : Check whether i < n if true execute from Step–39 to Step–42 otherwise return to main Step – 39 : Initialize j to zero 
Step – 40 : Check whether j < n if true execute Step–40 and Step–41 otherwise increment  
                   i by 1 and go to Step - 38 
Step – 41 : Print b[i][j] 
Step – 42 : Increment j by 1 and go to Step–39 
Step – 43 : Stop 

Program:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<math.h>
void inverse(float a[10][10],int n)
{
int i, j, k;
float b[10][10], r;
for(i = 0; i< n; i++)
{
for(j = 0; j < n; j++)
b[i][j] = 0.0;
b[i][i] = 1.0;
}
for(k = 0; k < n; k++)
for(i = 0; i < n; i++)
{
if(i == k)
continue;
r = a[i][k] / a[k][k];
for(j = 0; j < n; j++)
{
a[i][j] -=r * a[k][j];
b[i][j] -= r * b[k][j];
}
}
for(i = 0; i<n; i++)
for(j = 0; j<n; j++)
b[i][j] = b[i][j] / a[i][i];
printf("The inverse matrix is \n");
for(i = 0; i <n; i++)
{
for(j = 0; j < n; j++)
printf("%3f \t",b[i][j]);
printf("\n");
}
}
main()
{
int i, j, k, n;
float a[10][10];
clrscr();
printf("Enter the order of the matrix\n");
scanf("%d",&n);
printf("Enter the elements of matrix \n");
for(i = 0; i < n; i++)
for(j = 0; j < n; j++)
scanf("%f",&a[i][j]);
inverse(a,n);
getch();
return 0;
}

Output:
Run 1:
Enter the order of the matrix
2
Enter the elements of matrix
1 2 3 4
The inverse matrix is
-2.000000 1.000000
1.500000 -0.500000
Run 2:
Enter the order of the matrix
3
Enter the elements of matrix
1.0 2.0 0.0 2.0 3.0 0.5 1.0 1.0 1.0
The inverse matrix is
-5.000000 4.000000 -2.000000
3.000000 -2.000000 1.000000
2.000000 -2.000000 2.000000

Post a Comment

0 Comments