> (And for you nitpickers, I realize my suggested approach 
> to this problem
> is highly inefficient/error-prone with large values/etc.

Sorry for nitpicking. :-)

This group seems to cover a wide range of coding abilities 
and it's sometimes difficult to judge the level of a 
question. If one has never written a C program before, 
implementing a simple function is a tough job. But there are 
also some advanced coders here and my remarks were really 
aimed at them.

For jagomaniak, here's a sketch of how I would approach the 
problem.

1. Get epsilon from the command line.
2. Wait for user input.
3. Compute sine.
4. Print result and exit (or go to 2?)

Now to refine the steps.

1.1. int main(int argc, char** argv).
1.2. Check argc == 2 or exit.
1.3. Convert argv[1] from a string to a float or double 
variable, epsilon.

2.1. Prompt the user.
2.2. Use scanf() to acquire a float or double.
2.3. Error exit on invalid or out-of-range input.

3.1. We'll need two utility functions: power(n, m) and 
factorial(n).
3.2. Set a float or double variable x = 0.
3.3. A loop that adds terms to x until the magnitude of the 
term added is less than epsilon.
3.4 Return x.

More details of 3.1:

3.1.1. Write power(m,n) to raise an integer m to a power n, 
and test it separately.
3.1.2. Write factorial(n) to calculate the factorial of an 
integer n, and test it separately. (Warning: beware of 
overflow! Factorials of quite small numbers can be very 
big.)


Step 1 is straightforward and you should be able to find 
plenty of examples online if you google "argc argv".

Step 2 should be easy once you know how to use scanf(). 
(However, there's a classic buffer overflow bug waiting to 
pounce!)

Step 3 is the difficult bit, but you can solve it by 
breaking it down into smaller chunks: the principle of 
divide-and-rule.

David

Reply via email to