Vitajte na [www.pocitac.win] Pripojiť k domovskej stránke Obľúbené stránky
Vytvorte program , ktorý definuje premennú konštantnú veľkosť pre rekurzia , a definuje prototyp funkcie FindMax :
# include const int SIZE = 10 ; int FindMax ( int list [ ] , int current_index , int highest_index ) ; int main ( ) { } Definujte funkciu FindMax po hlavnej funkcie hľadať pole rekurzívne pre maximálne hodnoty . Táto funkcia krokov cez pole , porovnáva hodnoty , a konečne vráti index najvyššie celé číslo : int main ( ) { } int FindMax ( int list [ ,"] , int current_index , int high_index ) { } Nastaviť rekurzívne základnú vec vo funkcii FindMax . Toto vyhlásenie sa zastaví rekurzia , akonáhle je dosiahnutý koniec zoznamu : int FindMax ( int list [ ] , int current_index , int high_index ) { if ( current_index == SIZE ) { return high_index ; } } Definujte rekurzívne vyhľadávacie činnosť vo funkcii FindMax . If - else budú vždy tvrdiť , že high_index má hodnotu indexu najvyššie celé číslo v zozname : int FindMax ( int list [ ] , int current_index , int high_index ) { ( current_index == SIZE ) { return high_index ; } else if ( list [ high_index ] < zoznam [ current_index ] ) { return FindMax ( zoznam ( index + 1 ) , current_index ) ; } else { return FindMax ( zoznam ( index + 1 ) , high_index ) ; } } Definujte pole s náhodnom mixe čísel vnútri hlavné funkcie : int main ( ) { int arr [ 10 ] = { 3 , 2 , 6 , 7 , 10 , 45 , 8 , 99 , 0 , 11 } ; } deklarovať číslo v hlavné funkcie a volania funkcie FindMax nastaviť hodnotu tohto číslo na indexe najväčšie hodnoty v zozname : Spojené # include const int SIZE = 10 ; int FindMax ( int list [ ] , int current_index , int highest_index ) ; int main ( ) { int arr [ 10 ] = { 3 , 2 , 6 , 7 , 10 , 45 , 8 , 99 , 0 , 11 } , celým int = najväčšie FindMax ( arr , 0 , 0 ) ; celým } celým int FindMax ( int list [ ] , int current_index , int high_index ) { if ( current_index == SIZE ) { return high_index ; } else if ( list [ high_index ] < Zoznam [ current_index ] ) { return FindMax ( zoznam , ( index + 1 ) , current_index ) ; } else { return FindMax ( zoznam ( index + 1 ) , high_index ) ; } }
2
3
4
5
6