## 33 Replies - 3113 Views - Last Post: 01 September 2014 - 09:13 AM

### #1

# how to make multiple subroutine and return multiple arrays?

Posted 17 August 2014 - 02:32 AM

could please help me this question?

I am writing a program in c++ but i dont know how to make subroutines and return multiple arrays.

Before that, i wrote my program on matlab. could my program to be structured as following? could you please explain more detail? Thank you so much.

<header>

initial value of program;

subroutine1()

{

calculating the first work;

return 2 or 3 arrays;

}

subroutine2()

{

the result of subroutine1 is initial value of subroutine2;

calculting the second work;

return 2 or 3 arrays;

}

subroutine3()

{

the result of subroutine 2 is initial value of subroutine 3;

calculting the third work;

return 2 or 3 arrays;

}

.....

int main()

{

subroutine1;

subroutine2;

subroutine3;

....

the final result is here!

}

##
**Replies To:** how to make multiple subroutine and return multiple arrays?

### #2

## Re: how to make multiple subroutine and return multiple arrays?

Posted 17 August 2014 - 05:17 AM

If you need more than three you might use a class containing vectors or a vector of vectors and pass that by reference.

### #3

## Re: how to make multiple subroutine and return multiple arrays?

Posted 17 August 2014 - 06:17 AM

CTphpnwb, on 17 August 2014 - 05:17 AM, said:

If you need more than three you might use a class containing vectors or a vector of vectors and pass that by reference.

### #4

## Re: how to make multiple subroutine and return multiple arrays?

Posted 17 August 2014 - 06:24 AM

This is a part of my program. Could you please help me? Could you please modify it to follow a true way(use class or vector of vector as you said)? Thank you so much.

#include "stdafx.h" #include <iostream> #include <fstream> #include <math.h> #include <ctime> #define pi 3.14159 #define nx 501 // number of grid in x direction #define ny 301 // number of grid in y direction #define ng nx*ny // total number of grid nodes #define N 100 //number of panels// using namespace std; double xmin=-3.5, xmax=1.5, ymin=-1.5, ymax=1.5; /*computational domain*/ double dx=(xmax-xmin)/(nx-1), dy=(ymax-ymin)/(ny-1); /*spacing grid*/ double x[nx],y[ny]; double xg[nx][ny],yg[nx][ny]; /*mesh*/ double mask[nx][ny]; double xf[N+1],yf[N+1]; /*the first subroutine*/ double grid2d(void) { int i,j; for (i=0;i<nx;i++) /* this step make grid*/ { x[i]=xmin+dx*i; } for (j=0;j<ny;j++) { y[j]=ymin+dy*j; } for (i=0;i<nx;i++) for (j=0;j<ny;j++) { xg[i][j]=x[i]; yg[i][j]=y[j]; } return 0; }/*after the first subroutine, x[],y[],xg[],yg[] is used in next steps*/ /*the first subroutine is independent with the second subroutine*/ double geometry(void) /*the second subroutine*/ { int i; for (i=0;i<N+1;i++) { xf[i]=0.5*cos(i*2*pi/N); yf[i]=0.5*sin(i*2*pi/N); } return 0; }/*after the second subroutine, xf,yf are used in the next steps*/ double mask2d () /*the third subroutine. this subtoutine use xg,yg(in the first subroutine) and xf,yf(in the second subroutine)*/ { int i,j,k,k1; double normalx[N], normaly[N]; double xfc[N], yfc[N]; double lpanel[N]; double r[N]; double epsilon=2*sqrt(double (2))*dx; double d[nx][ny]; double rmin; double dotproduct, sign; for (i=0;i<N;i++) { xfc[i]=0.5*(xf[i+1]+xf[i]); yfc[i]=0.5*(yf[i+1]+yf[i]); } for (i=0;i<N;i++) { lpanel[i]=sqrt((xf[i+1]-xf[i])*(xf[i+1]-xf[i])+(yf[i+1]-yf[i])*(yf[i+1]-yf[i])); normalx[i]=(yf[i+1]-yf[i])/lpanel[i]; normaly[i]=(xf[i+1]-xf[i])/lpanel[i]; } for (i=0;i<nx;i++) for (j=0;j<ny;j++) { rmin=100; for (k=0;k<N;k++) { r[k]=sqrt((xg[i][j]-xfc[k])*(xg[i][j]-xfc[k])+(yg[i][j]-yfc[k])*(yg[i][j]-yfc[k])); if (r[k]< rmin) { rmin=r[k]; k1=k; } dotproduct=normalx[k1]*(xfc[k1]-xg[i][j])-normaly[k1]*(yfc[k1]-yg[i][j]); sign=dotproduct/abs(dotproduct); d[i][j]=rmin*sign; if (d[i][j]<-epsilon) mask[i][j]=0; else if (abs(d[i][j])<=epsilon) mask[i][j]=0.5*(1+d[i][j]/epsilon+1/pi*sin(pi*d[i][j]/epsilon)); else mask[i][j]=1; } } }/*after this step, mask is calculated, and it is the initial value for next step*/ int main() { int i; for (i=0;i<100;i++) { grid2d(); geometry(); mask2d(); } return 0; }

