Hi all,
I'm trying to figure out how I can call the function estimate from the main. It takes as input:

float data[], int n, int m;

and should output the float *xms and the float array float d[]:

float *xms, float d[]

I don't know what to write to achieve this. Even if this code is wrong, could someone please show me how I should go about calling such a function, i.e. what to write in the main to retrieve *xms and float d[] given an input data[], int n, int m.
Thanks,
Ger.

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "nrutil.h"

void estimate(float data[], int n, int m, float *xms, float d[])

int main(void)
{
double data[] =
{
//Loads(~1000) of data vavues between +1 and -1. Ex: -0.15709, -0.25413,...
};
float xms[m];
float float d[m];

for (i=0;i<m;i++)
{
//Function call here to fill xms[] and d[]???
}

void estimate(float data[], int n, int m, float *xms, float d[])
{
int k,j,i,m=20;
float p=0.0, *wk1,*wk2,*wkm;
int n = sizeof(data) / sizeof(data[0]);

wk1 = vector(1,n);
wk2 = vector(1,n);
wkm = vector(1,m);

for(j=1;j<=n;j++)
{
p += data[j]*data[j];
}

*xms = p/n;

wk1[1] = data[1];
wk2[n-1] = data[n];

for (j=2;j<=n-1;j++)
{
wk1[j] = data[j];
wk2[j-1] = data[j];
}

for (k=1;k<=m;k++)
{

float num=0.0,denom=0.0;
for (j=1;j<=(n-k);j++)
{
num += wk1[j]*wk2[j];
denom += wk1[j]*wk1[j] + wk2[j]*wk2[j];
}

d[k]=2.0*num/denom;

xms *= (1.0- d[k]*d[k]);

if (k ==m)
{
free_vector(wkm,1,m);
free_vector(wk2,1,n);
free_vector(wk2,1,n);

return;
}

for (i=1;i<=k;i++) wkm[i]=d[i];

for (j=1;j<=(n-k-1);j++)
{
wk1[j] -= wkm[k]*wk2[j];
wk2[j] = wk2[j+1]-wkm[k]*wk1[j+1];
}
}
}

>>I don't know what to write to achieve this
That's not the only thing you don't know how to write. Your program is littered with tons of errors. Don't try to write that much code without compiling it firs, and fix all the errors so that the compiler does not find any errors before writing more code. It might be a lot easier to just start all over again and write only a few lines of code at a time because the code you have written is going to be difficult for you to correct.

Examples: lines 14 and 15 won't compile because m was never declared. And function main() is missing braces { and }.

Okey dokey, here's a more refined version. I'm getting just 2 errors, a missing ";" before the main and an "unexpected end of file" at the main definition. I can't see why this is happening. I know there's a lot of gobbeldy gook there, but my problem is that I don't know how to call a function involving pointers and arrays. Can you help me out in this respect?
Thanks,
Ger.

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define MAX_DATA 1001
#define ORDER 20

double data[] = 
{
//Loads(~1000) of data vavues between +1 and -1. Ex: -0.15709, -0.25413,...
};

int n = MAX_DATA;
int m = ORDER;

void estimate(float data[], int n, int m, float *xms, float d[])

int main(void)
{
	for (i=0;i<m;i++)
	{
		//Function call here to fill d[] and calculate xms[]???
	}

	return EXIT_SUCCESS;
}

void estimate(float data[], int n, int m, float *xms, float d[])
{
	int k,j,i;
	float p=0.0, *wk1,*wk2,*wkm;

	wk1 = vector(1,n);
	wk2 = vector(1,n);
	wkm = vector(1,m);

	for(j=1;j<=n;j++)
	{
		p += data[j]*data[j];
	}

	*xms = p/n;

	wk1[1] = data[1];
	wk2[n-1] = data[n];

	for (j=2;j<=n-1;j++)
	{
		wk1[j] = data[j];
		wk2[j-1] = data[j];
	}
	for (k=1;k<=m;k++)
	{
		float num=0.0,denom=0.0;
		for (j=1;j<=(n-k);j++)
		{
			num += wk1[j]*wk2[j];
			denom += wk1[j]*wk1[j] + wk2[j]*wk2[j];
		}
		d[k]=2.0*num/denom;
		xms *= (1.0- d[k]*d[k]);

		if (k ==m)
		{
			free_vector(wkm,1,m);
			free_vector(wk2,1,n);
			free_vector(wk2,1,n);
			return;
		}
		for (i=1;i<=k;i++) wkm[i]=d[i];

		for (j=1;j<=(n-k-1);j++)
		{
			wk1[j] -= wkm[k]*wk2[j];
			wk2[j] = wk2[j+1]-wkm[k]*wk1[j+1];
		}
	}
}

>> I'm getting just 2 errors, a missing ";" before the main
Look at that line just before main(). Don't you see anything wrong with it ????

>>"unexpected end of file" at the main definition
You have a ton of errors in that program, other than the problem ^^^ above. Fix above problem, recompile and you will get millions of other errors. Fix only the first one, recompile then repeat until all problems are resolved.

For example:

double data[] = 
{
//Loads(~1000) of data vavues between +1 and -1. Ex: -0.15709, -0.25413,...
};

You can't leave that like that. The array must have at least 1 element in it. If you don't know how many to allocate then just make up something for now double data[1000] = {0};

>> Don't you see anything wrong with it ????
I wasn't sure whether or not I needed a semi-colon after the function declaration, obviously I do!
>> Fix only the first one, recompile then repeat until all problems are resolved.
Did that and fixed the 7 errors.

>>For example:

double data[] = 
{
//Loads(~1000) of data vavues between +1 and -1. Ex: -0.15709, -0.25413,...
};

>>You can't leave that like that. The array must have at least 1 element in it. If you don't >>know how many to allocate then just make up something for now

I was aware that the program wouldn't compile without any data values to process, I just inserted a comment here to illustrate the fact that I was using 1000 data values as opposed to inserting them in the never ending post! :) I've shortened the array and inserted 20 values this time. Here's the code, it compiles fine for me with just two warnings about "posible loss of data" from converting doubles to floats which I'm not concerned about. I've also included the header file "nrutil.h". Can you please help me with the function call?

Header file "nritil.h"

#ifndef _NR_UTILS_H_
#define _NR_UTILS_H_

static float sqrarg;
#define SQR(a) ((sqrarg=(a)) == 0.0 ? 0.0 : sqrarg*sqrarg)

static double dsqrarg;
#define DSQR(a) ((dsqrarg=(a)) == 0.0 ? 0.0 : dsqrarg*dsqrarg)

static double dmaxarg1,dmaxarg2;
#define DMAX(a,b) (dmaxarg1=(a),dmaxarg2=(b),(dmaxarg1) > (dmaxarg2) ?\
        (dmaxarg1) : (dmaxarg2))

