Reputation: 11
can anyone provide some help with it . i am totally unable to do this one.i tried to do some coding but it didn't work at all. Anyways i have included the code.It is incomplete and wrong anyways.
#include<stdio.h>
#include<math.h>
main()
{
int rows, columns, iter, i,j,k;
printf("enter the number of rows and columns:");
scanf("%d, %d",&rows, &columns);
double a[rows][columns], b[rows][1],x[rows][1];
printf("enter the elements of A matrix:");
for(i=0;i<rows;i++)
{
for(j=0;j<columns;j++)
scanf("%lf",&a[i][j]);
}
printf("\n enter the elements of B matrix:");
for(i=0,i<rows;i++)
scanf("%lf",&b[i][0]);
printf("\n");
for (iter=1; iter<rows; iter++)
{
for(i=iter;i<rows;i++)
b[i][0] = b[i][0]- a[k][iter-1]*(b[i-1][0]/a[iter-1][iter-1]);
for(j=iter-1,k=iter;j<columns;j++)
a[i][j] = a[i][j]-a[i-1][j]*(a[k][iter-1]/a[iter-1][iter-1]);
}
printf("the elements of the matrix A are now:");
for(i=0;i<rows; i++)
{
for(j=0; j<columns; j++)
printf("%lf",a[i][j]);
}
return(0);
}
After this back substitution has to be done . The equation is [a][x] = [b] and we have to find the values of x for given [a] and [b] matrices.
Upvotes: 0
Views: 3297
Reputation: 5802
The first thig that I notice is this loop
for(i=0;i<=columns-1;i++)
{
for(j=0;j<=columns-1;j++)
{
scanf("%lf",&a[i][j]);
}
}
I'm guessing that one of these columns
should be rows
instead, porbably the one used in i<=columns-1
.
Also, just out of interest why i<=columns-1
instead of i < columns
, they should be equivalent in all cases that matter.
Upvotes: 0
Reputation: 28837
int rows, columns, iter, i,j,k;
printf("enter the number of rows and columns:");
scanf("%d, %d",&rows, &columns);
double a[i][j], b[i],x[i];
Using i, and j to specify array sizes is a very questionable activity, as i and j have not been initialized.
Upvotes: 0
Reputation: 941555
Nobody uses code like this, it has serious numerical stability issues. Using LU-decomp is a good approach, many libraries available for it. Numerical Recipes, if you have to.
Upvotes: 1