Ответ:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define N 10
void swap(double *row1, double *row2, int n) {
for(int i = 0; i < n; i++) {
double temp = row1[i];
row1[i] = row2[i];
row2[i] = temp;
}
double determinant(double matrix[N][N], int n) {
double det = 1;
if (fabs(matrix[i][i]) < 1e-10) {
int row = i + 1;
while (row < n && fabs(matrix[row][i]) < 1e-10) {
row++;
if (row == n) {
return 0;
swap(matrix[i], matrix[row], n);
det = -det;
det *= matrix[i][i];
for(int j = i + 1; j < n; j++) {
double ratio = matrix[j][i] / matrix[i][i];
for(int k = i; k < n; k++) {
matrix[j][k] -= ratio * matrix[i][k];
return det;
int main() {
int n;
printf("Enter the size of the square matrix (max 10): ");
scanf("%d", &n);
double matrix[N][N];
printf("Enter the elements of the square matrix:\n");
for(int j = 0; j < n; j++) {
scanf("%lf", &matrix[i][j]);
double det = determinant(matrix, n);
printf("Determinant of the matrix is: %lf\n", det);
Объяснение:
Copyright © 2024 SCHOLAR.TIPS - All rights reserved.
Answers & Comments
Ответ:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define N 10
void swap(double *row1, double *row2, int n) {
for(int i = 0; i < n; i++) {
double temp = row1[i];
row1[i] = row2[i];
row2[i] = temp;
}
}
double determinant(double matrix[N][N], int n) {
double det = 1;
for(int i = 0; i < n; i++) {
if (fabs(matrix[i][i]) < 1e-10) {
int row = i + 1;
while (row < n && fabs(matrix[row][i]) < 1e-10) {
row++;
}
if (row == n) {
return 0;
}
swap(matrix[i], matrix[row], n);
det = -det;
}
det *= matrix[i][i];
for(int j = i + 1; j < n; j++) {
double ratio = matrix[j][i] / matrix[i][i];
for(int k = i; k < n; k++) {
matrix[j][k] -= ratio * matrix[i][k];
}
}
}
return det;
}
int main() {
int n;
printf("Enter the size of the square matrix (max 10): ");
scanf("%d", &n);
double matrix[N][N];
printf("Enter the elements of the square matrix:\n");
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++) {
scanf("%lf", &matrix[i][j]);
}
}
double det = determinant(matrix, n);
printf("Determinant of the matrix is: %lf\n", det);
return 0;
}
Объяснение: