Computer Science Canada Programming C, C++, Java, PHP, Ruby, Turing, VB   Username:   Password: Wiki   Blog   Search   Turing   Chat Room  Members
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."

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!
 Display posts from previous: All Posts1 Day7 Days2 Weeks1 Month3 Months6 Months1 Year Oldest FirstNewest First

Page 1 of 1  [ 5 Posts ]
 Jump to:  Select a forum  CompSci.ca ------------ - Network News - General Discussion     General Forums   -----------------   - Hello World   - Featured Poll   - Contests     Contest Forums   -----------------   - DWITE   - [FP] Contest 2006/2008   - [FP] 2005/2006 Archive   - [FP] 2004/2005 Archive   - Off Topic     Lounges   ---------   - User Lounge   - VIP Lounge     Programming -------------- - General Programming     General Programming Forums   --------------------------------   - Functional Programming   - Logical Programming   - C     C   --   - C Help   - C Tutorials   - C Submissions   - C++     C++   ----   - C++ Help   - C++ Tutorials   - C++ Submissions   - Java     Java   -----   - Java Help   - Java Tutorials   - Java Submissions   - Ruby     Ruby   -----   - Ruby Help   - Ruby Tutorials   - Ruby Submissions   - Turing     Turing   --------   - Turing Help   - Turing Tutorials   - Turing Submissions   - PHP     PHP   ----   - PHP Help   - PHP Tutorials   - PHP Submissions   - Python     Python   --------   - Python Help   - Python Tutorials   - Python Submissions   - Visual Basic and Other Basics     VB   ---   - Visual Basic Help   - Visual Basic Tutorials   - Visual Basic Submissions     Education ----------- - Student Life   Graphics and Design ----------------------- - Web Design     Web Design Forums   ---------------------   - (X)HTML Help   - (X)HTML Tutorials   - Flash MX Help   - Flash MX Tutorials   - Graphics     Graphics Forums   ------------------   - Photoshop Tutorials   - The Showroom   - 2D Graphics   - 3D Graphics     Teams ------ - dTeam Public

 Style: Appalachia blueSilver eMJay subAppalachia subBlue subCanvas subEmjay subGrey subSilver subVereor Search: