I know its bad to wait last minute ,but I have been working on the code the last couple of days with no result ,and now I'm finally starting to panic.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.

Here is the 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;
            
            }

Recommended Answers

All 5 Replies

I know its bad to wait last minute ,but I have been working on the code the last couple of days with no result ,and now I'm finally starting to panic.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.

Here is the 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;
            
            }

This is my new function for the code but now it gives me 665000 cubic units.

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;
            
            }

As far as i see, i see no error in the program. Please recheck the y values....
I tried the program and i got the 980000 cub ft.....

As far as i see, i see no error in the program. Please recheck the y values....
I tried the program and i got the 980000 cub ft.....

Thanks for checking ,but the answer is supposed to be 985000 cubic feet. I plugged the numbers back in and I still got 982500.0 cubic feet.

The first thing I recommend is that, in order to simplify reasoning about the Simpson's Rule function, you should move the part that gathers the elevation data from the function proper, storing the data in an array which you can then pass to the function. You'll need to allocate the array before using it, and delete it after you've finished with it, but this isn't too serious a problem.

Second, I would look at the example implementation on the Wikipedia page about Simpson's Rule, as it may clarify just how the rule normally works.

I have tried to apply both of these suggestions myself, but the function is still not quite correct. Nonetheless, I think that this altered code may help you in working out the correct method.

#include <iostream>
#include <cmath>
#include <iomanip>

using namespace std;

// Prototypes
double SimpsonArea(int, int*, double);

int main()
{
    int NumPoints, RdWidth;
    double RdLength, HillCrossSectionalArea, DirtVolume;
    char Usrp;

    do
    {
        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";

        int *yValues = new int[NumPoints];

        for(int i = 0; i < NumPoints; i++)
        {
            cout <<"Enter y value #" <<i+1 <<":";
            cin >> yValues[i];
        }

        HillCrossSectionalArea = SimpsonArea(NumPoints, yValues, RdLength);

        delete yValues;

        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 SimpsonArea (int n, int* yValue, double length)
{
    double sum = yValue[0], deltaX;

    deltaX = length / n;

    for(int i = 1; i < n; i += 2)
    {
        sum += yValue[i] * 4.0;
    }

    for(int i = 2; i < n - 1; i += 2)
    {
        sum += yValue[i] * 2.0;
    }

    sum += yValue[n-1];

    return (deltaX * sum) / 3;
}

When I tested this (using Code::Blocks, which I highly recommend over Dev-C++ and other older IDEs), I got a final value of 895454.5 Cu. Ft., which is still off. Despite this, I do think that this is headed in the direction you will want.

I've gone back to my version of the original algorithm:

double SimpsonArea (int n, int* yValue, double length)
{
    double sum = 0, deltaX;

    for(int i = 0; i < n; i++)
    {
        if ((i == 0) || (i == (n - 1)))
            sum += yValue[i] * 4.0;
        else
            sum += yValue[i];
    }

    deltaX = length / (n - 1);
    return deltaX * sum;
}

... and I'm having trouble seeing where the problem lies. Can you give us the original description of the algorithm that the professor gave you to use?

(Oh, and I simplified the testing process by re-writing the main() function to read the data from a CSV file; I can pass that along as well if you'd like.)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.