Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    Regular Coder
    Join Date
    Oct 2006
    Posts
    197
    Thanks
    9
    Thanked 0 Times in 0 Posts

    A few simple questions about simply mean average code

    Hi,

    Below I have code from a model answer. I dont understand a few things about it though.

    The first is, is the mean value stored in "meanValue"?

    The second is, would it not be better to use "meanValue" instead of the "middle" place filler.

    The "10" in the my_List, is that just saying that in the list there are a list of 10 individual float numbers.

    Finally with this line:
    Code:
    void sortMiddle(float myList[10], float middle, float inferior[10], float superior[10])
    What is the purpose of including things like float middle, float inferior etc inside the brackets of the sortMiddle?

    Code:
    float meanValue(float myList[10])
    {
          float sum=0;
          int counter=0;
          while (counter <10)
          {
                   sum += myList[counter];
                   counter++;
           }
    return sum/counter;
    }
    void sortMiddle(float myList[10], float middle, float inferior[10], float superior[10])
    {
          int counter1=0, counter 2=0, counter 3=0;
          while (counter1<10)
          {
                 if (myList[counter1] <= middle)
                    {
                         inferior[counter2]=myList[counter1];
                         counter2++;
                    }
                 else
                    {
                         superior[counter3]=myList[counter1];
                         counter3++;
                     }
                 counter1++;
        }
    }

  • #2
    Senior Coder
    Join Date
    Apr 2003
    Location
    England
    Posts
    1,192
    Thanks
    5
    Thanked 13 Times in 13 Posts
    meanValue is a function that takes a list of 10 floating point numbers and returns the mean value. It does not store anything.

    I would assume that "middle" is nothing to do with the mean. This would be a different average - the median. To find the mean you add everything up and divide by the number of elements. To find the median you sort the elements and find the middle value (or the mean of the two middle values if there is not one).

    The 10 is the size of the list and as a beginner this is probably all you need to know. If you write 10, it will have a size of 10 even if you only want to use 1 or 2 of them and you can access them with myList[0] to myList[9]. It tells the computer how much space to allocate for the array. If a "float" is 4 bytes and there are 10 of them, it will allocate 40 bytes of memory.

    The things like "float middle" inside the parenthesis for sortMiddle(...) are telling it what variables can be passed into that function.
    In an "add" function you may have something like
    Code:
    int add(int a, int b)
    {
      return a + b;
    }
    You would call this with something like
    Code:
    int sum = add(2,3);
    /* "sum" now contains 5 */
    In this example, the content of the function can use a and b because you put them there. When you call it with 2,3 then inside the function "a" will contain the value 2 and "b" will contain the value 3.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •