| ||||||||||||||||||||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
Ok new thing, area between 2 parabola
My computer pogramming teacher asked us to find the area between y=x^2 and y = (-1) * (x-1)^2 + 4. we must write a code and, with using only slices of triangle area, find the area between the 2 parabolas to as close as possible. I have no clue at all how to code this one. He doesnt want us using calculus or integrals or antying.
|
|
#2
|
||||
|
||||
|
Make the two formulas into a f() and a g(). I suggest taking and returning floats in both cases. Also, the correct c++ syntax is to use the pow() function rather than the ^ operator, which means something else entirely.
I suggest defining a type for coordinates; typedef std: air<float,float> coord; is my suggestion, though struct coord { float x; float y; }; will work.Make a function taking three coords and returning the area of the triangle. Find the start and end points (detect intersections? ask user? hardcode values?) Define a stepsize. (Constant? Range/Constant?) More on this below. Now, the main loop; find the area of f(x), g(x), f(x+stepsize); find the area of f(x+stepsize), g(x), g(x+stepsize); increase x by stepsize, and redo that until you reach the endpoint. The sum of all those areas, is the answer you want. ---- Make sure to work on the skill of breaking down a problem into steps like I did above; problem solving is a significant part of programming skill. ---- Stepsize: The algorithm outlined above will be inaccurate when curves (like x^2) are involved. When the squared factor is of opposite signs, both sides get inaccurate in the same direction, which instead of partially cancelling each other out form a bias. The inaccuracy is decreased by using a smaller stepsize. As long as you're using floats rather than double, you should make sure the step size is no smaller than 0.00001 times the furthest-from-zero of the begin and end values, or you may end up with the x increment not actually changing it. (iow: infinite loop) To make the tradeoff clear, consider if you had to do this calculation 500000 times for each frame in a game; probably in addition to other stuff. How small a stepsize can you afford? (At that point, you'd probably use a calculus-type solution anyway.)
__________________
Quote:
|
|
#3
|
||||||
|
||||||
|
Quote:
Tell your teacher he's teaching you bad coding practices. If there is a mathematical formula for something, that ALWAYS leads to a faster and more efficient program than brute forcing it. If you've ever participated in a programming championship, you'll know that's the ONLY way to fix the problems there. That said: C Code:
I'm waiving my usual `do not supply complete solutions for homework' rule here, as the assignment is stupid. To prove this to your teacher: enter a precision of 10 or larger, start the program, and then at the same time solve the problem by hand using integrals. You'll probably beat the computer.
__________________
This is my code. Is it not nifty? "The biggest problem encountered while trying to design a system that was completely foolproof, was, that people tended to underestimate the ingenuity of complete fools." ---Douglas Adams Join the Itsacon fanclub! Zero Tolerance: Spammers banned so far: 564
![]() |
![]() |
| Viewing: Dev Articles Community Forums > Programming > C/C++ Help > Ok new thing, area between 2 parabola |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|