Arrays and Functions

Can an entire array be an argument of a function? And can a function return an entire array? Or do I have to pass and return individual elements of the array?

I am trying to do some vector algebra as follows:

[code]
// function to calculate cross product of two vectors
double crossproduct(double vector1[3],double vector2[3])
{
//initialize the cross product vector
double Xproduct[3]={0.0,0.0,0.0};
//calculate the i,j,k coefficients of the cross product
Xproduct[0]=vector1[1]*vector2[2]-vector2[1]*vector1[2];
Xproduct[1]=vector1[0]*vector2[2]-vector2[0]*vector1[2];
Xproduct[2]=vector1[0]*vector2[1]-vector2[0]*vector1[1];
return Xproduct[ ];
}
[code]

The compiler doesn't like the return statement
1
2
3
4
5
double *crossproduct(double vector1[],double vector2[], const int lengh)
{
/*...*/
   return Xproduct;
}


you are not allowed to return a pointer or reference to an automatic local variable well you are but thats not what you want(try it yourself) in short its because it doesn't exist anymore after the function returns so you have a pointer\ reference to an address you don't really care about. So Xproduct should be something like this;
1
2
3
4
5
double *Xproduct = new double[lengh]; // allocate it on a new memory
             /* you could initialize it first 
                  for(int i = 0; i < lengh; i++)
                        Xproduct[i] = 0.0;    */
             Xproduct[0] = vector1[1]*vector2[2]-vector2[1]*vector1[2];

now you can do your algebra and return it.
oh and in main() you also want a pointer
 
double *result = crossproduct(v1[], v2[], lengh);


you might wanna check this:
http://www.cplusplus.com/doc/tutorial/arrays.html
Jeff
Last edited on
so do I need to assign the array with the results of my calculations to an array declared in the main program?
you assign the results to a pointer in whatever scope you called your function it doesn't have to be main(), i assumed you were calling it from main thats why i mentioned it:

you can use this pointer like an array with the index operator[] :
1
2
3
4
5
6
7
8
9
10
11
int main()
{
const int lengh = 3;
double *result = crossproduct(v1[], v2[], lengh);
for(int i = 0; i < lengh; i++)
     cout << result[i] << '\n';

result[0] = 2.2;
//etc...
     return 0;
}
Last edited on
Just to close this thread out, lets see if I can answer my own questions.

Can an entire array be an argument of a function?

Yes but as it turns out you are actually passing the address of the array. I finally listened to what my compiler was telling me. (Yes I am spending too much time with it, now its speaks to me) Notice that the type of argument for the array in the function prototypes is "double*"

can a function return an entire array?

Not really. (1) ISO C++ forbids assignment of arrays, (2) My compiler said she couldn't convert double to double[3], and (3) even if you make your function "double*" all you get as Jeff pointed out is the address of an automatic variable that is ephemeral.

the program below has two ways for doing scalar multiplication of a vector


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
//scalar multiplication of a vector

#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;
void scalar1(double*, int, double, double*); //function using pointer
double scalar2(double*, int, double, int); //function returns a vector

int main (int nNumberofArg, char* pszArgs[])
{
 	double vector[3]={3.0,4.0,5.0};
 	double result1[3]={0.0,0.0,0.0};
 	double result2[3]={0.0,0.0,0.0};
 	int lengh=3;
 	double scalar=1.1;

 //function passes address of where result goes 
	
 scalar1(vector, lengh, scalar, &result1[0]); 

 for (int index=0; index<lengh; index++)
                {			  
 	result2[index]=scalar2(vector, lengh, scalar, index);
              // function returns one component at a time
	} 						 	
 	for (int i=0; i<lengh; i++)
 	{
 	cout << "using a pointer the values are  ";
	cout << result1[i] << endl;
                }
    for (int i=0; i<lengh; i++)
 	{
 	cout << "returning  elements array. the values are  ";
    cout << result2[i] << endl;
	}
	system("PAUSE");
return 0;
}
// this function uses a pointer
void scalar1(double d_array[], int lengh , double d_scalar, double* p_result)
	 {
	 double* ptr=p_result;
	 double d_scaled_vector[lengh];
	 for (int i=0; i<lengh; i++)
	 	 {
         d_scaled_vector[i]=d_scalar*d_array[i];  // scalar muliplication
		 }
	 for (int i=0; i<lengh; i++)
	 	 {
         *(ptr+i)=d_scaled_vector[i]; //ships results out to global array
		 }
	 return ;
	 }		
	 
double scalar2(double d_array[], int lengh , double d_scalar, int counter)
  {
  double d_scaled_vector[lengh];
  d_scaled_vector[counter]=d_scalar*d_array[counter];   // scalar muliplication
  return d_scaled_vector[counter];  //returns one component at a time for
  }	                                // for assignment to global array   		 	
 	
Last edited on
Topic archived. No new replies allowed.