Sum of the Geometric Progression 
Author Message
 Sum of the Geometric Progression

The following program is supposed to compute the
nth value in a geometric progression and then give the
sum of the progression up to the point.

The program gives the nth value correctly but the summation
is always to greater than what it should be.

For instance when I enter 2 and 3, I get 8 correctly as
the nth value but as a sum I get 16 when I should get
14.

2+4+8=14.

Can anyone explain why?

Thanx in advance

Alan

Here  is the program:

#include<stdio.h>
int main( void )
{

                int i,p,s,x,y;

                printf("enter any 2 numbers");
                scanf("%d %d",&x,&y);

                while (x!=0)
          {
                        for (p=1,s=0,i=1; i<=y;i++)
                        p*=x;
                        s=s+p*x;
                        printf("%d %d",p,s);
                printf("enter any 2 numbers");
                        scanf("%d %d",&x,&y);
          }

        return 0;

Quote:
}



Mon, 29 Apr 2002 03:00:00 GMT  
 Sum of the Geometric Progression

Quote:

> The following program is supposed to compute the
> nth value in a geometric progression and then give the
> sum of the progression up to the point.

> The program gives the nth value correctly but the summation
> is always to greater than what it should be.

Did you really need to use a loop and did you reallt need to restrict
yourself to integers?  If not, try something like:

#include <stdio.h>
#include <math.h>

int main(void)
{
    double p, x, y;
    char buf[BUFSIZ];
    while (1) {
        printf("enter the multipler and number of terms: ");
        fflush(stdout);
        if (!(fgets(buf, sizeof buf, stdin)) ||
            sscanf(buf, "%lf %lf", &x, &y) != 2)
            break;
        if (x != 1) {
            p = pow(x, y);
            printf("The sum from %g through %g is %g\n", x, p,
                    x * (p - 1) / (x - 1));
        }
        else
            printf("%g * 1 = %g\n", y, y);
    }
    return 0;

Quote:
}

/* vi: set cindent ts=4 sw=4 et tw=72: */

Quote:

> For instance when I enter 2 and 3, I get 8 correctly as
> the nth value but as a sum I get 16 when I should get
> 14.

> 2+4+8=14.

> Can anyone explain why?

> Thanx in advance

> Alan

> Here  is the program:

> #include<stdio.h>
> int main( void )
> {

>                 int i,p,s,x,y;

>                 printf("enter any 2 numbers");
>                 scanf("%d %d",&x,&y);

>                 while (x!=0)
>           {
>                         for (p=1,s=0,i=1; i<=y;i++)
>                         p*=x;
>                         s=s+p*x;
>                         printf("%d %d",p,s);
>                 printf("enter any 2 numbers");
>                         scanf("%d %d",&x,&y);
>           }

>         return 0;
> }

--

__________________________________________________________
Fight spam now!
Get your free anti-spam service: http://www.brightmail.com



Mon, 29 Apr 2002 03:00:00 GMT  
 Sum of the Geometric Progression

Quote:
> For instance when I enter 2 and 3, I get 8 correctly as
> the nth value but as a sum I get 16 when I should get
> 14.

> 2+4+8=14.

arent' u forgeting the 1 before the 2 or are u assuming that the series
starts from 2?

ther are very nice formulas u can use instead of loops to do it. look up a
maths text book on series. that should greatly simplify things.

riyaz



Mon, 29 Apr 2002 03:00:00 GMT  
 Sum of the Geometric Progression

Quote:


>> The following program is supposed to compute the
>> nth value in a geometric progression and then give the
>> sum of the progression up to the point.

>> The program gives the nth value correctly but the summation
>> is always to greater than what it should be.

>Did you really need to use a loop and did you reallt need to restrict
>yourself to integers?  If not, try something like:

>#include <stdio.h>
>#include <math.h>

>int main(void)
>{
>    double p, x, y;
>    char buf[BUFSIZ];
>    while (1) {
>        printf("enter the multipler and number of terms: ");
>        fflush(stdout);
>        if (!(fgets(buf, sizeof buf, stdin)) ||
>            sscanf(buf, "%lf %lf", &x, &y) != 2)
>            break;
>        if (x != 1) {
>            p = pow(x, y);
>            printf("The sum from %g through %g is %g\n", x, p,
>                    x * (p - 1) / (x - 1));
>        }
>        else
>            printf("%g * 1 = %g\n", y, y);
>    }
>    return 0;
>}

Interesting solution but the other stuff you added aside
from <math.h> I haven't learned yet and we weren't
allowed to use the power function to solve this problem.

Anyway as usual after I posted I figured out what my
problem was. I needn't to use brackets in the for
loop and put printf out of the loop but thanx anyway.

Alan



Mon, 29 Apr 2002 03:00:00 GMT  
 Sum of the Geometric Progression

Quote:

>> For instance when I enter 2 and 3, I get 8 correctly as
>> the nth value but as a sum I get 16 when I should get
>> 14.

>> 2+4+8=14.

>arent' u forgeting the 1 before the 2 or are u assuming that the series
>starts from 2?

>ther are very nice formulas u can use instead of loops to do it. look up a
>maths text book on series. that should greatly simplify things.

Yes thank you but as I said in response to the other poster, this
was a test question and we weren't allowed to use the power
function to solve it.

The professor gave us this question to prepare us for the next
topic which was creating functions.

Anyway as I said before I figured out the problem after I posted,
and all I needed to do was to add brackets to the for loop and put
printf outside of the loop.

Thank you

Alan



Mon, 29 Apr 2002 03:00:00 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Progression of Learning

2. Geometric elliptical arc computations??

3. Wanted: C functions for geometric routines

4. Geometric Pens

5. prime sums

6. Computing a Sum from an ArrayList members?

7. Sum of the digits in C

8. trying to add the sum of an integer digits

9. Most compilers can't do a simple sum

10. need sum help w/ my new OS

11. generic sum

12. Problem: Matching sums within two arrays

 

 
Powered by phpBB® Forum Software