This program is supposed to divide up a domain into n parts, each part having two points whose x values are (b-a)/n apart from each other. It then is supposed to add up the lines into a total sum.
#include <iostream>
#include <cmath>
using namespace std;
double f(double x);
double distance(double x1, double x2);
int main()
{
int n=0, a=0, b=0;
cout << "Enter number of segments to divide curve into: ";
cin >> n;
cout << "Enter the domain in the format (a B)/>: ";
cin >> a >> b;
double dx=0;
dx=(b-a)/n;
double d=0;
double sum=0;
for (int i=0; i<=n; i++)
{
cout << "Distance between f(" << (a+dx*i) << ") and f(" << (a+dx*(i+1)) << ") = ";
d=distance((a+(dx*i)),(a+(dx*(i+1))));
cout << d;
sum=+d;
}
cout << endl << "Total length is " << sum;
return 0;
}
double f(double x)
{
return cos(x);
}
double distance(double x1, double x2)
{
double d=sqrt(pow(x2-x1,2)+pow(f(x2)-f(x1),2));
return d;
}

New Topic/Question
Reply



MultiQuote




|