【发布时间】:2017-06-14 05:38:03
【问题描述】:
嘿,所以我想编写一个程序来制作梯队形式的矩阵(不是简化梯队形式)。除了矩阵的最后一个元素为 0 时,一切似乎都运行良好。那时,它只是将行划分为 1! 所以,我添加了一个while循环来解决这个问题,但它仍然不起作用,因为while循环没有被执行!谁能告诉我为什么?
#include <stdio.h>
#include <stdlib.h>
void Interchange(float A[][3],int row_pos,int column_pos,int m);
void RowDivide(float A[][3],int row_pos,int column_pos,int m);
void RowOperation(float A[][3],int row_pos,int row_op,int column_pos,int m);
int main()
{
int m;
printf("Enter the number of rows in the Matrix: ");
scanf("%d",&m);
float A[m][3];
printf("\nEnter the Matrix:\n");
for(int i=0 ; i<m ; i++)
{
for(int j=0 ; j<3 ; j++)
{
scanf("%f",&A[i][j]);
}
}
int column_pos=0;
for(int row_pos=0 ; row_pos<m ; row_pos++)
{
///For Interchanging
if(A[row_pos][column_pos] == 0)
{
Interchange(A,row_pos,column_pos,m);
}
///For Row Division
这个 While 循环没有被执行!!! 我知道它没有被执行,因为它不像我写的那样打印“a” 谁能告诉我为什么?!
while(A[row_pos][column_pos] == 0)
{
printf("a");
column_pos++;
}
RowDivide(A,row_pos,column_pos,m);
如果这里最后一个元素为0,则将其除以1!为什么?
///For Row Operations
if(row_pos == m-1)
{
break;
}
else
{
for(int row_op = row_pos+1 ; row_op<m ; row_op++)
RowOperation(A,row_pos,row_op,column_pos,m);
}
column_pos++;
}
printf("\nThe Matrix in Echolen Form:\n");
for(int i=0 ; i<m ; i++)
{
for(int j=0 ; j<3 ; j++)
{
printf("%0.2f ",A[i][j]);
}
printf("\n");
}
return 0;
}
void Interchange(float A[][3],int row_pos,int column_pos,int m)
{
float temp;
int cal_pos;
cal_pos=row_pos;
while(A[cal_pos][column_pos] == 0)
{
cal_pos++;
}
for(int i=row_pos ; i<row_pos+1 ; i++)
{
for(int j=column_pos ; j<3 ; j++)
{
temp = A[i][j];
A[i][j] = A[cal_pos][j];
A[cal_pos][j] = temp;
}
}
printf("\nThe Matrix after Interchanging Row %d is:\n",row_pos+1);
for(int i=0 ; i<m ; i++)
{
for(int j=0 ; j<3 ; j++)
{
printf("%0.2f ",A[i][j]);
}
printf("\n");
}
}
void RowDivide(float A[][3],int row_pos,int column_pos,int m)
{
float temp; ///To store the value of A[i][0] since it will get changed to 1 after dividing
for(int i=row_pos ; i<row_pos+1 ; i++)
{
temp = A[i][column_pos];
for(int j=0 ; j<3 ; j++)
{
A[i][j] = (A[i][j] / temp);
}
}
printf("\nThe Matrix after dividing the Row %d is:\n",row_pos+1);
for(int i=0 ; i<m ; i++)
{
for(int j=0 ; j<3 ; j++)
{
printf("%0.2f ",A[i][j]);
}
printf("\n");
}
}
void RowOperation(float A[][3],int row_pos,int row_op,int column_pos,int m)
{
float Cal_Operation; ///For the value of row that must be added
for(int i=row_op ; i<row_op+1 ; i++)
{
if(A[i][column_pos] == 0)
{
break;
}
else
{
Cal_Operation = -A[i][column_pos];
printf("\nCalculated Variable for Row %d= %0.2f\n",row_op+1,Cal_Operation);
for(int j=column_pos ; j<3 ; j++)
{
A[i][j] = A[i][j] + (Cal_Operation*A[row_pos][j]);
}
printf("The Matrix after Operating on Row %d is:\n",row_op+1);
for(int i=0 ; i<m ; i++)
{
for(int j=0 ; j<3 ; j++)
{
printf("%0.2f ",A[i][j]);
}
printf("\n");
}
}
}
}
【问题讨论】: