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?C-Making eine Matrix in Echelon Form?
#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");
}
}
}
}
Nein, ich will eigentlich die while-Schleife ausgeführt werden, wenn das Element 0. So fühlte ich, dass gearbeitet haben sollte ... Aber es ist nicht, auch wenn das Element 0 ist, wird die while-Schleife ausgeführt wird nicht tho es sollte. –
Aber ich schätze deine Hilfe immer noch sehr! Also vielen Dank dafür! Das bedeutet viel! :) –
Ich fand das Problem ist Gleitkommafehler: mit float Zahlen, die == 0 Operation macht wenig Sinn, und ich schlage vor, Sie überprüfen, stattdessen, wenn der absolute Wert der Zahl ist sehr, sehr wenig, zum Beispiel weniger als 0,00000001 (Sie sollten diesen Wert berechnen, der auf Maschinengenauigkeit basiert). Importieren Sie für fabs Funktion, dann überprüfen Sie etwas wie 'while (fabs (A [row_pos] [column_pos]) <= 0.00001)' –
Kroj