This program uses the basis of the Archimedes (287-212 BC) method, known as exhaustion, to approximate the integral of a given real function in a given space [a,b]. First it segments the given space into N subspaces. It calculates: a) the sum of the areas of orthogonal parallelograms of height equal to the minimum of the function in each segment and b) the sum of the areas of orthog. parallelograms of height equal to the max value of f(x) in each segment. Theoretically, when N right infty, the progressions are both converging to the same limit which is the integral of the function.

The program is simple. It requires the segmentation number N and the integration limits a,b. You can easily change the function to your desired one in the function at the end of the code. As you may see, after the calculations, it tries to graph the function. Well don’t expect much, although it has very good output in some functions like the f(x)=xsinx.

The program was written in Borland C++ 5.0. You will need the compiler bcc in order to compile and run it, but it’s very easy to make some alterations for other compilers or other OS’s.

Caution: This was written in DOS. You need to have the egavga.bgi in the same directory as the executable if you
want to graph the function.

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
84
#include <iostream.h>
#include <math.h>
#include <graphics.h>
#include <conio.h>
 
float f ( float ) ;
 
void main (int argc , char *argv[])
{
        float a,b, LowerSum=0 , UpperSum=0, right, left ;
        long  int n;
        int gdriver=DETECT, gmode, errorcode, *table;
 
        if (!argv[1]) cout << "Usage :";
        cout << argv[1];
 
        cout << "\nIntegral Of f(x)=xsinx \n"
             << "\nEnter segmentation number n: ";
        cin >> n; cout << n;
        cout << "\nEnter integration limits a,b: \n";
        cin >> a >> b;
 
        cout << "UpperSum Calculations: \n";
        for (long int k=1; k <= n; k++)
        {
         right= a+k*(b-a)/n;
 
         UpperSum = UpperSum + ( (b-a)/n ) * f ( right );
        }
 
        table = new int [n];
 
        cout << "\n\nLowerSum Calculations: \n";
        for (long int k=0; k <= n-1; k++)
        {
         left=a+k*(b-a)/n;
         table[k]=(int) abs(f( left)-240);
         LowerSum = LowerSum + ( (b-a)/n ) * f ( left );
        }
 
        cout << "\nUpperSum Of f(x)=x is "<< UpperSum;
        cout << "\nLowerSum Of f(x)=x is "<< LowerSum;
        cout << "\n\nPress any key..."; getch();
//graphing the integral
 initgraph (&gdriver, &gmode,"" );
 errorcode=graphresult();
 if (errorcode!= grOk)
 {
  cout << "An Error Occured:" << grapherrormsg (errorcode);
 }
 outtextxy( getmaxx()/2,10 , "Integral of f(x) ");
 
 // axis
 line (0,100,0,400);
 line (0,getmaxy()/2,500,getmaxy()/2);
 for (int i=1; i<=500; i++)
 { if (table[i] < 10 || table [i] > 470) break;
   putpixel(i, table[i], 5);
 
   if (table[i] < 239  )
   {
   for (int k=1; ;k++)
     {
      if (table[i]+k >= 239) break;
      putpixel(i, table[i]+k, 10);
     }
   }
    if (table[i] > 241 )
   {
   for (int k=1; ;k++)
     {
      if (table[i]-k <= 241 ) break;
      putpixel(i, table[i]-k, 14);
     }
   }
 }
getch();
closegraph();
}
 
float  f (float x)
{
 return x*sin(x));
}