This post has been edited by **Salem_c**: 17 August 2014 - 07:42 AM

Reason for edit:: added code tags - learn to use them yourself

### #5

## Re: how to make multiple subroutine and return multiple arrays?

Posted 17 August 2014 - 07:09 AM

It is common to use uppercase words etc for constants, and I know mathematically minded folks like to use letters, but it usually helps to be more descriptive. The choice is yours of course.

I'll change x to columns and y to rows. You can check whether you want x to be rows etc as you had, or if it matters.

#define PI 3.14159 #define NCOLS 501 // number of grid in x direction #define NROWS 301 // number of grid in y direction #define NNODES NUMROWS*NUMCOLS // total number of grid nodes #define N 100 //number of panels//

// you have columns first double mask[nx][ny]; // normally it is a row of columns. double mask[NROWS][NCOLS];

To declare a function that takes a 1D array, which can be updated.

void array_func_1D(double mygrid[]); // or void array_func_1D(double mygrid[NNODES]);

To call the array function.

double myarray[NNODES]; array_func_1D(myarray);

For a 2D array the size of the second dimension needs to be declared :

void array_func_2D(double myarray[][NCOLS]); // or void array_func_2D(double myarray[NROWS][NCOLS]);

.

This post has been edited by **#define**: 17 August 2014 - 07:11 AM

### #6

## Re: how to make multiple subroutine and return multiple arrays?

Posted 17 August 2014 - 09:07 AM

Here's a simple example of what I was talking about.

#include <iostream> #include <vector> using namespace::std; void alter_arrays(int ar1[5], int num1, int ar2[3], int num2) { for (int i = 0; i < num1; i++) { ar1[i] = 110+i; } for (int i = 0; i < num2; i++) { ar2[i] = 220+i; } } void show_array(int ar[], int number) { for (int i = 0; i < number; i++) { cout << i << ": " << ar[i] << endl; } } void show_vector(vector<int> &vec) { for (int i = 0; i < vec.size(); i++) { cout << i << ": " << vec[i] << endl; } } void alter_vector(vector<int> &vec) { for (int i = 0; i < vec.size(); i++) { vec[i] = 220+i; } } int main(int argc, const char * argv[]) { const int NUMBER_IN_FIRST = 5, NUMBER_IN_SECOND = 3; int first[NUMBER_IN_FIRST] = {0}, second[NUMBER_IN_SECOND] = {0}; cout << "Arrays\n"; show_array(first, NUMBER_IN_FIRST); cout << endl; show_array(second, NUMBER_IN_SECOND); cout << endl; alter_arrays(first, NUMBER_IN_FIRST, second, NUMBER_IN_SECOND); show_array(first, NUMBER_IN_FIRST); cout << endl; show_array(second, NUMBER_IN_SECOND); cout << endl; cout << "Vector:\n"; vector<int> my_vector(10, 1000); show_vector(my_vector); cout << endl; alter_vector(my_vector); show_vector(my_vector); return 0; }

