# cosine series

Page 1 of 1

## 4 Replies - 11050 Views - Last Post: 13 December 2007 - 09:35 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=39477&amp;s=f51f64b0d8fd1819b8761bed556b85f5&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 symidran

Reputation: 1
• Posts: 25
• Joined: 17-February 06

# cosine series

Posted 13 December 2007 - 12:28 PM

```#include<iostream.h>
#include<conio.h>
#include<math.h>

double fact(double k) {
if(k==0||k==1)
return 1;
else
return k*fact(k-1);
}

double coss(float x,int N) {
double sum=0,sign=1,term=0;
sum+=1;
int i;
for(i=2;i<=N;i+2)
{sign*=-1;
term=(sign*pow(x,i))/i;
sum+=term;
}
return sum;
}

void main()
{double num,n;
cout<<"Enter the value and accuracy";
cin>>num>>n;
cout<<"value is"<<coss(num,n);
getch();
}

```

I dont know but this program hangs after taking the input, are the calculations very large ? I think they should be in double's range.
Help is appreciated

Is This A Good Question/Topic? 1

## Replies To: cosine series

### #2 jjhaag

• me editor am smartastic

Reputation: 45
• Posts: 1,789
• Joined: 18-September 07

## Re: cosine series

Posted 13 December 2007 - 01:35 PM

Has nothing to do with the range of the double, and type overflow probably wouldn't cause a hang anyways.

The problem is with the increment step of your for loop, and that you aren't actually incrementing anything with this i+2 expression in your for loop control statement. That expression creates a temporary variable, stores the value of i+2 in it, then throws it away, without incrementing i by anything. You're probably looking for the assignment by addition operator here:
```	for (i=2; i<=N; i+=2) {...
```

Using the assignment-by-addition form, i is assigned a value of i+2 every round through the loop.

This post has been edited by jjhaag: 13 December 2007 - 01:36 PM

### #3 symidran

Reputation: 1
• Posts: 25
• Joined: 17-February 06

## Re: cosine series

Posted 13 December 2007 - 02:16 PM

jjhaag, on 13 Dec, 2007 - 10:35 AM, said:

You're probably looking for the assignment by addition operator here:
```	for (i=2; i<=N; i+=2) {...
```

Thx a million, just the help i needed

### #4 baavgai

• Dreaming Coder

Reputation: 6129
• Posts: 13,237
• Joined: 16-October 07

## Re: cosine series

Posted 13 December 2007 - 05:22 PM

Strange, I just gave an answer to this in java ( http://www.dreaminco...h...c=39472&hl= ).

If you are indeed doing a Taylor series for cos, you're missing the factorial call on that last i. Other than that, you're close. You might want to look at the comment on the other thread, though. Calling recursive functions for each iteration is messy.

Hope this helps.

### #5 symidran

Reputation: 1
• Posts: 25
• Joined: 17-February 06

## Re: cosine series

Posted 13 December 2007 - 09:35 PM

baavgai, on 13 Dec, 2007 - 02:22 PM, said:

If you are indeed doing a Taylor series for cos, you're missing the factorial call on that last i. Other than that, you're close. You might want to look at the comment on the other thread, though. Calling recursive functions for each iteration is messy.

Hope this helps.

Yeah I noticed that. Omitted the factorial just to find out where the error was. Finally getting values in range of +-10^-6 after 10 iterations, i think thats fine.
Thx for the help.