【问题标题】:C-Making a Matrix in Echelon Form?C-以梯队形式制作矩阵?
【发布时间】: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");
        }
    }

}
}

【问题讨论】:

    标签: c arrays forms matrix ref


    【解决方案1】:

    我不确定你在做什么,但如果你想让 while 循环找到第一个 0 元素,你应该把它改成:

    while(A[row_pos][column_pos] != 0)
    

    我可能误解了你的意图,如果是这样,请告诉我。

    【讨论】:

    • 不,我实际上希望当元素为 0 时执行 while 循环。所以,我觉得应该可以工作......但它不是,即使元素为 0,while 循环没有执行它应该执行的操作。
    • 但我仍然非常感谢您的帮助!所以非常感谢你!那意义重大! :)
    • 我发现问题是浮点错误:使用 == 0 操作的浮点数没有意义,我建议你检查一下,如果数字的绝对值非常小,则为例如小于 0.00000001(您应该计算该值,它基于机器精度)。为 fabs 函数导入 ,然后检查类似 while(fabs(A[row_pos][column_pos]) &lt;= 0.00001) 的内容
    • 我还不明白 Interchange 是做什么的,但是像我说的那样更改代码会使 while 实际上循环。不幸的是,我相信这会带来更多问题,因为您可以看到使用简单矩阵(如 3x3 (1,3,3,0,0,1,0,0,0))进行测试
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-08
    • 1970-01-01
    • 2013-01-06
    • 1970-01-01
    相关资源
    最近更新 更多