I'm having a problem in C when I'm trying to find the largest float of an array, but my largest int works just fine. I think I might be going past the array length but I don't see how it is possible.
int largestInt(int array[], int length){
int max = array[0];
int i;
for( i=1; i<length; i++){
if(array[i] > max){
max = array[i];
}
}
return max;
}
The above code works fine for ints, however if I change it to work with floats as follows,
float largestFloat(float array[], int length){
float max = array[0];
int i;
for( i=1; i<length; i++){
if(array[i] > max){
max = array[i];
}
}
return max;
}
Sometimes it will give me the right answer, and sometimes it will just give me a huge number not even in the original array. Which leads me to believe that I'm going past the length of the array.
float f[15] = {9.5, 45.64, 313.11, 113.89, 81.56, 250.00, 11.9, 469.98, 313.11, 4.68, 34.33, 8013.55, -10.15, 11.5, 88.0} <-- filled with 15 values
largestFloat(f,15);
This is what I would run.