### Making a loop and table for outputs

Hello,
I have a program here that can calculate the area of trapezoids from a given function F(x)=2ln(3x) . I have a loop here to ask for a different amount of trapezoids to find the area, but im not sure how to get it to spit out the results for EACH input of trapezoids in a table. The # of trapezoids, the area of said number of trapezoids, and the % difference. in a table. The percent difference is 100*(P-H)/H. Where P is the program values and H is the hand calculation answer (47.826723) .

 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556`` ``````/*-----------------------------------------------------------------*/ /* Program chapter6_12 */ /* */ /* This program estimates the area under a given curve */ /* using trapezoids with equal bases. */ #include //Required for cin, cout #include //Required for exp() using namespace std; // Function prototypes. double integrate(double a, double b, int n); double f(double x); int main() { // Declare objects int num_trapezoids; double a, b, area,k; // Get input from user. cout << "Enter the interval endpoints, a and b\n"; cin >> a >> b; cout << "Enter the number of trapezoids\n"; cin >> num_trapezoids; //Starting with 5 // Estimate area under the curve of 2ln(3x) area = integrate(a, b, num_trapezoids); for (k = 1; k <= 4; k = k + 1) { //How many times we want it to ask cout << "Enter the number of trapezoids again x 10." << endl; cin >> num_trapezoids; } // Print result. cout << "Using " << num_trapezoids << " trapezoids, the estimated area is " << area << endl; return 0; } /*-----------------------------------------------------------------*/ /*-----------------------------------------------------------------*/ double integrate(double a, double b, int n) { // Declare objects. double sum(0), x, base, area; base = (b-a)/n; for(int k=2; k<=n; k++) { x = a + base*(k-1); sum = sum + f(x); } area = 0.5*base*(f(a) + 2*sum + f(b)); return area; } double f(double x) { return(2*log(3*x)); } /*-----------------------------------------------------------------*/``````
Last edited on
Sounds like you are trying something like this - so all you need do is extend it to tabulate the results and comparison percentage. You might have to adjust the line `current_trapezoids = 10 * (k + 1) * num_trapezoids;`.

Tabulation hint: `#include <iomanip> `

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960`` ``````/*-----------------------------------------------------------------*/ /* Program chapter6_12 */ /* */ /* This program estimates the area under a given curve */ /* using trapezoids with equal bases. */ #include // cin, cout #include // exp() using namespace std; double integrate(double a, double b, int n); double f(double x); int main() { int num_trapezoids; double a{0}, b{0}, area{0}; cout << "Enter the interval endpoints, a and b: "; cin >> a >> b; cout << "Enter the number of trapezoids: "; cin >> num_trapezoids; int no_times{0}; cout << "Enter the number of trapezoids again x 10: "; cin >> no_times; int current_trapezoids{0}; for(int k = 0; k < no_times; k++) { current_trapezoids = 10 * (k + 1) * num_trapezoids; area = integrate(a, b, current_trapezoids); cout << "Using " << current_trapezoids << " trapezoids, estimated area is " << area << endl; } return 0; } double integrate(double a, double b, int n) { double sum{0}, x, base, area; base = (b-a)/n; for(int k=2; k<=n; k++) { x = a + base*(k-1); sum = sum + f(x); } area = 0.5*base*(f(a) + 2*sum + f(b)); return area; } double f(double x) { return(2 * log( 3 * x ) ); }``````

 ```Enter the interval endpoints, a and b: 1 10 Enter the number of trapezoids: 5 Enter the number of trapezoids again x 10: 4 Using 50 trapezoids, estimated area is 47.8219 Using 100 trapezoids, estimated area is 47.8255 Using 150 trapezoids, estimated area is 47.8262 Using 200 trapezoids, estimated area is 47.8264 Program ended with exit code: 0```

e.g.
 ``12345678910`` ``````int current_trapezoids{num_trapezoids}; for(int k = 0; k < no_times; k++) { current_trapezoids *= 10; area = integrate(a, b, current_trapezoids); cout << "Using " << current_trapezoids << " trapezoids, estimated area is " << area << endl; }``````

 ```Enter the interval endpoints, a and b: 1 10 Enter the number of trapezoids: 1 Enter the number of trapezoids again x 10: 5 Using 10 trapezoids, estimated area is 47.7083 Using 100 trapezoids, estimated area is 47.8255 Using 1000 trapezoids, estimated area is 47.8267 Using 10000 trapezoids, estimated area is 47.8267 Using 100000 trapezoids, estimated area is 47.8267 Program ended with exit code: 0```
Last edited on
This is similar to what I was looking for but I was trying to get something more like:

# of Trapezoids Integration Value Percent Difference
5 47.3777 −0.93885378
50 47.8219 −0.01008432
500
5000 etc..

The interval is from 1 to 10, which I do not think I mentioned. The −0.93885378 is from 100*((47.3777-47.826723))/(47.826723). Which is the Percent difference formula from before. Where 47.3777 is the output of the program, and 47.826723 is from the hand calculation. I am confused as to how I can get it to generate these numbers and in a table format together.
Last edited on
 This is similar to what I was looking for but I was trying to get something more like:
You're joking.
Okay, nevermind. Thank you for helping.
 ``12345678910111213141516171819202122232425262728293031`` ``````#include #include #include using namespace std; double integrate( double a, double b, int n, double f( double ) ); int main() { auto f = []( double x ){ return 2 * log( 3 * x ); }; auto fintegral = []( double x ){ return 2 * x * ( log( 3 * x ) - 1 ); }; double a = 1, b = 10; double exactI = fintegral( b ) - fintegral( a ); #define SP << setw( 15 ) << cout SP "intervals" SP "estimate" SP "% error" << '\n'; for ( int n = 10; n <= 1000000; n *= 10 ) { double I = integrate( a, b, n, f); cout SP n SP I SP 100 * ( I - exactI ) / exactI << '\n'; } } double integrate( double a, double b, int n, double f( double ) ) { double sum = 0.5 * ( f( a ) + f( b ) ); double dx = ( b - a ) / n; for ( int k = 1; k < n; k++ ) sum += f( a + k * dx ); return sum * dx; }``````

 ``` intervals estimate % error 10 47.7083 -0.247625 100 47.8255 -0.00253966 1000 47.8267 -2.54041e-05 10000 47.8267 -2.54042e-07 100000 47.8267 -2.54024e-09 1000000 47.8267 -2.8911e-11```

Last edited on
If'n you have a compiler capable of using C++20 there is the <format> library for creating tabular output.
 ``12345678910111213141516171819202122232425262728293031323334353637`` ``````#include #include #include #include double integrate(double a, double b, int n, double f(double)); int main() { auto f = [ ] (double x) { return 2 * log(3 * x); }; auto fintegral = [ ] (double x) { return 2 * x * (log(3 * x) - 1); }; double a { 1 }; double b { 10 }; double exactI { fintegral(b) - fintegral(a) }; std::cout << std::format("{:>10}{:>15}{:>15}\n", "intervals", "estimate", "% error"); std::string fmt_tbl { "{:10}{:15.7}{:15.7}\n" }; for (int n { 10 }; n <= 1000000; n *= 10) { double I { integrate(a, b, n, f) }; std::cout << std::format(fmt_tbl, n, I, 100 * (I - exactI) / exactI); } } double integrate(double a, double b, int n, double f(double)) { double sum = 0.5 * (f(a) + f(b)); double dx = (b - a) / n; for (int k { 1 }; k < n; k++) sum += f(a + k * dx); return sum * dx; }``````
 ``` intervals estimate % error 10 47.70829 -0.2476246 100 47.82551 -0.002539661 1000 47.82671 -2.540413e-05 10000 47.82672 -2.540418e-07 100000 47.82672 -2.540242e-09 1000000 47.82672 -2.891095e-11```

If'n your compiler doesn't have <format>/C++20 capability, or you don't want to use C++20 there is the fmt library: