my homework is to make a c program displayin de cosine series.....i made up one referrin buks but can it be made simpler or somethin using while statements..........

#include<iostream.h>
#include<conio.h>
#include<math.h>
   int fact(int);//Global defenition
void main()
{
//cos(x)
//to find the power pow(number,power);
//to find root sqrt(number)-- in math.h

float cosx(float,int);


float x;
int n;
float final;

cout<<"\n Enter the limit ";
cin>>n;
cout<<"\n enter the value for x ";
cin>>x;

final=cosx(x,n);
cout<<"\n sum of cos series  for "<<n<<" terms is "<<final;
getch();
}

float cosx(float x,int lm)
{

   float term;
    float sum=1;
	for (int i=0;i<lm-1;i++)
	{
  		//find factorial of c
  	int c=2;

 	term=pow(x,c)/fact(c);
 	term=term*-1;
 	sum=sum+term;
 	c=c+2;
	 }
return(sum);

}


int fact(int num)
{int res=1;
     for (int  j=1;j<=num;j++)
       { res=res*j;}

return(res);
}
mvmalderen commented: Because each post you make, you post your rubbish without using code tags, and you refuse the advice to use code tags each time. -4

Recommended Answers

All 4 Replies

Is using code tags really that difficult for you?
Haven't you found the button yet, after 49 posts?

Information about code tags is found all over the website:

1) in the Rules you were asked to read when you registered
2) in the text at the top of this forum
3) in the announcement at the top of this forum titled Please use BB Code and Inlinecode tags
4) in the sticky post above titled Read Me: Read This Before Posting
5) any place CODE tags were used
6) Even on the background of the box you actually typed your message in!

How can you just neglect that? There's no explanation solid enough to not use code tags.
Please come back when you've learned how to use code tags, and when you've learned to use int main() instead of void main() . void main() is evil, but some people just don't seem to get it.

Come on, you started your thread in the wrong forum and it seems like you're still not aware of the fact that you won't get helped.
It's maybe better to not waste your time on wasting my time, I would really appreciate that.

> You cannot give more Reputation to anuizath2007 today.
<arnie>
I'll be back!
</arnie>

Haha, bookmarked in my reputation queue :P

you have a problem! You set c =2 inside the loop but add 2 to it at the bottom of the loop, which gets replaced at the top of the loop with 2 again.

I made your factorial incremental so that your function runs faster.
Also you're using floats' but you're using the double function pow(), where when using floats, you should be using powf().

You may want to re-examine your function and see if it realy does what you wanted!

float cosx(float x,int lm)
{
   float term;
   float sum=1;

     // Calculate our factorial on the fly
   float fkt = 1.0f * 2.0f;     // !2
   int c = 2;

   for (int i=0; i < lm-1; i++)
  {
      term = - powf( x, c ) / fkt;
       sum = sum + term;

      fkt = fkt * (float)c++;  // Factorial xN   x (N+1)
      fkt = fkt * (float)c++;
   }

   return(sum);
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.