Difference between revisions of "Using an Accumulator"

From Progzoo
Jump to: navigation, search
(Using an accumulator to count.)
(Using an accumulator to find the maximum)
 
(18 intermediate revisions by 2 users not shown)
Line 3: Line 3:
 
</p>
 
</p>
 
<dl>
 
<dl>
<dt>Initialise</dt>
+
<dt>Initialise
 
<dd>
 
<dd>
 
We declare our accumulator and set it to an initial values.
 
We declare our accumulator and set it to an initial values.
</dd>
+
<dt>Update
<dt>Update</dt>
+
 
<dd>
 
<dd>
 
Inside the loop we update the accumulator - we take into account the
 
Inside the loop we update the accumulator - we take into account the
 
current data item.
 
current data item.
</dd>
+
<dt>Output/use
<dt>Output/use</dt>
+
 
<dd>
 
<dd>
 
After the loop our accumulator contains the required value. We use it
 
After the loop our accumulator contains the required value. We use it
 
or output it.
 
or output it.
</dd>
+
==Using an accumulator to add==
</dl>
+
==Using an accumulator to add.==
+
 
<question className="P" title="Using an accumulator to add.">
 
<question className="P" title="Using an accumulator to add.">
 
<shell lang="java" className="P" import="shells.xml#main"/>
 
<shell lang="java" className="P" import="shells.xml#main"/>
Line 35: Line 31:
 
</question>
 
</question>
  
==Using an accumulator to count.==
+
==Using an accumulator to count==
 
<question className="P" title="Using an accumulator to count.">
 
<question className="P" title="Using an accumulator to count.">
 
<shell lang="java" className="P" import="shells.xml#main"/>
 
<shell lang="java" className="P" import="shells.xml#main"/>
Line 52: Line 48:
 
</question>
 
</question>
  
==Using an accumulator to count on a condition.==
+
==Using an accumulator to count on a condition==
 
<question className="P" title="Using an accumulator to count.">
 
<question className="P" title="Using an accumulator to count.">
 
<shell lang="java" className="P" import="shells.xml#main"/>
 
<shell lang="java" className="P" import="shells.xml#main"/>
Line 71: Line 67:
 
</question>
 
</question>
  
==Using an accumulator to multiply.==
+
==Using an accumulator to multiply==
 
<question className="P" title="Using an accumulator to multiply.">
 
<question className="P" title="Using an accumulator to multiply.">
 
<shell lang="java" className="P" import="shells.xml#main"/>
 
<shell lang="java" className="P" import="shells.xml#main"/>
<blurb>
+
<p class='imper'>Print the product of the numbers.</p>
<ul>
+
*We initialise to 1. (If you initialise to 0 then the result will always be 0.)
<li> We initialise to 1. </li>
+
*We multiply the accumulator.
<li> We multiply the accumulator. </li>
+
*The value printed is the product: 1*2*7*1*1
<li> The value printed is the product: 1*2*7*1*1 </li>
+
</ul>
+
</blurb>
+
 
<prog lang="java">
 
<prog lang="java">
 
int acc = 1;
 
int acc = 1;
Line 90: Line 83:
 
</prog>
 
</prog>
 
</question>
 
</question>
==Using an accumulator to find the maximum.==
+
 
 +
==Using an accumulator to find the maximum==
 
<question className="P" title="Using an accumulator to find the maximum.">
 
<question className="P" title="Using an accumulator to find the maximum.">
 
<shell lang="java" className="P" import="shells.xml#main"/>
 
<shell lang="java" className="P" import="shells.xml#main"/>
<blurb>
+
<p class='imper'>Print the largest number in the list.</p>
<ul>
+
*We initialise to 0.
<li> We initialise to 0. </li>
+
*We take the max.
<li> We take the max. </li>
+
*The value printed is the largest:
<li> The value printed is the largest:
+
At each stage the accumulator stays the same or gets bigger (set to the current value).
<pre>max(max(max(max(0,2),7),1),1)</pre> </li>
+
max(max(max(max(0,2),7),1),1)
</ul>
+
<hint tease='What about smaller numbers?'>
</blurb>
+
If all of the numbers in the list were negative, for example <code>{-2,-7,-1,-1}</code> then this program
 +
would print 0 instead of the correct answer -1. You can fix that by starting the accumulator at the smallest possible
 +
integer:
 +
int acc = Integer.MIN_VALUE;
 +
for (int i : new int[]{2,7,1,1})
 +
{
 +
  acc = Math.max(acc,i);
 +
}
 +
System.out.println(acc);
 +
</hint>
 
<prog lang="java">
 
<prog lang="java">
 
int acc = 0;
 
int acc = 0;
Line 110: Line 113:
 
</prog>
 
</prog>
 
</question>
 
</question>
==Using an accumulator to concatenate.==
+
 
 +
==Using an accumulator to concatenate==
 
<question className="P" title="Using an accumulator to concatenate.">
 
<question className="P" title="Using an accumulator to concatenate.">
 
<shell lang="java" className="P" import="shells.xml#main"/>
 