### #7

## Re: how to make multiple subroutine and return multiple arrays?

Posted 17 August 2014 - 10:55 AM

#include "stdafx.h" #include <iostream> #include <fstream> #include <math.h> #include <ctime> #define PI 3.14159 #define NCOLS 501 // number of grid in x direction #define NROWS 301 // number of grid in y direction #define NNODES NCOLS*NROWS // total number of grid nodes using namespace std; double xmin=-3.5, xmax=1.5, ymin=-1.5, ymax=1.5; /*computational domain*/ double dx=(xmax-xmin)/(NCOLS-1), dy=(ymax-ymin)/(NROWS-1); /*spacing grid*/ double x[NCOLS], y[NROWS]; double xg[NCOLS][NROWS],yg[NCOLS][NROWS]; /*the first subroutine*/ void grid1d(double x[NCOLS],double y[NROWS]); /*the second subroutine*/ void grid2d(double xg[NCOLS][NROWS],double yg[NCOLS][NROWS]); int main() { int i; double x[NCOLS], y[NROWS]; double xg[NCOLS][NROWS],yg[NCOLS][NROWS]; grid1d(x,y); grid2d(xg,yg); return 0; } void grid1d(double x[NCOLS],double y[NROWS]) /* this step make grid in one dimension*/ { int i,j; for (i=0;i<NCOLS;i++) { x[i]=xmin+dx*i; } for (j=0;j<NROWS;j++) { y[j]=ymin+dy*j; } } void grid2d(double xg[NCOLS][NROWS],double yg[NCOLS][NROWS]) /* this step make grid in two dimension*/ { { int i,j; for (i=0;i<NCOLS;i++) for (j=0;j<NROWS;j++) { xg[i][j]=x[i]; yg[i][j]=y[j]; } }

This post has been edited by **JackOfAllTrades**: 18 August 2014 - 03:46 AM

Reason for edit:: Added code tags

### #8

## Re: how to make multiple subroutine and return multiple arrays?

Posted 17 August 2014 - 11:35 AM

**[**CODE

**]**so please use code tags.

Perhaps it would be less confusing, to me anyway, if you used

*NX*instead of

*NCOLS*, and

*NY*instead of

*NROWS*.

You appear to have declared the arrays twice once outside any functions (global space), and inside

*main*. You only need the ones in

*main*.

Was it a compiling error or a runtime error?

If it was a runtime error, you can find out where it was caused by printing messages.

eg

cout << "before grid function." << endl;

You can also print values to check them.

### #9

## Re: how to make multiple subroutine and return multiple arrays?

Posted 17 August 2014 - 12:23 PM

nguyenvanluc, on 17 August 2014 - 10:55 AM, said:

#include "stdafx.h" #include <iostream> #include <fstream> #include <math.h> #include <ctime> #define PI 3.14159 #define NCOLS 501 // number of grid in x direction #define NROWS 301 // number of grid in y direction #define NNODES NCOLS*NROWS // total number of grid nodes using namespace std; double xmin=-3.5, xmax=1.5, ymin=-1.5, ymax=1.5; /*computational domain*/ double dx=(xmax-xmin)/(NCOLS-1), dy=(ymax-ymin)/(NROWS-1); /*spacing grid*/ double x[NCOLS], y[NROWS]; double xg[NCOLS][NROWS],yg[NCOLS][NROWS]; /*the first subroutine*/ void grid1d(double x[NCOLS],double y[NROWS]); /*the second subroutine*/ void grid2d(double xg[NCOLS][NROWS],double yg[NCOLS][NROWS]); int main() { int i; double x[NCOLS], y[NROWS]; double xg[NCOLS][NROWS],yg[NCOLS][NROWS]; grid1d(x,y); grid2d(xg,yg); return 0; } void grid1d(double x[NCOLS],double y[NROWS]) /* this step make grid in one dimension*/ { int i,j; for (i=0;i<NCOLS;i++) { x[i]=xmin+dx*i; } for (j=0;j<NROWS;j++) { y[j]=ymin+dy*j; } } void grid2d(double xg[NCOLS][NROWS],double yg[NCOLS][NROWS]) /* this step make grid in two dimension*/ { { int i,j; for (i=0;i<NCOLS;i++) for (j=0;j<NROWS;j++) { xg[i][j]=x[i]; yg[i][j]=y[j]; } }

