-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmetnum.cpp
83 lines (68 loc) · 2.21 KB
/
metnum.cpp
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
#include <iostream>
#include <vector>
using namespace std;
// Fungsi untuk mencetak matriks
void printMatrix(const vector<vector<double>>& matrix) {
int rows = matrix.size();
int cols = matrix[0].size();
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
cout << matrix[i][j] << " ";
}
cout << endl;
}
}
// Fungsi untuk melakukan eliminasi Gauss-Jordan
void gaussJordanElimination(vector<vector<double>>& matrix) {
int rows = matrix.size();
int cols = matrix[0].size();
for (int i = 0; i < rows; i++) {
// Mencari baris dengan elemen terbesar pada kolom i
int maxRow = i;
for (int j = i + 1; j < rows; j++) {
if (matrix[j][i] > matrix[maxRow][i]) {
maxRow = j;
}
}
// Menukar baris dengan elemen terbesar dengan baris i
swap(matrix[i], matrix[maxRow]);
// Mengubah elemen diagonal menjadi 1
double pivot = matrix[i][i];
for (int j = i; j < cols; j++) {
matrix[i][j] /= pivot;
}
// Mengeliminasi elemen di bawah dan di atas pivot
for (int j = 0; j < rows; j++) {
if (j != i) {
double factor = matrix[j][i];
for (int k = i; k < cols; k++) {
matrix[j][k] -= factor * matrix[i][k];
}
}
}
}
}
int main() {
int rows, cols;
cout << 0.6762 + 0.2266 * 9.2 - 0.0064 * 9.2 * 9.2 << endl;
cout << 0.6762 + 0.2266 * 8 - 0.0064 * 8 * 8 << endl;
cout << 0.6762 + 0.2266 * 9 - 0.0064 * 9 * 9 << endl;
cout << 0.6762 + 0.2266 * 9.5 - 0.0064 * 9.5 * 9.5 << endl;
cout << "Masukkan jumlah baris: ";
cin >> rows;
cout << "Masukkan jumlah kolom: ";
cin >> cols;
vector<vector<double>> matrix(rows, vector<double>(cols));
cout << "Masukkan elemen matriks:" << endl;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
cin >> matrix[i][j];
}
}
cout << "Matriks awal:" << endl;
printMatrix(matrix);
gaussJordanElimination(matrix);
cout << "Matriks setelah eliminasi Gauss-Jordan:" << endl;
printMatrix(matrix);
return 0;
}