Skip to content

added Gauss Jordan Method #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file added .github/.keep
Empty file.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
[![Review Assignment Due Date](https://classroom.github.com/assets/deadline-readme-button-24ddc0f5d75046c5622901739e7c5dd533143b0c8e959d652212380cedb1ea36.svg)](https://classroom.github.com/a/1Btf62fS)
## C++ Assignment: Solving System of Linear Equations

### Objective:
Expand Down
80 changes: 80 additions & 0 deletions lineareq.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
#include<bits/stdc++.h>
#define SIZE 10
using namespace std;

int main()
{
float a[SIZE][SIZE], x[SIZE], ratio;
int i,j,k,n,c=0;

/* Setting precision and writing floating point values in fixed-point notation. */
cout<< setprecision(3)<< fixed;

/* Inputs */
/* 1. Reading number of unknowns */
cout<<"Enter number of unknowns: ";
cin>>n;

/* 2. Reading Augmented Matrix */
cout<<"Enter Coefficients of Augmented Matrix: "<< endl;
for(i=0;i<n;i++)
{
for(j=0;j<n+1;j++)
{
cout<<"a["<< i<<"]"<< j<<"]= ";
cin>>a[i][j];
}
}
/* Applying Gauss Jordan Elimination */
for(i=0;i<n;i++)
{
if (a[i][i] == 0)
{
c=1;
while(c<n-i)
{
if (a[i+c][i] == 0)
c++;
else
break;
}
if (c==n-i)
{
cout<<"NO SOLUTION";
return 0;
}

for(int k=0;k<n+1;k++)
{
int swap = a[i][k];
a[i][k] = a[i+c][k];
a[i+c][k] = swap;
}
}
for(j=0;j<n;j++)
{
if(i!=j)
{
ratio = a[j][i]/a[i][i];
for(k=0;k<n+1;k++)
{
a[j][k] = a[j][k] - ratio*a[i][k];
}
}
}
}
/* Obtaining Solution */
for(i=0;i<n;i++)
{
x[i] = a[i][n]/a[i][i];
}

/* Displaying Solution */
cout<< endl<<"Solution: "<< endl;
for(i=0;i<n;i++)
{
cout<<"x["<< i<<"] = "<< x[i]<< endl;
}

return(0);
}
Binary file added lineareq_output.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.