Two simple algorithms

  • #1
332
1
Hi

Please help me with the following queries. Please be specific and simple in your replies. Thanks a lot.

Page 1:
http://img15.imageshack.us/img15/6656/chp11page1.jpg

Page 2:
http://img248.imageshack.us/img248/202/chp11page2.jpg

On page 1 the author says "Is total mark sheets checked = 50" but in the next example on page 2 the author says "If total employees checked < 100". Isn't that "<100" thing really confusing? Is the algorithm suppose to stop once the value is greater than 100 which won't happen? If the author had assigned the first employee index 0, then it would be more apt to say "If total employees checked = 99".

Moreover, don't you think a vital step is missing from the algorithm on page 2 after Step 7 because there would be employees who wouldn't receive any bonus but need to included in the counter of total employees checked?

What is the need to initialize the counters to zero in Step 1 of both algorithms?

In Step 2 of the algorithm on Page 1 it says "Take the mark sheet of NEXT student" and the same is said in Step 3 of the algorithm on Page 2 "Take the appraisal form of the NEXT employee". What is that "NEXT" student/employee is the first student/employee; I mean when the algorithm has just stated working there would be always a 'first' case it will work on and once it is done with it, then it will proceed to the next case.

Please help me with the above questions. I seriously need your help! Thanks a lot.
 

Answers and Replies

  • #2
297
2
HiOn page 1 the author says "Is total mark sheets checked = 50" but in the next example on page 2 the author says "If total employees checked < 100". Isn't that "<100" thing really confusing? Is the algorithm suppose to stop once the value is greater than 100 which won't happen? If the author had assigned the first employee index 0, then it would be more apt to say "If total employees checked = 99".
There's different ways to do it. In the first, he says if it's NOT 50, loop back to a previous step. In the second, he says if it IS less than 100, then loop back to a previous step. Both slightly different ways of doing similar things. Sometimes one is clearer than the other. I wouldn't worry about it.

Note that, in the second example, the counter is incremented at the end of the processing. So the first employee is indeed 0. After that record is processed, the counter increments. Think about it. You're on the last employee. He's number 99 (starting at 0). So you've finished processing him. Now you increment the counter to 100. That number is not smaller than 100, so the loop exits. So it does in fact exit after the last employee. And despite starting at 0, the counter accurately reflects the number of employees processed (100).

Moreover, don't you think a vital step is missing from the algorithm on page 2 after Step 7 because there would be employees who wouldn't receive any bonus but need to included in the counter of total employees checked?
It specifically says there are three grades: A, B and C. So everyone gets a bonus in this case. And every employee is counted, regardless. Of course, if it was possible for an employee to not get a bonus (being neither A, B or C), you would need logic such as "otherwise, Bonus = 0" between steps 7 and 8.
What is the need to initialize the counters to zero in Step 1 of both algorithms?

Good question. The answer is that you can't assume a value for a variable until you assign it a value. It's likely to contain garbage until you initialize it. So a general rule of programming is to make sure you initialize all your variables to sane values.

In Step 2 of the algorithm on Page 1 it says "Take the mark sheet of NEXT student" and the same is said in Step 3 of the algorithm on Page 2 "Take the appraisal form of the NEXT employee". What is that "NEXT" student/employee is the first student/employee; I mean when the algorithm has just stated working there would be always a 'first' case it will work on and once it is done with it, then it will proceed to the next case.
Well, if you haven't started, you can consider the next one as the first one. Nothing wrong with that logic.
 
Last edited:
  • #3
34,884
6,621
Hi

Please help me with the following queries. Please be specific and simple in your replies. Thanks a lot.

Page 1:
http://img15.imageshack.us/img15/6656/chp11page1.jpg

Page 2:
http://img248.imageshack.us/img248/202/chp11page2.jpg

On page 1 the author says "Is total mark sheets checked = 50" but in the next example on page 2 the author says "If total employees checked < 100". Isn't that "<100" thing really confusing?
The author doesn't seem to be very consistent.
Is the algorithm suppose to stop once the value is greater than 100 which won't happen? If the author had assigned the first employee index 0, then it would be more apt to say "If total employees checked = 99".
Neither of these algorithms is very useful. Both are completely missing the step that returns to a previous step in a loop that is executed a specific number of times. The whole purpose of using a counter variable is to control the number of times a specific block of code (or steps in an algorithm) is executed.
Moreover, don't you think a vital step is missing from the algorithm on page 2 after Step 7 because there would be employees who wouldn't receive any bonus but need to included in the counter of total employees checked?
The description preceding the algorithm implies that all employees get a bonus, since all of them would be in one of three categories.
What is the need to initialize the counters to zero in Step 1 of both algorithms?
A counter variable that is incremented (added to) always has to start with some known number, typically zero.
In Step 2 of the algorithm on Page 1 it says "Take the mark sheet of NEXT student" and the same is said in Step 3 of the algorithm on Page 2 "Take the appraisal form of the NEXT employee". What is that "NEXT" student/employee is the first student/employee; I mean when the algorithm has just stated working there would be always a 'first' case it will work on and once it is done with it, then it will proceed to the next case.
At the beginning of the process, the NEXT student or employee will be the first one.
Please help me with the above questions. I seriously need your help! Thanks a lot.
 
  • #4
