Using a function input to define the length of an array

Hi,

I need to create a function that will evaluate a polynomial at a point x, with the inputs of the function being the coefficients of the polynomial, the order n, and a scalar value of x.

I was given an example problem with a third order polynomial, and so when I wrote the code and the function I defined my arrays as P[4], and everything works fine. However, when I try to replace the 4's with a variable (so P[n+1] rather than P[4]), as it is supposed to work with any order polynomial, visual studio gives me errors like "'P' : unknown size" for all of my arrays.

If anyone could shed some light on what's going wrong, it would be greatly appreciated.

Thanks!
Frank
Just thought I'd include my main function so it is easier to see what I am talking about.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <cmath>
using namespace std;

float polyval(int n, float* p, float x);
int main(void)
{
	int n;
	cout << "Enter polynomial order: ";
	cin >> n;
	float P[n+1], x;
	cout << "Enter coefficients: ";
	for (int i=0;i<n+1;i++) cin >> P[i];
	cout << "Enter a value of x: ";
	cin >> x;
	cout << "The polynomial has value p(x)= " << polyval(n,P,x) << endl;
	return 0;
}
I figured it out, I had to create the array using "new"
Topic archived. No new replies allowed.