Finding the maximum element of an array recursively

后端 未结 5 2066
遥遥无期
遥遥无期 2020-12-20 19:08

Consider this code, which computes the maximum element of an array.

#include 

int maximum(int arr[], int n)
{
    if (n == 1) {
        retur         


        
5条回答
  •  悲&欢浪女
    2020-12-20 19:34

    A possible recursive solution is to compare the previous and the current element.

    #include 
    
    static int max(int a, int b) {
        return a > b ? a : b;
    }
    int max_array(int *p, size_t size)
    {
        if (size > 1)   return max(p[size-1], max_array(p, size-1));
        else            return *p;
    }
    

提交回复
热议问题