cannot get the code to run. objective is to solve the program for function 1 being x^2 function 2 being sin(x^2) and function 3 being sin(x^2). Calculate parameters being deltax is equal to .0001. Program should be an integral calculator.
Bounds is pi = b(top bound) 0=a (lower bound) for f(x) = sin(x) and deltax = .0001
$ ./integral 1 0 3.1415926 0.0001
integral value = 2.000000
Bounds is 2 = b(top bound) 0=a (lower bound) for f(x) = (x^2) and deltax = .0001
$ ./integral 2 0 2 0.0001
integral value = 2.666667
What I have tried:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define PI 3.14159265
double factorial(unsigned int n)
{
double result;
if (n == 0) {
return 1;
}
result = n*factorial(n-1);
return result;
}
double power(double num, int n)
{
double result = 1.0;
double stable = num;
for(int i=1; i<n+1; i++)
{
result = result * stable;
}
return result;
}
double functionOne(double a, double b, double delta)
{
double answer = 0;
int i;
int sign = 1;
for (i=a; i<=b; i+= delta) {
double xterm = (pow(b,3)-pow(a,3))/3;
double nfact = factorial(i);
sign = sign * -1;
answer = answer + sign*xterm/nfact;
}
return answer;
}
double functionTwo(double a, double b)
{
double answer = (pow(b,3)-pow(a,3))/3;
return answer;
}
double functionThree(double a, double b, double delta)
{
double answer = 0;
int i;
int sign = 1;
for (i=a; i<=b; i+= delta) {
double xterm = (pow(b,3)-pow(a,3))/3;
sign = sign * -1;
answer = answer + sign*xterm;
}
return answer;
}
int main(int argc, char **argv)
{
double select = atof(argv[1]);
double a = atof(argv[2]);
double b = atof(argv[3]);
double delta = atof(argv[4]);
if(select == 1)
{
double answer = functionOne(a,b,delta);
printf("integral value is: %f\n", answer);
}
else if(select == 2)
{
double answer = functionTwo(a,b);
printf("integral value is: %f\n", answer);
}
else if(select == 3)
{
double answer = functionThree(a,b,delta);
printf("integral value is: %f\n", answer);
}
}