### Program received signal SIGSEGV, Segmentation fault.

Here's the entire program

error is for line 164 > u[i][y]=U;

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177`` ``````//Including necessary files #include #include #include #include #define nu 0.00001 #define h 0.1 #define U 100 using namespace std; ofstream mout("A8_output.doc"); //mout prints to word file ofstream xlsout("A8_comparison.xls"); //xlsout prints to excel file void ftcs(double t,double y); void implicit(double t,double y); void crank_nicholson(double t,double y); main() { //Declare necessary variables int i,j,prog=1; double t,y; char change,wish; //Naming the program and displaying the partial differential equation cout<<"Programme to solve partial differential equation :"; cout<<"\n\ndu/dt = nu * d^2(u)/dy^2"; mout<<"Programme to solve partial differential equation :"; mout<<"\n\ndu/dt = nu * d^2(u)/dy^2"; while (prog==1) { t=0.1; for (i=0;i<3;i++) { y=0.01; for (j=0;j<2;j++) { ftcs(t,y); implicit(t,y); crank_nicholson(t,y); y=y/10; } t=t/10; } cout<<"\n\nDo you want to try other delta_t and delta_y values?"; cout<<"\nIf yes then press 'y' and then press enter to continue"; cout<<"\nElse press any other key and then press enter to continue.\n\n"; cin>>change; mout<<"\n\nDo you want to try other delta_t and delta_y values?"; mout<<"\nIf yes then press 'y' and then press enter to continue"; mout<<"\nElse press any other key and then press enter to continue.\n\n"; while (change=='y') { cout<<"\n\nEnter the value of delta_t : "; //Loop to get a positive number do { //Verifying whether entered value is a number while (!(cin>>t)) { cout<<"\n\nThe value entered is not a number, please try again : "; cin.clear();//Clearing error flag cin.ignore(10000,'\n');//Ignoring previously entered non-number value } //Checking for negative value if (t<=0) cout<<"\n\nPlease enter a positive number : "; } while (t<=0); mout<<"\n\nEnter the value of delta_t : "; mout<>y)) { cout<<"\n\nThe value entered is not a number, please try again : "; cin.clear();//Clearing error flag cin.ignore(10000,'\n');//Ignoring previously entered non-number value } //Checking for negative value if (y<=0) cout<<"\n\nPlease enter a positive number : "; } while (y<=0); mout<<"\n\nEnter the value of delta_y : "; mout<>change; mout<<"\n\nDo you want to try other delta_t and delta_y values?"; mout<<"\nIf yes then press 'y' and then press enter to continue"; mout<<"\nElse press any other key and then press enter to continue.\n\n"; } //Asking for re-running the program cout<<"\n\nDo you want to re-run the programme?"; cout<<"\nIf yes press 'y' and then press enter"; cout<<"\nElse press anyother key and then press enter to exit.\n\n"; //Store user's wish cin>>wish; mout<<"\n\nDo you want to re-run the programme?"; mout<<"\nIf yes press 'y' and then press enter"; mout<<"\nElse press anyother key and then press enter to exit.\n\n"; mout<

Output------

 ``` Programme to solve partial differential equation : du/dt = nu * d^2(u)/dy^2 i=0 j=10 i=1 j=10 i=2 j=10 i=3 j=10 i=4 j=10 i=5 j=10 i=6 j=10 i=7 j=10 i=8 j=10 i=9 j=10 i=10 j=10 i=0 j=100 i=1 j=100 i=2 j=100 i=3 j=100 i=4 j=100 i=5 j=100 i=6 j=100 i=7 j=100 i=8 j=100 i=9 j=100 i=10 j=100 ```
Last edited on
Without even looking at the code, a seg fault is normally due to an array subscript being out of bounds.

The best thing is to use a debugger to keep track of the values of your variables, so can see where it all go wrong. This is by far the easiest way of finding these problems - beats staring at code for hours !!!

Please edit your post so it uses code tags - the <> button on the right - then we can see the offending line number.

HTH
Last edited on
 error is for line 164 > u[i][y]=U;

The dimension of u is t x y so you can't have i == t and you can't index the second dimension to y. That would be outside the memory of u.
@TheIdeasMan

Oops I had not used the code tags correctly.....now I have

Thanx for the help but i already tried it. It just shows the location where it happens but I am unable to figure out how to go around it (probably because I am not conversant with "debugger")

@histrungalot

Earlier i also thought the error to be same as you are suggesting but it works properly for t=0.1 and y=0.01 and when t=0.1 and y=0.001 it shows this error
( t and y are variables passed from main to function ftcs)

Correction: line 165 was added later and hence is not there in the output
Last edited on
With the debugger, you should have a watchlist of variable values. If you have already found the location of the error, then it should be easy to see which variable is causing the problem. You need to work backwards to see how the variable changes.
Topic archived. No new replies allowed.