viewing paste Find Largest Element in Array | C++

Posted on the | Last edited on
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
#include <iostream>
#include <cstdlib>
#include <cmath>
#include <ctime>
 
const int MAX = 4;
 
int peak(const int A[], int left, int right){
    
    if(left == right)
        return 0;
    
    if(A[left] >= A[right]){
        return peak(A,left,right-1);
    }else{
        return 1+peak(A,left+1,right);
    }
}
 
int peak(int A[], int n){
    
    return peak(A,0,n-1);
    
}
 
void fillArr(int*& A, int n){
    
    for(int i = 0; i < n; i++){
        
        A[i] = rand() % 255;
        
    }
    
}
 
void printArr(int* A, const int n){
    
    for (int i = 0; i < n; i++) {
        std::cout << A[i] << " ";
    }
    std::cout << std::endl;
    
}
 
int main () {
    
    srand(time(0));
    
    int* A = new int[MAX];
    fillArr(A,MAX);
    printArr(A,MAX);
    std::cout << "Peak at: " << peak(A,MAX);
    
    return 0;
}
Viewed 1638 times, submitted by Streusel.