Difference between revisions of "Accumulator Simple Lists Tutorial"

From Progzoo
Jump to: navigation, search
(Count Small Numbers)
(Sum Small Numbers)
Line 139: Line 139:
 
<p class='imper'>Print the sum of number of numbers less than 3.</p>
 
<p class='imper'>Print the sum of number of numbers less than 3.</p>
 
<prog lang="java">
 
<prog lang="java">
static int f(int [] a){
+
static int f(int [] a)
 +
{
 
   int acc = 1;
 
   int acc = 1;
 
   for (int i : a)
 
   for (int i : a)
Line 149: Line 150:
 
</prog>
 
</prog>
 
<answer lang="java"><![CDATA[
 
<answer lang="java"><![CDATA[
static int f(int [] l){
+
static int f(int [] l)
 +
{
 
   int acc = 0;
 
   int acc = 0;
 
   for (int i : l)
 
   for (int i : l)

Revision as of 20:21, 12 September 2012

Sum

Print the total of all of the numbers

  • We initialise to 0.
  • We add to the accumulator.


[Font] [Default] [Show] [Resize] [History] [Profile]

Product

Print the product of all of the numbers.

  • We initialise to 1.
  • We multiply the accumulator.


[Font] [Default] [Show] [Resize] [History] [Profile]

Min

Print the smallest item in the list.


[Font] [Default] [Show] [Resize] [History] [Profile]

Count Small Numbers

Print the number of numbers less than 3.


[Font] [Default] [Show] [Resize] [History] [Profile]

Sum Small Numbers

Print the sum of number of numbers less than 3.


[Font] [Default] [Show] [Resize] [History] [Profile]

Count Even Numbers

Print the number of even numbers.


[Font] [Default] [Show] [Resize] [History] [Profile]