Passing and returning value of an array using class

Hi All,

I'm trying to pass and return the values of an array using classes. I couldn't figure out the logic. I have displayed the program below in which main program has a for loop which I need to take into a new class function member. Someone please help me out in coding.

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

void print();

class ftcs
{     
      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 main()
{
    float h,u0,v,dx,dt,t,d,u[100];
    int n,cc;
    ftcs x;
    x.read_data(h,u0,v,dx,dt,t);
    
    d=v*(dt/(dx*dx));
    n=(h/dx)+1;
    cc=n+1;
    for(int i=0;i<=n;i++)
    {
            if(i==n) 
            {u[i]=u0;}else
            {u[i]=0;}
    }

    getch();
}

void print()
{
     cout<<"-----------------------------------------------------------------";
}  
huh ?
I don't really get what you are trying to say ...

My guess is
int read_data (float a,float b, float c, float d, float e, float f)
should be
void read_data (float& a,float& b, float& c, float& d, float& e, float& f)

if you want to move the for loop into a member function,, then just move them..
what suppose to be the problem ?
I can move the loop. but it should return all the values in an array. is it possible?. if possible how? can u show me that.

1
2
3
4
5
6
   for(int i=0;i<=n;i++)
    {
            if(i==n) 
            {u[i]=u0;}else
            {u[i]=0;}
    }
As I recall you can't flat out return the array, but you can return a pointer. Keep in mind you could also encapsulate the array in a struct our class. Always keep in mind STL containers are accessible for when they're well suited to a situation.
please use reasonable names for your variables

anyway in read_data() nothing will happen with those float values because you just passed the value of them, maybe, you want them to be passed as reference:

just llike what @rmxhaha said:
1
2
void read_data (float& a,float& b, float& c, float& d, float& e, float& f)
// the member function will modify the value of these 


you may want to create a pointer to the array and return that ?

I'm not really sure what you want
If you pass an array as parameter, it is passed by reference, which is, the values inside the array are also modified:

1
2
3
4
5
void read_data ( float* pointer_to_array, other parameters )
{
    /* not sure what you want to do here */
    /* modify the values ? */
}


in your main:
1
2
3
4
5
6
7
8
9
10
int main ()
{
    float u[100];
    
    /* bla bla */
    
    read_data ( &u , bla, bla );

    return 0;
}

Last edited on
to return array you have to return a pointer to an array you can't return an array.

1
2
3
4
5
typedef double (*value)[];

//declare function
*value myfunction();



the syntax might be wrong I have to check up on it, haven't done that in a while.
Topic archived. No new replies allowed.