...

View Full Version : A few simple questions about simply mean average code



spadez
05-12-2009, 04:33 PM
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:

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?



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++;
}
}

ghell
05-18-2009, 03:53 PM
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
int add(int a, int b)
{
return a + b;
}You would call this with something like
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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum