cosine series

Program hangs during runtime

Page 1 of 1

4 Replies - 10575 Views - Last Post: 13 December 2007 - 09:35 PM Rate Topic: -----

#1 symidran  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • 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  Icon User is offline

  • me editor am smartastic
  • member icon

Reputation: 44
  • View blog
  • 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

Was This Post Helpful? 0
  • +
  • -

#3 symidran  Icon User is offline

  • New D.I.C Head

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

Re: cosine series

Posted 13 December 2007 - 02:16 PM

View Postjjhaag, 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 :^:
Was This Post Helpful? 0
  • +
  • -

#4 baavgai  Icon User is online

  • Dreaming Coder
  • member icon

Reputation: 5800
  • View blog
  • Posts: 12,635
  • 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.
Was This Post Helpful? 0
  • +
  • -

#5 symidran  Icon User is offline

  • New D.I.C Head

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

Re: cosine series

Posted 13 December 2007 - 09:35 PM

View Postbaavgai, 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.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1