FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » how to make a function recursive
Show: Today's Messages :: Polls :: Message Navigator
Switch to threaded view of this topic Create a new topic Submit Reply
how to make a function recursive [message #174968] Wed, 27 July 2011 17:58 Go to next message
jr is currently offline  jr
Messages: 4
Registered: January 2011
Karma: 0
Junior Member
I have this circle of chairs from 1-100. I want to subtract every
other chair starting from chair 1 recursively until I find the last
remaining chair.
I could do it in passes with the function below by returning $out.

I thought I could make the function recursive by adding the return
(count($arr[$key])==1?$out: count_chairs($arr)) instead of $out but I
don't know how to pass the first array in and then pass in the
difference of the $out and the new array on each pass?

Thanks,
?php

function count_chairs($arr)
{
// how many elements in array
$i = count($arr);

// Creat an index to positions 0, 2, 4, ... $i
$index = range(1, $i, 2);

// Return the data at positions 0, 1, 2, ...
$out = array();
foreach($index as $key)
{
$out[] = $arr[$key];
}

return $out;
}

//fill array
$arychairs= range(1,100);

$filtered = count_chairs($arychairs);

echo "<pre>";
print_r($filtered);
echo "</pre>";


?>
-------------------------------------------------
The remaining chairs from the first pass is all the even chairs.
Array
(
[0] => 2
[1] => 4
[2] => 6
[3] => 8
[4] => 10
[5] => 12
[6] => 14
[7] => 16
[8] => 18
[9] => 20
[10] => 22
[11] => 24
[12] => 26
[13] => 28
[14] => 30
[15] => 32
[16] => 34
[17] => 36
[18] => 38
[19] => 40
[20] => 42
[21] => 44
[22] => 46
[23] => 48
[24] => 50
[25] => 52
[26] => 54
[27] => 56
[28] => 58
[29] => 60
[30] => 62
[31] => 64
[32] => 66
[33] => 68
[34] => 70
[35] => 72
[36] => 74
[37] => 76
[38] => 78
[39] => 80
[40] => 82
[41] => 84
[42] => 86
[43] => 88
[44] => 90
[45] => 92
[46] => 94
[47] => 96
[48] => 98
[49] => 100
)
Re: how to make a function recursive [message #174969 is a reply to message #174968] Wed, 27 July 2011 19:51 Go to previous message
Denis McMahon is currently offline  Denis McMahon
Messages: 634
Registered: September 2010
Karma: 0
Senior Member
On Wed, 27 Jul 2011 10:58:55 -0700, jr wrote:

The following will, I think, do what you want. As an exercise in learning
php, I leave it to you to work out how it does it. Sorry if these seems
rather abrupt of me, but your question sounds like a tutorial or homework
of some sort, so I'm not prepared to provide both an explanation and
working code.

<?php
function t($a)
{
$c = 0;
foreach ($a as $i=>$d) if ($d>0) $c++;
return $c;
}
function r($a,$i=0)
{
if (t($a)==1) return $a;
do
{
$i+=1;
if ($i==count($a)) $i=0;
}
while (($a[$i])<0);
$a[$i]=-1;
do
{
$i+=1;
if ($i==count($a)) $i=0;
}
while (($a[$i])<0);
return r($a,$i);
}
$a = array();
for ($i=0;$i<100;$i++) $a[]=1;
$a = r($a);
foreach ($a as $i=>$d) if ($d>0) echo "{$i} ";
echo "\n";
?>

Rgds

Denis McMahon
Re: how to make a function recursive [message #174970 is a reply to message #174968] Wed, 27 July 2011 19:51 Go to previous message
Denis McMahon is currently offline  Denis McMahon
Messages: 634
Registered: September 2010
Karma: 0
Senior Member
On Wed, 27 Jul 2011 10:58:55 -0700, jr wrote:

The following will, I think, do what you want. As an exercise in learning
php, I leave it to you to work out how it does it. Sorry if these seems
rather abrupt of me, but your question sounds like a tutorial or homework
of some sort, so I'm not prepared to provide both an explanation and
working code.

<?php
function t($a)
{
$c = 0;
foreach ($a as $i=>$d) if ($d>0) $c++;
return $c;
}
function r($a,$i=0)
{
if (t($a)==1) return $a;
do
{
$i+=1;
if ($i==count($a)) $i=0;
}
while (($a[$i])<0);
$a[$i]=-1;
do
{
$i+=1;
if ($i==count($a)) $i=0;
}
while (($a[$i])<0);
return r($a,$i);
}
$a = array();
for ($i=0;$i<100;$i++) $a[]=1;
$a = r($a);
foreach ($a as $i=>$d) if ($d>0) echo "{$i} ";
echo "\n";
?>

Rgds

Denis McMahon
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: Getting error while requesting data from the UPS webservice
Next Topic: Avoiding Proxy Caching
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Sun Nov 24 00:27:48 GMT 2024

Total time taken to generate the page: 0.02411 seconds