gpt4 book ai didi

C-制作梯形矩阵?

转载 作者:行者123 更新时间:2023-11-30 16:51:03 26 4
gpt4 key购买 nike

Hey, so I wanted to make a program for making a Matrix in Echelon form(Not Reduced Echelon Form). Everything seems to be working fine except when the last element of the matrix is 0. That time, it just divides the row and makes it 1! So, I added a while loop to fix that but it still doesn't work since the while loop is not getting executed! Could anyone tell me why?

#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

This While Loop doesn't get Executed!!! I know it's not getting executed cause it doesn't print "a" like I wrote Can anyone tell me as to why?!

    while(A[row_pos][column_pos] == 0)
{
printf("a");
column_pos++;
}
RowDivide(A,row_pos,column_pos,m);

If the last element over here is 0, it divides it and makes it 1! Why?

    ///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");
}
}

}
}

最佳答案

我不确定你在做什么,但如果你希望 while 循环找到第一个 0 元素,你应该将其更改为:

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

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

关于C-制作梯形矩阵?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41912603/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com