Connecting Tech Pros Worldwide Help | Site Map

recursively draw the lines

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 19th, 2005, 06:48 PM
Matt
Guest
 
Posts: n/a
Default recursively draw the lines

I am writing a recursive program to draw the lines recursively, given the
range[min,max] and number of intervals (n) between the range.

The problem is I don't know how to draw the line in point 0.375, as you see below.
Please advise! Thanks!


#include <iostream>
using namespace std;

void draw(double min, double max, int n);

int main()
{ draw(0,1,8);
}

void draw(double min, double max, int n)
{ if (n != 1)
{ double mid = (max - min)/2;
cout << mid << endl;
draw(min, mid, n/2);
}
}


The program output:
0.5
0.25
0.125

Here's the expected output:
0.5
0.25
0.125
0.375
0.75
0.625
0.875

  #2  
Old July 19th, 2005, 06:48 PM
.oO LGV Oo.
Guest
 
Posts: n/a
Default Re: recursively draw the lines


"Matt" <jrefactors@hotmail.com> a écrit dans le message de
news:ba8a039e.0310050943.52034ee@posting.google.co m...[color=blue]
> I am writing a recursive program to draw the lines recursively, given the
> range[min,max] and number of intervals (n) between the range.
>
> The problem is I don't know how to draw the line in point 0.375, as you[/color]
see below.[color=blue]
> Please advise! Thanks!
>
>
> #include <iostream>
> using namespace std;
>
> void draw(double min, double max, int n);
>
> int main()
> { draw(0,1,8);
> }
>
> void draw(double min, double max, int n)
> { if (n != 1)
> { double mid = (max - min)/2;
> cout << mid << endl;
> draw(min, mid, n/2);
> }
> }
>
>
> The program output:
> 0.5
> 0.25
> 0.125
>
> Here's the expected output:
> 0.5
> 0.25
> 0.125
> 0.375
> 0.75
> 0.625
> 0.875[/color]

shouldn't you call draw(mid, max too, n/2) ?


  #3  
Old July 19th, 2005, 06:48 PM
Pete Becker
Guest
 
Posts: n/a
Default Re: recursively draw the lines

".oO LGV Oo." wrote:[color=blue]
>
>
> shouldn't you call draw(mid, max too, n/2) ?[/color]

draw(mid, max, n - n/2);

n could be odd.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,840 network members.