please help... i`m new with some of c++ programs

i need to evaluate
y= 1 - x + (x^2/2)- (x^3/6) + (x^4/24)+...+(-1)^n.x^n/n!


to put in my function call..
but i don`t know how to write ..! i`m new with these functions!!

i can`t figure out how to write that form i mentioned upward!!!

with the note that func ( int n ; double x; )



thanks who helps...
and after i`ll post my results for people to discuss...



this is my question:
1. Write a program that includes a function to evaluate the first n terms in the series specified by .
Where n (defined as int) and x (defined as double) are input parameters to that function.
Last edited on
please notice that i can use (-1) for the trick upthere as its (plus, min, plus, min... )series

but i`m demand to use twice ... once to write using (-1)
but the other time .. i have to write the new program with the same series set without the help of (-1)
A loop. The equation has N terms so a loop could evaluate one term per iteration. (The first two could be handled separately.)

(-1)^n.x^n/n!
# can be divided into two:
(-1)^n # the sign
x^n/n! # the value

The sign can only be 1 or -1, depending on whether n is odd or even. No need for power; use modulo.

The value can be further expanded:
x^n / n!
==
( x^(n-1) * x ) / ( (n-1)! * n )
1
2
3
4
5
6
7
8
9
10
double numerator = x;
size_t denominator = 1;

size_t term = 2;
numerator *= x; // == x^2
denominator *= term;  // == 2!

term = 3;
numerator *= x; // == x^3
denominator *= term;  // == 3! 
keskiverto
.. i got the idea.. but i don`t know how to enter it ..

i`m not demanding anyone to do my homework, it`s just that i`m new to these things as an electrical engineer!!

if you or anyone could help me lil bit more i`ll be so thankful:(
Last edited on
A hint:
1
2
3
4
5
6
// calculate 42/1 + 42/2 + ... + 42/9
float sum = 0.0f;
for ( int num = 1; num < 10; ++num ) {
  sum += 42.0f / num;
}
std::cout << "Sum " << sum << '\n';
so here is the solution to my question,, thanks to whom helped!!:)

and sorry that i post late

so here is my code:


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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include <iostream>

using namespace std;

double power(int n, double x)

{

	double d =1;
	for (int i = 1 ; i<=n ; i++)
		d *= x;

	return d;


}

double fact(int n)

{

	double d =1;
	for (int i = 1 ; i<=n ; i++)
		d *= n;

	return d;


}



double function (int n, double x)

{
	double  y = 0;

	for ( int i=0; i< n ; i++)
	{if (i%2 == 0)
	y = y + (power(i , x) / fact (i) );
	
	else 
		y = y - (power(i , x) / fact (i) );
	}
	return y;
}



int main ()
{ 
	int n ;
	double x;
	double result;

	cout<< "enter integers N & X: \n";
cin>> n >> x;

result =function(n ,x);

cout<< "the result is: " << result<< endl;



return 0;
}



/*

  enter integers N & X:
3 5
the result is: 2.25
Press any key to continue

  */


/*

  enter integers N & X:
4 2
the result is: -0.296296
Press any key to continue
*/
Topic archived. No new replies allowed.