Power series help!

Whats wrong with my code?! I need to be able to enter the N but im not sure how to do it and make my function work properly.




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
#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;
}
Your code is messed up. If you want to let the user enter the size of an array, then you need to allocate it in the heap otherwise the size of an array must be const. Second, double ai; you declared it as a double not an array, so you need to alter it. cin>>ai[n]; ??????

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
#include <iostream>
#include <cmath>

using namespace std;

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


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

	{
		cout<<"Please enter a group of numbers"<< endl;
		cin>> n ;
		ai = new double[n];
		cout<<"enter an x value"<< endl;
		cin>>x;
		y = series(n,ai,x); // <------ y is not declared in this scope?
		
	}

	return 0;
}

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;
}
There are a great deal of fundamental errors here. I think you need to reread how to use:
-Declaring variables
-Arrays
-functions

In that order. Then rewrite this program from scratch.

I could address each error in your code blow by blow here, but I think you will find it to be of more benefit to reread the stuff. I suspect there will be other things fundamentally wrong other than what we see here.
Topic archived. No new replies allowed.