Urgent! Trouble with a Programming Assignment Please help

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:
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
#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;
            
            } 
Last edited on
Ok I have tried changing up the function in my program ,but now it won't compile all the time when I click to compile. My computer will just freeze and I have to turn it off and on ,so far I've had to do this 6 times.
This is my new function for the code but now it gives me 665000 cubic units.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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;
            
            }    
I have been working on my code since my last post and now I have it so that it gives me 980000 cubic feet! so close and yet so far away the answer needs to be 985000 cubic feet.
My new code:
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

#include <iostream>
#include <conio.h>
#include <math.h>
#include <iomanip>

using namespace std;
// Prototypes
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%2==0)
                       sum+=yValue*4.0;
                      else
                        sum+=yValue*2.0;
                        
                      
            }
            
            deltaX=(length/static_cast<double>(n-1))/3.0;
            
            return deltaX*sum;
            
            }    
         

it should be:
1
2
3
4
if(i%2==0)
   sum+=yValue*2.0;
else
   sum+=yValue*4.0;
Topic archived. No new replies allowed.