Created
April 16, 2013 18:55
-
-
Save georgemendonca/5398559 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /** | |
| * Classe que implementa o algoritmo do Maior Elemento | |
| * (Método Simples) | |
| * @author George Mendonça | |
| * 16/04/2013 | |
| */ | |
| Class MaiorNumero { | |
| /** | |
| * Método que retorna o maior elemento de um array | |
| * @param Array $arrElementos | |
| * @return Integer $max | |
| */ | |
| public function Max($arrElementos = Array()) { | |
| $max = 0; | |
| // Valida se array possui elementos | |
| if(!empty($arrElementos)) { | |
| $max = $arrElementos[0]; | |
| // Varre o array em procura do maior elemento | |
| for($i=1; $i<count($arrElementos); $i++) { | |
| // Valida Maior Elemento | |
| if($max < $arrElementos[$i]) { | |
| $max = $arrElementos[$i]; | |
| } | |
| } | |
| return $max; | |
| } | |
| return 'Array sem elementos!'; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| // Chamando a classe MaiorNumero | |
| require 'MaiorNumero.class.php'; | |
| // Definindo o array de elementos | |
| $arrElem = Array(1001 , 88 , -90, 25, 77, 200, -522, 10099); | |
| // Recuperando o maior elemento por meio do objeto da class MaiorNumero() | |
| $oMaior = new MaiorNumero(); | |
| echo $oMaior->max($arrElem); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment