Write function:

  • int arrayMin (int array [], int size). Return the value of the minimum element in the array.
  • int arrayMax (int array [], int size). Return the value of the maximum element in the array.
int arrayMin(int array[], int size) { int min = array[0]; for ( int i = 0; i < size; i++ ) { if ( array[i] < min ) { min = array[i]; } } return min; } int arrayMax(int array[], int size) { int max = array[0]; for ( int i = 0; i < size; i++ ) { if ( array[i] > max ) { max = array[i]; } } return max; } 

Somewhere wrong?

2 answers 2

Generally speaking, if the transferred array size is incorrect (that is, 0 or less than zero) , then the program is incorrect. Well, the iteration over the array can be automatically started from index 1, not 0.

I would prefer the int arrayMax(int array[], unsigned int size) prototype for such a function, and I would continue to check if (size == 0) { ... } .

But in general, yes, except for these items, everything is correct.

  • Thank you, you are right! ) - arcs_host

Here's another interesting option:

 function sortNumber(a,b) { return a - b; //в обратную сторону return b - a; } var n = ["10", "5", "40", "25", "100", "1"]; document.write(n.sort(sortNumber));