473,395 Members | 1,468 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Recursion Problem

34
Okay, I have a problem that goes something like this: I have a union of road stripe painters where each painter will only paint a section of 2 miles on the road and charge $200 for the service. If the road is longer than 2 miles, the painter will divide it into three equal parts, charge $100, and subcontract each road to three different painters. Each of the hire will do the same thing where if the road is longer than 2 miles, each of the painters will subcontract another 3 and so on and so on. So the diagram would look something like a k-ary tree. What I have to find is the total cost it takes to paint a certain amount of road length. To illustrate, given an arbritary length of 7 miles the result would be something like:

7 = $100
2.6, 2.6, 2.6 = 3 x $100 = $300
.96, .96, .96, .96, .96, .96, .96, .96, .96 = 9 x $200 = 1800
Total cost - $2200

The code I have so far for a function that calculates the total cost is:

Expand|Select|Wrap|Line Numbers
  1. int costofpainting(double length, int power)
  2. {
  3.     double multiplier(0.0);
  4.     int answer, i;
  5.  
  6.     multiplier = 1.0;
  7.     if(length > 2.0)
  8.     {
  9.         for(i = 0; i < power; i++)
  10.             multiplier = multiplier * 3.0;
  11.  
  12.         answer = (multiplier * 100) + costofpainting(length/3.0, power + 1);
  13.     }
  14.     else if(length <= 0.0)
  15.         return -1;
  16.     else if(length <= 2.0)
  17.     {
  18.         for(i = 0; i < power; i++)
  19.             multiplier = multiplier * 3.0;
  20.  
  21.         return (multiplier * 200);
  22.     }
  23. }
I used a simple driver to test it:

Expand|Select|Wrap|Line Numbers
  1. int main()
  2. {
  3.     int num;
  4.     num = costofpainting(7.0, 0);
  5.  
  6.     cout << num << '\n';
  7.  
  8. }
However, the ending results prints out 80320 which is incorrect. The actual prototype given for the costofpainting function is "int costofpainting(double length);" But no matter how hard I try I couldnt figure out a formula that would work to compute the cost unless I have an additional variable passed in, hence int power. That way, with each division of the number entered, power will be increased to indicate how many sets of three painters are hired with each subcontracting. As far as tracing the algorithm that I wrote, it seems fine, unless I'm doing something wrong. Anyone can clarify or help me figure out how to do it by using the function prototype given?

Regards,
Jthep
Nov 16 '07 #1
1 912
JosAH
11,448 Expert 8TB
You're overcomplicating matters; a piece of road is either <= 2 miles or it is
longer. so (in pseudo code):

Expand|Select|Wrap|Line Numbers
  1. cost(double miles) =
  2.    if road <= 2 miles then cost = 200 // can do it yourself
  3.    else cost = 100+3*cost(miles/3) // get 100 and divide the job
  4.  
kind regards,

Jos
Nov 16 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

43
by: Lorenzo Villari | last post by:
I've tried to transform this into a not recursive version but without luck... #include <stdio.h> void countdown(int p) { int x;
10
by: paulw | last post by:
Hi Please give problems that "HAS TO" to use recursion (recursive calls to itself.) Preferrably real world examples, not knights tour. I'm thinking about eliminating the use of stack... ...
75
by: Sathyaish | last post by:
Can every problem that has an iterative solution also be expressed in terms of a recursive solution? I tried one example, and am in the process of trying out more examples, increasing their...
18
by: MTD | last post by:
Hello all, I've been messing about for fun creating a trial division factorizing function and I'm naturally interested in optimising it as much as possible. I've been told that iteration in...
6
by: Andre Kempe | last post by:
hej folks. i have a heap with fixed size and want to determine the depth of a element with given index at compile-time. therefore i wrote some templates. however, when i use template...
12
by: NOO Recursion | last post by:
Hi everyone! I am trying to write a program that will search a 12x12 for a thing called a "blob". A blob in the grid is made up of asterisks. A blob contains at least one asterisk. If an...
13
by: Mumia W. | last post by:
Hello all. I have a C++ program that can count the YOYOs that are in a grid of Y's and O's. For example, this Y O Y O O Y O Y O Y O O Y O Y Y O Y O Y O O Y O O Y Y O Y O
2
by: Victor Lin | last post by:
Hi, I encounter a problem with pickle. I download a html from: ...
30
by: Jeff Bigham | last post by:
So, it appears that Javascript has a recursion limit of about 1000 levels on FF, maybe less/more on other browsers. Should such deep recursion then generally be avoided in Javascript?...
35
by: Muzammil | last post by:
int harmonic(int n) { if (n=1) { return 1; } else { return harmonic(n-1)+1/n; } } can any help me ??
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.