Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <iomanip>
- #include <cmath>
- using namespace std;
- void print(float** MTRX_ORIG, int N);
- float** create(int N);
- float* alg_progon(float** MTRX, int N);
- int main()
- {
- int n;
- float** mtrx;
- cout << "Enter the number of equations n = ";
- cin >> n;
- mtrx = create(n);
- cout << endl << "Your matrix:" << endl;
- print(mtrx, n);
- float* answer = new float[n];
- answer = alg_progon(mtrx, n);
- cout << "Answer:\n";
- for (int i = 0; i < n; i++)
- {
- cout << "x" << i + 1 << " = " << round(answer[i] * 100) / 100 << endl;
- }
- return 0;
- }
- void print(float** MTRX_ORIG, int N)
- {
- for (int i = 0; i < N; i++)
- {
- for (int j = 0; j <= N; j++)
- {
- if (j < N)
- cout << setw(10) << round(MTRX_ORIG[i][j] * 1000) / 1000;
- else
- cout << setw(10) << "|" << round(MTRX_ORIG[i][j] * 1000) / 1000;
- }
- cout << endl;
- }
- cout << endl;
- }
- float** create(int N)
- {
- float** MTRX_ORIG = new float* [N];
- for (int i = 0; i < N; i++)
- {
- MTRX_ORIG[i] = new float[N + 1];
- }
- cout << endl << "Enter the element matrix:" << endl;
- for (int i = 0; i < N; i++)
- for (int j = 0; j <= N; j++)
- {
- cin >> MTRX_ORIG[i][j];
- }
- return MTRX_ORIG;
- }
- float* alg_progon(float** MTRX, int N)
- {
- float* answer = new float[N];
- float* bet = new float[N];
- float* alf = new float[N];
- float y;
- for (int i = 0; i < N; i++)
- {
- if (i == 0)
- {
- y = MTRX[i][i];
- alf[i] = -(MTRX[i][i + 1]) / y;
- bet[i] = MTRX[i][N] / y;
- }
- else
- {
- y = MTRX[i][i] + MTRX[i][i - 1] * alf[i - 1];
- alf[i] = (-MTRX[i][i + 1]) / y;
- bet[i] = (MTRX[i][N] - MTRX[i][i - 1] * bet[i - 1]) / y;
- }
- cout << "y" << i + 1 << " = " << round(y*100)/100 << endl;
- cout << "alf" << i + 1 << " = " << round(alf[i] * 100) / 100 << endl;
- cout << "bet" << i + 1 << " = " << round(bet[i] * 100) / 100 << endl;
- cout << endl;
- }
- for (int i = N - 1; i >= 0; i--)
- {
- if (i == N - 1)
- answer[i] = bet[i];
- else answer[i] = alf[i] * answer[i + 1] + bet[i];
- }
- return answer;
- }
Add Comment
Please, Sign In to add comment