编写一个C程序,将矩阵二次型化为最简型
时间: 2023-05-25 09:05:04 浏览: 105
#include <stdio.h>
int main()
{
int n, i, j, k, temp;
float a[10][10];
printf("Enter the dimension of matrix: ");
scanf("%d", &n);
printf("Enter the matrix elements:\n");
for(i=0; i<n; i++)
for(j=0; j<n; j++)
scanf("%f", &a[i][j]);
// Gaussian elimination
for(k=0; k<n; k++)
{
if(a[k][k] == 0.0)
{
printf("Pivot element is 0. Calculation terminated.\n");
return 0;
}
for(i=k+1; i<n; i++)
{
temp = a[i][k] / a[k][k];
for(j=k; j<n; j++)
a[i][j] = a[i][j] - temp * a[k][j];
}
}
// Print the diagonal matrix
printf("\nDiagonal matrix:\n");
for(i=0; i<n; i++)
{
for(j=0; j<n; j++)
{
if(i == j)
printf("%f ", a[i][j]);
else
printf("0.000000 ");
}
printf("\n");
}
return 0;
}
阅读全文