static double dminarg1,dminarg2;
#define DMIN(a,b) (dminarg1=(a),dminarg2=(b),(dminarg1) < (dminarg2) ?\
        (dminarg1) : (dminarg2))

static float maxarg1,maxarg2;
#define FMAX(a,b) (maxarg1=(a),maxarg2=(b),(maxarg1) > (maxarg2) ?\
        (maxarg1) : (maxarg2))

static float minarg1,minarg2;
#define FMIN(a,b) (minarg1=(a),minarg2=(b),(minarg1) < (minarg2) ?\
        (minarg1) : (minarg2))

static long lmaxarg1,lmaxarg2;
#define LMAX(a,b) (lmaxarg1=(a),lmaxarg2=(b),(lmaxarg1) > (lmaxarg2) ?\
        (lmaxarg1) : (lmaxarg2))

static long lminarg1,lminarg2;
#define LMIN(a,b) (lminarg1=(a),lminarg2=(b),(lminarg1) < (lminarg2) ?\
        (lminarg1) : (lminarg2))

static int imaxarg1,imaxarg2;
#define IMAX(a,b) (imaxarg1=(a),imaxarg2=(b),(imaxarg1) > (imaxarg2) ?\
        (imaxarg1) : (imaxarg2))

static int iminarg1,iminarg2;
#define IMIN(a,b) (iminarg1=(a),iminarg2=(b),(iminarg1) < (iminarg2) ?\
        (iminarg1) : (iminarg2))

#define SIGN(a,b) ((b) >= 0.0 ? fabs(a) : -fabs(a))

void nrerror(char error_text[]);
float *vector(long nl, long nh);
int *ivector(long nl, long nh);
unsigned char *cvector(long nl, long nh);
unsigned long *lvector(long nl, long nh);
double *dvector(long nl, long nh);
float **matrix(long nrl, long nrh, long ncl, long nch);
double **dmatrix(long nrl, long nrh, long ncl, long nch);
int **imatrix(long nrl, long nrh, long ncl, long nch);
float **submatrix(float **a, long oldrl, long oldrh, long oldcl, long oldch,
	long newrl, long newcl);
float **convert_matrix(float *a, long nrl, long nrh, long ncl, long nch);
float ***f3tensor(long nrl, long nrh, long ncl, long nch, long ndl, long ndh);
void free_vector(float *v, long nl, long nh);
void free_ivector(int *v, long nl, long nh);
void free_cvector(unsigned char *v, long nl, long nh);
void free_lvector(unsigned long *v, long nl, long nh);
void free_dvector(double *v, long nl, long nh);
void free_matrix(float **m, long nrl, long nrh, long ncl, long nch);
void free_dmatrix(double **m, long nrl, long nrh, long ncl, long nch);
void free_imatrix(int **m, long nrl, long nrh, long ncl, long nch);
void free_submatrix(float **b, long nrl, long nrh, long ncl, long nch);
void free_convert_matrix(float **b, long nrl, long nrh, long ncl, long nch);
void free_f3tensor(float ***t, long nrl, long nrh, long ncl, long nch,
	long ndl, long ndh);

#endif /* _NR_UTILS_H_ */

Source file:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "nrutil.h"
#define MAX_DATA 20
#define ORDER 20

