2
\$\begingroup\$

For this program, the user needs to enter an exponent and the program will calculate \$e\$ (Euler's number) to the power of the exponent the user inputs.

This is done by two ways:

  1. By the math.h library

  2. By a for loop (Series)

Then the program calculates the difference of these two functions (which we now call \$d\$) and calculates \$n\$.

\$n = \frac{d}{0,001}\$

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




main()
{
int a, exponent, h;
float math, iteraties, c, stdio, z, verschil;

    char stop='n';
    while (stop!='j')
    {

      printf("vul exponent in:\t\t");
      scanf("%d",&exponent);
      syntax: math= exp(exponent);
      c=1.0;
      h=0;
      stdio=1.0;
      a=0;
      for (iteraties=1.0; a<1; iteraties++)
      {
        h=h+1;
        Syntax: z= pow(exponent,h);

        c=c*iteraties;
        stdio=stdio+(z/c);
        verschil=math-stdio;
        if(verschil<0.01)
        {
            a=2;
        }
      }
      printf("\nexp (%d) uit math.h is:\t\t%f",exponent, math);
      printf("\nexp (%d) m.b.v. de reeks is:\t%f",exponent,stdio);
      printf("\nhet verschil is: \t\t%f", verschil);
      printf("\naantal iteraties is:\t\t%.f", iteraties);


      printf("\n\nstoppen n/j\t\t\t");
      getchar();
      scanf("%c",&stop);
    }

}
\$\endgroup\$
  • 3
    \$\begingroup\$ You might have more success if you translate the variables to English. \$\endgroup\$ – Nic Hartley Jun 7 '15 at 19:09
3
\$\begingroup\$

What's the point of the syntax: and Syntax: labels? You don't use them, so remove them. And if you must use two labels, make them less similar to each other.

When the condition in the while loop is true from the start, consider rewriting it as a do { ... } while loop.

You don't need the a variable. You can easily rewrite the program without it.

The coding style is very odd:

  • Excessive vertical spacing (many empty lines) at many places
  • Too compact: it's recommended to put spaces around operators

Other tips:

  • Instead of h=h+1 you can do ++h, as you did in the for loop
  • Instead of x = x * y you can use x *= y
  • Instead of x = x + y you can use x += y

With the above suggestions (and some more) applied, the code becomes slightly simpler:

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

main()
{
    int exponent, h;
    float math, iteraties, c, stdio, z, verschil;

    char stop = 'n';
    do
    {
        printf("vul exponent in:\t\t");
        scanf("%d", &exponent);
        math = exp(exponent);
        c = 1.0;
        h = 0;
        stdio = 1.0;
        for (iteraties = 1.0; ; iteraties++)
        {
            ++h;
            z = pow(exponent, h);

            c *= iteraties;
            stdio += z / c;
            verschil = math - stdio;
            if (verschil < 0.01)
            {
                iteraties++;
                break;
            }
        }
        printf("\nexp (%d) uit math.h is:\t\t%f",exponent, math);
        printf("\nexp (%d) m.b.v. de reeks is:\t%f",exponent,stdio);
        printf("\nhet verschil is: \t\t%f", verschil);
        printf("\naantal iteraties is:\t\t%.f", iteraties);

        printf("\n\nstoppen n/j\t\t\t");
        getchar();
        scanf("%c", &stop);
    } while (stop != 'j');
}
\$\endgroup\$
  • 1
    \$\begingroup\$ Adding to this, I believe the inner loop would be much clearer as a while. The for loop with a break and no exit condition in the statement is not very idiomatic. It's ok in such a short program, but I'd much rather initialize iteraties on a separate row, and have a do { ++iteraties; ...... } while (verschil >= 0.01);. This is much more similar to how I'd think it (go on computing while the difference is too large). It's also easier to add a max number of iterations. This can also be done all in the for statement, as @vnp wrote. \$\endgroup\$ – Gauthier Jun 10 '15 at 11:16
  • \$\begingroup\$ Good point, and if you post as an answer I would upvote it ;-) \$\endgroup\$ – janos Jun 10 '15 at 11:30
1
\$\begingroup\$

Naming.

Avoid single-letter identifiers. Try to give them a meaningful names. For example, c is actually a factorial, and is better be called such.

Algorithm.

You correctly calculate factorials incrementally. Notice that power factors may also be calculated incrementally:

    term = 1.0;
    for (...) {
        term *= exponent / iteraties;
        stdio += term;
        ...
    }

which eliminates the unnecessary (and unclear) h and c.

Loop organization.

janos noticed that a is superfluous. I make one more step and move the termination test to where it belongs, that is into a loop header:

    for (iteraties = 1; math - stdio > 0.001; iteraties++)
\$\endgroup\$
0
\$\begingroup\$

Your program keeps on computing until a condition is met. The idiom for this is a while loop. It's ok to do it in a for loop, although these are usually used when the number of iterations is known in advanced, and while loops when it's not (this is not always true but I'd say that following this will cause less surprise, which is a good thing).

As such, I'd reimplement your loop this way:

iteraties = 0;
do
{
    ++iteraties;
    h = h + 1;
    z = pow(exponent,h);

    c = c * iteraties;
    stdio = stdio + (z / c);
}
while (math - stdio < 0.01);

Adding && (iteraties < MAX_NUM_ITERATIONS) to the condition is then an easy and readable thing to do.

Also, I'd create a #define for 0.01. Hard-coded values are almost never a good idea.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.