<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>davinci’s notebook &#187; pointers</title>
	<atom:link href="http://stargrads.net/blogs/davinci/tag/pointers/feed/" rel="self" type="application/rss+xml" />
	<link>http://stargrads.net/blogs/davinci</link>
	<description>everything is an experiment</description>
	<lastBuildDate>Mon, 21 Mar 2011 18:31:14 +0000</lastBuildDate>
	<language>fa</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<item>
		<title>Programming exercise: maximum value in integer array, part 1</title>
		<link>http://stargrads.net/blogs/davinci/2009/09/programming-exercise-maximum-value-in-integer-array-part-1/</link>
		<comments>http://stargrads.net/blogs/davinci/2009/09/programming-exercise-maximum-value-in-integer-array-part-1/#comments</comments>
		<pubDate>Wed, 30 Sep 2009 06:32:20 +0000</pubDate>
		<dc:creator>davinci</dc:creator>
				<category><![CDATA[programming and technical issues]]></category>
		<category><![CDATA[arrays]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[Common Lisp. Scheme]]></category>
		<category><![CDATA[comparison of programming languages]]></category>
		<category><![CDATA[fold]]></category>
		<category><![CDATA[int]]></category>
		<category><![CDATA[integers]]></category>
		<category><![CDATA[iteration]]></category>
		<category><![CDATA[iterators]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Lisp]]></category>
		<category><![CDATA[max]]></category>
		<category><![CDATA[maximum]]></category>
		<category><![CDATA[pointers]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[programming exercises]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[recursion]]></category>
		<category><![CDATA[reduce]]></category>
		<category><![CDATA[STL]]></category>
		<category><![CDATA[tail recursion]]></category>

		<guid isPermaLink="false">http://stargrads.net/blogs/davinci/?p=2145</guid>
		<description><![CDATA[
In this post, I write a program to find the maximum value in an integer array in several programming languages.
]]></description>
			<content:encoded><![CDATA[<p>This exercise is just a <em>little</em> bit more substantial than the <a href="http://stargrads.net/blogs/davinci/2009/09/programming-exercise-hello-world/">last one</a> &#8212; but not by very much.  Given an array of \(n\) non-negative integers, find the maximum value in the array, or return \(-1\) if the array is empty.  Obviously, the use of any built-in maximum-finding function is forbidden.  While the problem is almost trivial, it does illustrate how each language works with array or vector data types, as well as how it handles iteration.  </p>
<p>This exercise, like some of the other ones that I will also be going through, is from the book <a href="http://www.amazon.ca/gp/product/047012167X?ie=UTF8&amp;tag=davincisnoteb-20&amp;linkCode=as2&amp;camp=15121&amp;creative=330641&amp;creativeASIN=047012167X"><i>Programming Interviews Exposed</i></a><img src="http://www.assoc-amazon.ca/e/ir?t=davincisnoteb-20&amp;l=as2&amp;o=15&amp;a=047012167X" width="1" height="1" border="0" alt="" style="border:none !important;margin:0px !important" /> by John Mongan and Noah Suojanen<sup><a class='footnote' id='note-2145-1' href='#footnote-2145-ms00pie'>[1]</a></sup><span id="more-2145"></span>.<!--adsensestart--></p>
<p>For consistency, let&#8217;s assume that the array for which we&#8217;re trying to find the maximum value is \(\{6, 3, 8, 7, 1, 2, 0, 9, 4, 5\}\).  Let&#8217;s also assume for simplicity that \(n\) is given, that we don&#8217;t have to check for errors (the input is a list of non-negative integers as promised), and that we can write \(-1\) for the error return code.  (In production code, we would of course define a named constant such as <code>ARRAY_EMPTY_ERROR</code>, or use some other form of error-handling such as exceptions.)<!--adsensestop--></p>
<p>Let&#8217;s begin with C.  The code is pretty straightforward, except that I had to remind myself to declare the loop counter \(i\) outside of the loop.  (Declaring it in the loop initialisation is not allowed in standards prior to C99.)  It&#8217;s also a lot harder to code on paper than it is on a keyboard (my handwriting is terrible, due to atrophy).  Here&#8217;s the program in C:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
</pre></td><td class="code"><pre class="c" style="font-family:monospace;"><span style="color: #339933;">#include&amp;lt;stdio.h&amp;gt;</span>
&nbsp;
<span style="color: #993333;">int</span> arraymax<span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span> array<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #993333;">int</span> n<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #993333;">int</span> currentMax<span style="color: #339933;">;</span>
    <span style="color: #993333;">int</span> i<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span> n<span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;=</span> <span style="color: #0000dd;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #b1b100;">return</span> <span style="color: #339933;">-</span><span style="color: #0000dd;">1</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span>
&nbsp;
    currentMax <span style="color: #339933;">=</span> array<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span> i <span style="color: #339933;">=</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span> n<span style="color: #339933;">;</span> i<span style="color: #339933;">++</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span> array<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span> <span style="color: #339933;">&amp;</span>gt<span style="color: #339933;">;</span> currentMax <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            currentMax <span style="color: #339933;">=</span> array<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #b1b100;">return</span> currentMax<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #993333;">int</span> main<span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span> argc<span style="color: #339933;">,</span> <span style="color: #993333;">char</span> <span style="color: #339933;">*</span>argv<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #993333;">int</span> testinput<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span> <span style="color: #0000dd;">6</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">3</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">8</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">7</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">2</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">9</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">4</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">5</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
    <span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;max = %d<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span> arraymax<span style="color: #009900;">&#40;</span>testinput<span style="color: #339933;">,</span> <span style="color: #0000dd;">10</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>I could, of course, have written the exact same program in C++, but to make things interesting, I&#8217;m going to use the <abbr title="Standard Template Library">STL</abbr> <code>vector</code> class:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
</pre></td><td class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #339900;">#include&amp;lt;iostream&amp;gt;</span>
<span style="color: #339900;">#include&amp;lt;vector&amp;gt;</span>
&nbsp;
<span style="color: #0000ff;">using</span> <span style="color: #0000ff;">namespace</span> std<span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">int</span> vectormax<span style="color: #008000;">&#40;</span>vector<span style="color: #000040;">&amp;</span>lt<span style="color: #008080;">;</span><span style="color: #0000ff;">int</span><span style="color: #000040;">&amp;</span>gt<span style="color: #008080;">;</span> vec<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    <span style="color: #0000ff;">int</span> currentMax<span style="color: #008080;">;</span>
&nbsp;
    <span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span> vec.<span style="color: #007788;">empty</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
        <span style="color: #0000ff;">return</span> <span style="color: #000040;">-</span><span style="color: #0000dd;">1</span><span style="color: #008080;">;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    currentMax <span style="color: #000080;">=</span> vec<span style="color: #008000;">&#91;</span><span style="color: #0000dd;">0</span><span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span>
    <span style="color: #0000ff;">for</span><span style="color: #008000;">&#40;</span> <span style="color: #0000ff;">int</span> i <span style="color: #000080;">=</span> <span style="color: #0000dd;">1</span><span style="color: #008080;">;</span> i <span style="color: #000040;">&amp;</span>lt<span style="color: #008080;">;</span> vec.<span style="color: #007788;">size</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> i<span style="color: #000040;">++</span> <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
        <span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span> vec<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span> <span style="color: #000040;">&amp;</span>gt<span style="color: #008080;">;</span> currentMax <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
            currentMax <span style="color: #000080;">=</span> vec<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span>
        <span style="color: #008000;">&#125;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #0000ff;">return</span> currentMax<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0000ff;">int</span> main<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    <span style="color: #0000ff;">int</span> values<span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> <span style="color: #000080;">=</span> <span style="color: #008000;">&#123;</span> <span style="color: #0000dd;">6</span>, <span style="color: #0000dd;">3</span>, <span style="color: #0000dd;">8</span>, <span style="color: #0000dd;">7</span>, <span style="color: #0000dd;">1</span>, <span style="color: #0000dd;">2</span>, <span style="color: #0000dd;">0</span>, <span style="color: #0000dd;">9</span>, <span style="color: #0000dd;">4</span>, <span style="color: #0000dd;">5</span> <span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
    vector<span style="color: #000040;">&amp;</span>lt<span style="color: #008080;">;</span><span style="color: #0000ff;">int</span><span style="color: #000040;">&amp;</span>gt<span style="color: #008080;">;</span> testinput<span style="color: #008000;">&#40;</span>values, values<span style="color: #000040;">+</span><span style="color: #0000dd;">10</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    <span style="color: #0000dd;">cout</span> <span style="color: #000040;">&amp;</span>lt<span style="color: #008080;">;</span><span style="color: #000040;">&amp;</span>lt<span style="color: #008080;">;</span> <span style="color: #FF0000;">&quot;max = &quot;</span> <span style="color: #000040;">&amp;</span>lt<span style="color: #008080;">;</span><span style="color: #000040;">&amp;</span>lt<span style="color: #008080;">;</span> vectormax<span style="color: #008000;">&#40;</span>testinput<span style="color: #008000;">&#41;</span> <span style="color: #000040;">&amp;</span>lt<span style="color: #008080;">;</span><span style="color: #000040;">&amp;</span>lt<span style="color: #008080;">;</span> endl<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>The only tricky part was I had to look up how to initialise a vector without looping through the values and using <code>push_back</code>.  The code above calls the iteration constructor, using the trick that an <code>int</code> array in C++ is just a pointer to its first element, and the fact that pointers are iterators.</p>
<p>Apparently, in the proposed <a href="http://en.wikipedia.org/wiki/C%2B%2B0x">c++0x</a> standard, it will be possible to initialise a vector using a static array like <a href="http://en.wikipedia.org/wiki/C%2B%2B0x#Initializer_lists">this</a>:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>26
27
</pre></td><td class="code"><pre class="cpp" style="font-family:monospace;">    <span style="color: #666666;">// Use the initialiser list constructor.</span>
    vector<span style="color: #000040;">&amp;</span>lt<span style="color: #008080;">;</span><span style="color: #0000ff;">int</span><span style="color: #000040;">&amp;</span>gt<span style="color: #008080;">;</span> testinput <span style="color: #000080;">=</span> <span style="color: #008000;">&#123;</span> <span style="color: #0000dd;">6</span>, <span style="color: #0000dd;">3</span>, <span style="color: #0000dd;">8</span>, <span style="color: #0000dd;">7</span>, <span style="color: #0000dd;">1</span>, <span style="color: #0000dd;">2</span>, <span style="color: #0000dd;">0</span>, <span style="color: #0000dd;">9</span>, <span style="color: #0000dd;">4</span>, <span style="color: #0000dd;">5</span> <span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span></pre></td></tr></table></div>

<p>In the program above, I&#8217;ve used indices as if the vector were just a regular array.  The preferred way to access vector elements in C++ is to use iterators.  This means replacing lines 14&#8211;15 with the following:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>14
15
16
17
18
19
</pre></td><td class="code"><pre class="cpp" style="font-family:monospace;">    vector<span style="color: #000040;">&amp;</span>lt<span style="color: #008080;">;</span><span style="color: #0000ff;">int</span><span style="color: #000040;">&amp;</span>gt<span style="color: #008080;">;::</span><span style="color: #007788;">iterator</span> vi <span style="color: #000080;">=</span> vec.<span style="color: #007788;">begin</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    <span style="color: #0000ff;">for</span><span style="color: #008000;">&#40;</span> currentMax <span style="color: #000080;">=</span> <span style="color: #000040;">*</span>vi<span style="color: #000040;">++</span><span style="color: #008080;">;</span> vi <span style="color: #000040;">!</span><span style="color: #000080;">=</span> vec.<span style="color: #007788;">end</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> vi<span style="color: #000040;">++</span> <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
        <span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span> <span style="color: #000040;">*</span>vi <span style="color: #000040;">&amp;</span>gt<span style="color: #008080;">;</span> currentMax <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
            currentMax <span style="color: #000080;">=</span> <span style="color: #000040;">*</span>vi<span style="color: #008080;">;</span>
        <span style="color: #008000;">&#125;</span>
    <span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>I had to think a little bit about the loop initialisation condition, <code>currentMax = *vi++</code>.  The iterator is <a href="http://stackoverflow.com/questions/859770/-on-a-dereferenced-pointer-in-c">dereferenced before it is incremented</a>, which is the desired behaviour.</p>
<p>There are a number of differences between the C# and C/C++ programs.  First, there&#8217;s the <code>foreach</code> statement, which makes for nicer-looking loops.  Then there&#8217;s the fact that the array type specifier <code>[]</code> must appear before the variable name, rather than after.  Here&#8217;s the code:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">System</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> ArrayMax <span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">int</span> arraymax<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">int</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> array<span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        <span style="color: #0600FF; font-weight: bold;">if</span><span style="color: #008000;">&#40;</span> array<span style="color: #008000;">.</span><span style="color: #0000FF;">Length</span> <span style="color: #008000;">==</span> <span style="color: #FF0000;">0</span> <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
            <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #008000;">-</span><span style="color: #FF0000;">1</span><span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
&nbsp;
        <span style="color: #6666cc; font-weight: bold;">int</span> currentMax <span style="color: #008000;">=</span> array<span style="color: #008000;">&#91;</span><span style="color: #FF0000;">0</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">;</span>
        <span style="color: #0600FF; font-weight: bold;">foreach</span><span style="color: #008000;">&#40;</span> <span style="color: #6666cc; font-weight: bold;">int</span> value <span style="color: #0600FF; font-weight: bold;">in</span> array <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
            <span style="color: #0600FF; font-weight: bold;">if</span><span style="color: #008000;">&#40;</span> value <span style="color: #008000;">&amp;</span>gt<span style="color: #008000;">;</span> currentMax <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
                currentMax <span style="color: #008000;">=</span> value<span style="color: #008000;">;</span>
            <span style="color: #008000;">&#125;</span>
        <span style="color: #008000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF; font-weight: bold;">return</span> currentMax<span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">void</span> Main<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
        <span style="color: #6666cc; font-weight: bold;">int</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> testinput <span style="color: #008000;">=</span> <span style="color: #008000;">&#123;</span> <span style="color: #FF0000;">6</span>, <span style="color: #FF0000;">3</span>, <span style="color: #FF0000;">8</span>, <span style="color: #FF0000;">7</span>, <span style="color: #FF0000;">1</span>, <span style="color: #FF0000;">2</span>, <span style="color: #FF0000;">0</span>, <span style="color: #FF0000;">9</span>, <span style="color: #FF0000;">4</span>, <span style="color: #FF0000;">5</span> <span style="color: #008000;">&#125;</span><span style="color: #008000;">;</span>
        Console<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;max = {0}<span style="color: #008080; font-weight: bold;">\n</span>&quot;</span>, arraymax<span style="color: #008000;">&#40;</span>testinput<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>I had to remember to make the <code>arraymax</code> function static so that it can be called without instantiating the <code>ArrayMax</code> class.</p>
<p>The Java code is essentially identical to the C# code, <em>mutatis mutandis</em>:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> ArrayMax <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">int</span> arraymax<span style="color: #009900;">&#40;</span> <span style="color: #000066; font-weight: bold;">int</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> array <span style="color: #009900;">&#41;</span> 
    <span style="color: #009900;">&#123;</span> 
        <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span> array.<span style="color: #006633;">length</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">0</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #000066; font-weight: bold;">int</span> currentMax <span style="color: #339933;">=</span> array<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span> <span style="color: #000066; font-weight: bold;">int</span> i <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span> array.<span style="color: #006633;">length</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span> array<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span> <span style="color: #339933;">&amp;</span>gt<span style="color: #339933;">;</span> currentMax <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                currentMax <span style="color: #339933;">=</span> array<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">return</span> currentMax<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span> <span style="color: #003399;">String</span> args<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000066; font-weight: bold;">int</span> testinput<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span> <span style="color: #cc66cc;">6</span>, <span style="color: #cc66cc;">3</span>, <span style="color: #cc66cc;">8</span>, <span style="color: #cc66cc;">7</span>, <span style="color: #cc66cc;">1</span>, <span style="color: #cc66cc;">2</span>, <span style="color: #cc66cc;">0</span>, <span style="color: #cc66cc;">9</span>, <span style="color: #cc66cc;">4</span>, <span style="color: #cc66cc;">5</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
        <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;max = &quot;</span> <span style="color: #339933;">+</span> arraymax<span style="color: #009900;">&#40;</span>testinput<span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Beginning in J2SE v1.5/5.0<sup><a class='footnote' id='note-2145-2' href='#footnote-2145-java_version_history'>[2]</a></sup>, Java introduced a &#8220;for each&#8221; loop, and so lines 9&#8211;13 above should be replaced with this:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>9
10
11
12
13
</pre></td><td class="code"><pre class="java" style="font-family:monospace;">        <span style="color: #000000; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span> <span style="color: #000066; font-weight: bold;">int</span> value <span style="color: #339933;">:</span> array <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span> value <span style="color: #339933;">&amp;</span>gt<span style="color: #339933;">;</span> currentMax <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                currentMax <span style="color: #339933;">=</span> value<span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Now that we&#8217;ve gotten the standard general-purpose imperative and object-oriented (i.e., <a href="http://en.wikipedia.org/wiki/C-like_syntax">&#8220;C-like&#8221;</a>) languages out of the way, it&#8217;s time for some fun(ctional programming).</p>
<p>Of course, Lisp and Scheme already have a <code>max</code> function defined to get the maximum value in a list of integers, but as mentioned before, its use is forbidden to us.  Nevertheless, the solution to the given problem in these languages is still very simple.  How can we express the function that returns the maximum value of a list of integers?  One way to do it is to express it as the <a href="http://en.wikipedia.org/wiki/Fold_(higher-order_function)">folding (or reducing)</a> of the list with a function that compares <em>two</em> integer values and returns the larger one.  Expressed in Lisp, this function is:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">defun</span> larger <span style="color: #66cc66;">&#40;</span>x y<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&amp;</span>gt<span style="color: #808080; font-style: italic;">; x y) x y))</span></pre></td></tr></table></div>

<p>The rest of the Lisp program easily follows:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>2
3
4
5
6
7
8
</pre></td><td class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">defun</span> list-<span style="color: #b1b100;">max</span> <span style="color: #66cc66;">&#40;</span>intlist<span style="color: #66cc66;">&#41;</span> 
    <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">null</span> intlist<span style="color: #66cc66;">&#41;</span> -<span style="color: #cc66cc;">1</span>
          <span style="color: #66cc66;">&#40;</span>reduce #'larger intlist<span style="color: #66cc66;">&#41;</span>
     <span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">setq</span> intlist '<span style="color: #66cc66;">&#40;</span> <span style="color: #cc66cc;">6</span> <span style="color: #cc66cc;">3</span> <span style="color: #cc66cc;">8</span> <span style="color: #cc66cc;">7</span> <span style="color: #cc66cc;">1</span> <span style="color: #cc66cc;">2</span> <span style="color: #cc66cc;">0</span> <span style="color: #cc66cc;">9</span> <span style="color: #cc66cc;">4</span> <span style="color: #cc66cc;">5</span> <span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span>format t <span style="color: #ff0000;">&quot;max = ~D&quot;</span> <span style="color: #66cc66;">&#40;</span>list-<span style="color: #b1b100;">max</span> intlist<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></td></tr></table></div>

<p>(Incidentally, it&#8217;s because of things like <code>#'</code>, which is called the sharp-quote, that some people don&#8217;t like Lisp.)</p>
<p>The Scheme program is essentially identical:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code"><pre class="scheme" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">define</span> <span style="color: #66cc66;">&#40;</span>listmax intlist<span style="color: #66cc66;">&#41;</span> 
    <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">null?</span> intlist<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">-</span><span style="color: #cc66cc;">1</span>
          <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">let</span> <span style="color: #66cc66;">&#40;</span> <span style="color: #66cc66;">&#40;</span>larger <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">lambda</span> <span style="color: #66cc66;">&#40;</span>x y<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&amp;</span>gt<span style="color: #808080; font-style: italic;">; x y) x y)) ) )</span>
            <span style="color: #66cc66;">&#40;</span>fold larger <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">car</span> intlist<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">cdr</span> intlist<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">define</span> intlist '<span style="color: #66cc66;">&#40;</span> <span style="color: #cc66cc;">6</span> <span style="color: #cc66cc;">3</span> <span style="color: #cc66cc;">8</span> <span style="color: #cc66cc;">7</span> <span style="color: #cc66cc;">1</span> <span style="color: #cc66cc;">2</span> <span style="color: #cc66cc;">0</span> <span style="color: #cc66cc;">9</span> <span style="color: #cc66cc;">4</span> <span style="color: #cc66cc;">5</span> <span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">display</span> <span style="color: #ff0000;">&quot;max = &quot;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">display</span> <span style="color: #66cc66;">&#40;</span>listmax intlist<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></td></tr></table></div>

<p>The Python code is also very similar:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">def</span> listmax<span style="color: black;">&#40;</span>intlist<span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">if</span> intlist == <span style="color: black;">&#91;</span><span style="color: black;">&#93;</span>:
        <span style="color: #ff7700;font-weight:bold;">return</span> -<span style="color: #ff4500;">1</span>
    <span style="color: #ff7700;font-weight:bold;">else</span>: 
        <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">reduce</span><span style="color: black;">&#40;</span> <span style="color: #ff7700;font-weight:bold;">lambda</span> x, y : x <span style="color: #ff7700;font-weight:bold;">if</span> x <span style="color: #66cc66;">&amp;</span>gt<span style="color: #66cc66;">;</span> y <span style="color: #ff7700;font-weight:bold;">else</span> y, intlist<span style="color: black;">&#41;</span>
&nbsp;
testinput = <span style="color: black;">&#91;</span> <span style="color: #ff4500;">6</span>, <span style="color: #ff4500;">3</span>, <span style="color: #ff4500;">8</span>, <span style="color: #ff4500;">7</span>, <span style="color: #ff4500;">1</span>, <span style="color: #ff4500;">2</span>, <span style="color: #ff4500;">0</span>, <span style="color: #ff4500;">9</span>, <span style="color: #ff4500;">4</span>, <span style="color: #ff4500;">5</span> <span style="color: black;">&#93;</span>
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;max =&quot;</span>, listmax<span style="color: black;">&#40;</span>testinput<span style="color: black;">&#41;</span></pre></td></tr></table></div>

<p>I really like the Python code.  It combines the simplicity of the fold idea used in Lisp/Scheme with the clarity of an ALGOL-like syntax.</p>
<p>I should mention that another way of expressing the function that returns the maximum value in a list of integers is as a recursive function that returns the only element in a list of size one, and returns the larger of the first element and the result of the function applied to the rest of the list for a list of size two or more.  However, unless the program is written using <a href="http://en.wikipedia.org/wiki/Tail_recursion">tail recursion</a>, and one is using a compiler that optimises tail-recursive calls, the stack will grow with the size of the list, so this solution is not as good as the one using fold/reduce.</p>
<p>I was originally also going to write the program in the remaining languages listed in the <a href="http://stargrads.net/blogs/davinci/2009/09/programming-exercise-hello-world/">previous post</a>, but I&#8217;ve spent far too much time already on this exercise, and I&#8217;d like to move on to some more difficult ones.  And besides, the program in the other functional languages will be very similar except for syntax, and there&#8217;s almost no point in coding a &#8220;maximum integer value in an array&#8221; function in the mathematical languages, since an optimised version of such a function is already built into them.  I may return to this exercise later with additional languages, so I&#8217;ll append &#8220;part 1&#8243; to the title of this post.</p>
<p>&#8211; davinci 11796</p>
<img src="http://stargrads.net/blogs/davinci/?ak_action=api_record_view&id=2145&type=feed" alt="" /><p>Related posts:<ol>
<li><a href='http://stargrads.net/blogs/davinci/2009/10/programming-exercise-permutations-of-a-string/' rel='bookmark' title='Programming exercise: permutations of a string'>Programming exercise: permutations of a string</a></li>
<li><a href='http://stargrads.net/blogs/davinci/2009/10/programming-exercise-combinations-of-a-string/' rel='bookmark' title='Programming exercise: combinations of a string'>Programming exercise: combinations of a string</a></li>
<li><a href='http://stargrads.net/blogs/davinci/2009/09/programming-exercise-hello-world/' rel='bookmark' title='Programming exercise: Hello, world!'>Programming exercise: Hello, world!</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://stargrads.net/blogs/davinci/2009/09/programming-exercise-maximum-value-in-integer-array-part-1/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

