How to sum (1+x+x^1)+....(1+x+x^n) in C

This is a discussion on How to sum (1+x+x^1)+....(1+x+x^n) in C within the C Programming forums, part of the General Programming Boards category; Could anywhone help me to write sum for S=(1+x+x^i)+(1+x+x^(i+1) ) + ... + (1+x+x^n) ? Exponent i i staking values ...

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    8

    Question How to sum (1+x+x^1)+....(1+x+x^n) in C

    Could anywhone help me to write sum for S=(1+x+x^i)+(1+x+x^(i+1) ) + ... + (1+x+x^n) ? Exponent i i staking values from i=1 to i=n . User gives n.
    Thank You very much in advance .

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,652
    What have you tried?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Mar 2010
    Posts
    8
    Ive tried for loop but it gives me incorect value

    Code:
      for (i=1;i<=n;i++)
    			{
    				Sum=(1+x+pow(x,i));
    			
    			}
    
    
    			printf("sum  : %.2f\n",SumB);

  4. #4
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    There are two ways to compute this:

    1) The way you did it, only YOU ARE NOT ADDING TO SUM just initializing sum to the last term computed.

    2) Get a pen and paper and compute the sum.

    Ill give you a hint:

    The result is n*(x+1) + (x^(n+1) - 1)/(x-1)

  5. #5
    Registered User
    Join Date
    Mar 2010
    Posts
    8
    Well Claudia, I appreciated your answer, but after 12h and counting in font of the monitor, ill try to resolve this problem ,but i would be very gratefull too you, if you could tell me a little bit more. Thank you so much

  6. #6
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Quote Originally Posted by i6472 View Post
    Ive tried for loop but it gives me incorect value

    Code:
      for (i=1;i<=n;i++)
    			{
    				Sum=(1+x+pow(x,i));
    			
    			}
    
    
    			printf("sum  : %.2f\n",SumB);
    Change this to:

    Code:
     double sum = 0.0;
     for (i=1;i<=n;i++)
    			{
    				sum= sum + (1+x+pow(x,i));
    			
    			}
    
    
    			printf("sum  : %.2f\n",sum);
    Try that.

    You were not adding anything to sum, insteand just re-initializing sum to the current term in the iteration. Secondly, you were not even printing the results of sum but the results of sumB some variable we know nothing about given your posting.

    As for the math behind it, I recommend you brush up your algebra by reading online about geometric series.
    http://en.wikipedia.org/wiki/Geom
    etric_series


    Also, think I miscalculated in the previous post, the corect result i believe is what i wrote there minus 1.

  7. #7
    Registered User
    Join Date
    Mar 2010
    Posts
    8
    Thank you VERY much, im so grateful for your help. The reason for sumB is my mistake cause ive tried so many things to do the job,and forget to delete it. The program originaly is typed for region with non english speaking population so many variables is writen in diferent languange with english alphabet. Sory for any inconveniace.

  8. #8
    Registered User
    Join Date
    Mar 2010
    Posts
    8
    Here is the task and the problem in it,i dont know whats wrong
    Code:
    //////////////////////////////////////////////
    //How to calculate Sum ///////////////////////
    //when you have x betwen values///////////////
    //xmin and xmax///////////////////////////////
    //with step Dx ///////////////////////////////
    //and exponent i is taking////////////////////
    //value betwen 1 and n ///////////////////////
    // S=(1+x+x^i)+(1+x+x^(i+1))+...+(1+x+x^n)////
    //////////////////////////////////////////////
    #include <stdio.h>
    #include <math.h>
    
    
    main()
    {
    	double x,xMIN,xMAX,Dx,Sum;
    	int i,n;
    
    	printf("Enter the first border for x : ");
    	scanf("%lf",&xMIN);
    
    	printf("Enter the second border for x : ");
    	scanf("%lf",&xMAX);
    
    	printf("Enter step for x (Dx) : ");
    	scanf("%lf",&Dx);
    
    	printf("Enter top exponent : ");
    	scanf("%d",&n);
    	
    
    
    		for (x=xMIN;x<=xMAX;x+=Dx)
    		{
    			printf("X is :%.2f",x);
    			for (i=1;i<=n;i++)
    			{
    				Sum=(1+x+pow(x,i));
    			
    
    			}
    
    		printf("sum  : %.2f\n",Sum);
    			
    		}
    
    
    }

  9. #9
    Registered User
    Join Date
    Mar 2010
    Posts
    8
    i works like a charm id just added sum=0; after printf of the sum thank you Very much !!!

  10. #10
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Good job.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Application Repeatition
    By lgcarter in forum C Programming
    Replies: 3
    Last Post: 06-16-2009, 02:07 PM
  2. Replies: 1
    Last Post: 05-28-2009, 01:28 PM
  3. Minor Problem
    By stewie1986 in forum C Programming
    Replies: 6
    Last Post: 11-30-2007, 07:40 AM
  4. a sum equal to or in excess of 100
    By lyoncourt in forum C Programming
    Replies: 6
    Last Post: 10-07-2007, 05:43 PM
  5. string to int conversion not using atoi()
    By linucksrox in forum C Programming
    Replies: 2
    Last Post: 05-19-2004, 12:17 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21