# Thread: Need help understanding basic list code

1. ## Need help understanding basic list code

Hello. Im new on this forum so please go easy. I have an exam coming up soon and Im having trouble understanding the solution to the last answer in the exam. Ive listed both the question and answer below.

Question:

How would you declare a list of floating numbers in C? Write a function that calculates the mean value of these numbers, and another function that sorts the list into the list of numbers lower than this mean value and the list of numbers higher than this mean value.
Answer:

Code:
```float list[];

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++;
}
}```
This is pretty advanced for me as I have been learning at a lower level. I would really appreciate it if someone could help me understand the example answer code.

Rick

2. Unfortunately, the "model answer" is wrong since it will not compile due to the first line. The good thing is that this is easily fixed by changing it to say...
Code:
`float list[10];`
or better yet, just removing that line and treating it as if the functions will be called with a suitable array.

Anyway, do you understand the implementation of the meanValue function? It looks straightforward enough to me.

The sortMiddle is also quite straightforward: those numbers less or equal to the middle are copied to one array, while the rest are copied to the other array. There are a few gotchas though: the declarations of counter2 and counter3 have typographical errors, and there is no way for the caller to determine how many numbers have been copied to each of the destination arrays.

3. Hi,

So i understand that the top segment checks to see if the counter is less than 10, and then calculates the sum as equal to myList[counter], and then increments the counter. Im not really sure what the return does, where does it store the result of sum / counter?

The next issue in reading the code that I have is the word "middle" on the first if conditional.

4. Originally Posted by hiven
So i understand that the top segment checks to see if the counter is less than 10, and then calculates the sum as equal to myList[counter], and then increments the counter.
You probably have the right idea, but more accurately: the meanValue function sums the 10 numbers in the float array and then computes the arithmetic mean.

Originally Posted by hiven
Im not really sure what the return does, where does it store the result of sum / counter?
Review your notes on how to call a function and use a function's return value.

Originally Posted by hiven
The next issue in reading the code that I have is the word "middle" on the first if conditional.
What do you mean?

5. I hat it when they use the word "list" but that don't say "linked". Do they mean a linked-list or not!

6. I guess at this point I should start from scratch.

"How would you declare a list of floating numbers in C?"

Is the answer to this simply:

float list[];

float is the type (replacing possibly int or double), list is the name. Are the []'s really nessesary, and do they just show at it currently is empty (no values).

7. Originally Posted by hiven
Is the answer to this simply:

float list[];

float is the type (replacing possibly int or double), list is the name. Are the []'s really nessesary, and do they just show at it currently is empty (no values).
Compile and run this program:
Code:
```int main(void)
{
float list[];
return 0;
}```

8. It tells me that list needs a size. Something instead like this works:

float list[1];

I dont really understand what I should be taking from this though, what does it show?

9. Well, given that the compiler complained that the size is missing, you now know that you need to provide a size. So, I can now tell you that the brackets are indeed necessary to declare an array, and you certainly can have arrays of types other than float.

10. float list[];
How is it that the same line is used in the model answers without a size, is that an error in the model answer?

11. Originally Posted by hiven
How is it that the same line is used in the model answers without a size, is that an error in the model answer?
Yes, as I noted in post #2, the "model answer" is wrong.

12. Ah ok that clears it up. So in order to answer the first part of the question:

"How would you declare a list of floating numbers in C?"

Would it be something as simple as this, with x being the size:

float list[x];

Or is there more to the declaration than this?

13. Originally Posted by hiven
Would it be something as simple as this, with x being the size:

float list[x];
Yes, though x should be a compile time constant, unless you want to use a variable length array, which is a relatively recent feature (it has been around for a decade in standard C, but standard C is two decades old).

14. Ok. Ive commented the code. There is one line that I dont fully understand what it is doing, and I also dont understand where the value of sum/counter is going.

Code:
```float list[];

float meanValue(float myList[10])            /* I dont understand this line */
{
float sum=0;                                 /* define sum and set value to 0 */
int counter=0;                               /* define counter and set value to 0 */
while (counter <10)                       /* if counter is less that 10 */
{
sum += myList[counter];      /* set sum equal to myList[counter] */
counter++;                           /* increment counter */
}
return sum/counter;                            /* return the sum divided by counter */
}```
Once that is cleared up, what exactly is the complete code doing? What i think it is doing is reading each value in myList one by one and then taking the value of the my_list and dividing it by its number in the list to find out the mean for each value. Is that correct?

15. Originally Posted by hiven
I also dont understand where the value of sum/counter is going.
It goes to whatever receives the return value of the function.

The comment on this line is inaccurate:
Code:
`sum += myList[counter];      /* set sum equal to myList[counter] */`
It adds myList[counter] to sum.

Originally Posted by hiven
Once that is cleared up, what exactly is the complete code doing? What i think it is doing is reading each value in myList one by one and then taking the value of the my_list and dividing it by its number in the list to find out the mean for each value. Is that correct?
No, that is not correct.

Popular pages Recent additions