954,504 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Function headers, arguments and storing results to arrays

Hi there,

I'm trying to write a program that calculates the probability distribution for different initial conditions of a population model. The probability is calculated over a for loop over the parameter 'q' (which can be considered as a scaled version of the correcting "struggle for life" term in the Malthus model).

The program includes methods for normalising the resulting probability distributions, and to test if they are working the program (in a seperate source file) performs a numerical integration (according to a Gauss-Laguerre method which basically picks out a number of, say 8, particular values of q and assigns them weights).

This function is:

double norm(double pst, double y)
{
double ab [] = {0.170, 0.904, 2.251, 4.267, 7.046, 10.759, 15.741, 22.863};
double w [] = {3.69E-01, 4.19E-01, 1.76E-01, 3.33E-02, 2.79E-03, 9.08E-05, 8.49E-07, 1.05E-09};
double v [8];

for (int i=0; i<8; i++)
{
if (abs(ab[i]-y)<0.0001)
{
v[i] = w[i]*pst;
return v[i];
}
else
{
return 0;
}
}


where pst is the probability for a particular value of q.

In the main source file, I put the function header as:

double norm(double, double);


Is that correct?

Also I call the function within the for loop:

for (double y =0.001; y<30.00; y+=0.001)
{
double pst = n*pow(y,n1)*exp(-y);
double q[] = norm(pst, y);
}


The idea being that for the 8 values of 'q' corresponding to those needed by the Gauss-Laguerre method, the norm function will return a number which I want to store in an array of 8 in the program.

The main problem is the error:

error C2064: term does not evaluate to a function taking 2 arguments


The second problem is that I don't want the array to be overwritten each time the function finds a matching value of 'q'...any ideas on how to store matches within the for loop?
It doesn't matter whether I need to declare the array size or not as there are always 8 matches.

Right...think that's it. I know this is relatively simple from a programming point of view, so I am posting this in the beginners forum. Any help would be much appreciated!

Thanks.

jibber87
Newbie Poster
2 posts since Mar 2009
Reputation Points: 10
Solved Threads: 0
 

There's not enough code to reproduce your error. Please write a complete sample program that has the error and then post it.

>double q[] = norm(pst, y);
This in particular is wrong. norm returns a double, not an array of double.

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

Yeah, you're right...I had tried with and without the square brackets there.
I think I have now found the problem (which, again, wasn't actually listed in the code I posted)....such a stupid mistake.
See if you can spot it!

#include <fstream>
#include <iostream>
#include <cmath>
#include "norm.h"
using namespace std;

double sumarray(double arg[], int length) 
{
    double a = 0.0;
	for (int n=0; n<length; n++)
	{
    a += arg[n];
	}
	return a;
}

int main(void)
{
ofstream outfile("pst.txt");

double alpha = 0.01;
double sigma = 1.0;
double rabs = (2*alpha)/(sigma*sigma);
double n1 = rabs-1;
double r = -rabs;
double gamma = 49.44;
double norm = pow(2,r)/gamma;
double a = (sigma*sigma)/2;
double b = pow(a, n1);
double n = norm*b;
double s;

for (float i =1; i<30000; i++)
{
	double y = i/1000;
	double pst = n*pow(y,n1)*exp(-y);
	double z = norm(pst, y);
	if (z!=0)
	{
	outfile << y << "\t" << z << endl;
	}
}
}


So the problem was that I had a variable with the same name as the function. Wow.
Also it didn't seem to like the looping in the norm function itself, which I believe is shown in my first post.
Any ideas why?

Thanks.

jibber87
Newbie Poster
2 posts since Mar 2009
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You