0

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.

phuclv
  • 37,963
  • 15
  • 156
  • 475
wzsun
  • 295
  • 2
  • 7
  • 15
  • 2
    You need to show us the code which calls the `largestFloat()` function. The problem might be there. – ChrisWue Sep 21 '13 at 01:23
  • 1
    Are you sure that `length` is correctly set every time you call `largestFloat`? Have you tried stepping-through with a debugger attached? – Dai Sep 21 '13 at 01:24
  • I make an array "float f[15];" and fill it all with values. Then I run "largestFloat(f,15);" – wzsun Sep 21 '13 at 01:26
  • instead of passing length you could determine it to make sure that isn't the issue as in [http://stackoverflow.com/a/37539/2145211] – Harrison Sep 21 '13 at 01:31
  • @wzsun Could you show all the values you use to initialize, so we can be sure the types and length is proper? – Ogre Psalm33 Sep 21 '13 at 01:31
  • http://i.imgur.com/6A9IZBw.png?1 Here is an image describing the error that I am experience currently: The initial values are edited above – wzsun Sep 21 '13 at 01:33
  • The behavior you describe can only be explained by compiler bug or by something in your code that you are not showing us. What you posted above looks fine and will not produce such unexplainable results. – AnT stands with Russia Sep 21 '13 at 01:41
  • In your image you appear to change the values in the array prior to calling the method. Could you be doing something there that causes this? – Harrison Sep 21 '13 at 01:44
  • @wzsun: In your screenshot the first run also shows an incorrect float max value. However, that max value coincides with the max value in the altered array printed afterwards. This looks like a pretty strange coincidence. There must be something important going on there that you are not showing us. – AnT stands with Russia Sep 21 '13 at 01:45
  • The problem was in my header file, I feel so dumb now. Thank you for your help. – wzsun Sep 21 '13 at 01:55
  • 1
    Show us a [small self-contained program](http://sscce.org/) that illustrates the problem. It needs to include your `largestFloat` function and a short `main` function that creates an array, calls `largestFloat`, and prints the result. Something I can copy-and-paste and run on my own system, and (ideally) get the same output you do. – Keith Thompson Sep 21 '13 at 01:55
  • Might be an issue with the format string passed to `printf`. – ChrisWue Sep 21 '13 at 07:48

2 Answers2

2

Not seeing an entire example, I'd have to say that you are correct. The array size is probably wrong. First fix the return type:

float largestFloat(float array[], int length){

Next, you might want to add a guard against an empty array, since that will automatically overlflow fetching array[0]:

if (length < 1) return 0;

The rest of largestFloat() is good.

Then call with:

float f[15] = {2.3 ... 102} <-- filled with 15 values
size_t length = sizeof f / sizeof f[0];
float f_max = largestFloat(f, length);
printf("max=%g, length=%d\n", f_max, length);

That will compute (at compile time) the actual size of the array f. Look for cases where the length is not what you thought it should be. This can happen if you type a . instead of a , between values that don't already have a decimal point. That and miscounting are the only ways I know of to get 14 or fewer values from what appears to be 15.

Mike Housky
  • 3,959
  • 1
  • 17
  • 31
0

The problem is that you find the largest float but then return the int value

int largestFloat(float array[], int length){     // return type is int
    float max = array[0];                        // max is float
    int i;

    for( i=1; i<length; i++){
         if(array[i] > max){
               max = array[i];
         }
    }

    return max;
} 
phuclv
  • 37,963
  • 15
  • 156
  • 475
  • 2
    It certainly doesn't help, but it should simply floor the float, not return something obviously not in the array. – zneak Sep 21 '13 at 01:29
  • Yea this was a typo on my part. Sorry – wzsun Sep 21 '13 at 01:30
  • 3
    @wzsun: Copy and paste your code into the question instead of retyping it. If you got the return type wrong, you likely got other things wrong when you retyped your code. For that reason, copy/paste is far superior to retyping. – user2357112 Sep 21 '13 at 01:47