<shell lang="java" className="P" import="shells.xml#main"/>
<blurb>
+
<p class='imper'>Concatenate all items in the list</p>
<ul>
+
Notice that we are using a list of strings this time.
<li> We initialise to "". </li>
+
*We initialise to the empty string "".
<li> We concatenate the next value. </li>
+
*We concatenate the next value (stick at the end).
<li> Each time i gets converted to a string and is put at the end.
+
WARNING - Using an accumulator like this can use a great deal of memory. You will find using a [[StringBuilder]] is more efficient.
</li>
+
StringBuilder acc = new StringBuilder();
<li>The answer is ""+2+7+1+1</li>
+
for (String s:new String[]{"one","two"})
</ul>
+
{
</blurb>
+
  acc.append(s);
 +
}
 +
System.out.println(acc);
 +
 
 
<prog lang="java">
 
<prog lang="java">
 
String acc = "";
 
String acc = "";
for (int i : new int[]{2,7,1,1})
+
for (String s : new String[]{"two","seven","one","one"})
 
{
 
{
   acc += i;
+
   acc += s;
 
}
 
}
 
System.out.println(acc);
 
System.out.println(acc);
 
</prog>
 
</prog>
 
</question>
 
</question>
==Using two accumulators to find the mean.==
+
 
 +
==Using two accumulators to find the mean==
 
<question className="P" title="Using two accumulators to find the mean.">
 
<question className="P" title="Using two accumulators to find the mean.">
 
<shell lang="java" className="P" import="shells.xml#main"/>
 
<shell lang="java" className="P" import="shells.xml#main"/>
<blurb>
+
<p class='imper'>Print the mean of all items in the list.</p>
<ul>
+
*We initialise both <code>sum</code> and <code>count</code> to 0.
<li> We initialise both to 0.  </li>
+
*We add to the sum and increment the count.
<li> We add to the sum and increment the count. </li>
+
*We divide the sum by the count to get the average.
<li> We divide the sum by the count to get the average.
+
</li>
+
</ul>
+
</blurb>
+
 
<prog lang="java">
 
<prog lang="java">
 
int count = 0;
 
int count = 0;
 
int sum  = 0;
 
int sum  = 0;
for (int i : new int[]{2,7,1,1})
+
for (int i : new int[]{3,7,1,1})
 
{
 
{
 
   count++;
 
   count++;
Line 151: Line 155:
 
}
 
}
 
System.out.println((float)sum/count);
 
System.out.println((float)sum/count);
</prog>
 
</question>
 
==Using an accumulator to calculate.==
 
<question className="P" title="Using an accumulator to calculate.">
 
<shell lang="java" className="P" import="shells.xml#main"/>
 
<blurb>
 
<ul>
 
<li> We initialise to 0.  </li>
 
<li> We multiply by the radix and add the next digit.  </li>
 
<li> By the end the number 2 has been multiplied by 10 three times,
 
the number has been multiplied by 10 twice...
 
</li>
 
<li>10*(10*(10*2+7)+1)+1</li>
 
</ul>
 
</blurb>
 
<prog lang="java">
 
int acc = 0;
 
for (int i : new int[]{2,7,1,1})
 
{
 
  acc = 10*acc+i;
 
}
 
System.out.println(acc);
 
 
</prog>
 
</prog>
 
</question>
 
</question>

Latest revision as of 18:35, 20 June 2009

When using an accumulating variable there are three stages:

Initialise
We declare our accumulator and set it to an initial values.
Update
Inside the loop we update the accumulator - we take into account the current data item.
Output/use
After the loop our accumulator contains the required value. We use it or output it.

Using an accumulator to add

Print the total of all of the numbers

  • We initialise to 0.
  • We add to the accumulator.
  • The value printed is the sum 0+2+7+1+1.


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

Using an accumulator to count

Print the number of items in the list.

  • We initialise to 0.
  • We increment the accumulator.
  • The value printed is the count: 0+1+1+1+1.


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

Using an accumulator to count on a condition

Count the number of 1s in the list

  • We initialise to 0.
  • We increment the accumulator only if the current value equals 1.


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

Using an accumulator to multiply

Print the product of the numbers.

  • We initialise to 1. (If you initialise to 0 then the result will always be 0.)
  • We multiply the accumulator.
  • The value printed is the product: 1*2*7*1*1


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

Using an accumulator to find the maximum

Print the largest number in the list.

  • We initialise to 0.
  • We take the max.
  • The value printed is the largest:

At each stage the accumulator stays the same or gets bigger (set to the current value).

max(max(max(max(0,2),7),1),1)


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

Using an accumulator to concatenate

Concatenate all items in the list

Notice that we are using a list of strings this time.

  • We initialise to the empty string "".
  • We concatenate the next value (stick at the end).

WARNING - Using an accumulator like this can use a great deal of memory. You will find using a StringBuilder is more efficient.

StringBuilder acc = new StringBuilder();
for (String s:new String[]{"one","two"})
{
  acc.append(s);
}
System.out.println(acc);


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

Using two accumulators to find the mean

Print the mean of all items in the list.

  • We initialise both sum and count to 0.
  • We add to the sum and increment the count.
  • We divide the sum by the count to get the average.


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