How uses the recursion arithmetic to find the maximum value and the minimum value from an array ?

How uses the recursion arithmetic to find the maximum value and the minimum value from an array

Answer this question

How uses the recursion arithmetic to find the maximum value and the minimum value from an array ?

  • Sacristy

    Can you explain me why do you use Int32 instead of int


  • Randall Moore

    Just some simplyfication:

    static int maximum(int[] arr, int length)
    {
    if (length == 1)
    return arr[length];
    return Math.Max(maximum(arr, length - 1), arr[length]);
    }


  • AMG44

    thank shakalama :-)

    static int maximum(int[] arr, int length)
    {
    int tmp;

    if (length == 1)
    return arr[length];
    tmp = maximum(arr, length - 1);
    if (arr[length] > tmp)
    return arr[length];
    return tmp;

    //Above explanation each code meaning
    }


  • Edward V. Wright

    Method that finds maximum value in int array:
    static int maximum(int[] arr, int length)
    {
    int tmp;

    if (length == 1)
    return arr[length];
    tmp = maximum(arr, length - 1);
    if (arr[length] > tmp)
    return arr[length];
    return tmp;
    }

    Method that finds minimum value in int array:
    static int minimum(int[] arr, int length)
    {
    int tmp;

    if (length == 1)
    return arr[length];
    tmp = minimum(arr, length - 1);
    if (arr[length] < tmp)
    return arr[length];
    return tmp;
    }

    You call them: maximum(intArray, intArray.Length-1);


  • Pradeep Raj

    Nice Boris, but just let me correct your code a little bit:
    static Int32 Maximum(Int32[] arr, Int32 lastArrayIndex)
            {
                Int32 tmp;
                if (lastArrayIndex == 0) return arr[lastArrayIndex];
                tmp = Maximum(arr, lastArrayIndex - 1);
                if (arr[lastArrayIndex] > tmp) return arr[lastArrayIndex];
                return tmp;
            }

            static Int32 Minimum(Int32[] arr, Int32 lastArrayIndex)
            {
                Int32 tmp;
                if (lastArrayIndex == 0) return arr[lastArrayIndex];
                tmp = Minimum(arr, lastArrayIndex - 1);
                if (arr[lastArrayIndex] < tmp) return arr[lastArrayIndex];
                return tmp;
            }

    You can them: Maximum(intArray, intArray.Length-1), Minimum(intArray, intArray.Length-1).

    Note: Boris's previous code cannot handle the array with single element, for instance:{5}

    Sheva



  • AnswerFinder

     MeetJayBlack wrote:
     

     but most prefer to use int as it is easier to type and more familiar among C++ programmers.

    I prefer to use primitive type names instead of its corresponding aliases is basically because when you write  code as follows:

    int i = Int32.TryParse("11");

    long l = Int64.TryParse("11111111111111");

    bool b = Boolean.TryParse("false");

    then

    Int32 i = Int32.TryParse("11");

    Int64 l = Int64.TryParse("11111111111111");

    Boolean b = Boolean.TryParse("false");

    Which one is more elegant and readable

    I personally believe the first code sample to some extent doesn't make any sense, and the latter one can give you a good feel of the code.

     

    Sheva

     



  • bljacobs

    Thanks everybody :-) :-)

    ========================================

    if (length == 1)
    return arr[length]; //What meaning does this code represent

    ========================================

    tmp = maximum(arr, length - 1); //What meaning does this code represent

    ========================================

    if (arr[length] > tmp)
    return arr[length]; //What meaning does this code represent

    ===========================================

    return tmp; //What meaning does this code represent
    ==========================================


  • MasterG152

    Basically there is no difference between Int32 & int. Int32 is the System.Int32 class, while int is an alias for System.Int32.It's down to user's preference which one to use but most prefer to use int as it is easier to type and more familiar among C++ programmers.


  • Chris L60969

    hi,

    i didn't understand either  what does it mean

    but i do it this way


    static void Main(string[] args)
    {
    int[] myintarray = { 1, 7, 9, 5, 15, 5, 8, 7, 20 };
    int maxvalue = 0;
    int minvalue = 0;
    for(int i = 0 ; i < myintarray.Length;i++)
    {
    int current;
    current = myintarray[ i ];
    if(i==0)
    {
    maxvalue = current;
    minvalue=current;
    }
    else

    {
    if (current > maxvalue)
    {
    maxvalue = current;
    }
    if (current < minvalue)
    {
    minvalue = current;
    }
    }
    }
    Console.WriteLine("maxvalue = {0}",maxvalue );
    Console.WriteLine("minvalue = {0}", minvalue);
    Console.Read();
    }


     

    hope this will help



  • Anujpathania

    how about using Array.Sort on the array and then the first element is your min value and the last element is your max value

    Imran.

  • How uses the recursion arithmetic to find the maximum value and the minimum value from an array ?