if limit is 4

how to use for calculat this program?

#include<stdio.h>

int main(void)

{

double tpt =0;

int i, limit;

for(i=0; i<=limit; i++)

{

1 1 1 1 1

tot += - + - + - + - + -

1*1 1*2 1*2*3 1*2*3*4 (limit-1)

(4-1)

1*2*3

}

This is a discussion on *for* within the **C Programming** forums, part of the General Programming Boards category; if limit is 4
how to use for calculat this program?
#include<stdio.h>
int main(void)
{
double tpt =0;
int i, ...

- 03-17-2002 #1

- Join Date
- Feb 2002
- Posts
- 11

## for

if limit is 4

how to use for calculat this program?

#include<stdio.h>

int main(void)

{

double tpt =0;

int i, limit;

for(i=0; i<=limit; i++)

{

1 1 1 1 1

tot += - + - + - + - + -

1*1 1*2 1*2*3 1*2*3*4 (limit-1)

(4-1)

1*2*3

}

- 03-17-2002 #2
>1 1 1 1 1 tot += - + - + - + - + - 1*1 1*2 1*2*3 1*2*3*4 (limit-1) (4-1) 1*2*3

What is this? This isn't even *close* to a valid expression.

-PreludeMy best code is written with the delete key.

- 03-18-2002 #3

- Join Date
- Aug 2001
- Location
- Groningen (NL)
- Posts
- 2,386

Hmmm, saw this post too late. There was another posting by you with this question. Can you tell us in words what you try to do?

- 03-18-2002 #4

- Join Date
- Jan 2002
- Location
- Vancouver
- Posts
- 2,220

Perhaps you should set limit to a value before comparing it.

- 03-18-2002 #5

- Join Date
- Feb 2002
- Posts
- 11

## for

Euler's equation

- 03-18-2002 #6
> Euler's equation

Um... okay.... Still doesn't explain your problem... Almost nothing in your code is valid C.-Govtcheez

govtcheez03@hotmail.com

- 03-18-2002 #7

- Join Date
- Jan 2002
- Location
- Vancouver
- Posts
- 2,220

"Bad programming = Compiler errors" -- Euler's equation.

- 03-19-2002 #8

- Join Date
- Feb 2002
- Posts
- 11

What's wrong in this program?

How to fix it?

use loop to terminates when the difference between

two succesive values of e differ by less than

0.0000001

#include<stdio.h>

void getLimit(int *);

void calculation(int);

int main(void)

{

int limit;

getLimit(&limit);

calculation(limit);

return 0;

}

/**************************GET LIMIT********************/

void getLimit(int *limit)

{

printf("Enter limit: ");

scanf("%d", limit);

return;

}

/**************************CALCULATION************* *******/

void calculation(int limit)

{

int i, j, p, flag=0;

double sum, diff, psum=1;

for(i=1; i<limit; i++)

{

p=1;

for(j=1; j<=i; j++)

p=p*j;

sum=psum + (1/p);

diff=psum-sum;

if(diff < 0.0000001)

{

flag=1;

break;

}

if(flag==1)

printf("Prev-next:%.2lf %.2lf %.2lf\n", psum, sum, diff);

else

printf("Num of iteration: %d, e=%.5lf\n", limit, sum);

}

return;

}

- 03-20-2002 #9

- Join Date
- Aug 2001
- Location
- Groningen (NL)
- Posts
- 2,386

Your program looks a bit unstructured. Take some more effort in designing your algorithm.

Second, there are many formula's of Euler. But I think your pointing at the formula to calculate e.

e = sigma (x=0->inf) (1 / x!)

Which you need to implement as

e = sigma (x=0->limit) (1 / x!)

Which is

e = 1 / 0! + 1 / 1! + 1 / 2! + ... + 1 / limit!

As you can see there are two main calculations to be made

1. Sum

2. Faculty

A sum can be calculated by using a for-loop and faculty can be a function:

Code:e = 0 for (x = 0; x < limit; x++) { e += 1 / fac (x); }

x! = x * (x-1) * (x-2) * ... * 3 * 2 * 1

So you see the end-step is reached if x is 1 and the recursion step is x * (x - 1). The function is now easily created:

Code:int fac (int i) { if (i == 1 || i == 0) { return 1; } else { return (i * fac (i - 1)); } }

- Exactly how to get started with C++ (or C) today
- C Tutorial
- C++ Tutorial
- 5 ways you can learn to program faster
- The 5 Most Common Problems New Programmers Face
- How to set up a compiler
- 8 Common programming Mistakes
- What is C++11?
- Creating a game, from start to finish

- How to create a shared library on Linux with GCC - December 30, 2011
- Enum classes and nullptr in C++11 - November 27, 2011
- Learn about The Hash Table - November 20, 2011
- Rvalue References and Move Semantics in C++11 - November 13, 2011
- C and C++ for Java Programmers - November 5, 2011
- A Gentle Introduction to C++ IO Streams - October 10, 2011