为什么我的数学错了?辛普森的规则,C ++

时间:2014-02-05 02:25:02

标签: c++ math

好吧所以所有内容都会编译,但是当我手动进行数学运算(并通过Wolfram Alpha检查)时,数学结果远低于预期的答案。

这是我的主要内容:

#include <cstdlib>
#include <iostream>
#include <fstream>
#include "functions.h"
#include "functions.cpp"

int main(int argc, char* argv[])
{
    //Declare Array Constant Size and Array
    const int SIZE = 9;
    double dataPoints[SIZE];

    //Get File and Data
    //Get File from Command Line
    if(argc < 2)
    {
        std::cerr << "\nUsage: a.out file\n";
        return EXIT_FAILURE;
    }

    //Get Data and Save to Array
    const char* file = argv[2];
    functions::openReadSaveData(file, dataPoints, SIZE);

    //Perform Calculations and Display Max Number of Fishing Licenses
    functions::calculateFishingLicenses(dataPoints, SIZE);

    return EXIT_SUCCESS;
}

以下是我的函数原型:

#ifndef FUNCTIONS_H
#define FUNCTIONS_H

namespace functions
{
    //Pre: A user-defined data file input from the command line
    //Post: Program opens file, reads it, and saves the data to array to be used in calculations
    double openReadSaveData(const char*, double [], const int);

    //Pre: A 1 dimensional array filled with data from the data file
    //Post: Program calculates maximum number of fishing licenses lake may issue and displays the value in addition to an annotation
    void calculateFishingLicenses(int [], const int);
}

#endif

以下是我的实施:

#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
#include "pa1functions.h"

namespace functions
{
    double openReadSaveData(const char* file, double data[], const int NUMPOINTS)
    {
        using std::cerr;
        using std::cout;
        using std::ifstream;

        //Open File
        ifstream inFile(file);

        //Error Checking for File
        if(!inFile)
        {
            cerr << "\nError: Unable to open file.\n";
            return EXIT_FAILURE;
        }
        else
        {
            cout << "\nFile opened successfully.";
        }

        //Get and Save Data
        int index = 0;
        while(index < NUMPOINTS && inFile >> data[index])
        {
            index++;
        }
        cout << "\n";

        inFile.close();
    }

    void calculateFishingLicenses(double data[], const int NUMPOINTS)
    {
        using std::cout;

        //Modify Output
        std::cout << std::fixed << std::setprecision(0) << std::endl;

        //Declare Variables
        double lakeVolume;
        double fishInLake;
        double fishToBeCaught;
        double fishingLicenses;

        //Constants
        const double AVERAGEDEPTH = 20;
        const float HOVERN = (200/3);
        const double FISHRATIO = 1000;
        const float FISHPERCENTAGE = 0.75;
        const double FISHPERLICENSE = 20;

        //Simpson's Rule
        lakeVolume = (AVERAGEDEPTH * HOVERN) * (data[0] + (4 * data[1]) + (2 * data[2]) + (4 * data[3]) + (2 * data[4]) + (4 * data[5]) + (2 * data[6]) + (4 * data[7]) + data[8]);
        cout << "\n" << lakeVolume;

        //Calculate Number of Fish in the Lake
        fishInLake = lakeVolume/FISHRATIO;
        cout << "\n" << fishInLake;

        //Calculate Number of Fish to Be Caught
        fishToBeCaught = fishInLake * FISHPERCENTAGE;
        cout << "\n" << fishToBeCaught;

        //Calculate Number of Fishing Licenses
        fishingLicenses = fishToBeCaught/FISHPERLICENSE;

        //Display Result with Annotation
        cout << "\n" << fishingLicenses;
    }

} //End of Namespace

我使用两种不同类型的数据,两次答案都是错误的。 我正在UNIX服务器上用g ++编译。

我不知道出了什么问题,当我把它输入我的计算器时,我得到415而不是程序的411(使用数据点:0,175,250,500,600,550,450,200,0) 。当我用备用数据解决时,答案应该是160620,而不是159015(分数是25,16,150,5000,750000,7,15000,273000,350000)。

有什么想法吗?

提前致谢!

0 个答案:

没有答案