Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Returning an Array from a Function 1

Status
Not open for further replies.

Charliec2uk

Programmer
Jan 21, 2005
6
GB
I have a created a dyamic array:

double *torque_array = new double [range];

where range is a value passed from the Gui.

Am trying to then trying to get a function to put values in this a array, (about 1500 values) by passing it as a pointer. How ever this doesn't work. I would be really grateful for some assitance on this. If possible a basic example would be so helpful.

Many thanks
 
Here's a simple example:
[tt]
void insertvalues(double*arr)
{
for(int n=0;n<range;n++)
{
*arr=values[n];
arr++;
}
}

...

insertvalues(torque_array);
[/tt]
 
Sorry to bother, but just to be sure the, arr++ command increments to pointer? And this is going to sound absurd, but what does the values[n] command return?
 
Sorry again. If I post the code. This function evaluates a couple of integrals. What I want it do, and what it refuses to do, is populate a dynamic array defined the above post.

void DriveTorque(int p_initial, int gamma1, int gamma2, double out_radius, double in_radius, int resolution, int p_limit,double h, double r_dash, double capacitance, int range, double &torque_output)
{
double temp = 0;
double a,b,c,d;
double upper_limit, lower_limit;
int index = 0;

gamma1 = (gamma1/360)*(2*pi);
gamma2 = (gamma2/360)*(2*pi);
p_initial = (p_initial/360)*(2*pi);
p_limit = (p_limit/360)*(2*pi);

temp = p_initial - p_limit;

double *n_vector = new double [temp];
double *torque = new double [range];

n_vector[1] = (1/resolution);
for (index = 2; index = sizeof(n_vector); index++)
n_vector[index] = (1/resolution)+n_vector[index-1];

//n_vector = p_initial: 1/(resolution): p_limit;
//[g h] = size(n_vector) ;
//if range == 0
// range = h;

for (index = 1; index <= range ; index++)
upper_limit = gamma2-n_vector[index];
lower_limit = gamma1-n_vector[index];
a = capacitance*((r_dash / pow(cos(upper_limit),2))*log((r_dash/cos(upper_limit))-out_radius)+(out_radius/cos(upper_limit)));
b = capacitance*((r_dash / pow(cos(upper_limit),2))*log((r_dash/cos(upper_limit))-in_radius)+(in_radius/cos(upper_limit)));
c = capacitance*((r_dash / pow(cos(lower_limit),2))*log((r_dash/cos(lower_limit))-out_radius)+(out_radius/cos(lower_limit)));
d = capacitance*((r_dash / pow(cos(lower_limit),2))*log((r_dash/cos(lower_limit))-in_radius)+(in_radius/cos(lower_limit)));
temp = a-b-c+d;
*torque_output = temp;
torque_output++;

// phase_angle[index] = n_vector[index]-((gamma2+gamma1)/2);
// phase_angle[index] = (phase_angle(index)/(2*pi))*360;


//torque_output = torque;
}

At the moment, when I try and assign the point to temp, the compiler complains of an 'illegal use of floating point'
 
Just to answer the questions in your first reply:

"arr" is a pointer to torque_array, which is passed to the function; each time a value has been inserted into the array, arr is incremented to point to the next available space in the array.

The previous example assumes the values are being transferred from an existing array called "values", but you would of course substitute that with your own logic. For instance, you might use instead:
[tt]
void insertvalues(double*arr)
{
double d;
while(getvalue(&d))
{
*arr=d;
arr++;
}
}

...

insertvalues(torque_array);
[/tt]
where "getvalue" is a function which supplies the next available value, and returns true for success or false for failure.


I haven't examined your code yet, but there's one thing I noticed: are all the indented lines following the second "for" statement supposed to be part of the "for" loop? If they are, then they need to be enclosed in curly brackets.
 
Now I'm not an engineer, and I don't know what the function is supposed to do, but I noticed what seemed to be some mistakes in the code, so I rewrote the function as follows (in my own style; sorry if it doesn't match yours):
[tt]
void DriveTorque
(
int p_initial,int gamma1,int gamma2,double out_radius,
double in_radius,int resolution,int p_limit,double h,
double r_dash,double capacitance,int range,
double*torque_output
)
{
gamma1 = gamma1/360.0*2*pi;
gamma2 = gamma2/360.0*2*pi;
p_initial = p_initial/360.0*2*pi;
p_limit = p_limit/360.0*2*pi;
double*n_vector=new double[p_initial-p_limit];
n_vector[1] = 1.0/resolution;
for(int index=2;index<p_initial-p_limit;index++)
n_vector[index]=(1.0/resolution)+n_vector[index-1];
for(int index=1;index<=range;index++)
{
double
upper_limit=gamma2-n_vector[index],
lower_limit=gamma1-n_vector[index];
*torque_output=
capacitance
*
(
(
r_dash
/pow(cos(upper_limit),2)
*log((r_dash/cos(upper_limit))-out_radius)
+(out_radius/cos(upper_limit))
)
-
(
r_dash
/pow(cos(upper_limit),2)
*log((r_dash/cos(upper_limit))-in_radius)
+(in_radius/cos(upper_limit))
)
-
(
r_dash
/pow(cos(lower_limit),2)
*log((r_dash/cos(lower_limit))-out_radius)
+(out_radius/cos(lower_limit))
)
+
(
r_dash
/pow(cos(lower_limit),2)
*log((r_dash/cos(lower_limit))-in_radius)
+(in_radius/cos(lower_limit))
)
);
torque_output++;
}
delete[]n_vector;
}
[/tt]
The main corrections were as follows:
- When you divide two integers, the result is truncated to an integer, so gamma1/360, gamma2/360, p_limit/360, p_initial/360, and 1/resolution would give wrong results. When 360.0 and 1.0 are used, floating-point division happens, and precision is retained.
- The first "for" loop was an endless loop, since you were assigning sizeof(n_vector) to "index" at each iteration. Also, sizeof(n_vector) always equals 4, the size of a pointer.
- The torque_output argument was declared as a reference but used like a pointer.
- The arrays n_vector[] and torque[] were not freed in the function, which would result in memory leakage.
- The array torque[] was created but never used.

I haven't tested this new code, so you would probably have more changes to make.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top