I have a problem referring to Geometric progression
Using a few mathematics formula I have to compare the value En that I calculate using
Sinfinite and Sn.

This value En(which determines accuracy of the calculated number I believe) should than be compared to an arbitrary value E.
When En becomes smaller than E, the program should be terminated and the calculated value should be printed on the screen.

The sum is always 0 when I try to run this program.

any ideas?
thanks in advance

PS: It has to be in a while loop, not in a for.

#include <stdio.h>
#include <math.h>
int main(void)
{
    double a;
    int N = 0;
    double r;
    int n = 0;
    double Sn = 0;
    double Sinfinite;
    double En = 0;
    double E;

    printf("Value of a: ");
    scanf("%lf", &a);
    printf("Value of r: ");
    scanf("%lf", &r);
    printf("Value of E: ");
    scanf("%lf", &E);



    /*Comparing En with E*/
    while ( En>=E){
           Sn += a*pow(r, n);
           n++;

           }

    Sinfinite = a/(1-r);
    En = Sinfinite - Sn;

    printf("Sum is %0.3f\t", Sn);
    printf("Sinfinite = %.3f", Sinfinite);
    printf("En = %.3f\t", En);
    printf("N = %d", N);

    return 0;
}

Two things:

1) If your compiler is Turbo C, you won't load the floating point package you need, with that code. You need to have at least one of the doubles assigned an initial value of 0.0.

(since TC's float package can be stubborn about this, post back if you're still stuck and I'll give you some code guaranteed to force it to be loaded. The above should do it, however.)

2) pow() expects two doubles as parameters. You're giving it one double, and one integer, instead. Whether that works or not, depends on the relative sizes of integers and doubles, and your compiler design.

See if the above solves your problem.

En is set to 0 at line 11 and never changes after that. Therefore the loop (as long as E is positive) never executes. You need to initialize En to a really big number and reevaluate it at each iteration.

Adak: The code compiles and runs, and the %lf conversion apparently works. What gave you the idea that OP has trouble with floating point?
> Whether that works or not, depends on the relative sizes of integers and doubles, and your compiler design
The int to double promotion is a language feature, and it works always.

If a program that compiles and runs, doesn't give the right answer, I put everything up as a suspect. Naturally, you want to prioritize the suspect list for checking, and logic errors are far more common, but "always" things, still make the bottom of my "to be checked" list.

You might think my list is nit-picking, but I can assure you, my programs have no lice! ;)

I think I've found it.

dividing the double with an integer shouldn't be a problem. The integer would automatically change into a double.(so I have been told)

Nezachem was right, the loop would go on as long as En was bigger than E but it never was so i had to give it a value bigger than 1.
Also thanks to the rest!

In the end my problem had nothing to do with C syntax. Just 2 formulas i forgot to put IN the loop instead of outside of it.

Meaning I've spent the entire week on figuring out a problem that isn't even going to improve my C skills.

oh well, again thanks!

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

int main(void)
{
    /* declareren van variabelen*/
      double a;
      double r;
      double n = 0.0;
      double Sn = 0;
      double Sinfinite;
      double En = 100;
      double E;

        /*Invoer van variabelen*/
        printf("Value of a: ");
        scanf("%lf", &a);
        printf("Value of r: ");
        scanf("%lf", &r);
        printf("Value of E: ");
        scanf("%lf", &E);


 /*Sommeren zolang En groter of gelijk is aan E*/
     for(Sn = 0 ;En>=E;n++)
     {
        Sinfinite = a/(1-r);
        En = Sinfinite - Sn;
        Sn += a*pow(r, n);
     }


    /* Uitvoer printen op scherm*/
      printf("%s\t%s\t%s\t%s\n","Sn","Sinfinite", "E","N");
      printf("%.3f\t%.3f\t\t%.3f\t%.3f\n", Sn, Sinfinite, E, n);

      return 0;
}
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.