Thread: Need help understanding basic list code

1. Ah that makes more sense. So in this example the sum is adding the value of each value in myList at a time, adding them up, and then dividing that number by the counter number.

Is this right? By looking at this code am i correct to assume that final value of counter will probably be 10?

Thank you for your help btw.

2. Originally Posted by hiven
So in this example the sum is adding the value of each value in myList at a time, adding them up, and then dividing that number by the counter number.
Yes

Originally Posted by hiven
By looking at this code am i correct to assume that final value of counter will probably be 10?
It will definitely be 10 after the loop terminates.

3. Code:
```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++;
}
}```
Ive been reading through this code and there are a few things I dont understand now. I thought the mean value worked out would be stored in the meanValue, is this correct?

However in the second part of the code (above) there are no checks to see if the number read in the myList is bigger or smaller than this. It is only checked against middle, and middle doesnt seem to be assigned a value.

4. Im really sorry to be a pest, but my exam is on wednesday and so far I cant solve my outstanding issues despite reading up on functions.

I would really really appreciate if anyone could give me some insight.

5. What issue are you having? If 'middle' is the average of the elements in the array, then your program is checking to see if it's greater than that number, and incrementing counter3, and if not, incrementing counter2.

Quzah.

6. used only pointers while sorting to less and greater (without counters)

output:
Code:
```[guest@station eng]\$ ./test
0.1     0.5     0.1     0.5     2.5     3.1     0.7       1       5   5.025
0.1     0.5     0.1     0.5     0.7       1       0       0       0       0
2.5     3.1       5   5.025       0       0       0       0       0       0
1.8525
[guest@station eng]\$```

7. Hi,

The issues im having is understanding where mean value is stored, and how "middle" is being used, since it doesnt seem to be equated to anything or given any value.

8. It assumes you're actually passing it a value that you've decided is the "middle value". So why don't you throw it together in a small sample, provide it some values, and pass it different middle values to see what it's doing?

Quzah.

9. Wait so sorry just so I understand, middle is just a place filler?

Am i correct in saying that the result of the sum/counter is meanValue? Would it not be better to use this instead of "middle"?

10. Ive been looking at this again and I feel like I understand it but there are few things I could do with clarification if anyone has the time to help.

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.

11. Look at what the function does. You pass it three lists. One for high numbers. One for low numbers. One with all the numbers. You pass it one value. Everything over that, goes into high. Everything lower, goes into low.

It's up to you to decide what stuff you pass it.

12. hiven, middle is the middle and meanvalue is the meanvalue
you may confuse this two terms and the middle understand as middle value of the array int n[5] - n[2]
meanvalue is a mean value for all values when you take every value, count their sum and then divide this sum to number of all values (this is mean value of all this values)
if you will use meanvalue, then you will not confuse meanvalue and middle value

Popular pages Recent additions