160k views
2 votes
Write a function that returns a pointer to the MAXIMUM value of an array of double values. If the array is empty, return NULL. double* maximum(double *a, int size) { }

1 Answer

3 votes

Answer:

double* maximum(double *a, int size) {

if(size == 0)

return NULL;

int i;

double max = 0.0;

double *mp;

for(i = 0; i < size; i++){

if(a[i] > max){

max = a[i];

mp = &a[i];

}

}

return mp;

}

Step-by-step explanation:

You iterate through the vector and find the maximum value. You go updating the pointer throughout the loop. I am going to write a C function

double* maximum(double *a, int size) {

if(size == 0)

return NULL;

int i;

double max = 0.0;

double *mp;

for(i = 0; i < size; i++){

if(a[i] > max){

max = a[i];

mp = &a[i];

}

}

return mp;

}

User Essie
by
5.3k points