C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old October 27th, 2009, 01:14 AM
dvsumosize dvsumosize is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2009
Posts: 3 dvsumosize User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 33 m 33 sec
Reputation Power: 0
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.

Reply With Quote
  #2  
Old October 28th, 2009, 05:18 AM
MaHuJa's Avatar
MaHuJa MaHuJa is offline
Contributing User
Click here for more information.
 
Join Date: Dec 2007
Posts: 990 MaHuJa User rank is Private First Class (20 - 50 Reputation Level)MaHuJa User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 1 Week 15 h 32 m 39 sec
Reputation Power: 3
Send a message via Skype to MaHuJa Send a message via XFire to MaHuJa
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:
Programming by Coincidence
Fred types in some more code, tries it, and it still seems to work. [Then] the program suddenly stops working. [...] Fred doesn’t know why the code is failing because he didn’t know why it worked in the first place.

Reply With Quote
  #3  
Old October 29th, 2009, 04:14 AM
Itsacon's Avatar
Itsacon Itsacon is offline
Command Line Warrior
Click here for more information
 
Join Date: Aug 2004
Location: Sector ZZ9 Plural Z Alpha
Posts: 1,030 Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)  Folding Points: 1978056 Folding Title: Super Ultimate Folder - Level 4Folding Points: 1978056 Folding Title: Super Ultimate Folder - Level 4Folding Points: 1978056 Folding Title: Super Ultimate Folder - Level 4Folding Points: 1978056 Folding Title: Super Ultimate Folder - Level 4Folding Points: 1978056 Folding Title: Super Ultimate Folder - Level 4Folding Points: 1978056 Folding Title: Super Ultimate Folder - Level 4Folding Points: 1978056 Folding Title: Super Ultimate Folder - Level 4Folding Points: 1978056 Folding Title: Super Ultimate Folder - Level 4Folding Points: 1978056 Folding Title: Super Ultimate Folder - Level 4
Time spent in forums: 1 Week 9 h 13 m 59 sec
Reputation Power: 7
Send a message via ICQ to Itsacon
Quote:
Originally Posted by dvsumosize
He doesnt want us using calculus or integrals or antying.


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:
Original - C Code
  1. #include <stdio.h>
  2. #include <math.h>
  3.  
  4. /*  First function    */
  5. double y1(double x)
  6. {
  7.     return (x*x);
  8. }
  9.  
  10. /*  Second function   */
  11. double y2(double x)
  12. {
  13.     /*return (-1.0) * (x-1) * (x-1) + 4.0;
  14.     return (-1.0) * x * x + 2 * x + 3;*/
  15.     return (-1.0 * x + 3) * (x + 1.0);
  16. }
  17.  
  18. /*  Calculate difference. Defaults to zero  if negative (so we get only the area in between the graphs.   
  19.     Since y2 is the upper function, that one should always be the bigger value  */
  20. double difference(double x)
  21. {
  22.     double diff;
  23.     diff = y2(x) - y1(x);
  24.     return diff > 0 ? diff : 0.0;
  25. }
  26.  
  27.  
  28. int main()
  29. {
  30.     int precision;
  31.     double stepsize, surface, i;
  32.    
  33.     printf("Please enter the desired precision (digits behind decimal point)\n");
  34.     scanf("%i", &precision);
  35.    
  36.     stepsize = 1.0 / pow(10.0, (double) precision);
  37.     printf("Using steps of %.*f\n", precision, stepsize);
  38.    
  39.     /*  First intersection lies at -0.822875655, so start at x = -1   */
  40.     /*  Second intersection lies at 1.822875656, so terminate at x = 2 */
  41.     for(i = -1.0, surface = 0; i < 2.0; i += stepsize)
  42.     {
  43.         surface += stepsize * difference(i);
  44.     }
  45.    
  46.     printf("surface area between graphs is %.*lf\n", precision, surface);
  47.    
  48.     return 0;
  49. }


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

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > Ok new thing, area between 2 parabola


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump




 Free IT White Papers!
 
Create the Optimal Architecture for your Critical Applications
Warburton's the largest independently owned bakery in the UK faced a number of difficult challenges in providing the most robust yet efficient IT infrastructure for their organization's success. IBM's services combined with their xSeries servers created the perfect platform for their SAP environment with sufficient flexibility, and did so in very time effective fashion.

Request Your Free Technology Downloads!
 
Five Best Practices for Deploying a Successful Service-Oriented Architecture
This white paper describes the benefits you can expect with SOA, and how IBM can help take your business there.

Request Your Free Technology Downloads!
 
Gartner Magic Quadrant for Application Delivery Controllers
Gartner summarizes its view on Application Delivery Controllers, evaluates strengths and weaknesses of solutions, and provides Magic Quadrant reporting for a quick comparison across all vendors. Learn from Gartner how you can benefit from an all-in-one device like Citrix NetScaler that delivers the highest levels of availability, performance and security.

Request Your Free Technology Downloads!
 
Knowledge is Power
What you don't know can hurt you, and is likely costing you money and increasing your security risks during an era of scarce resources. This white paper proposes six key strategies that enterprise security managers can use to improve their network defense posture.

Request Your Free Technology Downloads!
 
Rationalizing the Multi-Tool Environment
The rationalized multi-tool approach is flexible, scalable and cost effective. It provides the necessary input to the IT service management business processes. It preserves prior investments in monitoring tools, empowers technologists to select the best tools with which to do their jobs, and enhances effective response to incidents.

Request Your Free Technology Downloads!
 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 




© 2003-2010 by Developer Shed. All rights reserved. DS Cluster 11 Hosted by Hostway
For more Enterprise Application Development news, visit eWeek