Equation with functions (C++)

It's me again hehe.. well, i have to do 10 exercises in C++. I already did 9. I'm here because i can't understand the last one... i have the sigma function of the equation only, but i don't know how to do the other part (xi/i!). This is the excersice:

http://oi58.tinypic.com/x4kxtk.jpg

It is repeated twice , just down I translated it into English so that it is understood since I speak Spanish. So you know now that what i'm saying with the "xi/i!" part. Here is my code with the sigma (i=1 Σ n) part:

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
  #include <iostream>
#include <conio.h>
#include <windows.h>
using namespace std;

//FUNCTION SIGMA
int sigma(int a, int b){

int sum = 0;


//THIS SUM I TO N
	for (int i=0; i<=a; i++){
   	sum = sum+i;
      }

   return sum;

}

int x;    //GLOBAL VARIABLES INT
int n;

int main(){

textcolor(11);
gotoxy(6,4); cprintf("I can solve sigma");
textcolor(15);
gotoxy(24,6); cprintf("Enter N: ");
cin >> x;
textcolor(15);
gotoxy(24,8); cprintf("Enter X: ");
cin >> n;
//PRINT THE RESULT
textcolor(15);
gotoxy(24,10); cprintf("Result is: "); cout << sigma (n, x);

getch();
return 0;
}


That's all, but i don't know what to do with X and the other part of the equation :( if anyone please can give me a solution for this!? Thank you for reading!!
Last edited on
Is the problem poorly stated? It says to "solve" the equation, but it doesn't say which variables are known and which are unknown. Are they all unknown? Is (R = 0, n = 1, x = 0) a solution?
The engineer who teaches us told us that R has to be found with Equation.
He gave us a little guide he made in a program but i did not understand it , I'll show you:

http://oi58.tinypic.com/x5ayw6.jpg
OK, so it's not really "solve" then, but "implement".

Your summation is not implemented correctly. You need to compute x / 1 + x^2 / 2 + x^3 / 6 + x^4 / 24 + ... + x^n / n!. You're only computing 0 + 1 + 2 + 3 + ... + n
I know, but thats why i'm asking for the solution, i don't know too much of C++ thats why i'm studying, so, if you can write the code for me, i will thank you helios!
Well, time to start learning, eh?

x^0 = 1
x^1 = 1 * x
x^2 = 1 * x * x
x^3 = 1 * x * x * x
etc.

0! = 1
1! = 1
2! = 1 * 2
3! = 1 * 2 * 3
etc.

Figure out how to do this with loops. Notice that you sigma function is already very similar to a factorial implementation.
Last edited on
Thanks helios, i did it but the result isn't the same at the final. I'll show you the code and if you detect the problem, please, tell me, thank you!

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
#include <iostream>
#include <conio.h>
#include <windows.h>
#include <math.h>
using namespace std;

//FUNCTION SIGMA
float sigma(float a, float b){

float division;
float sum = 0;
float operation = 1;
float i = 1;

	while(i <= b){
         a = pow (a,i);
   		for (int z = 1; i <= b; z++){
         operation = operation*z;
         division = a / operation;
      	sum = division+sum;
         i++;
       }
    }
    return sum;
}

int x;    //GLOBAL VARIABLES INT
int n;

int main(){

gotoxy(6,4); cprintf("I can solve sigma");
textcolor(15);
gotoxy(24,6); cprintf("Enter X: ");
cin >> x;
textcolor(15);
gotoxy(24,8); cprintf("Enter N: ");
cin >> n;
textcolor(15);
gotoxy(24,10); cprintf("Result is: "); cout << sigma(x, n);

getch();
return 0;
}
Last edited on
Line 16: Work out in paper what values a will take each time through the loop. Don't write it numerically, the math expression will do.

Also:
1. Fix your indentation. Your code is impossible to follow.
2. Once you do that, you should see another reason why the code doesn't work.

If I can give you an hint , look for recursive function in Google.

The method should not take more than 10 lines of code for sure.

This is not an iterative method. It is a fast method call technique but that will increase the call stack frame size so make sure to start with 10 or something not 4000 !

Reply with a recursive function and then we can help you with arithmetics.

Good search !
Topic archived. No new replies allowed.