using namespace std ; const int SIZE
= 10 ;
int findMax
( int list [ ] , int current_index , int highest_index ) ;
int main
( ) {
} kopen van 2
Definieer de findMax functie na de belangrijkste functie om een array recursief zoeken naar de maximale waarde . Deze functie doorloopt de array , vergelijkt waarden , en tot slot de index van het hoogste getal retourneert :
int main
( ) {
}
int findMax
( int lijst [ ,"] , int current_index , int high_index ) {
}
3
Opzetten van een recursieve basisscenario in de findMax functie . Deze verklaring zal recursie stoppen zodra het einde van de lijst is bereikt :
int findMax
( int list [ ] , int current_index , int high_index ) {
if ( current_index == SIZE ) { return high_index ; } }
4
de recursieve zoeken actie Definieer in de findMax functie . De if-else statements zullen altijd beweren dat high_index houdt de indexwaarde van het hoogste getal in de lijst :
int findMax
( int list [ ] , int current_index , int high_index ) {
indien ( current_index == SIZE ) { return high_index ; }
else if (lijst [ high_index ] < list [ current_index ] ) { return findMax ( lijst ( index + 1 ) , current_index ) ; } else { return findMax ( lijst ( index + 1 ) , high_index ) ; } }
5
Definieer een array met een willekeurige mix van gehele getallen in de belangrijkste functie :
int main
( ) {
int arr [ 10 ] = { 3 , 2 , 6 , 7 , 10 , 45 , 8 , 99 , 0 , 11 } ; }
6
verstaan een geheel getal belangrijkste functie , en bel de findMax functie om de waarde van die integer ingesteld om de index van de grootste waarde in de lijst :
# include using namespace std ;
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 } ;
int grootst = findMax ( arr , 0 , 0 ) ;
}
int findMax ( int list [ ] , int current_index , int high_index ) {
if ( current_index == SIZE ) { return high_index ; }
else if (lijst [ high_index ] < lijst [ current_index ] ) { return findMax ( lijst ( index + 1 ) , current_index ) ; } else {return findMax ( lijst ( index + 1 ) , high_index ) ; } }