[Tutor] Help with program

Dave Angel davea at davea.name
Tue Feb 17 03:31:49 CET 2015


On 02/16/2015 11:27 AM, Courtney Skinner wrote:
> Hello,
>
> I am trying to build a program that approximates the value of cosine - this is my program so far. It is not returning the right values. Could you tell me what I am doing wrong?
>
>

You've got several answers that point out several problems in your code. 
  But I think you're missing a key concept.

If you're faced with a problem that's beyond your present abilities, or 
that's got you stumped, always consider factoring the problem into 
simpler ones.

To me the first thing you should factor out is a factorial function. 
Write one, that takes a positive int and returns the factorial, and test 
it against the one in the math library.

Once it's correct, then use it in the cosine problem.  Now you've got a 
simpler loop to write.  And you know part of the code works.

Next, see if you can avoid most of those three variables you're using. 
For example, What do you get when you calculate
    (-1) ** (i)

Can you use that to simplify things?


-- 
DaveA


More information about the Tutor mailing list