silverdace
Programmer
Hi I am trying to learn C as part of my research. I need to return two arrays from a function. How do I take arrays as arguments and then let the function operate on those arrays and then return the modified arrays?
Here is the function code :
#include <math.h>
#include <stdlib.h>
#include <stdio.h>
#include "Head.h"
void gaussleg128(double X1, double X2, double X[], double W[], int N)
{
double Z1, Z, XM, XL, PP, P1, P2, P3;
int I, J, M;
M = ((N + 1) / 2) - ((N + 1) % 2);
XM = 0.5 * (X1 + X2);
XL = 0.5 * (X2 - X1);
for (I = 1; I <= M; I++) {
Z = cos((PI * (I - 0.25)) / (N + 0.5));
do { P1 = 1.0;
P2 = 0.0;
for (J = 1; J <= N; ++J)
{
P3 = P2;
P2 = P1;
P1 = ((2.0 * J - 1.0) * Z * P2 - (J - 1.0) * P3) / J;
}
PP = N * (Z * P1 - P2) / (pow(Z, 2.0) - 1.0);
Z1 = Z;
Z = Z1 - P1 / PP;
} while (fabs(Z - Z1) <= EPS);
X = XM - XL * Z;
X[N + 1 - I] = XM + XL * Z;
W = 2.0 * XL / ((1.0 - pow(Z, 2.0)) * pow(PP, 2.0));
W[N + 1 - I] = W;
}
}
And here is the "main" code.
/* 3D Scattering */
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "Head.h"
int main ()
{
extern double abs128[128], wt128[128], dist[9];
double radius, dmf, mass, X, pmin1, pmin3, recmass1, recmass3, W1, W2, moments[9][9], norm1, norm3;
int k, I, t, k2, intval[9];
for (t = 0; t <= 128; t++)
abs128[t] = 0.0;
wt128[t] = 0.0;
gaussleg128(-1.0, 1.0, abs128, wt128, 128);
plus rest of prog.............
Here is the function code :
#include <math.h>
#include <stdlib.h>
#include <stdio.h>
#include "Head.h"
void gaussleg128(double X1, double X2, double X[], double W[], int N)
{
double Z1, Z, XM, XL, PP, P1, P2, P3;
int I, J, M;
M = ((N + 1) / 2) - ((N + 1) % 2);
XM = 0.5 * (X1 + X2);
XL = 0.5 * (X2 - X1);
for (I = 1; I <= M; I++) {
Z = cos((PI * (I - 0.25)) / (N + 0.5));
do { P1 = 1.0;
P2 = 0.0;
for (J = 1; J <= N; ++J)
{
P3 = P2;
P2 = P1;
P1 = ((2.0 * J - 1.0) * Z * P2 - (J - 1.0) * P3) / J;
}
PP = N * (Z * P1 - P2) / (pow(Z, 2.0) - 1.0);
Z1 = Z;
Z = Z1 - P1 / PP;
} while (fabs(Z - Z1) <= EPS);
X = XM - XL * Z;
X[N + 1 - I] = XM + XL * Z;
W = 2.0 * XL / ((1.0 - pow(Z, 2.0)) * pow(PP, 2.0));
W[N + 1 - I] = W;
}
}
And here is the "main" code.
/* 3D Scattering */
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "Head.h"
int main ()
{
extern double abs128[128], wt128[128], dist[9];
double radius, dmf, mass, X, pmin1, pmin3, recmass1, recmass3, W1, W2, moments[9][9], norm1, norm3;
int k, I, t, k2, intval[9];
for (t = 0; t <= 128; t++)
abs128[t] = 0.0;
wt128[t] = 0.0;
gaussleg128(-1.0, 1.0, abs128, wt128, 128);
plus rest of prog.............