Hi I need help with finding the area underneath a curve. Here is what I have so far;

#include "stdafx.h"
    #include <iostream>
    #include <string>
    #include <cmath>

    using namespace std;

    struct term
        float coeF;
        float exP;
        char name;

    struct function
        int size;
        term terms[10];

    struct Integral
        float lowerBound;
        float upperBound;
        function func;
    float findHeightOfFunction(function func, float x);

    int main()
        Integral integral;

        char userContinue = 'y';

        for(int i=0;i<10;i++)
            //fill the term
            cout<<"Enter in a coefficient: ";

            cout<<"Enter in an exponent: ";

            cout<<"Do you want to enter in more terms: ";

            if(userContinue != 'y')

        cout<<"Enter in the lower bound: ";

        cout<<"Enter in the upper bound: ";

        //integral is filled, find area under curve
        float sum = 0.0;

        for(float x = integral.lowerBound; x < integral.upperBound; x = x + .001)
            sum = sum + (.001 * findHeightOfFunction (integral.func, x ));
        return sum;

        return 0;


    float findHeightOfFunction(function func, float x)



    // pow function here....
    //integral.f.terms[?].coefficient*pow(X,integral.f.terms[ ? ].exponent);
    //for(int i=a i++)
    //Need Loop to Find Height

any help is appreciated.

3 Years
Discussion Span
Last Post by Israel_1

I was rushing out to work when I posted.

What this program is supposed to do is collect from 2 to up to ten terms from a user to find the area underneath the curve.

The input to the program will be a function. For example:
f(x) = x^2


It looks like you are computing the height of the function by moving over an x-quantity 0.001, computing the area of that small segment, and then adding it to the running total. I think that is the triangle rule; is that the method you are supposed to use?

In any case, where are you having problems? Compiling the program? Running it? Getting incorrect results?


Hello David,

The answer to your question about the running total and x quantity 0.001 is yes, and I think the rule is rectangle but I'm not an expert just trying to understand how this works. I say rectangle because I saw an example where if we break down an area by a bunch of small lines of 0.001 and then add them together we get a more accurate calculation of the area we are trying to find.

I'm a beginer just starting to learn programming and when I get stuck it's like running into a wall with no where to turn unless someone points me to the right direction.

I spent days trying to figure out this other program which reads a text file and then user get to choose x most frequent words & frequencies, words with x characters etc.: The problem was this piece of code "for(int i=0; i < xMostFreqW;i++)" i had "> instead "

And I also discover I could use more then 2 vectors for this program too funny!

//Print the results
        cout << "The Total Number of Words Is: " << words.size() << "\n";
        cout << "There Are: " << distinctWords.size() << " Distinct Words" << endl;

        //bubble sort
        for(int j=0; j < frequencies.size()-1; j++)
            for(int i=0; i < frequencies.size()-1; i++)
                if(frequencies[i] < frequencies[i + 1])

                    //swap left & right
                    //1.copy left val
                    int temp = frequencies[i];
                    string tempstring = distinctWords[i];

                    //2. Store the right inside left
                    frequencies[i] = frequencies[i+1];
                    distinctWords[i] = distinctWords[i+1];

                    //3. Store temp into right
                    frequencies[i+1] = temp;
                    distinctWords[i+1] = tempstring;


        //find x most frequent words

        int xMostFreqW; 
        cout<<"Enter a number to find the x most frequent words: ";
        cin >> xMostFreqW ;     

        for(int i=0; i < xMostFreqW;i++)
            //cout <<distinctWords[i]<<" "<<frequencies[i]<<endl;
            cout << frequencies[i] << " "<<distinctWords[i] << endl;    

        //find the x most freq words that are y letters or more

Okay, coming back to this problem I'm working on. I'm stuck here trying to uderstand what I need to do next:

float findHeightOfFunction(function func, float x)


I think I need to do a loop to find the heght. The truth is I don't understand how, what do I use from what I have?

Also, this part of the code has a squigly line underneath with an error:

the begining "integral"


I appreciate your help.


If your function is always going to be composed of A*x^0 + B*x^1 + ... N*x^n then why don't you integrate the function analytically and then evaluate it at the bounds x1 and x2 and subtract them.
f(x) = 10x - 5x^2
x1 = 1, x2 = 5
int(f(x)) = (10/2)x^2 - (5/3)x^3 + C
int(f(5)) - int(f(1)) = Area under curve
((10/2)*5^2 - (5/3)*5^3 + C) - ((10/2)*1^2 - (5/3)*1^3 + C) = Area under curve
Note C-C = 0 so we do not need to solve for C

This method will give you the exact answer rather than a numerical approximation found using rectangle/triangle/simpsons methods.

Coding power rule integration is really easy because it follows an easy form, with the exception of int(x^-1) which is ln(x) + C.
Note ln(x) in C/C++ is log(x) and log(x) is log10(x)

The form is:
f(x) = A*x^n
int(f(x)) = (A/(n+1))*x^(n+1) + C

Because you are always going to be evaluating between two bounds, you can ignore the whole + C part of the integration in your code.

Using this analytical method in your code will not require many changes other than adding a function that will integrate the function input by the user. You can also use a similar function to findHeightOfFunction() that just evaluates the integrated function at a specific point.

As for your findHeightOfFunction() function, you are on the right track with the whole loop idea and the line of code you have that multiplies the coeff with the power. One problem that I see with this is that you have no idea how many terms the user has input into the function. I would either use STL container and push the terms onto that, so you can use the .length property to determine how many terms are in the function or zero all the coeff/exps in the terms array. Using the second method, when you are looping through you can stop on the term that has a coeff of zero and break out of the loop.

If you choose not to use the analytical method then you can continue on with what you have, but note the suggestions in the paragraph above and add in checking for if the lower bound is actually less than the upper bound, otherwise your loop will go forever. Also, maybe you would want the user to input N steps rather than using a hardcoded step size.
h = step size, N = number of steps, x1 = lower bound, x2 = upper bound
h = (x2-x1)/(N+1)

Hope this helps, I could code this all for you but that would be no fun for you!


I will work on this this weekend and provide an update. I really appreciate your help!

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.