using arrays in class

Hi all,

I'm new to c++. I'm coding a program where the array values which are declared using classes needs to be brought back to main program. In main program after calling the function member. I have given a for loop where the contents of the array U needs to be printed. This is the main motive of the program. How could I do that. 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
  #include <iostream.h>
#include <conio.h>
#include <algorithm>

void print();

class explict
{
      private:
              float a,b,c,d,e,f,u[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 *u, int m,float c)
                 {
                         for(int i=0;i<=m-1;i++)
                            {
                                    if(i==m-1) 
                                    {u[i]=c;}else
                                    {u[i]=0;}
                                    //cout<<i<<"\t"<<u[i]<<"\n";
                            }
                 }
                 

                 
         
              
};

int main()
{
    float h,u0,v,dx,dt,t,d,u[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 ( &u[100] ,n+1,u0 );
    
    for(int i=0;i<=n-1;i++)
                {
                        cout<<i<<"\t"<<u[i]<<"\n";
                }
    

       
    getch();
}

void print()
{
     cout<<"-----------------------------------------------------------------";
}  
An array will be implicitely casted to a pointer:
x.array ( u ,n+1,u0 ); // u is treated as the poiter to the first element

x.array ( &u[100] ,n+1,u0 ); // Here you pass the pointer to the element after the last one
Topic archived. No new replies allowed.