Help Quick with Power Series please

I need to compute a power series and I'm not sure whats wrong!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
  /*	Write a funtion which takes an array of coefficients,ai and an integer n, and returns
	the value of the following equation. Also write a code fragment to show how to call your 
	function.
	y=a0+a1x+a2x^2+a3x^3+...anx^n
*/

#include <iostream>
#include <cmath>

using namespace std;

// define power function 
	double series(int n, double ai[n], double x);


int main(void) 
{ 
	double x;
	int n;
	double ai;

	{
		cout<<"Please enter a group of numbers"<< endl;
		cin>>ai[n];
		
		cout<<"enter an x value"<< endl;
		cin>>x;
		y=series(ai[n],n,x);
		
	}

	
}

	double series(int n, double ai[], double x)
{
	double y=0;
	for (int i=n; i>=0;i--)
	{
		y=y+ai[i]*pow(x,i);
	}
	return y;
}
	
Last edited on
Well, a few things. For one, 'ai' is a variable of type double, not an array of elements 'n'. Two, look at line 28. You have ai[n] and n backwards.
Topic archived. No new replies allowed.