Click here to Skip to main content
15,886,518 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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;	// flip sign from 1 to -1 and vice versa
		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;	// flip sign from 1 to -1 and vice versa
		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);
  }
  
}
Posted
Updated 14-May-21 12:38pm
v3
Comments
Patrice T 14-May-21 17:40pm    
The end of your code is missing.
Member 15200008 14-May-21 17:57pm    
I updated
Patrice T 14-May-21 18:07pm    
Nice, now the error messages.
[no name] 14-May-21 17:40pm    
The is no "main" entry point.
Member 15200008 14-May-21 17:57pm    
i updated

1 solution

Given:
C
int i = 1;
double delta = .001;
i += delta;

What is the value of i at the end of the code snippet? Does that help you fix the issue with your program?
 
Share this answer
 
v2
Comments
Member 15200008 14-May-21 21:03pm    
Delta is only given a value based as a parameter as a input by the user when the program is compiled. But the program will not compile and I am not sure why via my code. When it runs there is no errors though.
k5054 15-May-21 10:00am    
Take a look at functionThreee. The parameter passed in as delta is a double. The for loop uses a integer index i, but then adds the double delta to i. Since i is an integer, it can only have the values 0, ±1, ±2, ±3 etc, then adding an delta < 1.0 does not change the value of i.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900