# Trouble with Simpson's rule in my program

• 11-27-2011
chinesebarbie
Trouble with Simpson's rule in my program
I am having trouble with my program. I have been working on it for the last couple of days. I am supposed to use Simpson's rule but I am having trouble implementing it into my program.
You have to input:
measure points= 11
enter for y value : 0, 6, 10, 13, 17, 22, 25, 20, 13, 5, 0
the answer is supposed to be 985000 cu. ft. but I keep getting 982500.0 cu. ft.
Here is the code:
Code:

```#include <iostream> #include <conio.h> #include <math.h> #include <iomanip> using namespace std; // Prototyptes double SimsonArea(int, double); int main() {     int NumPoints, RdWidth;     double RdLength, HillCrossSectionalArea, DirtVolume;     char Usrp;         do     {         system("CLS");         cout << "\n\n";           cout <<"\nEnter the length of roadway through the hill:";         cin >> RdLength;         cout <<"\n      Enter the width of the roadway:";         cin >> RdWidth;         cout <<"\nEnter the number of points at which hill";         cout <<"\n          elevation was measured:";         cin >> NumPoints;         cout <<"\nEnter the hill elevations(y values) at the";         cout <<endl << NumPoints << " equally-spaced points:";         cout <<"\n\n";                 HillCrossSectionalArea = SimsonArea(NumPoints,RdLength);                 DirtVolume = RdWidth * HillCrossSectionalArea;                 cout << setprecision(1) << fixed << showpoint;         cout << "\n\n";         cout << "---> The volume of dirt to be removed is";         cout << "\n  Approximately " << DirtVolume << " cubic units.";                 cin.ignore();         cout << "\n\n\n\t Do Another (Y or N):";         cin.get(Usrp);         Usrp = toupper(Usrp);                 } while (Usrp == 'Y');                 }                                         double SimsonArea (int n, double length)         {                 double sum = 0, yValue, deltaX;                             for(int i = 0; i <= n-1; i++)             {                     cout <<"Enter y value #" <<i+1 <<":";                     cin >> yValue;                     if(i==0 || i==n)                       sum += yValue*4.0;                       else                         sum+=yValue;             }                         deltaX=length/static_cast<double>(n-1);             return deltaX*sum;                         }```
• 11-27-2011
chinesebarbie
New function
This is my new function for the code but now it gives me 665000 cubic units.
Code:

```double SimsonArea (int n, double length)         {                 double sum = 0, yValue, deltaX;                             for(int i = 0; i <= n-1; i++)             {                     cout <<"Enter y value #" <<i+1 <<":";                     cin >> yValue;                                         if(i==0 && i==n)                       sum+=yValue*4.0;                       else                         sum+=yValue*2.0;                                   }             deltaX=length/static_cast<double>(n-1);             deltaX/=3.0;             return deltaX*sum;                         }```