I need my function in php to be recursive

0

I need this function to be recursive, I'm a beginner in php and I'm not really understanding how I can do this. :

<?php

    function mostraArray() {

        $cena=array($_REQUEST["numeroa"], $_REQUEST["numerob"], $_REQUEST["numeroc"], $_REQUEST["numerod"], $_REQUEST["numeroe"]);

        for ($i=0; $i<=count($cena)-1; $i++) {
            return $cena[$i];
            if ($i<4){
                return ", ";
            }
        }
    }

    function ordenaArray() {

        $cena=array($_REQUEST["numeroa"], $_REQUEST["numerob"], $_REQUEST["numeroc"], $_REQUEST["numerod"], $_REQUEST["numeroe"]);
        rsort($cena);

        for ($i=0; $i<=count($cena)-1; $i++) {
            return $cena[$i];
            if ($i<4){
                return ", ";
            }
        }
    }

    echo "<p align=\"center\">Números pela <u>ordem de saída</u>: ".mostraArray()."</p>";
    echo "<p align=\"center\">Números pela <u>ordem decrescente</u>: ".ordenaArray()."</p>"
?>

obs: It's for php class, this is the exercise

    
asked by anonymous 19.10.2016 / 05:01

2 answers

0

The only way I found to do something recursive was this:

<?php

    function mostraArray($array) 
    {
        $string = '';
        foreach($array as $value) {
            $string .= "{$value}, ";        
        }        
        return mb_substr($string, 0, -2);
    }

    function ordenaArray($array, $e = 0) 
    {
        for($j = $e + 1; $j < count($array); $j++) {
            if(isset($array[$j]) and $array[$e] < $array[$j]) {
                $tmp = $array[$e];
                $array[$e] = $array[$j];
                $array[$j] = $tmp;
            }
        }

        if($e < count($array) - 1)
            $array = ordenaArray($array, $e + 1);

        return $array;
    }

    echo "<p align='center'>Números pela <u>ordem de saída</u>: ".mostraArray([5,1,25,12,3])."</p>";
    echo "<p align='center'>Números pela <u>ordem decrescente</u>: ".mostraArray(ordenaArray([5,1,25,12,3]))."</p>"
?>

I made it simple, and mostraArray() y was where I used the recursion, it works as a ordenaArra , is called X times, where X is the size of the vector, in each one of this time, the element of the time and compares with everyone in front of him, when he finds a bigger one he will change his position with it, do a technique called insertion sort. In the end it takes the last result and returns as a result, so I call the for method with the resulting mostraArray() .

The flow of organization would look something like this:

[5,1,25,12,3]
[1,25,5,12,3]
[25,1,5,12,3]
[25,1,5,12,3]
[25,5,1,12,3]
[25,1,12,5,3]
[25,12,1,5,3]
[25,12,5,1,3]
[25,12,5,3,1]

Note the method used.

    
19.10.2016 / 16:32
0

It was thus the easiest and most optimized way to solve the exercise but thanks to everyone it helped to understand the recursive functions.

<?php
    $cena=array($_REQUEST["numeroa"], $_REQUEST["numerob"], $_REQUEST["numeroc"], $_REQUEST["numerod"], $_REQUEST["numeroe"]);

    function mostraArray($cena) {
        for ($i=0; $i<count($cena); $i++) {
            echo $cena[$i];
            if ($i<4) {
                echo ", ";
            }
        }
    }

    function ordenaArray($cena) {
        rsort($cena);
        return $cena;
    }

    echo "<p align=\"center\">Números pela <u>ordem de saída</u>: ".mostraArray($cena)."</p>";
    echo "<p align=\"center\">Números pela <u>ordem decrescente</u>: ".mostraArray(ordenaArray($cena))."</p>"
?>
    
20.10.2016 / 05:36