alternating series

Finding it difficult to understand

Page 1 of 1

5 Replies - 5048 Views - Last Post: 18 December 2008 - 11:20 AM Rate Topic: -----

#1 progstud  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 21-November 08

alternating series

Posted 18 December 2008 - 08:13 AM

Hi guys. I am finding it very difficult to understand the logic how to code this alternating series. e.g 1+x-(x^2)+(x^3)...
Any help pls. This is what I've got.
#include<stdio.h>
#include<stdlib.h>
#include<math.h>

int main(void)
{
	int number;
	int i;
	int sum=0;
	int power;
	int value;
	
	printf("Enter a number");
	scanf("%d",&number);
	
	for(i=0;i<=3;i++)
	{
		
		power =pow(number,i); 
		sum= sum +power;
		number = number*(-1);
	}
	printf("The sum is %d",sum);
	return 0;
}


Is This A Good Question/Topic? 0
  • +

Replies To: alternating series

#2 progstud  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 21-November 08

Re: alternating series

Posted 18 December 2008 - 08:51 AM

any help pls? Thanks a lot.
Was This Post Helpful? 0
  • +
  • -

#3 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: alternating series

Posted 18 December 2008 - 10:52 AM

1+x-(x^2)+(x^3)...

What are the first two terms? The result you are going to calculate needs to include them.

I would not use integers for this problem but rather doubles since that is what pow() is going to return as it's result.
See pow()
Was This Post Helpful? 0
  • +
  • -

#4 progstud  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 21-November 08

Re: alternating series

Posted 18 December 2008 - 11:01 AM

This question I am asking is part of the cosine series(without math library). I managed to get here. I am not sure of the output I am having. Am I alternating the sign correctly.Thanks a lot.
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<math.h>

double cos(double x,double N);

void main()
{
	double num;
	double n;
	printf("Enter the value and accuracy");
	scanf("%lf%lf",&num,&n);
	printf("value is %f",cos(num,n));

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


int factorial(int number)
{
	if(number<=1)
	{
		return 1;
	}else
	{
		return number*factorial(number-1);
	}//end if...

}

Was This Post Helpful? 0
  • +
  • -

#5 amir_ju  Icon User is offline

  • D.I.C Head

Reputation: 17
  • View blog
  • Posts: 82
  • Joined: 20-June 08

Re: alternating series

Posted 18 December 2008 - 11:17 AM

cos(x) = 1 - x^2/2! + x^4/4! - x^6/6! + ...
are you sure you you want to evaluate 1+x-(x^2)+(x^3)... ?

This post has been edited by amir_ju: 18 December 2008 - 11:18 AM

Was This Post Helpful? 0
  • +
  • -

#6 progstud  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 21-November 08

Re: alternating series

Posted 18 December 2008 - 11:20 AM

sorry I haven't been so clear. I am trying to eveluate the cosine series.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1