Counters and Accumulators
Author |
Message |
Tallguy
|
Posted: Fri Jun 01, 2007 8:25 am Post subject: Counters and Accumulators |
|
|
Repetitive structures: Counters and Accumulators
Counters
OK. Most of you have been using counters since you started doing animation. When one needs to count how many times a program executes a loop (without using a counted loop), one uses a counter. Counters need two things in order to function: 1) an Initial Value, and; 2) an Incremental Value. The initial value is usually (but not always) zero, and must be located outside the loop. It tells the program what number to begin counting at. The incremental value, on the other hand, tells the program what to count by (one's usually, but it could be any number) and must be located inside the loop. Try this:
Turing: |
%Program to count ten passes through a conditional loop
var counter: int:=0
loop
counter:= counter +1
put counter, " "..
exit when counter = 10
end loop
|
Try changing the increment from 1 to 2. What happens?
Accumulators
Accumulators are identical to counters with one difference. While they must have both an initial value and an incremental value; the incremental value is determined by a variable. Accumulators are used to keep a running total of numbers during successive passes through a loop. Try this:
Turing: |
var answer: string
var gasbill, totalbill: real
var billnumber:int:=1
totalbill:=0
loop
put "Enter the value of gas bill number ", billnumber, "."
put "Type 0 to exit program."
get gasbill
if gasbill = 0 then
put "Are you sure? (Y/N)"
get answer
exit when answer = "y" or answer = "Y"
end if
totalbill:= totalbill + gasbill
billnumber:= billnumber +1
end loop
put "You had ", billnumber - 1, "gas bills."
put "they totalled ", totalbill, "dollars."
|
|
|
|
|
|
|
Sponsor Sponsor
|
|
|
DifinityRJ
|
Posted: Fri Jun 01, 2007 10:50 am Post subject: Re: Counters and Accumulators |
|
|
Did you get this passed by an admin to write this tutorial about counters and accumulators? |
|
|
|
|
|
Cervantes
|
Posted: Fri Jun 01, 2007 6:49 pm Post subject: RE:Counters and Accumulators |
|
|
Er, it's not that we require that you get your tutorial approved before posting, but rather that if you're serious about making a tutorial, we appreciate it if you send it to us first so we can help out and offer advice. There's a lot involved in writing quality tutorials.
I don't really think this topic needs a tutorial, though. It's not a special kind of syntax, and it's a not mind bending language concept, and it's not a difficult algorithm.
I've cleaned the post up a bit so now it has code tags, and some minor headers. |
|
|
|
|
|
Tallguy
|
Posted: Sat Jun 02, 2007 3:37 pm Post subject: RE:Counters and Accumulators |
|
|
srry for not running it by u first, thanks for fixing the mistakes i made |
|
|
|
|
|
DaveAngus
|
Posted: Tue Mar 18, 2008 9:22 am Post subject: RE:Counters and Accumulators |
|
|
Yea this is a pretty easy concept.
Not to sure why theres a tutorial.
But...then again...
we were all in a starting position in this program and sometimes you need to learn the basics.
Thanks for posting! |
|
|
|
|
|
|
|