Lesson 9。

This commit is contained in:
unlockable
2022-11-11 23:49:31 +08:00
parent 777e33d692
commit b4c35a325c
5 changed files with 235 additions and 0 deletions

91
09/Exercise02.c Normal file
View File

@@ -0,0 +1,91 @@
#include <stdio.h>
#include <stdbool.h>
double matA[4][4] = {
{1.1161, 0.1254, 0.1397, 0.1490},
{0.1582, 1.1675, 0.1768, 0.1871},
{0.2368, 0.2471, 0.2568, 1.2671},
{0.1968, 0.2071, 1.2168 ,0.2271}
};
double matB[4] = {
1.5471, 1.6471, 1.8471, 1.7571
};
void rowManipulation(int originRow, double coefficent, int rowBeingAdded) {
int col = 0;
for (col = 0; col < 4; col++) {
matA[rowBeingAdded][col] += matA[originRow][col] * coefficent;
}
matB[rowBeingAdded] += matB[originRow] * coefficent;
}
void timesOfRow(int rowCount, double time) {
int col = 0;
for (col = 0; col < 4; col++) {
matA[rowCount][col] *= time;
}
matB[rowCount] *= time;
}
int findNoneZeroItem(int startRowIndex, int columnIndex) {
bool found = false;
int pos = startRowIndex;
for (pos = startRowIndex; pos < 4; pos++) {
if (matA[pos][columnIndex] != 0) {
return pos;
}
}
return -1;
}
void swapRow(int firstRowIndex, int secondRowIndex) {
double temp = 0;
int i = 0;
for (i = 0; i < 4; i++) {
temp = matA[firstRowIndex][i];
matA[firstRowIndex][i] = matA[secondRowIndex][i];
matA[secondRowIndex][i] = temp;
}
temp = matB[firstRowIndex];
matB[firstRowIndex] = matB[secondRowIndex];
matB[secondRowIndex] = temp;
}
void printOut() {
int i = 0, j = 0;
for (i = 0; i < 4; i++) {
for (j = 0; j < 4; j++) {
printf("%lf\t", matA[i][j]);
}
printf("%lf", matB[i]);
printf("\n");
}
}
int main() {
int x[4] = {0};
int column = 0, row = 0;
int tempRow = 0;
int noneZeroRow = 0;
for (column = 0; column < 4; column++) {
noneZeroRow = findNoneZeroItem(row, column);
if (noneZeroRow == -1) {
continue;
}
else {
swapRow(row, noneZeroRow);
timesOfRow(row, 1.0/matA[row][column]);
for (tempRow = 0; tempRow < 4; tempRow++) {
if (tempRow != row)
rowManipulation(row, -matA[tempRow][column], tempRow);
}
row++;
}
}
for (row = 0; row < 4; row++) {
printf("x(%d) = %lf\n", row+1, matB[row]);
}
return 0;
}