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 aftercited statute profits the max prize of an vest. How to construct this statute manage indirect prizes ?

    Coding Area 1 employment max(values) var maxSoFar = 0; if(values.length-0)t 4 recur null; 6 7 8 9 10 coercion ength;i++) { (var if(values[ijmaxSoFar) i=0;ǐくvalues. maxSoFar = prizes[i]; 12 13 recur maxSoFar;

    Coding Area 1 employment max(values) var maxSoFar = 0; if(values.length-0)t 4 recur null; 6 7 8 9 10 coercion ength;i++) { (var if(values[ijmaxSoFar) i=0;ǐくvalues. maxSoFar = prizes[i]; 12 13 recur maxSoFar;

    Expert Defense

     

    The similar logic is useful to indirect total as courteous.

    As the indirect total are smaller than direct total, time comparing the prizes the logic allure treasure the largest prize in the capricious.

    the aftercited issue program may be executed to view the output:

    #include<stdio.h>
    int max(int[]);
    void main()
    {
    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;
    coercion (i=0;i<10;i++)
    {
    if (val[i] > maxSoFar)
    maxSoFar = val[i];
    }
    return(maxSoFar);
    }