double data[] = 
{
-0.15709,-0.25413,-0.1545,-0.011505,-0.052141,-0.24125,-0.28251,0.020464,0.48291,0.69804,0.46961,0.019997,
-0.26036,-0.22194,-0.059284,-0.027764,-0.14373,-0.21843,-0.14153,-0.030971
};

int n = MAX_DATA;
int m = ORDER;
float xms;
float d[ORDER];

void estimate(float data[], int n, int m, float *xms, float d[]);

int main(void)
{
	

	for (int i=0;i<m;i++)
	{
		//Function call here to fill d[] and calculate xms[]???
	}

	return EXIT_SUCCESS;
}

void estimate(float data[], int n, int m, float *xms, float d[])
{
	int k,j,i;
	float p=0.0, *wk1,*wk2,*wkm;

	wk1 = vector(1,n);
	wk2 = vector(1,n);
	wkm = vector(1,m);

	for(j=1;j<=n;j++)
	{
		p += data[j]*data[j];
	}

	*xms = p/n;

	wk1[1] = data[1];
	wk2[n-1] = data[n];

	for (j=2;j<=n-1;j++)
	{
		wk1[j] = data[j];
		wk2[j-1] = data[j];
	}
	for (k=1;k<=m;k++)
	{
		float num=0.0,denom=0.0;
		for (j=1;j<=(n-k);j++)
		{
			num += wk1[j]*wk2[j];
			denom += wk1[j]*wk1[j] + wk2[j]*wk2[j];
		}
		d[k]=2.0*num/denom;
		*xms *= (1.0- d[k]*d[k]);

		if (k ==m)
		{
			free_vector(wkm,1,m);
			free_vector(wk2,1,n);
			free_vector(wk2,1,n);
			return;
		}
		for (i=1;i<=k;i++) wkm[i]=d[i];

		for (j=1;j<=(n-k-1);j++)
		{
			wk1[j] -= wkm[k]*wk2[j];
			wk2[j] = wk2[j+1]-wkm[k]*wk1[j+1];
		}
	}
}

>>Can you please help me with the function call?
What about it that you don't understand ? All you have to do is pass the variable names estimate(data, n, m, &xms,d);

So if I write:

int main(void)
{
    estimate(data, n, m, &xms,d);

    printf("The elements of d[] are: ");
    for (i=0;i<m;i++)
    {
        printf("\n%f, d[i]");
    }
    printf("\nMean squared error is: %f, xms");

    return EXIT_SUCCESS;
}

Then I should see the values for xms and the 20 d values? The reason I'm asking as opposed to trying it out is that I realized I needed another file, "nrutil.c" to use the function. It's taken from the online book "Numerical Recipes in C", and they also provide the nrutil.h and nrutil.c files, but when I tried to build the solution, I got errors about the number of parameters'vector' and 'free_vector' take. A bit strange seeing as all of that code is directly from the book and the header and source files. But my current issue is calling the function. Is the above correct?
Ger.

>> Is the above correct?
Looks ok to me, assuming you have declared all those variables correctly.

>>It's taken from the online book "Numerical Recipes in C
Don't make the mistake of assuming all books are 100% correct. They aren't, I have found lots of misprints in them. Check the author's or the printer's web site to see if they have corrections, most have corrected code that you can download.

I haven't got too much experience with C, so I wasn't sure how to call the function. It's good to know even if I don't get this to work. I did a search for contact info of the authors of the book, and discovered that there's is a whole forum for it! The authors give answers to questions, but probably not too frequently. I posted my question there anyways. Thanks once again for your help AD! All the best..

So if I write:

No estimate(data, n, m, &xms,d); data, n, m, d have no value for (i=0;i<m;i++) m has no value printf("\n%f, d[i]"); Improper syntax. Try printf("\n%f", d[i]); instead.

n, m and data do have values:

#define MAX_DATA 20
#define ORDER 20

double data[] =
{
-0.15709,-0.25413,-0.1545,-0.011505,-0.052141,-0.24125,-0.28251,0.020464,0.48291,0.69804,0.46961,0.019997,
-0.26036,-0.22194,-0.059284,-0.027764,-0.14373,-0.21843,-0.14153,-0.030971
};

int n = MAX_DATA;
int m = ORDER;
float xms;
float d[ORDER];

xms and d[] don't have values, but they are the values I want the function to calculate..

just because you include the header file doesn't mean you're getting the code. the errors you described earlier sound like you're not linking properly.

the solution depends on your compiler, and how their source code is provided... whether it's in a .dll or a .lib or whatever

I figured it out (well, one of my more experienced classmates did!). I was compiling the project in Visual C++, where I had the source files in .cpp files. Usually this isn't a problem and C code compiles fine in .cpp source files, but the functions vector and free_vector are apparently part of a C++ library. So when I tried to call them, the compiler used the C++ function instead of the one defined in "nrutil.c". It all works fine now and I am one happy camper!! :) The function call worked like you said Ancient Dragon.
Thanks all!

>>Usually this isn't a problem and C code compiles fine in .cpp source files
No it doesn't. There are many c constructs that will not compile as *.cpp file. If yours did then you were just lucky. Safest way is to always code C programs in *.c file.

Well for the basic C that I've written, I've never had this problem before. Live and learn!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.