Thread: Need help understanding basic list code

  1. #16
    Registered User
    Join Date
    May 2009
    Posts
    16
    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. #17
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    Quote 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

    Quote 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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #18
    Registered User
    Join Date
    May 2009
    Posts
    16
    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. #19
    Registered User
    Join Date
    May 2009
    Posts
    16
    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. #20
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.
    Hope is the first step on the road to disappointment.

  6. #21
    Registered User
    Join Date
    Apr 2009
    Location
    Russia
    Posts
    116
    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. #22
    Registered User
    Join Date
    May 2009
    Posts
    16
    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. #23
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.
    Hope is the first step on the road to disappointment.

  9. #24
    Registered User
    Join Date
    May 2009
    Posts
    16
    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. #25
    Registered User
    Join Date
    May 2009
    Posts
    16
    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. #26
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.
    Hope is the first step on the road to disappointment.

  12. #27
    Registered User
    Join Date
    Apr 2009
    Location
    Russia
    Posts
    116
    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 subscribe to a feed

Similar Threads

  1. deleting a node in linked list
    By BoneXXX in forum C Programming
    Replies: 18
    Last Post: 12-17-2007, 12:30 PM
  2. circular doubly linked list help
    By gunnerz in forum C++ Programming
    Replies: 5
    Last Post: 04-28-2007, 08:38 PM
  3. singly linked circular list
    By DarkDot in forum C++ Programming
    Replies: 0
    Last Post: 04-24-2007, 08:55 PM
  4. Problem with linked list ADT and incomplete structure
    By prawntoast in forum C Programming
    Replies: 1
    Last Post: 04-30-2005, 01:29 AM
  5. Replies: 6
    Last Post: 03-02-2005, 02:45 AM