Finding the Range in C

Discussion in 'Mac Programming' started by jcmoney1010, Oct 7, 2012.

  1. jcmoney1010 macrumors newbie

    Sep 21, 2007
    So within my program I have written a function that finds the range of a set of numbers, unfortunately it doesn't seem to be working correctly. Instead of giving me the difference between the smallest number and the biggest number, its giving me this:

    (3) Range
    (4) Restart/Exit
    I've looked all over the internet, and while I found a couple useful examples of how to do it, they didn't seem to fix this problem. Here is my function, if anyone needs to see the entire code, just let me know.

    float range(float numbers[], int amount)
      int i;
      float diff=0;
      for (i=1;i<=amount;i++){
          return diff;
    I've tried everything I can think of, any suggestions
  2. chown33 macrumors 604

    Aug 9, 2009
    Sailing beyond the sunset
    1. What are the actual values in the array? Have you tried it with known test data, such as the values 10, 100, 1000?

    2. C array indexes start at 0, not 1. If a C array has 4 members, their indexes are 0 thru 3. 4 would be invalid. If you go past the end of an array, you get invalid values, you don't get an error saying you went past the end of the array.

    3. I'm not sure I understand what you mean by "range". Do you mean the lowest and highest values? The lowest value and the delta to the highest value?
  3. lee1210 macrumors 68040


    Jan 10, 2005
    Dallas, TX
    I don't know what the actual goal is here. I think it's to find the difference between the smallest and largest elements in an array. So to do this you have to find the smallest and largest elements. If the array is unsorted you can do this in O(n) time, looping from 0 to n-1 and tracking the current highest and current lowest number. At the end you calculate the highest minus the lowest. Note: the highest float minus the lowest float is too big for a float to hold.
    If the array is already sorted, this is a constant time operation. Subtract the value of the first element from the value of the last element, done. If the array isn't already sorted, don't do it unless it will be helpful to have this sorted later.

    Currently you do a useless loop that assigns to diff the value of the first element off the end of the array (assuming amount is the number of elements) minus the second element ([1] is the second element, C arrays are 0 based). It does this same operation amount times.

    I'm guessing going off the end of your array is the main issue, but unless that random value off the end is the same as the max value, and [1] is the smallest, you won't get the right answer.

    This is a homework program, so I can't write code. Suggesting algorithms is probably too much, too, but I think you're learning to code. If the algorithmic component was the core you should go watch reality TV for a few minutes and try to forget this.

  4. subsonix macrumors 68040

    Feb 2, 2008
    It looks like you have a fence post error in your for loop (using <=). If you want to find the difference between the smallest and largest value in your array you need to keep track of "smallest" and "largest" in the loop with two variables. After the loop you can subtract "smallest" from "largest" and get the difference.

Share This Page