<?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>C ++ &#187; Function</title>
	<atom:link href="http://www.bizzymicbizness.com/cplusplus/tag/function/feed" rel="self" type="application/rss+xml" />
	<link>http://www.bizzymicbizness.com/cplusplus</link>
	<description>All about C++</description>
	<lastBuildDate>Mon, 19 Oct 2009 12:38:17 +0000</lastBuildDate>
	
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>More Array Function Examples</title>
		<link>http://www.bizzymicbizness.com/cplusplus/more-array-function-examples</link>
		<comments>http://www.bizzymicbizness.com/cplusplus/more-array-function-examples#comments</comments>
		<pubDate>Sun, 18 Oct 2009 12:36:59 +0000</pubDate>
		<dc:creator>BMB</dc:creator>
				<category><![CDATA[C++ Glossary]]></category>
		<category><![CDATA[Array]]></category>
		<category><![CDATA[Borland C++]]></category>
		<category><![CDATA[Bottom-Up Programming]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[const]]></category>
		<category><![CDATA[Function]]></category>
		<category><![CDATA[iostream]]></category>
		<category><![CDATA[OOP]]></category>
		<category><![CDATA[Pointer]]></category>
		<category><![CDATA[Procedural Programming]]></category>
		<category><![CDATA[Top-Down Programming]]></category>

		<guid isPermaLink="false">http://www.bizzymicbizness.com/cplusplus/?p=801</guid>
		<description><![CDATA[When you choose to use an array to represent data, you are making a design decision

But design decisions should go beyond how data is stored; they should also involve how the data is used


Often, you’ll find it profitable to write specific functions to handle specific data operations

(The profits here include increased program reliability, ease of [...]]]></description>
			<content:encoded><![CDATA[<p>When you choose to use an array to represent data, you are making a design decision</p>
<ul>
<li>But design decisions should go beyond how data is stored; they should also involve how the data is used</li>
</ul>
<p><br class="blank" /><br />
Often, you’ll find it profitable to write specific functions to handle specific data operations</p>
<ul>
<li>(The profits here include increased program reliability, ease of modification, and ease of debugging.)
<ul>
<li>Also, when you begin integrating storage properties with operations when you think about a program, you are taking an important step toward the OOP mind-set; that, too, might prove profitable in the future</li>
</ul>
</li>
</ul>
<p><br class="blank" /><br />
Let’s examine a simple case</p>
<ul>
<li>Suppose you want to use an array to keep track of the dollar values of your real estate
<ul>
<li>(If necessary, suppose you have real estate.)
<ul>
<li>You have to decide what type to use
<ul>
<li>Certainly, <strong>double</strong> is less restrictive in its range than <strong>int</strong> or <strong>long</strong>, and it provides enough significant digits to represent the values precisely</li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
</ul>
<p><br class="blank" /><br />
Next, you have to decide on the number of array elements</p>
<ul>
<li>(With dynamic arrays created with new, you can put off that decision, but let’s keep things simple.)
<ul>
<li>Let’s say that you have no more than five properties, so you can use an array of five <strong>doubles</strong></li>
</ul>
</li>
</ul>
<p><br class="blank" /><br />
Now consider the possible operations you might want to execute with the real estate array</p>
<ul>
<li>Two very basic ones are:</li>
<ul>
<li>Reading values into the array</li>
<li>And displaying the array contents</li>
</ul>
<li>Let’s add one more operation to the list:</li>
<ul>
<li>Reassessing the value of the properties</li>
<ul>
<li>For simplicity, assume that all your properties increase or decrease in value at the same rate</li>
</ul>
</ul>
<li>(Remember, this is a topic in C++, not on real estate management.)</li>
</ul>
<p><br class="blank" /><br />
Next, fit a function to each operation and then write the code accordingly</p>
<ul>
<li>We’ll go through the steps of creating these pieces of a program next
<ul>
<li>Afterward, we’ll fit them into a complete example</li>
</ul>
</li>
</ul>
<p><br class="blank" /><br />
<br class="blank" /></p>
<h2>Filling the Array</h2>
<p><br class="blank" /><br />
Because a function with an array name argument accesses the original array, not a copy, you can use a function call to assign values to array elements</p>
<ul>
<li>One argument to the function will be the name of the array to be filled
<ul>
<li>In general, a program might manage more than one person’s investments, hence more than one array, so you don’t want to build the array size into the function</li>
<ul>
<li>Instead, you pass the array size as a second argument</li>
</ul>
</ul>
<li>Also, it’s possible that you might want to quit reading data before filling the array, so you want to build that feature in to the function</li>
<ul>
<li>Because you might enter fewer than the maximum number of elements, it makes sense to have the function return the actual number of values entered
<ul>
<li>These considerations suggest the following function prototype (A-1)</li>
</ul>
</li>
</ul>
</li>
</ul>
<p><br class="blank" /><br />
Example of (A-1)</p>

<div class="syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">int</span> fill_array<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">double</span> ar<span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span>, <span style="color: #0000ff;">int</span> limit<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></pre></div></div>

<p><br class="blank" /><br />
The function takes an array name argument and an argument specifying the maximum number of items to be read, and the function returns the actual number of items read</p>
<ul>
<li>For example, if you use this function with an array of five elements, you pass <strong>5</strong> as the second argument
<ul>
<li>If you then enter only three values, the function returns <strong>3</strong></li>
</ul>
</li>
</ul>
<p><br class="blank" /><br />
You can use a loop to read successive values into the array, but how can you terminate the loop early?</p>
<ul>
<li>One way is to use a special value to indicate the end of input</li>
<ul>
<li>Because no property should have a negative value, you can use a negative number to indicate the end of input</li>
</ul>
<li>Also, the function should do something about bad input</li>
<ul>
<li>Such as terminating further input</li>
</ul>
<li>Given this, you can code the function as follows (A-2):</li>
<ul>
<li>Note that this code includes a prompt to the user</li>
<ul>
<li>If the user enters a non-negative value, the value is assigned to the array</li>
<ul>
<li>Otherwise, the loop terminates</li>
</ul>
<li>If the user enters only valid values</li>
<ul>
<li>The loop terminates after it reads limit values</li>
</ul>
</ul>
<li>The last thing the loop does is increment <strong>i</strong>, so after the loop terminates, <strong>i</strong> is one greater than the last array index, hence it’s equal to the number of filled elements</li>
<ul>
<li>The function then returns that value</li>
</ul>
</ul>
</ul>
<p><br class="blank" /><br />
Example of (A-2)</p>

<div class="syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">int</span> fill_array<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">double</span> ar<span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span>, <span style="color: #0000ff;">int</span> limit<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
	<span style="color: #0000ff;">using</span> <span style="color: #0000ff;">namespace</span> std<span style="color: #008080;">;</span>
	<span style="color: #0000ff;">double</span> temp<span style="color: #008080;">;</span>
	<span style="color: #0000ff;">int</span> i<span style="color: #008080;">;</span>
	<span style="color: #0000ff;">for</span> <span style="color: #008000;">&#40;</span>i <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span> i <span style="color: #000080;">&lt;</span> limit<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: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Enter value #&quot;</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #008000;">&#40;</span>i <span style="color: #000040;">+</span> <span style="color: #0000dd;">1</span><span style="color: #008000;">&#41;</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;: &quot;</span><span style="color: #008080;">;</span>
		<span style="color: #0000dd;">cin</span> <span style="color: #000080;">&gt;&gt;</span> temp<span style="color: #008080;">;</span>
		<span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span><span style="color: #000040;">!</span><span style="color: #0000dd;">cin</span><span style="color: #008000;">&#41;</span> <span style="color: #666666;">// bad input</span>
		<span style="color: #008000;">&#123;</span>
&nbsp;
			<span style="color: #0000dd;">cin</span>.<span style="color: #007788;">clear</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
			<span style="color: #0000ff;">while</span> <span style="color: #008000;">&#40;</span><span style="color: #0000dd;">cin</span>.<span style="color: #007788;">get</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #000040;">!</span><span style="color: #000080;">=</span> <span style="color: #FF0000;">'<span style="color: #000099; font-weight: bold;">\n</span>'</span><span style="color: #008000;">&#41;</span>
				<span style="color: #0000ff;">continue</span><span style="color: #008080;">;</span>
			<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Bad input; input process terminated.<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #008080;">;</span>
			<span style="color: #0000ff;">break</span><span style="color: #008080;">;</span>
		<span style="color: #008000;">&#125;</span>
		<span style="color: #0000ff;">else</span> <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>temp <span style="color: #000080;">&lt;</span> <span style="color: #0000dd;">0</span><span style="color: #008000;">&#41;</span> <span style="color: #666666;">// signal to terminate</span>
			<span style="color: #0000ff;">break</span><span style="color: #008080;">;</span>
		ar<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span> <span style="color: #000080;">=</span> temp<span style="color: #008080;">;</span>
	<span style="color: #008000;">&#125;</span>
	<span style="color: #0000ff;">return</span> i<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p><br class="blank" /><br />
<br class="blank" /></p>
<h2>Showing the Array and Protecting It with const</h2>
<p><br class="blank" /><br />
Building a function to display the array contents is simple</p>
<ul>
<li>You pass the name of the array and the number of filled elements to the function, which then uses a loop to display each element</li>
</ul>
<p><br class="blank" /><br />
But there is another consideration—guaranteeing that the display function doesn’t alter the original array</p>
<ul>
<li>Unless the purpose of a function is to alter data passed to it, you should safeguard it from doing so</li>
<ul>
<li>That protection comes automatically with ordinary arguments because C++ passes them by value, and the function works with a copy</li>
</ul>
<li>But functions that use an array work with the original</li>
<ul>
<li>After all, that’s why the <strong>fill_array()</strong> function is able to do its job</li>
</ul>
</ul>
<p><br class="blank" /><br />
To keep a function from accidentally altering the contents of an array argument</p>
<ul>
<li>You can use the keyword <strong>const</strong> when you declare the formal argument (A-3)</li>
<ul>
<li>The declaration states that the pointer <strong>ar</strong> points to constant data</li>
<ul>
<li>This means that you can’t use <strong>ar</strong> to change the data</li>
<ul>
<li>That is, you can use a value such as <strong>ar[0]</strong>, but you can’t change that value</li>
</ul>
</ul>
</ul>
<li>Note that this doesn’t mean that the original array needs be constant; it just means that you can’t use <strong>ar</strong> in the <strong>show_array()</strong> function to change the data</li>
<ul>
<li>Thus, <strong>show_array()</strong> treats the array as read-only data</li>
</ul>
<li>Suppose you accidentally violate this restriction by doing something like the following in the <strong>show_array()</strong> function (A-4)</li>
<ul>
<li>In this case, the compiler will put a stop to your wrongful ways</li>
<ul>
<li>Borland C++, for example, gives an error message like this (edited slightly) (A-5)</li>
<ul>
<li>Other compilers may choose to express their displeasure in different words</li>
</ul>
<li>The message reminds you that C++ interprets the declaration <strong>const double</strong> <strong>ar[]</strong> to mean <strong>const double *ar</strong></li>
<ul>
<li>Thus, the declaration really says that <strong>ar</strong> points to a constant value</li>
<ul>
<li>We’ll discuss this in detail when we finish with the current example</li>
</ul>
</ul>
</ul>
</ul>
<li>Meanwhile, here is the code for the <strong>show_array()</strong> function (A-6)</li>
</ul>
<p><br class="blank" /><br />
Example of (A-3)</p>

<div class="syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">void</span> show_array<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> <span style="color: #0000ff;">double</span> ar<span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span>, <span style="color: #0000ff;">int</span> n<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></pre></div></div>

<p><br class="blank" /><br />
Example of (A-4)</p>

<div class="syntax"><div class="code"><pre class="cpp" style="font-family:monospace;">ar<span style="color: #008000;">&#91;</span><span style="color: #0000dd;">0</span><span style="color: #008000;">&#93;</span> <span style="color: #000040;">+</span><span style="color: #000080;">=</span> <span style="color: #0000dd;">10</span><span style="color: #008080;">;</span></pre></div></div>

<p><br class="blank" /><br />
Example of (A-5)</p>
<div class="syntax">
<div class="code">
<pre class="cpp" style="font-family:monospace;">
Cannot modify a const object in function show_array(const double *,int)
</pre>
</div>
</div>
<p><br class="blank" /><br />
Example of (A-6)</p>

<div class="syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">void</span> show_array<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> <span style="color: #0000ff;">double</span> ar<span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span>, <span style="color: #0000ff;">int</span> n<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
	<span style="color: #0000ff;">using</span> <span style="color: #0000ff;">namespace</span> std<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;">0</span><span style="color: #008080;">;</span> i <span style="color: #000080;">&lt;</span> n<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: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Property #&quot;</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #008000;">&#40;</span>i <span style="color: #000040;">+</span> <span style="color: #0000dd;">1</span><span style="color: #008000;">&#41;</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;: $&quot;</span><span style="color: #008080;">;</span>
		<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> ar<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
	<span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p><br class="blank" /><br />
<br class="blank" /></p>
<h2>Modifying the Array</h2>
<p><br class="blank" /><br />
The third operation for the array in this example is multiplying each element by the same revaluation factor</p>
<ul>
<li>You need to pass three arguments to the function:</li>
<ul>
<li>The factor</li>
<li>The array</li>
<li>And the number of elements</li>
</ul>
<li>No return value is needed, so the function can look like this (A-7)</li>
<ul>
<li>Because this function is supposed to alter the array values, you don’t use <strong>const</strong> when you declare <strong>ar</strong></li>
</ul>
</ul>
<p><br class="blank" /><br />
Example of (A-7)</p>

<div class="syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">void</span> revalue<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">double</span> r, <span style="color: #0000ff;">double</span> ar<span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span>, <span style="color: #0000ff;">int</span> n<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</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;">0</span><span style="color: #008080;">;</span> i <span style="color: #000080;">&lt;</span> n<span style="color: #008080;">;</span> i<span style="color: #000040;">++</span><span style="color: #008000;">&#41;</span>
		ar<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span> <span style="color: #000040;">*</span><span style="color: #000080;">=</span> r<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p><br class="blank" /><br />
<br class="blank" /></p>
<h2>Putting the Pieces Together</h2>
<p><br class="blank" /><br />
Now that you’ve defined a data type in terms of how it’s stored (an array) and how it’s used (three functions), you can put together a program that uses the design</p>
<ul>
<li>Because you’ve already built all the array-handling tools</li>
<ul>
<li>You’ve greatly simplified programming <strong>main()</strong></li>
</ul>
<li>Most of the remaining programming work consists of having <strong>main()</strong> call the functions you’ve just developed</li>
<li>(A-8) shows the result</li>
<ul>
<li>It places a <strong>using</strong> directive in just those functions that use the <strong>iostream</strong> facilities</li>
</ul>
</ul>
<p><br class="blank" /><br />
Example of (A-8)</p>

<div class="syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #666666;">// arrfun3.cpp -- array functions and const</span>
<span style="color: #339900;">#include &lt;iostream&gt;</span>
<span style="color: #0000ff;">const</span> <span style="color: #0000ff;">int</span> Max <span style="color: #000080;">=</span> <span style="color: #0000dd;">5</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #666666;">// function prototypes</span>
<span style="color: #0000ff;">int</span> fill_array<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">double</span> ar<span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span>, <span style="color: #0000ff;">int</span> limit<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #0000ff;">void</span> show_array<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> <span style="color: #0000ff;">double</span> ar<span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span>, <span style="color: #0000ff;">int</span> n<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> <span style="color: #666666;">// don’t change data</span>
<span style="color: #0000ff;">void</span> revalue<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">double</span> r, <span style="color: #0000ff;">double</span> ar<span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span>, <span style="color: #0000ff;">int</span> n<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</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;">using</span> <span style="color: #0000ff;">namespace</span> std<span style="color: #008080;">;</span>
	<span style="color: #0000ff;">double</span> properties<span style="color: #008000;">&#91;</span>Max<span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span>
&nbsp;
	<span style="color: #0000ff;">int</span> size <span style="color: #000080;">=</span> fill_array<span style="color: #008000;">&#40;</span>properties, Max<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	show_array<span style="color: #008000;">&#40;</span>properties, size<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Enter reassessment rate: &quot;</span><span style="color: #008080;">;</span>
	<span style="color: #0000ff;">double</span> factor<span style="color: #008080;">;</span>
	<span style="color: #0000dd;">cin</span> <span style="color: #000080;">&gt;&gt;</span> factor<span style="color: #008080;">;</span>
	revalue<span style="color: #008000;">&#40;</span>factor, properties, size<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	show_array<span style="color: #008000;">&#40;</span>properties, size<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Done.<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #008080;">;</span>
	<span style="color: #0000ff;">return</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0000ff;">int</span> fill_array<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">double</span> ar<span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span>, <span style="color: #0000ff;">int</span> limit<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
	<span style="color: #0000ff;">using</span> <span style="color: #0000ff;">namespace</span> std<span style="color: #008080;">;</span>
	<span style="color: #0000ff;">double</span> temp<span style="color: #008080;">;</span>
	<span style="color: #0000ff;">int</span> i<span style="color: #008080;">;</span>
	<span style="color: #0000ff;">for</span> <span style="color: #008000;">&#40;</span>i <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span> i <span style="color: #000080;">&lt;</span> limit<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: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Enter value #&quot;</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #008000;">&#40;</span>i <span style="color: #000040;">+</span> <span style="color: #0000dd;">1</span><span style="color: #008000;">&#41;</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;: &quot;</span><span style="color: #008080;">;</span>
		<span style="color: #0000dd;">cin</span> <span style="color: #000080;">&gt;&gt;</span> temp<span style="color: #008080;">;</span>
		<span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span><span style="color: #000040;">!</span><span style="color: #0000dd;">cin</span><span style="color: #008000;">&#41;</span> <span style="color: #666666;">// bad input</span>
		<span style="color: #008000;">&#123;</span>
			<span style="color: #0000dd;">cin</span>.<span style="color: #007788;">clear</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
			<span style="color: #0000ff;">while</span> <span style="color: #008000;">&#40;</span><span style="color: #0000dd;">cin</span>.<span style="color: #007788;">get</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #000040;">!</span><span style="color: #000080;">=</span> <span style="color: #FF0000;">'<span style="color: #000099; font-weight: bold;">\n</span>'</span><span style="color: #008000;">&#41;</span>
				<span style="color: #0000ff;">continue</span><span style="color: #008080;">;</span>
			<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Bad input; input process terminated.<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #008080;">;</span>
			<span style="color: #0000ff;">break</span><span style="color: #008080;">;</span>
		<span style="color: #008000;">&#125;</span>
		<span style="color: #0000ff;">else</span> <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>temp <span style="color: #000080;">&lt;</span> <span style="color: #0000dd;">0</span><span style="color: #008000;">&#41;</span> <span style="color: #666666;">// signal to terminate</span>
			<span style="color: #0000ff;">break</span><span style="color: #008080;">;</span>
		ar<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span> <span style="color: #000080;">=</span> temp<span style="color: #008080;">;</span>
	<span style="color: #008000;">&#125;</span>
	<span style="color: #0000ff;">return</span> i<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #666666;">// the following function can use, but not alter,</span>
<span style="color: #666666;">// the array whose address is ar</span>
<span style="color: #0000ff;">void</span> show_array<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> <span style="color: #0000ff;">double</span> ar<span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span>, <span style="color: #0000ff;">int</span> n<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
	<span style="color: #0000ff;">using</span> <span style="color: #0000ff;">namespace</span> std<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;">0</span><span style="color: #008080;">;</span> i <span style="color: #000080;">&lt;</span> n<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: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Property #&quot;</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #008000;">&#40;</span>i <span style="color: #000040;">+</span> <span style="color: #0000dd;">1</span><span style="color: #008000;">&#41;</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;: $&quot;</span><span style="color: #008080;">;</span>
		<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> ar<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
	<span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #666666;">// multiplies each element of ar[] by r</span>
<span style="color: #0000ff;">void</span> revalue<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">double</span> r, <span style="color: #0000ff;">double</span> ar<span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span>, <span style="color: #0000ff;">int</span> n<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</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;">0</span><span style="color: #008080;">;</span> i <span style="color: #000080;">&lt;</span> n<span style="color: #008080;">;</span> i<span style="color: #000040;">++</span><span style="color: #008000;">&#41;</span>
		ar<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span> <span style="color: #000040;">*</span><span style="color: #000080;">=</span> r<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p><br class="blank" /><br />
Here are two sample runs of the program in (A-8):</p>
<p style="text-align: center;"><img class="aligncenter size-full wp-image-802" title="Output" src="http://www.bizzymicbizness.com/cplusplus/wp-content/uploads/2009/10/Output6.jpg" alt="Output" width="598" height="275" /></p>
<p><br class="blank" /></p>
<p style="text-align: center;"><img class="aligncenter size-full wp-image-803" title="Output Second" src="http://www.bizzymicbizness.com/cplusplus/wp-content/uploads/2009/10/Output-Second2.jpg" alt="Output Second" width="597" height="275" /></p>
<p><br class="blank" /><br />
Recall that <strong>fill_array()</strong> prescribes that input should quit when the user enters five properties or enters a negative number, whichever comes first</p>
<ul>
<li>The first output example illustrates reaching the five-property limit</li>
<ul>
<li>And the second output example illustrates that entering a negative value terminates the input phase</li>
</ul>
</ul>
<p><br class="blank" /><br />
<br class="blank" /></p>
<h2>Program Notes</h2>
<p><br class="blank" /><br />
We’ve already discussed the important programming details related to the real estate example, so let’s reflect on the process</p>
<ul>
<li>You began by thinking about the data type and designed appropriate functions to handle the data</li>
<ul>
<li>Then, you assembled these functions into a program</li>
<ul>
<li>This is sometimes called bottom-up programming because the design process moves from the component parts to the whole</li>
<ul>
<li>This approach is well suited to OOP, which concentrates on data representation and manipulation first</li>
</ul>
</ul>
</ul>
<li>Traditional procedural programming, on the other hand, leans toward top-down programming</li>
<ul>
<li>In which you develop a modular grand design first and then turn your attention to the details</li>
</ul>
<li>Both methods are useful, and both lead to modular programs</li>
</ul>
<p><br class="blank" /> </p>
]]></content:encoded>
			<wfw:commentRss>http://www.bizzymicbizness.com/cplusplus/more-array-function-examples/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Passing and Returning Structures</title>
		<link>http://www.bizzymicbizness.com/cplusplus/passing-and-returning-structures</link>
		<comments>http://www.bizzymicbizness.com/cplusplus/passing-and-returning-structures#comments</comments>
		<pubDate>Thu, 08 Oct 2009 04:25:44 +0000</pubDate>
		<dc:creator>BMB</dc:creator>
				<category><![CDATA[C++ Glossary]]></category>
		<category><![CDATA[Function]]></category>
		<category><![CDATA[Function Argument Types]]></category>
		<category><![CDATA[Function Return Types]]></category>
		<category><![CDATA[Passing and Returning Structures]]></category>
		<category><![CDATA[struct]]></category>
		<category><![CDATA[Variables]]></category>

		<guid isPermaLink="false">http://www.bizzymicbizness.com/cplusplus/?p=622</guid>
		<description><![CDATA[Passing structures by value makes the most sense when the structure is relatively compact

So let’s look at a couple examples along those lines


The first example deals with travel time (not to be confused with time travel)

Some maps will tell you that

It is 2 hours, 56 minutes, from Zero One City to Binary Falls
And 1 hour, [...]]]></description>
			<content:encoded><![CDATA[<p>Passing structures by value makes the most sense when the structure is relatively compact</p>
<ul>
<li>So let’s look at a couple examples along those lines</li>
</ul>
<p><br class="blank" /><br />
The first example deals with travel time (not to be confused with time travel)</p>
<ul>
<li>Some maps will tell you that
<ul>
<li>It is 2 hours, 56 minutes, from Zero One City to Binary Falls</li>
<li>And 1 hour, 16 minutes, from Binary Falls to Hexadecimal Valley</li>
</ul>
</li>
</ul>
<p><br class="blank" /><br />
You can use a structure to represent such times</p>
<ul>
<li>Using one member for the hour value</li>
<li>And a second member for the minute value</li>
</ul>
<p><br class="blank" /><br />
Adding two times is a little tricky</p>
<ul>
<li>Because you might have to transfer some of the minutes to the hours part
<ul>
<li>For example, the two preceding times sum to 3 hours, 72 minutes, which should be converted to 4 hours, 12 minutes</li>
</ul>
</li>
</ul>
<p><br class="blank" /><br />
Let’s develop a structure to represent a time value</p>
<ul>
<li>And then a function that takes two such structures as arguments
<ul>
<li>And returns a structure that represents their sum</li>
</ul>
</li>
</ul>
<p><br class="blank" /><br />
Defining the structure is simple (A-1):<br />
<br class="blank" /><br />
Example of (A-1)</p>

<div class="syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">struct</span> travel_time
<span style="color: #008000;">&#123;</span>
	<span style="color: #0000ff;">int</span> hours<span style="color: #008080;">;</span>
	<span style="color: #0000ff;">int</span> mins<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span></pre></div></div>

<p><br class="blank" /><br />
Next, consider the prototype for a <strong>sum()</strong> function that returns the sum of two such structures</p>
<ul>
<li>The return value should be type <strong>travel_time</strong>, and so should the two arguments
<ul>
<li>Thus, the prototype should look like this (A-2):</li>
</ul>
</li>
</ul>
<p><br class="blank" /><br />
Example of (A-2)</p>

<div class="syntax"><div class="code"><pre class="cpp" style="font-family:monospace;">travel_time sum<span style="color: #008000;">&#40;</span>travel_time t1, travel_time t2<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></pre></div></div>

<p><br class="blank" /><br />
To add two times, you first add the minute members </p>
<ul>
<li>Integer division by 60 yields the number of hours to carry over</li>
<ul>
<li>And the modulus operation (<strong>%</strong>) yields the number of minutes left</li>
</ul>
</ul>
<p><br class="blank" /><br />
(A-3) incorporates the above approach into the <strong>sum()</strong> function</p>
<ul>
<li>And adds a <strong>show_time()</strong> function
<ul>
<li>To display the contents of a <strong>travel_time</strong> structure</li>
</ul>
</li>
</ul>
<p> <br />
<br class="blank" /><br />
Example of (A-3)</p>

<div class="syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #666666;">// travel.cpp -- using structures with functions</span>
<span style="color: #339900;">#include &lt;iostream&gt;</span>
<span style="color: #0000ff;">struct</span> travel_time
<span style="color: #008000;">&#123;</span>
	<span style="color: #0000ff;">int</span> hours<span style="color: #008080;">;</span>
	<span style="color: #0000ff;">int</span> mins<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
<span style="color: #0000ff;">const</span> <span style="color: #0000ff;">int</span> Mins_per_hr <span style="color: #000080;">=</span> <span style="color: #0000dd;">60</span><span style="color: #008080;">;</span>
&nbsp;
travel_time sum<span style="color: #008000;">&#40;</span>travel_time t1, travel_time t2<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #0000ff;">void</span> show_time<span style="color: #008000;">&#40;</span>travel_time t<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</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;">using</span> <span style="color: #0000ff;">namespace</span> std<span style="color: #008080;">;</span>
	travel_time day1 <span style="color: #000080;">=</span> <span style="color: #008000;">&#123;</span><span style="color: #0000dd;">2</span>, <span style="color: #0000dd;">56</span><span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span> <span style="color: #666666;">// 2 hrs, 56 min</span>
	travel_time day2 <span style="color: #000080;">=</span> <span style="color: #008000;">&#123;</span><span style="color: #0000dd;">1</span>, <span style="color: #0000dd;">16</span><span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span> <span style="color: #666666;">// 1 hrs, 16 min</span>
&nbsp;
	travel_time trip <span style="color: #000080;">=</span> sum<span style="color: #008000;">&#40;</span>day1, day2<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Two-day total: &quot;</span><span style="color: #008080;">;</span>
	show_time<span style="color: #008000;">&#40;</span>trip<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
	travel_time day3<span style="color: #000080;">=</span> <span style="color: #008000;">&#123;</span><span style="color: #0000dd;">5</span>, <span style="color: #0000dd;">12</span><span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Three-day total: &quot;</span><span style="color: #008080;">;</span>
	show_time<span style="color: #008000;">&#40;</span>sum<span style="color: #008000;">&#40;</span>trip, day3<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
	<span style="color: #0000ff;">return</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
travel_time sum<span style="color: #008000;">&#40;</span>travel_time t1, travel_time t2<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
	travel_time total<span style="color: #008080;">;</span>
&nbsp;
	total.<span style="color: #007788;">mins</span> <span style="color: #000080;">=</span> <span style="color: #008000;">&#40;</span>t1.<span style="color: #007788;">mins</span> <span style="color: #000040;">+</span> t2.<span style="color: #007788;">mins</span><span style="color: #008000;">&#41;</span> <span style="color: #000040;">%</span> Mins_per_hr<span style="color: #008080;">;</span>
	total.<span style="color: #007788;">hours</span> <span style="color: #000080;">=</span> t1.<span style="color: #007788;">hours</span> <span style="color: #000040;">+</span> t2.<span style="color: #007788;">hours</span> <span style="color: #000040;">+</span>
		    <span style="color: #008000;">&#40;</span>t1.<span style="color: #007788;">mins</span> <span style="color: #000040;">+</span> t2.<span style="color: #007788;">mins</span><span style="color: #008000;">&#41;</span> <span style="color: #000040;">/</span> Mins_per_hr<span style="color: #008080;">;</span>
	<span style="color: #0000ff;">return</span> total<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0000ff;">void</span> show_time<span style="color: #008000;">&#40;</span>travel_time t<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
	<span style="color: #0000ff;">using</span> <span style="color: #0000ff;">namespace</span> std<span style="color: #008080;">;</span>
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> t.<span style="color: #007788;">hours</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot; hours, &quot;</span>
	     <span style="color: #000080;">&lt;&lt;</span> t.<span style="color: #007788;">mins</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot; minutes<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p><br class="blank" /><br />
Here <strong>travel_time</strong> acts just like a standard type name; you can use it to</p>
<ul>
<li>Declare variables</li>
<li>Function return types</li>
<li>Function argument types</li>
</ul>
<p><br class="blank" /><br />
Because variables such as <strong>total</strong> and <strong>t1</strong> are <strong>travel_time</strong> structures</p>
<ul>
<li>You can apply the dot membership operator to them</li>
</ul>
<p><br class="blank" /><br />
Note that because the <strong>sum()</strong> function returns a <strong>travel_time</strong> structure</p>
<ul>
<li>You can use it as an argument for the <strong>show_time()</strong> function</li>
</ul>
<p><br class="blank" /><br />
Because C++ functions, by default, pass arguments by value</p>
<ul>
<li>The <strong>show_time(sum(trip, day3))</strong> function call</li>
<ul>
<li>First evaluates the <strong>sum(trip, day3)</strong> function call in order to find its return value</li>
</ul>
<li>The <strong>show_time()</strong> call</li>
<ul>
<li>Then passes <strong>sum()</strong>’s return value, not the function itself to <strong>show_time()</strong></li>
</ul>
</ul>
<p><br class="blank" /><br />
Here’s the output of the program in (A-3):</p>
<p style="text-align: center;"><img class="aligncenter size-full wp-image-623" title="Output" src="http://www.bizzymicbizness.com/cplusplus/wp-content/uploads/2009/10/Output1.jpg" alt="Output" width="597" height="274" /></p>
<p><br class="blank" /></p>
]]></content:encoded>
			<wfw:commentRss>http://www.bizzymicbizness.com/cplusplus/passing-and-returning-structures/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>C++ Instructions</title>
		<link>http://www.bizzymicbizness.com/cplusplus/c-instructions</link>
		<comments>http://www.bizzymicbizness.com/cplusplus/c-instructions#comments</comments>
		<pubDate>Wed, 26 Aug 2009 14:15:53 +0000</pubDate>
		<dc:creator>BMB</dc:creator>
				<category><![CDATA[Introduction to C++]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[Function]]></category>

		<guid isPermaLink="false">http://www.bizzymicbizness.com/cplusplus/?p=77</guid>
		<description><![CDATA[C++ works by giving (separate) instructions to the computer

Instructions can be treated as assignments


Assignment will be…

Called a function


The primary function used in C++

Is called main


Function&#8217;s name is

Followed by an opening and a closing parentheses


Main function will always be written

At least as main()


When a program is written and you ask the computer to &#8220;execute&#8221; it, the [...]]]></description>
			<content:encoded><![CDATA[<p>C++ works by giving (separate) instructions to the computer</p>
<ul>
<li>Instructions can be treated as assignments</li>
</ul>
<p><br class="blank" /><br />
Assignment will be…</p>
<ul>
<li>Called a function</li>
</ul>
<p><br class="blank" /><br />
The primary function used in C++</p>
<ul>
<li>Is called <strong>main</strong></li>
</ul>
<p><br class="blank" /><br />
Function&#8217;s name is</p>
<ul>
<li>Followed by an opening and a closing parentheses</li>
</ul>
<p><br class="blank" /><br />
Main function will always be written</p>
<ul>
<li>At least as <strong>main()</strong></li>
</ul>
<p><br class="blank" /><br />
When a program is written and you ask the computer to &#8220;execute&#8221; it, the first thing to look for</p>
<ul>
<li>Is the <strong>main()</strong> function</li>
<ul>
<li>This means that every C++ program should have the <strong>main()</strong> function</li>
</ul>
</ul>
<p><br class="blank" /><br />
Function has</p>
<ul>
<li>A body; this is where the behavior (assignment) of the function would be &#8220;described&#8221;</li>
</ul>
<p> <br />
<br class="blank" /><br />
The body of a function</p>
<ul>
<li>Starts with an opening curly bracket &#8220;<strong>{</strong>&#8221; and closes with a closing curly bracket &#8220;<strong>}</strong>&#8220;</li>
<ul>
<li>Everything in the curly brackets belongs to, or is part of, the function</li>
</ul>
</ul>
<p><br class="blank" /><br />
The <strong>main()</strong> function can be written as (A-1):<br />
<br class="blank" /><br />
Example of (A-1)</p>
<ul>

<div class="syntax"><div class="code"><pre class="cpp" style="font-family:monospace;">main<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#123;</span><span style="color: #008000;">&#125;</span></pre></div></div>

</ul>
<p><br class="blank" /><br />
Whenever you create a program</p>
<ul>
<li>It is important to isolate any inclusion of a library on its own line (A-2)</li>
</ul>
<p><br class="blank" /><br />
Example of (A-2)</p>
<ul>

<div class="syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #339900;">#include &lt;iostream&gt;</span>
<span style="color: #0000ff;">using</span> <span style="color: #0000ff;">namespace</span> std<span style="color: #008080;">;</span>
main<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#123;</span><span style="color: #008000;">&#125;</span></pre></div></div>

</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.bizzymicbizness.com/cplusplus/c-instructions/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

