Results 1 to 2 of 2

Thread: Urgent C++ program won't work correctly

  1. #1
    Join Date
    Nov 2011
    Posts
    2

    Urgent C++ program won't work correctly

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

  2. #2
    Join Date
    Nov 2011
    Posts
    2

    Re: Urgent C++ program won't work correctly

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

Similar Threads

  1. Replies: 1
    Last Post: 11-11-2011, 04:46 AM
  2. Replies: 6
    Last Post: 12-03-2011, 05:13 PM
  3. Replies: 3
    Last Post: 13-05-2010, 04:48 AM
  4. Sound will not work - Urgent, please help
    By namita_ruhela25 in forum Hardware Peripherals
    Replies: 4
    Last Post: 24-12-2009, 10:54 AM
  5. When I start a program, nothing happens [URGENT!]
    By Espz in forum Windows Software
    Replies: 3
    Last Post: 14-12-2009, 12:58 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,523,112.28253 seconds with 17 queries