HiveBrain v1.2.0
Get Started
← Back to all entries
patterncMinor

Calculating e^x by math.h and by own means

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
meanscalculatingmathownand

Problem

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:

-
By the math.h library

-
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 
#include 

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);
    }

}

Solution

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 
#include 

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');
}

Code Snippets

#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');
}

Context

StackExchange Code Review Q#92929, answer score: 4

Revisions (0)

No revisions yet.