patterncMinor
Calculating e^x by math.h and by own means
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
Then the program calculates the difference of these two functions (which we now call \$d\$) and calculates \$n\$.
\$n = \frac{d}{0,001}\$
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
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
consider rewriting it as a
You don't need the
You can easily rewrite the program without it.
The coding style is very odd:
Other tips:
With the above suggestions (and some more) applied, the code becomes slightly simpler:
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+1you can do++h, as you did in the for loop
- Instead of
x = x yyou can usex = y
- Instead of
x = x + yyou can usex += 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.