Click here to Skip to main content
15,893,381 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I got following algorithm of Gauss Elimination method from the book. But I don't know how to print the roots of the simultaneous equations obtained from the following algorithm. The algorithm is given below:
VB
1. procedure Gaussian_Elimination(A, b, y)
  2. begin
  3.    for k:= 0 to n-1 do
  4.      begin
  5.       for j:=k + 1 to n-1 do
  6.          A[k,j] := A[k,j] /A[k,k];
  7.       y[k] : = b[k]/A[k,k];
  8.       A[k,k] :=1;
  9.       for i:= k + 1 to n-1 do
 10.       begin
 11.           for j:= k+1 to n-1 do
 12.              A[i,j]:= A[i,j] - A[i,k] * A[k, j];
 13.           b[i] := b[i] -A[i, k] * y[k];
 14.            A[i, k] :=0;
 15.       endfor;*//* Line 9 */
 /*16. endfor; *//*line 3*/
 /*17. end


Please check my code my code and tell me if the implmentation is correct or not. My code is given below:

#include <iostream>
#include <cmath>
//#include <mpi.h>
#include <fstream>
#include <ctime>
#include <vector>
#define n  3
using namespace std;

void GaussianElimination(double **,double *b ,double *y);
int main(int argc, char * argv[])
{
  /* values
  Row0 = 0, 2, 1; b0= -8
  Row1 = 1, -2, -3; b1 = 0
  Row3 = -1, 1, 2; b2= 3

  Ans = -4, -5, 2 */
 
  
  
 <pre>double *A[n]={NULL, NULL, NULL} ;
  double *b = NULL;
  double *y= NULL;

  for (int i=0; i < n; i++){
    A[i] = new double [n];
    if (A[i] == NULL){
       cout<< " problem with memory Allocation:";
       return -2;
    }
  }
  b = new double[n];
  if(b== NULL){
    cout<< " problem with memory Allocation:";
    return -2;
  }
 
  y = new double[n];
  if(y== NULL){
    cout<< " Problem with Memory Allocation:";
    return -2;
  }
  
  A[0][0] =0.0;A[0][1] =2.0; A[0][2] = 1.0; //A[0][3] = -8.0;
  A[1][0] =1.0;A[1][1] =-2.0; A[1][2] =-3.0; //A[1][3] = 0.0;
  A[2][0] =-1.0;A[0][1] =1.0; A[0][2] = 2.0; //A[0][3] = 3.0;
  b[0] = -8.0;
  b[1] = 0.0;
  b[2] = 3.0;


  GaussianElimination(A, b ,y);
  delete [] y;
  delete [ ]b;
  for(int i = 0; i < n; i++)
    delete [] A[i];
 // delete [ ] A; No need to delete A because A is not created dynamically  
}
  void GaussianElimination(double **A, double *b,double *y) {
     for(int k=0; k<n-1; ++k) {
        for(int j= k+1; j <n-1; ++j)
           A[k][j] = A[k][j]/A[k][k]; 
        y[k] = b[k]/A[k][k];
        for(int i=k+1; k<n-1; i++){
           for(int j=k+1; j<n-1; j++)
              A[i][j] = A[i][j] -A[i][k] *A[k][j];
           b[i]= b[i] -A[i][k] * y[k];
           A[i][k] = 0;
        }
     }

     for(int i=0; i<n-1; i++)
        cout<<y[i];
}        


I am printing the contents of y but its giving me segmentation fault. Some body please guide me.
<pre>$ ./a.out
Segmentation fault (core dumped)
$ 


Zulfi.

What I have tried:

I tried printing the value of 'b' also but it also gives me segmentation fault
Posted
Updated 28-Apr-19 20:25pm
v4

Quote:
Segmentation fault: how to print the solution for gauss elimination algorithm

This mean that your code try to use a memory space it don't own, somewhere you try to read/write after the end of an array.
Learn tool that will help you to find bug by yourself, learn debugger.
----
Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

1.11 — Debugging your program (stepping and breakpoints) | Learn C++[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
 
Share this answer
 
Comments
zak100 28-Apr-19 16:02pm    
I found that I was redeclaring b & y. I have removed that error but still I am getting segmentation fault.

Thanks for your response.

Zulfi.
Patrice T 28-Apr-19 16:03pm    
Update your question with new information and new code.
Use Improve question to update your question.
zak100 28-Apr-19 16:27pm    
Hi,
I can't see improvequestion option. Please guide me.

Zulfi.
Patrice T 28-Apr-19 18:08pm    
move mouse on last line of question, you will see.
Hi,

I think this algorithm can't work if the diagonal entry is zero. This is for dense simultaneous equations.
<pre> void GaussianElimination(double **A, double *b,double *y) {
     cout<<"Inside Gaussian 1";
     for(int k=0; k<=n-1; ++k) {
        cout<<"k =" <<k<<endl;
        for(int j= k+1; j <=n-1; ++j){
           cout<<"A["<<k<<"]["<<k<<"] =" <<A[k][k] <<endl;
           A[k][j] = A[k][j]/A[k][k]; 
         }
      
       
        y[k] = b[k]/A[k][k];
        cout<<"Reached";
        for(int i=k+1; i<=n-1; i++){
           for(int j=k+1; j<=n-1; j++)
              A[i][j] = A[i][j] -A[i][k] *A[k][j];
           b[i]= b[i] -A[i][k] * y[k];
           A[i][k] = 0;
        }
     }

     for(int i=0; i<n-1; i++)
        cout<<y[i];
}        

I am gettingfollowing output:
Testing0Testing1Inside Gaussian 1k =0
A[0][0] =0
A[0][0] =0
Segmentation fault (core dumped)

It can't print "Reached". I have to change the input.
If I am wrong please let me know.

Zulfi.
 
Share this answer
 
v3
Comments
k5054 28-Apr-19 20:02pm    
I think you need to look at the condition expression of your for loops:
for(initial;condition;increment)
zak100 28-Apr-19 20:12pm    
Sorry I can't figure it out why my middle term is wrong.I am following the algorithm:
for k:= 0 to n-1 do

Please guide me.

Zulfi.
Zulfi.
zak100 28-Apr-19 20:14pm    
In "Solution 2" I have corrected the upper limit.

Zulfi.
zak100 28-Apr-19 23:15pm    
I have corrected the problem with condition.

Zulfi.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900