need help range function

I need to come up with range function that should take 2 parameters 1d int array and the number of elements. It should return the difference between the largest element and the smallest element.

so far I have this :

int range(int a[], int size) {
int max = a [0];
int min = a [0];
for (int i = 0; i <= size; i++) {
if (max < a[i]) {
max = a[i];
}
}
}

But I am getting incorrect values, can anyone help?
Hey, welcome! Code tags <> to the side help a lot esp as your code gets larger.

This code is correct for finding max. now you need to find min and subtract them.

Your bug is the <= size.
array[10] is values 0-9, not 0-10 (0-10 is 11 items!).


Last edited on
Topic archived. No new replies allowed.