### #10

## Re: how to make multiple subroutine and return multiple arrays?

Posted 17 August 2014 - 12:43 PM

### #11

## Re: how to make multiple subroutine and return multiple arrays?

Posted 18 August 2014 - 02:56 AM

This is simple case and error is:" A problem caused the program to stop working correctly"

Please help me. Thank so much.

#include "stdafx.h" #include <iostream> #include <fstream> #include <math.h> #include <ctime> #define NCOLS 501 // number of grid in x direction #define NROWS 301 // number of grid in y direction #define NNODES NCOLS*NROWS // total number of grid nodes #define XMIN -3.5 #define XMAX 1.5 #define YMIN -1.5 #define YMAX 1.5 using namespace::std; void grid2d(double xg[NCOLS][NROWS],double yg[NCOLS][NROWS] ); int main() { int i; double xg[NCOLS][NROWS], yg[NCOLS][NROWS]; grid2d(xg,yg); cout << xg[3][3]; return 0; } void grid2d(double xg[NCOLS][NROWS],double yg[NCOLS][NROWS]) { int i,j; double dx=(XMAX-XMIN)/NCOLS, dy=(YMAX-YMIN)/NROWS; for (i=0;i<NCOLS;i++) for (j=0;j<NROWS;j++) { xg[i][j]=XMIN+dx*i; yg[i][j]=YMIN+dy*j; } }

### #12

## Re: how to make multiple subroutine and return multiple arrays?

Posted 18 August 2014 - 08:27 AM

#define NCOLS 15 #define NROWS 5

You could replace NCOLS with NX and NROWS with NY, for consistency of x and y, usually replace is under the edit menu :

#define NX 15 #define NY 5

### #13

## Re: how to make multiple subroutine and return multiple arrays?

Posted 18 August 2014 - 09:04 AM

The first problem is number of grid. Because my program will run with very high resolution about NX=1000;NY=1000 (even 2000x2000) so with your help, the problem have not yet solved.

The second problem, if i want to make for three arrays, could you please tell me about how to do for three arrays?

Thank you so much, #define.

And this is my program:

#include "stdafx.h" #include <iostream> #include <fstream> #include <math.h> #include <ctime> #define NX 51 // number of grid in x direction #define NY 31 // number of grid in y direction #define NNODES NCOLS*NROWS // total number of grid nodes #define XMIN -3.5 #define XMAX 1.5 #define YMIN -1.5 #define YMAX 1.5 using namespace::std; void grid2d(double xg[NX][NY],double yg[NX][NY] ); int main() { int i; double xg[NX][NY], yg[NX][NY]; double mask[NX][NY]; grid2d(xg,yg,mask); cout << xg[3][3]; return 0; } void grid2d(double xg[NX][NY],double yg[NX][NY],double mask[NX][NY]) { int i,j; double dx=(XMAX-XMIN)/NX, dy=(YMAX-YMIN)/NY; for (i=0;i<NX;i++) for (j=0;j<NY;j++) { xg[i][j]=XMIN+dx*i; yg[i][j]=YMIN+dy*j; mask[i][j]=xg[i][j]+yg[i][j]; } }

### #14

## Re: how to make multiple subroutine and return multiple arrays?

Posted 18 August 2014 - 09:46 AM

Making them global is easier, so put them in global space, but have a different name from the parameters and names in the functions, for reasons of sanity.

You've defined NNODES with NCOLS and NROWS.

The

*float*data type is smaller than the

*double*data type.

### #15

## Re: how to make multiple subroutine and return multiple arrays?

Posted 18 August 2014 - 10:35 AM

Could you please give me example?

Thank you so much.