function

can I have some help with this code. lets say i enter 6

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
  #include <iostream>
#include <iomanip>
using namespace std;

double series(int n);

int main(void){
	int nval = 0;

	cout << "Enter the value for n: ";
	cin >> nval;
	int sum = series(nval);
	cout << "The sum is " << fixed << setprecision(6) << sum << endl;
	return 0;
}

double series(int n){
	double term = 0.0, sum = 0.0;
	cout << "{ ";
	if (n > 0){
		sum = -1;
		cout << -1;
	}
	for (int i = 2; i <= n; i++){
		term = pow(i, i);
		cout << ", ";
		if ((i % 2) == 1){
			cout << " -";
			sum -= (1.0 / term);
		}
		else
			sum += (1.0 / term);
		cout << "1/" << term;
	}
	cout << " }\n";
	return term;
}
You don't indicate what help you need.

Line 12: series() returns a double. Why are you assigning it to an int?

Line 25: pow() is undefined because you haven't included the <cmath> header.


Hello newyork23,

In addition to what AbstractionAnon said about line 12 you will also have a problem with "n" in lines 5 and 17.

Hope that helps,

Andy
Last edited on
Topic archived. No new replies allowed.