Thread: Need help understanding basic list code

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    16

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

    Quote 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.

    Quote 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?
    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

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    I hat it when they use the word "list" but that don't say "linked". Do they mean a linked-list or not!
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

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

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

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

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

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

    Quote 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.
    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

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