297
2
Neither of these algorithms is very useful. Both are completely missing the step that returns to a previous step in a loop that is executed a specific number of times. The whole purpose of using a counter variable is to control the number of times a specific block of code (or steps in an algorithm) is executed.
Might be me, but I'm not sure what you mean by that. In example 2:

Step 10: If Total_Employees_Checked < 100 Then go to Step 2.

The counter is correctly incremented, and it loops back to an appropriate place. When it's done 100 employees, it falls through to Step 11. Seems to me it will loop properly, but maybe I'm missing something.
 
  • #5
332
1
There's different ways to do it. In the first, he says if it's NOT 50, loop back to a previous step. In the second, he says if it IS less than 100, then loop back to a previous step. Both slightly different ways of doing similar things. Sometimes one is clearer than the other. I wouldn't worry about it.

Note that, in the second example, the counter is incremented at the end of the processing. So the first employee is indeed 0. After that record is processed, the counter increments. Think about it. You're on the last employee. He's number 99 (starting at 0). So you've finished processing him. Now you increment the counter to 100. That number is not smaller than 100, so the loop exits. So it does in fact exit after the last employee. And despite starting at 0, the counter accurately reflects the number of employees processed (100).

Thank you very much, Grep, Mark. You people are so nice. I wish you all prosperity and best wishes.

Please be concise, simple and straightforward in your replies because I know very little.

Grep, I don't get the bold part. Rather I would like to say I think there is something wrong with it. If the first employee was assigned value 0, then the last one would be assigned 99; and 99 is less than 100, i.e. 99<100. That would mean the the procedure would step back to Step 2 again which I think it should because it has already counted 100 employees (from 0-99). I would say the author is suggesting that the counter assigned the value of 1 to the first employee. Once it has processed the the last employee, the counter would be 100 and this is obviously not less than 100, i.e. 100 is not less 100 rather they are both equal.

Do I make any sense? Please let me know as soon as possible. Thanks a lot.
 
  • #6
34,884
6,621
Might be me, but I'm not sure what you mean by that. In example 2:

Step 10: If Total_Employees_Checked < 100 Then go to Step 2.

The counter is correctly incremented, and it loops back to an appropriate place. When it's done 100 employees, it falls through to Step 11. Seems to me it will loop properly, but maybe I'm missing something.
Nope, my mistake. I don't know how I missed this in both algorithms.
 
  • #7
297
2
Thank you very much, Grep, Mark. You people are so nice. I wish you all prosperity and best wishes.

Please be concise, simple and straightforward in your replies because I know very little.

Grep, I don't get the bold part. Rather I would like to say I think there is something wrong with it. If the first employee was assigned value 0, then the last one would be assigned 99; and 99 is less than 100, i.e. 99<100. That would mean the the procedure would step back to Step 2 again which I think it should because it has already counted 100 employees (from 0-99). I would say the author is suggesting that the counter assigned the value of 1 to the first employee. Once it has processed the the last employee, the counter would be 100 and this is obviously not less than 100, i.e. 100 is not less 100 rather they are both equal.

Nope. Go through the logic step by step. While processing the last employee, the counter is indeed 99 (and we're on the 100th employee). It is then incremented before the check is made and after the employee is processed. So after processing the last employee, the counter goes up to 100. Now, we loop back to Step 2 if the counter is less than 100. It's not, it's equal to 100 now. So it doesn't loop back. Rather, it goes on to Step 11.

And thanks for the kind words. :)
 
  • #8
297
2
Nope, my mistake. I don't know how I missed this in both algorithms.
hehe It happens to us all. Which is why I so often add disclaimers like "... but maybe I'm missing something" and so on.
 
  • #10
332
1
Grep, I think I get it now. I was interpreting in a wrong way. And those kind words! I wish I could offer you wonderful people something more than them, perhaps a gift and a thank you card! :-)

Mark, thanks a lot. You have really helped me with many of my problems.
 

Related Threads on Two simple algorithms

  • Last Post
Replies
2
Views
585
Replies
8
Views
1K
  • Last Post
Replies
22
Views
2K
  • Last Post
Replies
2
Views
2K
  • Last Post
Replies
2
Views
3K
  • Last Post
Replies
4
Views
2K
  • Last Post
Replies
2
Views
6K
  • Last Post
Replies
2
Views
18K
  • Last Post
Replies
17
Views
8K
Top