1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
|
#include<string.h>
#include<math.h>
#include<conio.h>
#include<iostream>
#include<stdlib.h>
#include <stdio.h>
using namespace std;
void reduction(float a[15][15], int SIZE, int J, int E, float aa[15][15], int S, int R, int C)
{
int i, j;
float factor;
for (i = 0; i< SIZE; ++i);
{
if (i != J)
{
factor = (a[i][E]) / (a[E][E]);
for (j = 0; j < SIZE; ++j)
{
a[i][j] = a[i][j] - a[J][j] * factor;
aa[i][j] = aa[i][j] - aa[J][j] * factor;
}
}
}
}
int main()
{
char ans;
do {
system("cls");
static float mat[15][15] = { 0 };
static float A[15][15] = { 1 };
static float X[15] = { 0 };
float u, FACTOR;
int i, j, R, C, Rows, Column, g, h, o;
printf("\n\tEnter Number Rows:\n");
printf("\t");
scanf_s("%d", &R);
printf("\n\tEnter Number Columns:\n");
printf("\t");
scanf_s("%d", &C);
printf("\n\tEnter the elements of the matrix:\n");
for (i = 0; i< R; ++i)
{
X[i] = 0;
for (j = 0; j <= C; ++j)
{
mat[i][j] = 0;
}
}
for (Column = 0; Column < C; ++Column)
{
}
printf(" \n");
for (i = 0; i< R; ++i)
{
for (j = 0; j < C; ++j)
{
std::cin >> mat[i][j];
}
}
//clears the value of the result matrix 'a' to 1 in diagonal.
// 1 0 0 0
// 0 1 0 0
// 0 0 1 0
// 0 0 0 1
for (i = 0; i< R; ++i)
{
for (j = 0; j < C; ++j)
{
if (j == i)
{
A[i][j] = 1;
}
else
{
A[i][j] = 0;
}
}
}
for (i = 0; i< C; ++i)
{
if (mat[i][i] != 0)
{
reduction(mat, C, i, i, A, C, i, i);
}
else
{
for (i = (C - 1); i >= 0; --i)
{
if (mat[i][i] != 0)
{
reduction(mat, C, i, i, A, C, i, i);
}
}
}
}
printf("\nInverse Matrix");
for (i = 0; i< C; ++i)
{
for (j = 0; j < C; ++j)
{
if ((i == j) && ((mat[i][j]) != 1) && ((mat[i][j]) != 0))
{
for (h = 0; h < C; h++)
{
A[i][h] = A[i][h] * (1 / (mat[i][j]));
}
}
}
}
printf("\n");
for (i = 0; i< C; i++)
{
printf("\n");
for (j = 0; j < C; j++)
{
printf("%8.2f", A[i][j]);
}
}
printf("\n");
for (i = 0; i< C; ++i)
{
printf("\n");
for (j = 0; j < C; j++)
{
X[i] = X[i] + (A[i][j] * mat[j][C]);
}
printf("x%d = %8.2f ", i + 1, X[i]);
}
printf("\n \nTRY AGAIN? Y/N :");
ans = _getche();
} while ((ans == 'Y') || (ans == 'y'));
return 0;
}
|