Integral using functions

Mar 31, 2015 at 10:20pm
I am supposed to write a program that lets the user enter three positive values: a, b, and n. a is the lower bound and b is the upper bound and n is the power of x. I don't know where to begin to start creating this program using functions. Every where I have looked it is very hard to create an integral in c++. Any help with be much appreciated.
Mar 31, 2015 at 10:51pm
Could you solve this problem on paper without programming it?

For example...
Let's say a = 0, b = 4, and n = 2.

Could you write out the steps needed to solve the problem?
Mar 31, 2015 at 11:53pm
No the professor wants us to make a program that can solve integrals by plugging in the values of a, b and n.
Mar 31, 2015 at 11:56pm
Right, in the end you want to write a program.

If you cannot do this problem on paper (without a program), it is going to be very difficult for you to write a program to do it. You have to understand how to do an integral yourself before you can write a program to do that.

Do you understand how to solve an integral like this without using code?
Mar 31, 2015 at 11:57pm
Are you required to assume that the function can be anything or can you compute the integral using actual calculus?

I presume it is the latter, so just integrate xn over the proper interval. Your function should look like

1
2
3
4
5
6
7
8
double f( double a, double b, double n )
{
  /* compute 
    ⌠b
    │ xna
  here with simple calculus and return the result */
}

Do you know basic calculus?

(If not, this reading is a good start: http://www.mathsisfun.com/calculus/integration-introduction.html. tl;dr: The indefinite integral of x2 is x3/3+C. Plug in your a and b values, and find the difference.)

Hope this helps.
Apr 1, 2015 at 12:23am
I understand how to do integrals. My problem is that I am having trouble trying to get the logic into code to make the program run correctly.
Apr 1, 2015 at 12:32am
So you have the function signature (do you know if the values need to be doubles or ints or unsigned ints or something else?) by Duoas.

Suppose "n" is 2. When taking the integral, what becomes the coefficient of x, and what becomes the new power of x?

First tell me the numbers, then tell me what those numbers are in terms of n.
Apr 1, 2015 at 1:07am
I understand how to do integrals. My problem is that I am having trouble trying to get the logic into code to make the program run correctly.
At this point I'm not sure how much to believe from you. Your question is exceedingly vague, you refuse to clarify when asked, and you show no proof of effort.

So, can you write a function to solve a simple algebraic expression?

Because it sounds like that's all you are expected to do. You may find the std::pow() function in <cmath> useful.
Topic archived. No new replies allowed.