Thread: Programming challenge

  1. #1
    Registered User
    Join Date
    Feb 2018
    Posts
    1

    Programming challenge

    The cubic function ax^2 + bx + c achieves the value 0 for certain values of x (called the roots of the cubic) – these depend on the parameters a, b, c, and d. Note that unlike a quadratic function, a cubic function is guaranteed to have at least one real root. Your job is to numerically find one root of the cubic using an efficient search method called “the bisection method”. This works as follows.

    The bisection method operates by creating an interval that contains the root we seek, and then repeatedly shrinking that interval so that we “home in” on the solution. First, we choose two numbers, l and u. For starters, you may choose, for example, l = -10 and u=10 . These guesses are chosen such that f(l) and f(u) have different signs. This means the root we seek must lie in the interval between l and u . The trick underpinning the bisection method is to consider the midpoint m= (l+u)/2. If f(x) changes sign between m and u , then the root must lie between these two points, and therefore our interval should shrink accordingly – we can achieve this by setting m to be the new value of l. If, on the other hand, f(x) changes sign between l and m, then the root must lie between these two points – so we set m to be the new value of u . We continue this procedure until the search interval becomes extremely small, at which point the value of m will provide a good estimate of the root.

    The program should be called root.c and program execution should look like the following (user input in bold):

    ************************************************ Welcome to the cubic root estimator. This estimates the value of one root of f(x)=ax^3+bx^2+cx+d. ************************************************

    Enter the coefficients in the form “a b c d”: 1 -3 -3 -4

    There is a root at: x = 4.000 Do you wish to try another cubic [y/n]: n

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Looks more like homework than challenge.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. c++// Programming Challenge #11 in Chapter 8
    By Selena Yapura in forum C++ Programming
    Replies: 7
    Last Post: 04-23-2014, 02:43 PM
  2. Beginners C Programming Challenge
    By UCnLA in forum C Programming
    Replies: 2
    Last Post: 03-18-2008, 12:15 PM
  3. Programming challenge
    By sybariticak47 in forum Windows Programming
    Replies: 1
    Last Post: 01-05-2006, 02:14 AM
  4. programming challenge
    By grohyt in forum C++ Programming
    Replies: 3
    Last Post: 09-19-2005, 07:07 PM
  5. C programming challenge
    By lost in C in forum C Programming
    Replies: 6
    Last Post: 03-11-2002, 01:27 AM

Tags for this Thread