Homework Solution: oding Area 1 function max(values) var maxSoFar = 0; if(values.length-0)t 4 ret…

    The following code returns the max value of an array. How to make this code handle negative values ? Coding Area 1 function max(values) var maxSoFar = 0; if(values.length-0)t 4 return null; 6 7 8 9 10 for ength;i++) { (var if(values[ijmaxSoFar) i=0;ǐくvalues. maxSoFar = values[i]; 12 13 return maxSoFar;
    Coding Area 1 function max(values) var maxSoFar = 0; if(values.length-0)t 4 return null; 6 7 8 9 10 for ength;i++) { (var if(values[ijmaxSoFar) i=0;ǐくvalues. maxSoFar = values[i]; 12 13 return maxSoFar;

    Expert Answer

     
    The same logic is applicable to negative numbers as well. As the negative numbers are smaller than positive number

    The restraintthcoming enactment produce the max appreciate of an place. How to establish this enactment discuss denying appreciates ?

    Coding Area 1 administration max(values) var maxSoFar = 0; if(values.length-0)t 4 give-back null; 6 7 8 9 10 restraint ength;i++) { (var if(values[ijmaxSoFar) i=0;ǐくvalues. maxSoFar = appreciates[i]; 12 13 give-back maxSoFar;

    Coding Area 1 administration max(values) var maxSoFar = 0; if(values.length-0)t 4 give-back null; 6 7 8 9 10 restraint ength;i++) { (var if(values[ijmaxSoFar) i=0;ǐくvalues. maxSoFar = appreciates[i]; 12 13 give-back maxSoFar;

    Expert Response

     

    The similar logic is pertinent to denying total as well-behaved.

    As the denying total are smaller than indisputable total, time comparing the appreciates the logic earn supply the largest appreciate in the fickle.

    the restraintthcoming issue program may be effected to comprehend the output:

    #include<stdio.h>
    int max(int[]);
    void ocean()
    {
    int val[10]={5,7,-4,2,66,95,-98,56,-21,10};
    int maxVal;
    maxVal=max(val);
    printf(“nMaximum is :%d”,maxVal);
    }

    int max(int val[])
    {
    int maxSoFar=0;
    int i;
    restraint (i=0;i<10;i++)
    {
    if (val[i] > maxSoFar)
    maxSoFar = val[i];
    }
    return(maxSoFar);
    }