program not running while using classes

I have used classes for programming my problem. I would like to know why my program not works. It works fine until line number 73.

I found there is something wrong with class and objects on line number 75.

Please help me out. Thanks in advance.

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
  #include <iostream.h>
#include <conio.h>
#include <algorithm>

void print();

class explict
{
      private:
              float a,b,c,d,e,f,u[100];
              float un[100];
      
      public:
             int read_data (float &a,float &b, float &c, float &d, float &e, float &f)
                 {
                        print();
                        cout<<"\nThis program computes the velcoity of suddenly accelerated plate \nover a period of time \n";
                        print();
                        cout<<"\nPlease Enter the following details in SI units.....\n\n";
                        cout<<"Distance between the two plates (h)  m   :";cin>>a;
                        cout<<"Velocity of the moving plate    (u0) m/s :";cin>>b;
                        cout<<"Kinematic viscosity of fluid    (v)  m2/s:";cin>>c;
                        print();
                        cout<<"\nSetting the Grid for the problem...\n\nGrid Spacing required along\n1.Y-Direction\t\t:";cin>>d;
                        cout<<"2.Time step Interval\t:";cin>>e;
                        cout<<"Time step at which velcoity to be found:";cin>>f;
                        print();cout<<"\n";
                        
                 }
                 
                 int array (float *uu, int m,float c)
                 {
                         for(int i=0;i<=m-1;i++)
                            {
                                    if(i==m-1) 
                                    {uu[i]=c;}else
                                    {uu[i]=0;}
                                    //cout<<i<<"\t"<<u[i]<<"\n";
                            }
                 }
                
                 
              int ftcs (float aa,float bb,float a,float d,float* u,int m,float o)
                 {
                         for (int k=1;k<=aa/bb+1;k++)
                              {
                                   cout<<"\nTime Step:"<<k<<" Sec\n";   
                                   std::copy (u,u+m,un);       
                                   for (int j=1;j<a/d;j++)
                                     {
                                            u[j]=un[j]+o*(un[j+1]-(2*un[j])+un[j-1]);
                                            cout<<"\n"<<j<<"\t"<<u[j];
                                     }cout<<"\n";
                               }     
                 }    
};

int main()
{
    float h,u0,v,dx,dt,t,d,uu[100];
    int n,cc;
    explict x;
    
    x.read_data(h,u0,v,dx,dt,t);
    
    d=v*(dt/(dx*dx));
    n=(h/dx)+1;
    
    cout<<h<<"\t"<<dx<<"\t"<<n<<"\t"<<d<<"\n";
    x.array ( &uu[100] ,n+1,u0 );
    cout<<"Initialization Completed"<<"\n\n";
    float* u=&uu[100];
    cout<<"Starting Calculation"<<"\n";
    
    x.ftcs (t,dt,h,dx,&u[100],n+1,d);

    getch();
}

void print()
{
     cout<<"-----------------------------------------------------------------";
}  
Don't forget that arrays start from 0. :-)

You're accessing element 100 of an array that will have a subscript range of 0-99. The program should run fine, but you'll be causing undefined behaviour.
But I'm using the array elements from 0-40 only. can you be more clear about my mistake.
@Vigii: Try changing uu[100] to uu[99] used between 70 to 75
Hi Funprogrammer,

It's not working.

I found the values of h,dx,d are changing at line number 70. It turns out to zero after line number 69.

Why does it happen?

How to make sure the values remains unchanged through out the program.
There are some design issues here. At lines 10 and 11 these member variables are defined:
1
2
    float a,b,c,d,e,f,u[100];
    float un[100];

However, a whole lot more variables are defined inside main() so that the only one of the above variables which is actually used is float un[100];

I think the design should be consistent, either delete the unused variables in order to avoid confusion, or define all the variables inside the class and remove them from main(). Then from main() simply call the member functions without passing any parameters at all.

However, to focus on the specific errors in this code.
Consider these lines in main():
1
2
3
    float uu[100];

    x.array ( &uu[100] ,n+1,u0 );

First the array uu is defined, it has 100 elements, valid subscripts are in the range 0 to 99.

then function array() is called. One of the parameters is &uu[100]. What is this exactly? Well, uu[100] is the 101st element of the array, that is it is not a valid element because it is outside the array. Using the & operator then passes the address of this invalid element.
The correct code should be simply
 
    x.array ( uu, n+1, u0 );


Similarly at lines 72 and 75 the same type of error is made.

I assume the correct code at line 75 would be simply:
 
    x.ftcs (t,dt,h,dx,uu,n+1,d);

though because I'm not familiar with the algorithm being used I can't say whether this will give correct results.
Hi Chervil,

Thank you for your help. you resolved my issues.
Topic archived. No new replies allowed.