Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a C program to solve a set of n linear equations using the Gaussian elimin

ID: 3714311 • Letter: W

Question

Write a C program to solve a set of n linear equations using the Gaussian elimination algorithm. Your program should work for any appropriate system of equations up to and including 10 unknowns. Input to your program must come from a text data file that has been specified on the command line (see the sample run example below). This file might contain multiple sets of linear equations where each set is preceded by a line containing the number of unknowns (with no blank lines between data sets); the first integer in the data file will represent the total number of linear equation systems. You may assume that the data file is correct.

1

3

5 -1 -1.0 11

-1 5.0 -2 0

-2 -2 4.0 0

in the data file gaussHANDOUT.data. Note that data for the coefficients in the input file can be integers or floating point values.

Your basic algorithm, in main:

declarations & initializations

while there’s still data in the file do

input data

echo print data

do Gaussian elimination

print solution

end while

You must define separate functions for all input, output, and of course, the Gaussian elimination. Each of your functions, including main but excluding the function that actually performs the Gaussian elimination, should be less than 20 lines long. You may not define any global variables (exception: preprocessor defines are OK).

Terminal-tcsh-47x16 liberty:-/cprogs1% gcc gauss . c liberty:-/cprogs1% ?.out gaussHANDOUT.data D. Resler 11/14 Data file: gaussHANDOUT. data Number of unknowns: 3 Equations: 5.0x 1-1.0x 2+1.0x3 11.0 1.0x-1 + 5.0x-2 + -2.0x-3 = 0.0 -2.0x-1 + -2.0x-2 + .0x-3 = 0.0 x1 2.933333 x2 1.466667 x3-2.200000 4 liberty:-/cprogs/96

Explanation / Answer

#include<stdio.h>

int n,m;

// n Number of unknowns and m of equestion

int forwardElim(double m[][]);

void backSub(double m[][]);

void GElimination(double m[][])

{

  

int singular_flag = forwardElim(m);

if (singular_flag != -1)

{

printf("Singular Matrix. ");

if (m[singular_flag][n])

printf("Inconsistent System.");

else

printf("May have infinitely many "

"solutions.");

return;

}

  

backSub(m);

}

// function for elemntary operation of swapping two rows

void swap_row(double mat[][], int i, int j)

{

for (int k=0; k<=n; k++)

{

double temp = mat[i][k];

mat[i][k] = mat[j][k];

mat[j][k] = temp;

}

}

// function to print matrix content at any stage

void print(double mat[][])

{

for (int i=0; i<n; i++, printf(" "))

for (int j=0; j<=n; j++)

printf("%lf ", mat[i][j]);

printf(" ");

}

// function to reduce matrix to r.e.f.

int forwardElim(double mat[][])

{

for (int k=0; k<n; k++)

{

int i_max = k;

int v_max = mat[i_max][k];

  

for (int i = k+1; i < n; i++)

if (abs(mat[i][k]) > v_max)

v_max = mat[i][k], i_max = i;

if (!mat[k][i_max])

return k; // Matrix is singular

/* Swap the greatest value row with current row */

if (i_max != k)

swap_row(mat, k, i_max);

for (int i=k+1; i<n; i++)

{

/* factor f to set current row kth elemnt to 0,

* and subsequently remaining kth column to 0 */

double f = mat[i][k]/mat[k][k];

/* subtract fth multiple of corresponding kth

row element*/

for (int j=k+1; j<=n; j++)

mat[i][j] -= mat[k][j]*f;

/* filling lower triangular matrix with zeros*/

mat[i][k] = 0;

}

//print(mat); //for matrix state

}

//print(mat); //for matrix state

return -1;

}

// function to calculate the values of the unknowns

void backSub(double mat[][])

{

double x[n];  

  

for (int i = n-1; i >= 0; i--)

{

  

x[i] = mat[i][n];

/* Initialize j to i+1 since matrix is upper

triangular*/

for (int j=i+1; j<n; j++)

{

x[i] -= mat[i][j]*x[j];

}

  

x[i] = x[i]/mat[i][i];

}

printf(" Solution for the system given below: ");

for (int i=0; i<n; i++)

printf("%lf ", x[i]);

}

int main()

{

int n,m,i,j;

printf("enter the number of Unknown variable");

scanf("%d",&n);

printf("enter the number of of equations");

scanf("%d",&m);

double mat[m][n];

for(i=0;i<m;i++)

{

for(j=0;j<n;j++)

{

scanf("%lf",&mat[i][j]);

}

}

GElimination(mat);

return 0;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote