Program received signal SIGSEGV, Segmentation fault.

Im stuck with this error for quiet long please help me out.
Here's the entire program

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
//Including necessary files
#include<iostream>
#include<iomanip>
#include<fstream>
#include<math.h>

#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<<t;

                  cout<<"\n\nEnter the value of delta_y : ";

                  //Loop to get a positive number
                  do
                  {
                    //Verifying whether entered value is a number
                    while (!(cin>>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<<y;

                  ftcs(t,y);
                  implicit(t,y);
                  crank_nicholson(t,y);

                  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";
            }

            //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<<wish;

            if (wish=='y')
               prog=1;
            else
               prog=0;
      }
}

void ftcs(double f_t,double f_y)
{
     //cout<<"\n\nt = "<<f_t<<"\ty = "<<f_y;

     int i,j,t,y;
     double n_t,n_y,constant;

     constant=nu*f_t/(f_y*f_y);

     n_t=1/f_t;
     n_y=h/f_y;

     t=int(n_t+0.5);
     y=int(n_y+0.5);

     double u[t][y];

     for (i=0;i<=t;i++)
     {
         cout<<"\ni="<<i<<"  j="<<y;
         u[i][y]=U;
         cout<<"\t"<<u[i][y];
     }
     for (i=0;i<y;i++)
         u[0][i]=0;
}
void implicit(double i_t,double i_y)
{
     //cout<<"\n\n"<<i_t<<"\t"<<i_y;
}
void crank_nicholson(double cs_t,double cs_y)
{
     //cout<<"\n\n"<<cs_t<<"\t"<<cs_y;
}


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.