Trouble with simpson's rule in a Programming Assignment

Discussion in 'Programmer's Corner' started by chinesebarbiedoll, Nov 27, 2011.

  1. chinesebarbiedoll

    Thread Starter New Member

    Nov 9, 2011
    9
    0
    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:
    road length= 1000
    road width= 75
    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.:confused:
    Here is the code:
    Code ( (Unknown Language)):
    1.  
    2. #include <iostream>
    3. #include <conio.h>
    4. #include <math.h>
    5. #include <iomanip>
    6. using namespace std;
    7. // Prototyptes
    8. double SimsonArea(int, double);
    9. int main()
    10. {
    11.     int NumPoints, RdWidth;
    12.     double RdLength, HillCrossSectionalArea, DirtVolume;
    13.     char Usrp;
    14.    
    15.     do
    16.     {
    17.          system("CLS");
    18.          cout << "\n\n";
    19.    
    20.          cout <<"\nEnter the length of roadway through the hill:";
    21.          cin >> RdLength;
    22.          cout <<"\n      Enter the width of the roadway:";
    23.          cin >> RdWidth;
    24.          cout <<"\nEnter the number of points at which hill";
    25.          cout <<"\n          elevation was measured:";
    26.          cin >> NumPoints;
    27.          cout <<"\nEnter the hill elevations(y values) at the";
    28.          cout <<endl << NumPoints << " equally-spaced points:";
    29.          cout <<"\n\n";
    30.          
    31.          HillCrossSectionalArea = SimsonArea(NumPoints,RdLength);
    32.          
    33.          DirtVolume = RdWidth * HillCrossSectionalArea;
    34.          
    35.          cout << setprecision(1) << fixed << showpoint;
    36.          cout << "\n\n";
    37.          cout << "---> The volume of dirt to be removed is";
    38.          cout << "\n  Approximately " << DirtVolume << " cubic units.";
    39.          
    40.          cin.ignore();
    41.          cout << "\n\n\n\t Do Another (Y or N):";
    42.          cin.get(Usrp);
    43.          Usrp = toupper(Usrp);
    44.          
    45.          } while (Usrp == 'Y');
    46.          
    47.          }
    48.          
    49.          
    50.        
    51.          
    52.          double SimsonArea (int n, double length)
    53.          {
    54.                 double sum = 0, yValue, deltaX;
    55.                
    56.             for(int i = 0; i <= n-1; i++)
    57.             {
    58.                     cout <<"Enter y value #" <<i+1 <<":";
    59.                     cin >> yValue;
    60.                     if(i==0 || i==n)
    61.                        sum += yValue*4.0;
    62.                       else
    63.                         sum+=yValue;
    64.             }
    65.            
    66.             deltaX=length/static_cast<double>(n-1);
    67.             return deltaX*sum;
    68.            
    69.             }
    70.  
     
  2. chinesebarbiedoll

    Thread Starter New Member

    Nov 9, 2011
    9
    0
    This is my new function for the code but now it gives me 665000 cubic units.
    Code ( (Unknown Language)):
    1.  
    2. double SimsonArea (int n, double length)
    3.          {
    4.                 double sum = 0, yValue, deltaX;
    5.                
    6.             for(int i = 0; i <= n-1; i++)
    7.             {
    8.                     cout <<"Enter y value #" <<i+1 <<":";
    9.                     cin >> yValue;
    10.                    
    11.                     if(i==0 && i==n)
    12.                        sum+=yValue*4.0;
    13.                       else
    14.                         sum+=yValue*2.0;
    15.                      
    16.             }
    17.             deltaX=length/static_cast<double>(n-1);
    18.             deltaX/=3.0;
    19.             return deltaX*sum;
    20.            
    21.             }    
    22.  
     
Loading...