Here memory of two dimensional matrix is allocated dynamically using malloc / calloc which is the best method of allocating memory in C programming language.
Source Code Of Gauss Elimination Method Using Partial Pivoting and Dynamic Memory Allocation Using c/c++
**********************
IDE CODE :: BLOCK 10.05
partial_pivoting.c
**********************
#include<stdlib.h>
#include<stdio.h>
#include<math.h>
int main()
{
float **a,*temp,app,sum,mult;
int i,j,k,n,p;
//take no of terms
printf("Enter n : ");scanf("%d",&n);
//memory allocation
a = (float**)malloc(n*sizeof(float*));
for(i = 0; i < n; i++)
a[i] = (float*)malloc(n*sizeof(float));
temp = (float*)malloc(n*sizeof(float));
//take input from user
printf("Enter the elements of augmended matrix rowwise\n");
for(i=0;i<n;i++)
for(j=0;j<=n;j++)
scanf("%f",&a[i][j]);
//generation of scalar matrix
for(i=0;i<(n);i++){
app = a[i][i];
//initialization of p
p = i;
//find largest no of the columns and row no. of largest no.
for(k = i+1; k < n; k++)
if(fabs(app) < fabs(a[k][i])){
app = a[k][i] ;
p = k;
}
//swaping the elements of diagonal row and row containing largest no
for(j = 0; j <= n; j++)
{
temp[j] = a[p][j];
a[p][j] = a[i][j];
a[i][j] = temp[j];
}
//calculating triangular matrix
for(j=i+1;j<n;j++){
mult = a[j][i]/a[i][i];
for(k=0;k<=n;k++)
a[j][k] -= mult*a[i][k];
}
}
//for calculating value of z,y,x via backward substitution method
for(i=n-1;i>=0;i--)
{
sum = 0;
for(j=i+1;j<n;j++)
sum += a[i][j]*temp[j];
temp[i] = (a[i][n]-sum)/a[i][i];
}
printf("****The matrix is : ***\n");
for(i=0;i<n;i++){
for(j=0;j<=n;j++)
printf("%.2f\t",a[i][j]);
printf("\n");
}
//display solution
printf("-------------The solution is ----------\n");
for(i=0;i<n;i++)
printf("X[%d] = %.2f\n",i+1,temp[i]);
//free allocated memory
for(i = 0; i < n; i++)
free(a[i]);
free(a);
free(temp);
return 0;
}
********
OUTPUT
********
Enter n : 3
Enter the elements of augmende
0 4 5 8
7 8 9 2
2 3 5 1
****The matrix is : ***
7.00 8.00 9.00 8.00
0.00 4.00 5.00 2.00
-0.00 0.00 1.54 -1.64
-------------The solution is----------
X[1] = -2.09
X[2] = 2.81
X[3] = -.65
IDE CODE :: BLOCK 10.05
partial_pivoting.c
**********************
#include<stdlib.h>
#include<stdio.h>
#include<math.h>
int main()
{
float **a,*temp,app,sum,mult;
int i,j,k,n,p;
//take no of terms
printf("Enter n : ");scanf("%d",&n);
//memory allocation
a = (float**)malloc(n*sizeof(float*));
for(i = 0; i < n; i++)
a[i] = (float*)malloc(n*sizeof(float));
temp = (float*)malloc(n*sizeof(float));
//take input from user
printf("Enter the elements of augmended matrix rowwise\n");
for(i=0;i<n;i++)
for(j=0;j<=n;j++)
scanf("%f",&a[i][j]);
//generation of scalar matrix
for(i=0;i<(n);i++){
app = a[i][i];
//initialization of p
p = i;
//find largest no of the columns and row no. of largest no.
for(k = i+1; k < n; k++)
if(fabs(app) < fabs(a[k][i])){
app = a[k][i] ;
p = k;
}
//swaping the elements of diagonal row and row containing largest no
for(j = 0; j <= n; j++)
{
temp[j] = a[p][j];
a[p][j] = a[i][j];
a[i][j] = temp[j];
}
//calculating triangular matrix
for(j=i+1;j<n;j++){
mult = a[j][i]/a[i][i];
for(k=0;k<=n;k++)
a[j][k] -= mult*a[i][k];
}
}
//for calculating value of z,y,x via backward substitution method
for(i=n-1;i>=0;i--)
{
sum = 0;
for(j=i+1;j<n;j++)
sum += a[i][j]*temp[j];
temp[i] = (a[i][n]-sum)/a[i][i];
}
printf("****The matrix is : ***\n");
for(i=0;i<n;i++){
for(j=0;j<=n;j++)
printf("%.2f\t",a[i][j]);
printf("\n");
}
//display solution
printf("-------------The solution is ----------\n");
for(i=0;i<n;i++)
printf("X[%d] = %.2f\n",i+1,temp[i]);
//free allocated memory
for(i = 0; i < n; i++)
free(a[i]);
free(a);
free(temp);
return 0;
}
********
OUTPUT
********
Enter n : 3
Enter the elements of augmende
0 4 5 8
7 8 9 2
2 3 5 1
****The matrix is : ***
7.00 8.00 9.00 8.00
0.00 4.00 5.00 2.00
-0.00 0.00 1.54 -1.64
-------------The solution is----------
X[1] = -2.09
X[2] = 2.81
X[3] = -.65
Very good program..easy and helpful
ReplyDeletethanks brother
ReplyDeleteTQVM
ReplyDelete