Infinite loop + factorial

Hi Guys,

I have a task that I need a hand with as it seems I am not getting it the way I want it to work.

Basically the task is to do a C++ code that does "1 + Sum = 1 + ( 1/(2.1)! + 1/(2.2)! + 1/(2.3)! + ...) = 1 + ( ½ + 1/(1.2.3.4) + 1/(1.2.3.4.5.6) + ... )". Epsilon is 10^-6 and it needs to print out each result untill it reaches the epsilon //unfortunatly I am unable to paste the math formula here but I think that is what it needs to do.

So, I have been able to do the following // had help from a friend who knows C but not C++
C++, pasted just now:

1 int main() {
2 double sum = 1;
3 int k = 1;
4 double previous_sum = 1, this_sum;
5 double epsilon = 10^-6;
6
7 while (1) { // until k=infinity
8 this_sum = 1/(double)bang(2*k);
9 sum += this_sum;
10 if ( previous_sum - this_sum < epsilon ) {
11 break; // reached desired approximation
12 }
13 previous_sum = this_sum;
14 k++;
15 cout << "For k=" + k + ", the result is: " + sum;
16 }
17 cout << "Final result is: " + sum;
18 }
19
20 int bang( int to ) {
21 int result=1;
22 for ( int i=1; i <= to; i++ ) {
23 result *= i;
24 }
25 return result;
26 }

What I get is :
"In function 'int main()':
Line 8: error: 'bang' was not declared in this scope
compilation terminated due to -Wfatal-errors."

Can anyone help with my code or do share theirs that can achiove this sum
You have to declare bang before it can be used. Place this above main:
int bang(int);


^ is the bitwise xor operator.
double epsilon = 10^-6;
should be written
double epsilon = 10e-6;
fixed the errors and edited, so people can read it easily, but this is still not good programming style.

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>


int bang(int to);

int bang(int to) 
{
	int result=1;
	for ( int i=1; i <= to; i++ ) 
	{
		result *= i;
	}
	return result;
}


int main() 
{
	double sum = 1;
	int k = 1;
	double previous_sum = 1, this_sum;
	double epsilon = 10e-6;

	while(1) // until k=infinity
	{
		this_sum = 1/(double)bang(2*k);
		sum += this_sum;
		if ( previous_sum - this_sum < epsilon ) 
		{
			break; // reached desired approximation
		}
		previous_sum = this_sum;
		k++;
		std::cout << "For k=" << k << ", the result is: " << sum;
	}
	std::cout << "Final result is: " << sum;
}
Last edited on
Cheers Darkmaster!

Im in the office now and don't have a good compilator. I am using an online tool (http://codepad.org) which is giving me the below after I run the code in it:

"In function 'int main()':
Line 36: error: expected `}' at end of input
compilation terminated due to -Wfatal-errors."

I am sure that it is to do with the compilator, thogh. I am really below beginner so exuse my lack of knowledge and style. Also, you may find it to be lame but can you please let me know why yours start with #include <iostream> and ends with std::cout when I have been told it is to start with 1 int main().

Cheers,
Tod
Gees, I was blind!

Thanks again Darkmaster! You are the MAAA!
the includes are always on top since you first need to include libraries before you can use them in your code.
here i include iostream, which is needed for the output via cout;

the cout is at the end of the main, i didn't change that.

i placed the bang function above the main(), it's common to keep the main as last function, this way it is easy to find.
Topic archived. No new replies allowed.