Integration and Differentiation

I have an assignment for my c++ class at college and i wasn't able to go to class for the last week for medical reasons and i was looking for some help...

the assignment is to "Write a program that allows the user to select among built-in functions, an interval, and whether to integrate or differentiate. Make an interface a menu. Use arrays to save function values. "

I was able to spend like 30 mins with a friend and read the book and i was able to come up with this...


#include <iostream>
#include <cmath>
using namespace std;


double derivative(double (*)(double), double, double); //From Schaum #7.5
double riemann(double (*)(double), double, double, int); //From Schaum #7.4

//Built in functions
double fsqrt(double t){return sqrt(t);}
double fsin(double t){return sin(t);}
double fcos(double t){return cos(t);}
double ftan(double t){return tan(t);}
double fexp(double t){return exp(t);}
double flog(double t){return log(t);}
double flog10(double t){return log10(t);}




int main() {





return 0;
}






double derivative(double (*pf)(double t), double x, double h)
{

return ((*pf)(x+h) - (*pf)(x-h))/(2*h);

}

double riemann(double (*pf)(double t), double a, double b, int n)
{
double s = 0, h = (b-a)/n, x;
int i;
for (x = a, i = 0; i < n; x += h, i++)
s += (*pf)(x);

return s*h;

}




i dont know how to approach the main and work with the arrays, i was able to individually try the functions and they do work but i dont know how to select and interval and save it value... and how to do a UI
Any help would be appreciated,
ty
Here are some ideas:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include <iostream>
#include <string>
#include <cmath>

////////////////////////////
// Math stuff
typedef double(*func)(double);

double derivative(func f, double x1, double x2)
{
  return  (f(x1)-f(x2)) / (x1-x2) ; // Very rough
}

double integrate(func f, double x1, double x2)
{
  return ( f(x1) + f(x2) ); // Obviously wrong
}

/////////////////////////////
// Selections:  Make these not global
bool bintegrate = false; 
func function = 0;
double x1 = 0;
double x2 = 0;

/////////////////////////////
// Argument handling
int process_arg(std::string arg)
{
    if( arg.find("--op=") != std::string::npos )
    {
      if (arg.find("int") != std::string::npos)
        bintegrate = true; // There are safer ways to do this
      return 0;
    }

    if( arg.find("--func=") != std::string::npos )
    { 
      if (arg.find("sin") != std::string::npos)
        function = sin;
      else if (arg.find("cos") != std::string::npos)
        function = cos;
      else if (arg.find("sqrt") != std::string::npos)
        function = sqrt;
      else if (arg.find("exp") != std::string::npos)
        function = exp;
      else if (arg.find("log") != std::string::npos)
        function = log;
      // else if nothing is found, there is an error and this will crash, handle that
      return 0;
    }

    if( arg.find("--x1=") != std::string::npos )
    {
      arg = arg.substr(5); // This is very hard-coded (size of "--x1=") You can do better
      x1 = stod(arg); // stod is only C++11.  You could use stringstreams instead
      return 0;
    }

    if( arg.find("--x2=") != std::string::npos )
    {
      arg = arg.substr(5);
      x2 = stod(arg);
      return 0;
    }
    return 1; // Return 1 means we've screwed up a param, print the usage message again

}
int main(int argc, char* argv[])
{
  if (argc <= 1)
    std::cout << "Usage: cmd --op=<int|der> --func=<sin|cos|sqrt|exp|log> --x1=<value> --x2=<value>\n";

  for (int i = 1; i < argc; ++i)
    process_arg( argv[i] );

  if (bintegrate)
      integrate(function, x1, x2);
  else
      derivative(function, x1, x2);

  return 0;
}
Last edited on
Topic archived. No new replies allowed.