Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- int main()
- {
- int m, n;
- cout << "Введите размеры матрицы m, n: ";
- cin >> m >> n;
- double** A = new double*[m];
- for(int i = 0; i < m; i++)
- {
- A[i] = new double[n];
- }
- // Считываем
- for(int i = 0; i < m; i++)
- {
- for(int j = 0; j < n; j++)
- {
- cin >> A[i][j];
- }
- }
- // Метод Гаусса, но без вектора правой части (просто матрица)
- int rank = 0;
- int row = 0;
- for(int col = 0; col < n && row < m; col++)
- {
- // Находим непустую строку
- int pivotRow = row;
- while(pivotRow < m && A[pivotRow][col] == 0.0)
- {
- pivotRow++;
- }
- if(pivotRow == m) continue; // нет ведущего элемента в этом столбце
- // Меняем строки
- if(pivotRow != row)
- {
- for(int k = 0; k < n; k++)
- {
- double temp = A[row][k];
- A[row][k] = A[pivotRow][k];
- A[pivotRow][k] = temp;
- }
- }
- // Нормируем
- double pivot = A[row][col];
- for(int c = col; c < n; c++)
- {
- A[row][c] /= pivot;
- }
- // Зануляем
- for(int r = row+1; r < m; r++)
- {
- double factor = A[r][col];
- for(int c = col; c < n; c++)
- {
- A[r][c] -= factor * A[row][c];
- }
- }
- rank++;
- row++;
- }
- cout << "Ранг = " << rank << endl;
- // Освобождаем
- for(int i = 0; i < m; i++)
- {
- delete[] A[i];
- }
- delete[] A;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement