Hello!!
It's the first time I post at your forum..
I can clearly understand how to pass a dynamic array to a function and modified within the function..
My aim is to get back the array initialized as I am trying on the next sample of code..
Any help would mean a lot to me!
Code:#include <stdio.h> #include <stdlib.h> #include <math.h> #include <iostream> #include <fstream> using namespace std; #define N 3 //number of bodies void initialise( double *M[], double *X[], double *y[]) { *M[1]=0.001; *M[2]= 0.002; *M[3]= 1.; //Masses (the last body must be the gratest) *X[0]=0.; //Time *X[1]=0.9741; *X[2]=0.; *X[3]=0.1; //x,y,z for body 1 MOON *X[4]=2.2933; *X[5]=0.; *X[6]=1.; //x,y,z for body 2 EARTH *X[7]=0.0055607; *X[8]=0.; *X[9]=0.0021;//x,y,z for body 3 SUN *X[10]=0.2; *X[11]=1.0327; *X[12]=0.0005; //Vx,Vy,Vz for body 1 *X[13]=0.1; *X[14]=0.6320; *X[15]=0.0005; //Vx,Vy,Vz for body 2 *X[16]=0.01; *X[17]=0.0022967; *X[18]=0.0000015;//Vx,Vy,Vz for body 3 for ( int i=0; i<=3*(N-1); ++i ) *y[i] = *X[i]; for ( int i=3*(N-1)+1; i<=6*(N-1); ++i ) *y[i] = *X[i+3]; } int main() { double *M=new double[N+1]; //the masses of the bodies double *X=new double[6*N+1]; //time,positions,velocities of N bodies double *y=new double[6*(N-1)+1]; //time,positions,velocities of N-1 bodies initialise( &M, &X, &y); for (int i=0; i<=6*N; ++i) cout<<X[i]<<endl; return 0; }




Reply With Quote
