230k views
2 votes
Write a function that determines the maximum and minimum values from a one-dimensional array. Assume that the corresponding function prototype statement is void ranges(int x[], int npts, int *max_ptr, int *min_ptr) where npts contain the number of values in array x, and max_ptr and min_ptr are pointers to the variables in which to store the maximum and minimum values in the array.

User Mike Woolf
by
7.4k points

1 Answer

1 vote

Answer:

void ranges(int x[], int npts, int *max_ptr, int *min_ptr)

{

*max_ptr=*min_ptr=x[0];

for(int i=1;i<npts;i++)

{

if(x[i]>*max_ptr) //this will put max value in max_ptr

*max_ptr=x[i];

if(x[i]<*min_ptr) //this will put min value in min_ptr

*min_ptr=x[i];

}

}

Step-by-step explanation:

The above function can be called like :

ranges(x,n,&max,&min);

where x is array and n is number of elements and max and min are address of variables where maximum and minimum values to be stored respectively.

User JordyvD
by
7.5k points