Change this code so that you do division BEFORE doing multiplication for each it
ID: 3940053 • Letter: C
Question
Change this code so that you do division BEFORE doing multiplication for each item.
code:
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<stdlib.h>
#define PI 3.1415
//FIND FACTORIAL
double fact(int n)
{
int i;
double prod = 1.;
for(i = 1; i <= n; i++)
prod = prod * i;
return prod;
}
//FIND x TO THE POWER OF n
double power(double x, int n) {
int i;
double prod = 1.;
for (i = 0; i < n; i++)
prod = prod * x;
return prod;
}
//CALCULATE SIN(x)
double sineFun(double x) {
double sum = 0.;
int i, sign = 1;
for (i = 0; i < 21; i++) {
sum = sum + sign * power(x, 2 * i + 1) / fact(2 * i +1);
sign = -sign;
}
return sum;
}
//FUNCTION TO FIND COS(X) USING TAYLOR’S SERIE
double coseFun(double x) {
double value = 1.0;
int i, j;
for (i = 2, j = 1;i < 21;i += 2, j++ ) {
value += ( double ) power( -1.0, j ) * power( x, i ) / fact( i ); //taylor series expansion for cosine series
}
return value;
}
//FUNCTION TO FIND EXP(X) USING TAYLOR’S SERIES
double expoFun(double x) {
int i = 1;
float ex = 1;
while ( i < 21 ) {
ex += ( float ) power( x, i ) / fact( i ); //taylor series expansion for exponential
++i;
}
return ex;
}
int main() {
double x;
char more;
do {
printf(" Input X: ");
scanf("%lf", &x);
printf(" LibraryResult MyResult");
printf(" sin( %.2lf) %1lf %1lf", x, sin( x), sineFun( x));
printf(" cos( %.2lf) %1lf %1lf", x, cos( x), coseFun( x));
printf(" exp( %.2lf) %1lf %1lf", x, exp( x), expoFun( x));
printf(" Do more (Y/N)?:");
scanf(" %c", &more);
}while(more=='y'||more=='Y');
return 0;
}
Explanation / Answer
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<stdlib.h>
#define PI 3.1415
//FIND FACTORIAL
double fact(int n)
{
int i;
double prod = 1.;
for(i = 1; i <= n; i++)
prod = prod * i;
return prod;
}
//FIND x TO THE POWER OF n
double power(double x, int n) {
int i;
double prod = 1.;
for (i = 0; i < n; i++)
prod = prod * x;
return prod;
}
//CALCULATE SIN(x)
double sineFun(double x) {
double sum = 0.;
int i, sign = 1;
for (i = 0; i < 21; i++) {
sum = sum + sign * (power(x, 2 * i + 1) / fact(2 * i +1));
sign = -sign;
}
return sum;
}
//FUNCTION TO FIND COS(X) USING TAYLOR’S SERIE
double coseFun(double x) {
double value = 1.0;
int i, j;
for (i = 2, j = 1;i < 21;i += 2, j++ ) {
value += ( double ) power( -1.0, j ) * (power( x, i ) / fact( i ));
//taylor series expansion for cosine series
}
return value;
}
//FUNCTION TO FIND EXP(X) USING TAYLOR’S SERIES
double expoFun(double x) {
int i = 1;
float ex = 1;
while ( i < 21 ) {
ex += ( float ) power( x, i ) / fact( i );
//taylor series expansion for exponential
++i;
}
return ex;
}
int main() {
double x;
char more;
do {
printf(" Input X: ");
scanf("%lf", &x);
printf(" LibraryResult MyResult");
printf(" sin( %.2lf) %1lf %1lf", x, sin( x), sineFun( x));
printf(" cos( %.2lf) %1lf %1lf", x, cos( x), coseFun( x));
printf(" exp( %.2lf) %1lf %1lf", x, exp( x), expoFun( x));
printf(" Do more (Y/N)?:");
scanf(" %c", &more);
}while(more=='y'||more=='Y');
return 0;
}
------------------------
output sample 1:-
Input X: 3
LibraryResult MyResult
sin( 3.00) 0.141120 0.141120
cos( 3.00) -0.989992 -0.989992
exp( 3.00) 20.085537 20.085539
Do more (Y/N)?:y
Input X: 7
LibraryResult MyResult
sin( 7.00) 0.656987 0.656987
cos( 7.00) 0.753902 0.757094
exp( 7.00) 1096.633158 1096.617432
---------------------------------------------------------------------------------------------
If you have any query, please feel free to ask.
Thanks a lot.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.