int main()
{

         double t[]={15.70, 29.39, 41.14, 56.47, 75.61, 98.83 ,112.42, 125.61, 129.39, 133.45,138.94, 141.41, 143.67, 144.63, 144.95, 145.16, 146.25, 146.70, 147.26 , 148.15, 152.40};

	int n = sizeof (t)/sizeof (t[0]);
         FindLN(t, n);
	return 0;
}

void  FindLN(double t[], int n)
{
	int index;
	double lnt[n];

	for (index=0; index<n; index++)
	{
		lnt[index]=log(t[index]);
		lnnum[index]=log(num[index]);
	}

}

I get a few errors but Im quite certain they are all related

Errors

expected constant expression at double lnt[n];
cannot allocate array size 0 and lnt unknown size at this line


HELP Please

I forgot to include a few other parts of the code in order to simplify. I just want to know if it is possible to pass that value of n to the function as an array size?

It is possible to pass that value 'n' into the function.

What is not possible in C++ at the current version is to create an array like that (i.e. an array whose size is not known at compile time) on the stack. You will have to allocate it dynamically using 'new', or use a container from the standard library.

"expected constant expression at double lnt[n];" - this error means that the compiler won't deal with the variable 'n' as an array size for an array on the stack.

Edited 5 Years Ago by Moschops: n/a

You could do that with an template function.

template <int size> Function ()  { 
   double array[size];
   //...
 }
Function <10> (); // call

Though you should try using dynamic arrays instead.

Count your number of items in array and set a constant.

const int SIZE =21;

double t[SIZE]={...,...,...,...};

Then just pass SIZE to the function if you set it locally or just use SIZE if you declare it globally.

This article has been dead for over six months. Start a new discussion instead.