I need my php function to be recursive

Asked

Viewed 400 times

0

I need this function to be recursive, I’m beginner in php and I’m not quite sure how to 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, that’s the exercise

inserir a descrição da imagem aqui

  • 1

    Do the two functions need to be recursive? has some reason?

  • @rray It’s for php class, that’s the exercise: http://imgur.com/BWxgj6Z

  • Using only that can solve: $arr = array(5, 1, 25, 12, 3); and arsort($arr);.

  • But it needs to be with recursive function.. :/

2 answers

0

It was thus the easiest and most optimized way to solve the exercise but thanks to all 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>"
?>

0


The only way I could find 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>"
?>

The mostraArray() made it simpler, and ordenaArray was where I used recursiveness, it works as a for, is called X times, where X is the size of the vector, at each one this time, takes the element of the time and compares with everyone in front of it, when find a larger one it will exchange position with the same, make a technique called Insertion Sort. In the end it takes the last result and returns as a result, so I call the method mostraArray() with the array resultant.

The flow of organization would be more or less 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.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.