Closed Thread
Page 1 of 2 12 LastLast
Results 1 to 10 of 14

Thread: sentinel-control-structure

  1. #1
    jwxie518's Avatar
    jwxie518 is offline Speaks fluent binary
    Join Date
    Jan 2009
    Location
    New York City
    Posts
    1,175
    Blog Entries
    1
    Rep Power
    18

    sentinel-control-structure

    Hi, here is a piece of code i read from the book.
    The original question is : Develop a class averaging program that will process an arbitrary number of grades each time the program is run.

    Since there is no indication given of how many grades are to be entered, so they introduced sentinel-control-structure.

    Code:
    // average grade
    
    #include <iostream>
    #include <iomanip>
    using namespace std;
    
    int main()
        {
              int total,    // sum of grades
                  grade,    // one grade
                  gradeCounter;       // number of grades entered
              float average;          // class average
              
              //initialization
              total = 0;
              gradeCounter = 0;
              
              // PROCESSING
              cout << "Enter grade, -1 to end the program: ";
              cin >> grade;
              
              while ( grade != -1 ) {
                total = totoal + grade;
                gradeCounter = gradeCounter +1;
                cout << "Enter grade, -1 to end the program: ";
                cin >> grade;
              }
              
              // termination
              
              if ( gradeCounter !=0 ){
                average = static_cast< float > ( total ) / gradeCounter;
                cout << "Class average is " << setprecision( 2 )
                     << setiosflags ( ios::fixed | ios::showpoint )
                     << average << endl;
              }
              else
                cout << "NJ grade were entered" << endl;
                
              return 0; // program ended successfully
        }

    the following is code for counter-control-structure
    Code:
    // average grade
    
    #include <iostream>
    using namespace std;
    
    int main()
        {
              int total,   // sum of grades
                  gradeCounter,  // number of grades entered
                  grade,   // individual grade
                  average;   // average of grades in this class
              
              // now we should first re-set some of the values
              total = 0;       // make sure total always starts with zero
              gradeCounter = 1;   // starting from 1, ends at 10
              
              // enter all ten grades
              while ( gradeCounter <=10 ) {
                    cout << "Enter grade: ";
                    cin >> grade;
                    total = total + grade;
                    gradeCounter = gradeCounter +1;
              }
              cin.get();
              // get all ten and finally calcaulate this
              average = total / 10;
              cout << "Class average is " << average << endl;
              cin.get();
              return 0;
        }
    What I don't understand is to compare to counter-control-strcuture, which you know the number of grades will be entered, the book sets the initialization a bit different.

    (1) For sentinel, which is the first prorgram, the gradeCounter is set to zero, why? In counter example, the gradeCounter starts from +1. I just don't get the reason, maybe i am too dumb...?
    http://i3physics.com/blog
    *-*-*-*__ C++ revolutionized the modern programming language, but what happen to C+? Programming is just a study of chemistry __*-*-*-*

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Location
    Advertising world
    Posts
    Many

     
  3. #2
    Join Date
    Jul 2006
    Posts
    16,448
    Blog Entries
    74
    Rep Power
    143

    Re: sentinel-control-structure

    It's a matter of perspective.
    The first doesn't assume any will be read, and starts at 0 to reflect that.
    The second starts by getting ready to read the first element, and starts at 1 to represent that.
    The result is that the first uses the variable to store what HAS BEEN done vs what IS ABOUT TO BE done.
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  4. #3
    jwxie518's Avatar
    jwxie518 is offline Speaks fluent binary
    Join Date
    Jan 2009
    Location
    New York City
    Posts
    1,175
    Blog Entries
    1
    Rep Power
    18

    Re: sentinel-control-structure

    hey, wingedpanther, thanks for the answer
    but i am still pretty confused about it, yet i really want to get handy with these basics concepts, as you know i really am interested in programming

    well, i tried to see the difference by changing 0 to 1 in the first program
    the calculation will include the -1 (let say i entered 10 for 10 times, and the 11th time i typed -1 to end the program), the average is 9.09 which is corrected if we include -1 as one of the grade, but in this case, the result is totally wrong
    if we stay with 0, the result is right

    i know in the second program we set counter only up to 10, and anything beyond 10 will be ignored -> result will only be the average of those 10 grades...
    if i change the 1 to 0 in the second program, the program will run from 0 to 10, meaning 11 grades will be entered
    okay, this i get it

    (1) am i correct about this assumption? --> "definite counter always set from a specific number, initialize from 1; if the number of counter is not definite, then we initialize the counter from 0"

    (2) i am not quite understand what you mean by "The result is that the first uses the variable to store what HAS BEEN done vs what IS ABOUT TO BE done."

    (3) like what i did with the first program, i thought we set -1 to end the program, why did the program still think -1 is a grade value, and add to the total?
    whereas the counter starts from 0, the -1 will not add to the total.....
    http://i3physics.com/blog
    *-*-*-*__ C++ revolutionized the modern programming language, but what happen to C+? Programming is just a study of chemistry __*-*-*-*

  5. #4
    Join Date
    Jul 2006
    Posts
    16,448
    Blog Entries
    74
    Rep Power
    143

    Re: sentinel-control-structure

    Here's my thinking on it, and this may not answer your questions:

    For many people, when they think about "getting ten numbers", they think in terms of "start at 1, go through 10 (inclusive)". They then account for any issues with arrays, etc, as needed. When people think about "get some numbers", they think in terms of keeping track of how many have been collected. Initially, the number collected is 0 and they don't want to assume that there will be even 1 number collected.

    Note that going from 0 to 9 is equivalent to going from 1 to 10, as far as the number of values collected, but has a different mental model.

    I would have to look closely at what happens to the code execution for question 3).. have you traced it out by hand?
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  6. #5
    jwxie518's Avatar
    jwxie518 is offline Speaks fluent binary
    Join Date
    Jan 2009
    Location
    New York City
    Posts
    1,175
    Blog Entries
    1
    Rep Power
    18

    Re: sentinel-control-structure

    the following is pretty confusing, if any...


    if we really mean the 2nd program limit the # of counter only up to 10, then i think people will always initialize from 1 because, like you said, the program is counting just the number (so from 0 to 9 is still 10)

    but if we leave while ( gradeCounter <=10 ) as it is when gradeCounter = 0, then the program will probably think we are doing 0 to 10, imagine they are reading off the counting from a number line 0 1 2 3 4 5 6 7 8 9 10 (that's 11 counting), therefore the result is not correct

    for the first program, if gradeCounter is = 1 and leave everything else as it is
    now, run the program, we will do

    test #1 (10 times of 10 and 11th time with -1), the result will 10 x 10 divide by 11 grades = 9.09

    test #2 (9 times of 10, and 10th with -1), the result will be 10 x 9 divide by 9 grades = 9 which is correct
    when i look at the program very closely in test #1 and test #2, the program knows -1 is not a grade at all (it does not meet the condition) so it turns to false (end the program)

    in test #1, you will see 10 10 10 10 10 10 10 10 10 10 | -1 even though the program knows -1 is not a grade which returns a zero --> this is what the program thinks, although it's a zero value, it still counts another counter (11th) ...
    therefore, total grade = 10 x 10 +0 and total counter = 11
    so the result in #1 is correct: ( 10 x 10 + 0 ) / 11 = 9.09 in the first program


    in test #2, i entered 9 times of 10, and when it reaches the 10th time, i said -1, so the program will produce a result of [ ( 10 x 9 ) + 0 ] / 10 = 9.0


    now it's up to the question #3 i am stuck at

    when counter starts from 0, and if value = -1, then the program will just count 10 x 10 and then divide by 10 which gives you just 10
    but if we did what we did with test #1 and #2, the result is the opposite, the counter will continue to +1

    because in the first program, gradeCounter = gradeCounter +1 we know it is fine when counter sets to 0, why?
    i hope i didn't get you lost hahahaa
    http://i3physics.com/blog
    *-*-*-*__ C++ revolutionized the modern programming language, but what happen to C+? Programming is just a study of chemistry __*-*-*-*

  7. #6
    Join Date
    Jul 2006
    Posts
    16,448
    Blog Entries
    74
    Rep Power
    143

    Re: sentinel-control-structure

    Go ahead and trace through all the values in detail. One of the guys at work was dealing with this type of issue earlier this week. It took both of us a few hours to figure out what was happening. Tracing through the actual values on paper can be very enlightening.
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  8. #7
    jwxie518's Avatar
    jwxie518 is offline Speaks fluent binary
    Join Date
    Jan 2009
    Location
    New York City
    Posts
    1,175
    Blog Entries
    1
    Rep Power
    18

    Re: sentinel-control-structure

    i am doing the tracing, but i want to ask a question
    what is the real meaning of this gradeCounter = 0
    we set it to zero, so what would the program read it as?
    i don't know if that's just the same question as #3, but while i am doing the tracing, this question pops up ....

    this = is not equal, in this case is to start from 0? or just clear out the memory?
    i think i am leading to our #3 question
    http://i3physics.com/blog
    *-*-*-*__ C++ revolutionized the modern programming language, but what happen to C+? Programming is just a study of chemistry __*-*-*-*

  9. #8
    Join Date
    Jul 2006
    Posts
    16,448
    Blog Entries
    74
    Rep Power
    143

    Re: sentinel-control-structure

    gradeCounter = 0 initializes gradeCounter to 0.
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  10. #9
    jwxie518's Avatar
    jwxie518 is offline Speaks fluent binary
    Join Date
    Jan 2009
    Location
    New York City
    Posts
    1,175
    Blog Entries
    1
    Rep Power
    18

    Re: sentinel-control-structure

    i got it
    after doing 9 different testing tracing different types of counter
    i finally reach the reason why the author uses 0 instead of 1 in the first program

    -1 no grade
    10 -1 10
    10 20 -1 15
    10 20 30 -1 20
    0 + -1 (0) = 0 -> no grade, 0/0 will return the else message

    0 + 10 = 0
    0 + -1 (0) = 0 + 1 -> that's 10 -1 so 10 + 0 divide by 1 = 10

    0+10 = 0
    10+20 = 0+1
    30+ -1 (0) = 1+1 ---> 10 10 -1 so 30 + 0 divide by 2 = 15

    0+10 = 0
    10+20 = 0+1
    30+30 = 1+1
    60+ -1(0) = 2+1 ----> 10 20 30 -1 so 60 + 0 divide by 3 = 20
    this applies to almost any combination but except for one case (that i have to work on it again)
    since we want to make sure the human remembers how many numbers have been enter, and so starting from 0 is a good choice when the loop is adding + 1 to every value gets enter
    the -1 is the sentinel value (ends the loop), but it is still another value the program will count, so it is true that, although it returns a zero (-1 = 0), in the loop it's gradeCounter + 1.. so yeah

    i think that's how the program work (indeed yes base on the loop), but it's confuse when i only look at the -1 and 0 itself

    thanks wingedpanther, you really made this problem interesting....
    please correct me if anything is not correct

    yes we can use any combination, but the loop will be affected
    the + 1 must change in some cases
    but in a common real world, we always assume 0 to starts the +1

    in 2nd program, since it stops at just 10, therefore it is okay to use the 1 in the beginning
    http://i3physics.com/blog
    *-*-*-*__ C++ revolutionized the modern programming language, but what happen to C+? Programming is just a study of chemistry __*-*-*-*

  11. #10
    Join Date
    Jul 2006
    Posts
    16,448
    Blog Entries
    74
    Rep Power
    143

    Re: sentinel-control-structure

    It sounds like you've done the analysis well Good job.

    One of the things I've discovered over the years is that coding isn't the hard part, debugging is. When you start looking at larger projects, you will discover that there are LOTS of little things that are put in place to catch strange possibilities that aren't obvious. When I had to reimplement one piece of functionality, I had to create a flowchart of the existing logic to really understand what was going on. It's hard to do that kind of analysis, but it pays off, and makes you a better programmer.
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

Closed Thread
Page 1 of 2 12 LastLast

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. How to find a control that was created through a custom control (dll file)
    By ManyTimes in forum ASP, ASP.NET and Coldfusion
    Replies: 0
    Last Post: 05-31-2011, 04:08 PM
  2. Is there are difference between version control and revision control?
    By DarkLordofthePenguins in forum General Programming
    Replies: 4
    Last Post: 02-20-2011, 06:21 PM
  3. Replies: 3
    Last Post: 11-19-2010, 02:22 AM
  4. structure of PCI ID not clear
    By onus in forum C and C++
    Replies: 0
    Last Post: 11-02-2010, 02:50 AM
  5. Menu structure
    By Orjan in forum PHP Development
    Replies: 4
    Last Post: 07-21-2008, 10:37 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts