tag:blogger.com,1999:blog-60287719966396354672024-03-13T15:02:38.630-07:00C++ LearningAdminhttp://www.blogger.com/profile/02962796630033361160noreply@blogger.comBlogger34125tag:blogger.com,1999:blog-6028771996639635467.post-49110404948003340932010-12-11T02:14:00.000-08:002010-12-11T02:14:19.804-08:00Arrays as parameters<span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;">At some moment we may need to pass an array to a function as a parameter. In C++ it is not possible to pass a complete block of memory by value as a parameter to a function, but we are allowed to pass its address. In practice this has almost the same effect and it is a much faster and more efficient operation.</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><br />
</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><br />
</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;">In order to accept arrays as parameters the only thing that we have to do when declaring the function is to specify in its parameters the element type of the array, an identifier and a pair of void brackets</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"> </span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><tt>[]</tt></span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;">. For example, the following function:</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"> </span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><br />
</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><br />
</span><br />
<div class="auto" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"><table class="snippet" style="color: black; font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><tbody>
<tr><td class="rownum" style="color: #a0a0a0; min-width: 20px; text-align: right; vertical-align: top;"><tt> </tt></td><td class="source" style="background-attachment: initial; background-clip: initial; background-color: #efefff; background-image: initial; background-origin: initial; background-position: initial initial; background-repeat: initial initial; border-bottom-color: rgb(192, 192, 208); border-bottom-style: solid; border-bottom-width: 1px; border-left-color: rgb(192, 192, 208); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(192, 192, 208); border-right-style: solid; border-right-width: 1px; border-top-color: rgb(192, 192, 208); border-top-style: solid; border-top-width: 1px; vertical-align: top;"><pre style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"><code><var style="color: #0000b0; font-style: normal;">void</var> procedure (<var style="color: #0000b0; font-style: normal;">int</var> arg[])</code></pre></td></tr>
</tbody></table></div><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><br />
</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><br />
</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;">accepts a parameter of type "array of</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"> </span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><tt>int</tt></span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;">" called</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"> </span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><tt>arg</tt></span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;">. In order to pass to this function an array declared as:</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><br />
</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><br />
</span><div class="auto" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"><table class="snippet" style="color: black; font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><tbody>
<tr><td class="rownum" style="color: #a0a0a0; min-width: 20px; text-align: right; vertical-align: top;"><tt> </tt></td><td class="source" style="background-attachment: initial; background-clip: initial; background-color: #efefff; background-image: initial; background-origin: initial; background-position: initial initial; background-repeat: initial initial; border-bottom-color: rgb(192, 192, 208); border-bottom-style: solid; border-bottom-width: 1px; border-left-color: rgb(192, 192, 208); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(192, 192, 208); border-right-style: solid; border-right-width: 1px; border-top-color: rgb(192, 192, 208); border-top-style: solid; border-top-width: 1px; vertical-align: top;"><pre style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"><code><var style="color: #0000b0; font-style: normal;">int</var> myarray [40];</code></pre></td></tr>
</tbody></table></div><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><br />
</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><br />
</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;">it would be enough to write a call like this:</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><br />
</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><br />
</span><div class="auto" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"><table class="snippet" style="color: black; font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><tbody>
<tr><td class="rownum" style="color: #a0a0a0; min-width: 20px; text-align: right; vertical-align: top;"><tt> </tt></td><td class="source" style="background-attachment: initial; background-clip: initial; background-color: #efefff; background-image: initial; background-origin: initial; background-position: initial initial; background-repeat: initial initial; border-bottom-color: rgb(192, 192, 208); border-bottom-style: solid; border-bottom-width: 1px; border-left-color: rgb(192, 192, 208); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(192, 192, 208); border-right-style: solid; border-right-width: 1px; border-top-color: rgb(192, 192, 208); border-top-style: solid; border-top-width: 1px; vertical-align: top;"><pre style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"><code>procedure (myarray);</code></pre></td></tr>
</tbody></table></div><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><br />
</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><br />
</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;">Here you have a complete example:</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"> </span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><br />
</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><br />
</span><div class="auto" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"><table class="split" style="color: black; font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><tbody>
<tr><td class="rownum" style="color: #a0a0a0; min-width: 20px; text-align: right; vertical-align: top;"><tt><br />
</tt></td><td class="source" style="background-attachment: initial; background-clip: initial; background-color: #efefff; background-image: initial; background-origin: initial; background-position: initial initial; background-repeat: initial initial; border-bottom-color: rgb(192, 192, 208); border-bottom-style: solid; border-bottom-width: 1px; border-left-color: rgb(192, 192, 208); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(192, 192, 208); border-right-style: solid; border-right-width: 1px; border-top-color: rgb(192, 192, 208); border-top-style: solid; border-top-width: 1px; vertical-align: top; width: 568px;"><pre style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"><code><cite style="color: #007000; font-style: normal;">// arrays as parameters</cite>
<dfn style="color: #500070; font-style: normal;">#include <iostream></dfn>
<var style="color: #0000b0; font-style: normal;">using</var> <var style="color: #0000b0; font-style: normal;">namespace</var> std;
<var style="color: #0000b0; font-style: normal;">void</var> printarray (<var style="color: #0000b0; font-style: normal;">int</var> arg[], <var style="color: #0000b0; font-style: normal;">int</var> length) {
<var style="color: #0000b0; font-style: normal;">for</var> (<var style="color: #0000b0; font-style: normal;">int</var> n=0; n<length; n++)
cout << arg[n] << <kbd style="color: #600030; font-style: normal;">" "</kbd>;
cout << <kbd style="color: #600030; font-style: normal;">"\n"</kbd>;
}
<var style="color: #0000b0; font-style: normal;">int</var> main ()
{
<var style="color: #0000b0; font-style: normal;">int</var> firstarray[] = {5, 10, 15};
<var style="color: #0000b0; font-style: normal;">int</var> secondarray[] = {2, 4, 6, 8, 10};
printarray (firstarray,3);
printarray (secondarray,5);
<var style="color: #0000b0; font-style: normal;">return</var> 0;
}</code></pre></td><td class="output" style="background-attachment: initial; background-clip: initial; background-color: #e7e7e7; background-image: initial; background-origin: initial; background-position: initial initial; background-repeat: initial initial; border-bottom-color: rgb(192, 192, 192); border-bottom-style: solid; border-bottom-width: 1px; border-left-color: rgb(192, 192, 192); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(192, 192, 192); border-right-style: solid; border-right-width: 1px; border-top-color: rgb(192, 192, 192); border-top-style: solid; border-top-width: 1px; vertical-align: top; width: 563px;"><pre style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"><samp>5 10 15
2 4 6 8 10</samp></pre></td></tr>
</tbody></table></div><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><br />
</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><br />
</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;">As you can see, the first parameter (</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><tt>int arg[]</tt></span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;">) accepts any array whose elements are of type</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"> </span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><tt>int</tt></span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;">, whatever its length. For that reason we have included a second parameter that tells the function the length of each array that we pass to it as its first parameter. This allows the</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"> </span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><tt>for</tt></span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"> </span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;">loop that prints out the array to know the range to iterate in the passed array without going out of range.</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><br />
</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><br />
</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;">In a function declaration it is also possible to include multidimensional arrays. The format for a tridimensional array parameter is:</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><br />
</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><br />
</span><div class="auto" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"><table class="snippet" style="color: black; font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><tbody>
<tr><td class="rownum" style="color: #a0a0a0; min-width: 20px; text-align: right; vertical-align: top;"><tt> </tt></td><td class="source" style="background-attachment: initial; background-clip: initial; background-color: #efefff; background-image: initial; background-origin: initial; background-position: initial initial; background-repeat: initial initial; border-bottom-color: rgb(192, 192, 208); border-bottom-style: solid; border-bottom-width: 1px; border-left-color: rgb(192, 192, 208); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(192, 192, 208); border-right-style: solid; border-right-width: 1px; border-top-color: rgb(192, 192, 208); border-top-style: solid; border-top-width: 1px; vertical-align: top;"><pre style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"><code>base_type[][depth][depth]</code></pre></td></tr>
</tbody></table></div><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><br />
</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><br />
</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;">for example, a function with a multidimensional array as argument could be:</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"> </span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><br />
</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><br />
</span><div class="auto" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"><table class="snippet" style="color: black; font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><tbody>
<tr><td class="rownum" style="color: #a0a0a0; min-width: 20px; text-align: right; vertical-align: top;"><tt> </tt></td><td class="source" style="background-attachment: initial; background-clip: initial; background-color: #efefff; background-image: initial; background-origin: initial; background-position: initial initial; background-repeat: initial initial; border-bottom-color: rgb(192, 192, 208); border-bottom-style: solid; border-bottom-width: 1px; border-left-color: rgb(192, 192, 208); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(192, 192, 208); border-right-style: solid; border-right-width: 1px; border-top-color: rgb(192, 192, 208); border-top-style: solid; border-top-width: 1px; vertical-align: top;"><pre style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"><code><var style="color: #0000b0; font-style: normal;">void</var> procedure (<var style="color: #0000b0; font-style: normal;">int</var> myarray[][3][4])</code></pre></td></tr>
</tbody></table></div><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><br />
</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><br />
</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;">Notice that the first brackets</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"> </span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><tt>[]</tt></span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"> </span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;">are left blank while the following ones are not. This is so because the compiler must be able to determine within the function which is the depth of each additional dimension.</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><br />
</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><br />
</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;">Arrays, both simple or multidimensional, passed as function parameters are a quite common source of errors for novice programmers. I recommend the reading of the chapter about Pointers for a better understanding on how arrays operate.</span>Adminhttp://www.blogger.com/profile/02962796630033361160noreply@blogger.com0tag:blogger.com,1999:blog-6028771996639635467.post-87666116270625623222010-12-11T02:13:00.001-08:002010-12-11T02:13:13.609-08:00Multidimensional arrays<span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;">Multidimensional arrays can be described as "arrays of arrays". For example, a bidimensional array can be imagined as a bidimensional table made of elements, all of them of a same uniform data type.</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><br />
</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><br />
</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><img alt="" src="http://www.cplusplus.com/doc/tutorial/arrays/bidimensional_arrays1.gif" style="border-bottom-style: none; border-color: initial; border-left-style: none; border-right-style: none; border-top-style: none; border-width: initial;" /></span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><br />
</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><br />
</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><tt>jimmy</tt></span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"> </span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;">represents a bidimensional array of 3 per 5 elements of type</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"> </span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><tt>int</tt></span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;">. The way to declare this array in C++ would be:</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><br />
</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><br />
</span><br />
<div class="auto" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"><table class="snippet" style="color: black; font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><tbody>
<tr><td class="rownum" style="color: #a0a0a0; min-width: 20px; text-align: right; vertical-align: top;"><tt> </tt></td><td class="source" style="background-attachment: initial; background-clip: initial; background-color: #efefff; background-image: initial; background-origin: initial; background-position: initial initial; background-repeat: initial initial; border-bottom-color: rgb(192, 192, 208); border-bottom-style: solid; border-bottom-width: 1px; border-left-color: rgb(192, 192, 208); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(192, 192, 208); border-right-style: solid; border-right-width: 1px; border-top-color: rgb(192, 192, 208); border-top-style: solid; border-top-width: 1px; vertical-align: top;"><pre style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"><code><var style="color: #0000b0; font-style: normal;">int</var> jimmy [3][5];</code></pre></td></tr>
</tbody></table></div><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><br />
</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><br />
</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;">and, for example, the way to reference the second element vertically and fourth horizontally in an expression would be:</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"> </span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><br />
</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><br />
</span><div class="auto" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"><table class="snippet" style="color: black; font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><tbody>
<tr><td class="rownum" style="color: #a0a0a0; min-width: 20px; text-align: right; vertical-align: top;"><tt> </tt></td><td class="source" style="background-attachment: initial; background-clip: initial; background-color: #efefff; background-image: initial; background-origin: initial; background-position: initial initial; background-repeat: initial initial; border-bottom-color: rgb(192, 192, 208); border-bottom-style: solid; border-bottom-width: 1px; border-left-color: rgb(192, 192, 208); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(192, 192, 208); border-right-style: solid; border-right-width: 1px; border-top-color: rgb(192, 192, 208); border-top-style: solid; border-top-width: 1px; vertical-align: top;"><pre style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"><code>jimmy[1][3]</code></pre></td></tr>
</tbody></table></div><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><br />
</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><br />
</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><img alt="" src="http://www.cplusplus.com/doc/tutorial/arrays/bidimensional_arrays2.gif" style="border-bottom-style: none; border-color: initial; border-left-style: none; border-right-style: none; border-top-style: none; border-width: initial;" /></span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><br />
</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><br />
</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;">(remember that array indices always begin by zero).</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><br />
</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><br />
</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;">Multidimensional arrays are not limited to two indices (i.e., two dimensions). They can contain as many indices as needed. But be careful! The amount of memory needed for an array rapidly increases with each dimension. For example:</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><br />
</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><br />
</span><div class="auto" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"><table class="snippet" style="color: black; font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><tbody>
<tr><td class="rownum" style="color: #a0a0a0; min-width: 20px; text-align: right; vertical-align: top;"><tt> </tt></td><td class="source" style="background-attachment: initial; background-clip: initial; background-color: #efefff; background-image: initial; background-origin: initial; background-position: initial initial; background-repeat: initial initial; border-bottom-color: rgb(192, 192, 208); border-bottom-style: solid; border-bottom-width: 1px; border-left-color: rgb(192, 192, 208); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(192, 192, 208); border-right-style: solid; border-right-width: 1px; border-top-color: rgb(192, 192, 208); border-top-style: solid; border-top-width: 1px; vertical-align: top;"><pre style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"><code><var style="color: #0000b0; font-style: normal;">char</var> century [100][365][24][60][60];</code></pre></td></tr>
</tbody></table></div><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><br />
</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><br />
</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;">declares an array with a</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"> </span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><tt>char</tt></span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"> </span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;">element for each second in a century, that is more than 3 billion chars. So this declaration would consume more than 3 gigabytes of memory!</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><br />
</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><br />
</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;">Multidimensional arrays are just an abstraction for programmers, since we can obtain the same results with a simple array just by putting a factor between its indices:</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><br />
</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><br />
</span><div class="auto" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"><table class="snippet" style="color: black; font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><tbody>
<tr><td class="rownum" style="color: #a0a0a0; min-width: 20px; text-align: right; vertical-align: top;"><tt>1<br />
2</tt></td><td class="source" style="background-attachment: initial; background-clip: initial; background-color: #efefff; background-image: initial; background-origin: initial; background-position: initial initial; background-repeat: initial initial; border-bottom-color: rgb(192, 192, 208); border-bottom-style: solid; border-bottom-width: 1px; border-left-color: rgb(192, 192, 208); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(192, 192, 208); border-right-style: solid; border-right-width: 1px; border-top-color: rgb(192, 192, 208); border-top-style: solid; border-top-width: 1px; vertical-align: top;"><pre style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"><code><var style="color: #0000b0; font-style: normal;">int</var> jimmy [3][5]; <cite style="color: #007000; font-style: normal;">// is equivalent to</cite>
<var style="color: #0000b0; font-style: normal;">int</var> jimmy [15]; <cite style="color: #007000; font-style: normal;">// (3 * 5 = 15) </cite></code></pre></td></tr>
</tbody></table></div><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><br />
</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><br />
</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;">With the only difference that with multidimensional arrays the compiler remembers the depth of each imaginary dimension for us. Take as example these two pieces of code, with both exactly the same result. One uses a bidimensional array and the other one uses a simple array:</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"> </span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><br />
</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><br />
</span><table class="boxed" style="border-collapse: collapse; color: black; font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><tbody>
<tr><th style="background-attachment: initial; background-clip: initial; background-color: #e0e0e0; background-image: initial; background-origin: initial; background-position: initial initial; background-repeat: initial initial; border-bottom-color: black; border-bottom-style: solid; border-bottom-width: 1px; border-left-color: black; border-left-style: solid; border-left-width: 1px; border-right-color: black; border-right-style: solid; border-right-width: 1px; border-top-color: black; border-top-style: solid; border-top-width: 1px;">multidimensional array</th><th style="background-attachment: initial; background-clip: initial; background-color: #e0e0e0; background-image: initial; background-origin: initial; background-position: initial initial; background-repeat: initial initial; border-bottom-color: black; border-bottom-style: solid; border-bottom-width: 1px; border-left-color: black; border-left-style: solid; border-left-width: 1px; border-right-color: black; border-right-style: solid; border-right-width: 1px; border-top-color: black; border-top-style: solid; border-top-width: 1px;">pseudo-multidimensional array</th></tr>
<tr><td style="border-bottom-color: black; border-bottom-style: solid; border-bottom-width: 1px; border-left-color: black; border-left-style: solid; border-left-width: 1px; border-right-color: black; border-right-style: solid; border-right-width: 1px; border-top-color: black; border-top-style: solid; border-top-width: 1px;"><pre style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;">#define WIDTH 5
#define HEIGHT 3
int jimmy [HEIGHT][WIDTH];
int n,m;
int main ()
{
for (n=0;n<HEIGHT;n++)
for (m=0;m<WIDTH;m++)
{
jimmy[n][m]=(n+1)*(m+1);
}
return 0;
}
</pre></td><td style="border-bottom-color: black; border-bottom-style: solid; border-bottom-width: 1px; border-left-color: black; border-left-style: solid; border-left-width: 1px; border-right-color: black; border-right-style: solid; border-right-width: 1px; border-top-color: black; border-top-style: solid; border-top-width: 1px;"><pre style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;">#define WIDTH 5
#define HEIGHT 3
int jimmy [HEIGHT * WIDTH];
int n,m;
int main ()
{
for (n=0;n<HEIGHT;n++)
for (m=0;m<WIDTH;m++)
{
jimmy[n*WIDTH+m]=(n+1)*(m+1);
}
return 0;
}
</pre></td></tr>
</tbody></table><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><br />
</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><br />
</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;">None of the two source codes above produce any output on the screen, but both assign values to the memory block called jimmy in the following way:</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"> </span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><br />
</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><br />
</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><img alt="" src="http://www.cplusplus.com/doc/tutorial/arrays/bidimensional_arrays3.gif" style="border-bottom-style: none; border-color: initial; border-left-style: none; border-right-style: none; border-top-style: none; border-width: initial;" /></span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><br />
</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><br />
</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;">We have used "defined constants" (</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><tt>#define</tt></span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;">) to simplify possible future modifications of the program. For example, in case that we decided to enlarge the array to a height of 4 instead of 3 it could be done simply by changing the line:</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><br />
</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><br />
</span><div class="auto" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"><table class="snippet" style="color: black; font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><tbody>
<tr><td class="rownum" style="color: #a0a0a0; min-width: 20px; text-align: right; vertical-align: top;"><tt> </tt></td><td class="source" style="background-attachment: initial; background-clip: initial; background-color: #efefff; background-image: initial; background-origin: initial; background-position: initial initial; background-repeat: initial initial; border-bottom-color: rgb(192, 192, 208); border-bottom-style: solid; border-bottom-width: 1px; border-left-color: rgb(192, 192, 208); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(192, 192, 208); border-right-style: solid; border-right-width: 1px; border-top-color: rgb(192, 192, 208); border-top-style: solid; border-top-width: 1px; vertical-align: top;"><pre style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"><code><dfn style="color: #500070; font-style: normal;">#define HEIGHT 3 </dfn></code></pre></td></tr>
</tbody></table></div><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><br />
</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;">to:</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><br />
</span><div class="auto" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"><table class="snippet" style="color: black; font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><tbody>
<tr><td class="rownum" style="color: #a0a0a0; min-width: 20px; text-align: right; vertical-align: top;"><tt> </tt></td><td class="source" style="background-attachment: initial; background-clip: initial; background-color: #efefff; background-image: initial; background-origin: initial; background-position: initial initial; background-repeat: initial initial; border-bottom-color: rgb(192, 192, 208); border-bottom-style: solid; border-bottom-width: 1px; border-left-color: rgb(192, 192, 208); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(192, 192, 208); border-right-style: solid; border-right-width: 1px; border-top-color: rgb(192, 192, 208); border-top-style: solid; border-top-width: 1px; vertical-align: top;"><pre style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"><code><dfn style="color: #500070; font-style: normal;">#define HEIGHT 4 </dfn></code></pre></td></tr>
</tbody></table></div><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><br />
</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><br />
</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;">with no need to make any other modifications to the program.</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"> </span>Adminhttp://www.blogger.com/profile/02962796630033361160noreply@blogger.com0tag:blogger.com,1999:blog-6028771996639635467.post-91948019984837273612010-12-11T02:12:00.001-08:002010-12-11T02:12:31.329-08:00Accessing the values of an array.<span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;">In any point of a program in which an array is visible, we can access the value of any of its elements individually as if it was a normal variable, thus being able to both read and modify its value. The format is as simple as:</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><br />
</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><br />
</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><tt><br />
name[index]</tt></span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><br />
</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><br />
</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;">Following the previous examples in which</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"> </span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><tt>billy</tt></span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"> </span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;">had 5 elements and each of those elements was of type</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"> </span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><tt>int</tt></span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;">, the name which we can use to refer to each element is the following:</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><br />
</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><br />
</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><img alt="" src="http://www.cplusplus.com/doc/tutorial/arrays/arrays3.gif" style="border-bottom-style: none; border-color: initial; border-left-style: none; border-right-style: none; border-top-style: none; border-width: initial;" /></span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><br />
</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><br />
</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;">For example, to store the value</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"> </span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><tt>75</tt></span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"> </span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;">in the third element of</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"> </span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><tt>billy</tt></span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;">, we could write the following statement:</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><br />
</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><br />
</span><br />
<div class="auto" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"><table class="snippet" style="color: black; font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><tbody>
<tr><td class="rownum" style="color: #a0a0a0; min-width: 20px; text-align: right; vertical-align: top;"><tt> </tt></td><td class="source" style="background-attachment: initial; background-clip: initial; background-color: #efefff; background-image: initial; background-origin: initial; background-position: initial initial; background-repeat: initial initial; border-bottom-color: rgb(192, 192, 208); border-bottom-style: solid; border-bottom-width: 1px; border-left-color: rgb(192, 192, 208); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(192, 192, 208); border-right-style: solid; border-right-width: 1px; border-top-color: rgb(192, 192, 208); border-top-style: solid; border-top-width: 1px; vertical-align: top;"><pre style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"><code>billy[2] = 75;</code></pre></td></tr>
</tbody></table></div><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><br />
</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><br />
</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;">and, for example, to pass the value of the third element of</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"> </span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><tt>billy</tt></span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"> </span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;">to a variable called</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"> </span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><tt>a</tt></span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;">, we could write:</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><br />
</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><br />
</span><div class="auto" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"><table class="snippet" style="color: black; font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><tbody>
<tr><td class="rownum" style="color: #a0a0a0; min-width: 20px; text-align: right; vertical-align: top;"><tt> </tt></td><td class="source" style="background-attachment: initial; background-clip: initial; background-color: #efefff; background-image: initial; background-origin: initial; background-position: initial initial; background-repeat: initial initial; border-bottom-color: rgb(192, 192, 208); border-bottom-style: solid; border-bottom-width: 1px; border-left-color: rgb(192, 192, 208); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(192, 192, 208); border-right-style: solid; border-right-width: 1px; border-top-color: rgb(192, 192, 208); border-top-style: solid; border-top-width: 1px; vertical-align: top;"><pre style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"><code>a = billy[2];</code></pre></td></tr>
</tbody></table></div><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><br />
</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><br />
</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;">Therefore, the expression</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"> </span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><tt>billy[2]</tt></span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"> </span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;">is for all purposes like a variable of type</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"> </span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><tt>int</tt></span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;">.</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><br />
</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><br />
</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;">Notice that the third element of</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"> </span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><tt>billy</tt></span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"> </span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;">is specified</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"> </span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><tt>billy[2]</tt></span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;">, since the first one is</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"> </span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><tt>billy[0]</tt></span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;">, the second one is</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"> </span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><tt>billy[1]</tt></span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;">, and therefore, the third one is</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"> </span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><tt>billy[2]</tt></span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;">. By this same reason, its last element is</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"> </span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><tt>billy[4]</tt></span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;">. Therefore, if we write billy[5], we would be accessing the sixth element of</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"> </span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><tt>billy</tt></span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"> </span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;">and therefore exceeding the size of the array.</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><br />
</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><br />
</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;">In C++ it is syntactically correct to exceed the valid range of indices for an array. This can create problems, since accessing out-of-range elements do not cause compilation errors but can cause runtime errors. The reason why this is allowed will be seen further ahead when we begin to use pointers.</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><br />
</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><br />
</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;">At this point it is important to be able to clearly distinguish between the two uses that brackets</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"> </span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><tt>[ ]</tt></span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"> </span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;">have related to arrays. They perform two different tasks: one is to specify the size of arrays when they are declared; and the second one is to specify indices for concrete array elements. Do not confuse these two possible uses of brackets</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"> </span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><tt>[ ]</tt></span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"> </span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;">with arrays.</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><br />
</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><br />
</span><div class="auto" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"><table class="snippet" style="color: black; font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><tbody>
<tr><td class="rownum" style="color: #a0a0a0; min-width: 20px; text-align: right; vertical-align: top;"><tt>1<br />
2</tt></td><td class="source" style="background-attachment: initial; background-clip: initial; background-color: #efefff; background-image: initial; background-origin: initial; background-position: initial initial; background-repeat: initial initial; border-bottom-color: rgb(192, 192, 208); border-bottom-style: solid; border-bottom-width: 1px; border-left-color: rgb(192, 192, 208); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(192, 192, 208); border-right-style: solid; border-right-width: 1px; border-top-color: rgb(192, 192, 208); border-top-style: solid; border-top-width: 1px; vertical-align: top;"><pre style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"><code><var style="color: #0000b0; font-style: normal;">int</var> billy[5]; <cite style="color: #007000; font-style: normal;">// declaration of a new array</cite>
billy[2] = 75; <cite style="color: #007000; font-style: normal;">// access to an element of the array. </cite></code></pre></td></tr>
</tbody></table></div><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><br />
</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><br />
</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;">If you read carefully, you will see that a type specifier always precedes a variable or array declaration, while it never precedes an access.</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><br />
</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><br />
</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;">Some other valid operations with arrays:</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><br />
</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><br />
</span><div class="auto" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"><table class="snippet" style="color: black; font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><tbody>
<tr><td class="rownum" style="color: #a0a0a0; min-width: 20px; text-align: right; vertical-align: top;"><tt>1<br />
2<br />
3<br />
4</tt></td><td class="source" style="background-attachment: initial; background-clip: initial; background-color: #efefff; background-image: initial; background-origin: initial; background-position: initial initial; background-repeat: initial initial; border-bottom-color: rgb(192, 192, 208); border-bottom-style: solid; border-bottom-width: 1px; border-left-color: rgb(192, 192, 208); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(192, 192, 208); border-right-style: solid; border-right-width: 1px; border-top-color: rgb(192, 192, 208); border-top-style: solid; border-top-width: 1px; vertical-align: top;"><pre style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"><code>billy[0] = a;
billy[a] = 75;
b = billy [a+2];
billy[billy[a]] = billy[2] + 5;</code></pre></td></tr>
</tbody></table></div><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><br />
</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><br />
</span><div class="auto" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"><table class="split" style="color: black; font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><tbody>
<tr><td class="rownum" style="color: #a0a0a0; min-width: 20px; text-align: right; vertical-align: top;"><tt><br />
<br />
</tt></td><td class="source" style="background-attachment: initial; background-clip: initial; background-color: #efefff; background-image: initial; background-origin: initial; background-position: initial initial; background-repeat: initial initial; border-bottom-color: rgb(192, 192, 208); border-bottom-style: solid; border-bottom-width: 1px; border-left-color: rgb(192, 192, 208); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(192, 192, 208); border-right-style: solid; border-right-width: 1px; border-top-color: rgb(192, 192, 208); border-top-style: solid; border-top-width: 1px; vertical-align: top; width: 568px;"><pre style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"><code><cite style="color: #007000; font-style: normal;">// arrays example</cite>
<dfn style="color: #500070; font-style: normal;">#include <iostream></dfn>
<var style="color: #0000b0; font-style: normal;">using</var> <var style="color: #0000b0; font-style: normal;">namespace</var> std;
<var style="color: #0000b0; font-style: normal;">int</var> billy [] = {16, 2, 77, 40, 12071};
<var style="color: #0000b0; font-style: normal;">int</var> n, result=0;
<var style="color: #0000b0; font-style: normal;">int</var> main ()
{
<var style="color: #0000b0; font-style: normal;">for</var> ( n=0 ; n<5 ; n++ )
{
result += billy[n];
}
cout << result;
<var style="color: #0000b0; font-style: normal;">return</var> 0;
}</code></pre></td><td class="output" style="background-attachment: initial; background-clip: initial; background-color: #e7e7e7; background-image: initial; background-origin: initial; background-position: initial initial; background-repeat: initial initial; border-bottom-color: rgb(192, 192, 192); border-bottom-style: solid; border-bottom-width: 1px; border-left-color: rgb(192, 192, 192); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(192, 192, 192); border-right-style: solid; border-right-width: 1px; border-top-color: rgb(192, 192, 192); border-top-style: solid; border-top-width: 1px; vertical-align: top; width: 563px;"><pre style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"><samp>12206</samp></pre><div><samp><br />
</samp></div></td></tr>
</tbody></table></div>Adminhttp://www.blogger.com/profile/02962796630033361160noreply@blogger.com0tag:blogger.com,1999:blog-6028771996639635467.post-24703963916238494672010-12-11T02:11:00.002-08:002010-12-11T02:11:50.909-08:00Initializing arrays.<span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;">When declaring a regular array of local scope (within a function, for example), if we do not specify otherwise, its elements will not be initialized to any value by default, so their content will be undetermined until we store some value in them. The elements of global and static arrays, on the other hand, are automatically initialized with their default values, which for all fundamental types this means they are filled with zeros.</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><br />
</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><br />
</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;">In both cases, local and global, when we declare an array, we have the possibility to assign initial values to each one of its elements by enclosing the values in braces</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"> </span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><tt>{ }</tt></span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;">. For example:</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><br />
</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><br />
</span><br />
<div class="auto" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"><table class="snippet" style="color: black; font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><tbody>
<tr><td class="rownum" style="color: #a0a0a0; min-width: 20px; text-align: right; vertical-align: top;"><tt> </tt></td><td class="source" style="background-attachment: initial; background-clip: initial; background-color: #efefff; background-image: initial; background-origin: initial; background-position: initial initial; background-repeat: initial initial; border-bottom-color: rgb(192, 192, 208); border-bottom-style: solid; border-bottom-width: 1px; border-left-color: rgb(192, 192, 208); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(192, 192, 208); border-right-style: solid; border-right-width: 1px; border-top-color: rgb(192, 192, 208); border-top-style: solid; border-top-width: 1px; vertical-align: top;"><pre style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"><code><var style="color: #0000b0; font-style: normal;">int</var> billy [5] = { 16, 2, 77, 40, 12071 }; </code></pre></td></tr>
</tbody></table></div><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><br />
</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><br />
</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;">This declaration would have created an array like this:</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><br />
</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><br />
</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><img alt="" src="http://www.cplusplus.com/doc/tutorial/arrays/arrays2.gif" style="border-bottom-style: none; border-color: initial; border-left-style: none; border-right-style: none; border-top-style: none; border-width: initial;" /></span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><br />
</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><br />
</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;">The amount of values between braces</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"> </span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><tt>{ }</tt></span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"> </span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;">must not be larger than the number of elements that we declare for the array between square brackets</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"> </span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><tt>[ ]</tt></span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;">. For example, in the example of array</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"> </span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><tt>billy</tt></span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"> </span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;">we have declared that it has 5 elements and in the list of initial values within braces</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"> </span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><tt>{ }</tt></span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"> </span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;">we have specified 5 values, one for each element.</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><br />
</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><br />
</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;">When an initialization of values is provided for an array, C++ allows the possibility of leaving the square brackets empty</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"> </span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><tt>[ ]</tt></span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;">. In this case, the compiler will assume a size for the array that matches the number of values included between braces</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"> </span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><tt>{ }</tt></span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;">:</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><br />
</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><br />
</span><div class="auto" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"><table class="snippet" style="color: black; font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><tbody>
<tr><td class="rownum" style="color: #a0a0a0; min-width: 20px; text-align: right; vertical-align: top;"><tt> </tt></td><td class="source" style="background-attachment: initial; background-clip: initial; background-color: #efefff; background-image: initial; background-origin: initial; background-position: initial initial; background-repeat: initial initial; border-bottom-color: rgb(192, 192, 208); border-bottom-style: solid; border-bottom-width: 1px; border-left-color: rgb(192, 192, 208); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(192, 192, 208); border-right-style: solid; border-right-width: 1px; border-top-color: rgb(192, 192, 208); border-top-style: solid; border-top-width: 1px; vertical-align: top;"><pre style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"><code><var style="color: #0000b0; font-style: normal;">int</var> billy [] = { 16, 2, 77, 40, 12071 };</code></pre></td></tr>
</tbody></table></div><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><br />
</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><br />
</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;">After this declaration, array</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"> </span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><tt>billy</tt></span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"> </span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;">would be 5 ints long, since we have provided 5 initialization values.</span>Adminhttp://www.blogger.com/profile/02962796630033361160noreply@blogger.com0tag:blogger.com,1999:blog-6028771996639635467.post-51775862330438208032010-12-11T02:11:00.000-08:002010-12-11T02:11:07.944-08:00Arrays<span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;">An array is a series of elements of the same type placed in contiguous memory locations that can be individually referenced by adding an index to a unique identifier.</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><br />
</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><br />
</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;">That means that, for example, we can store 5 values of type</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"> </span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><tt>int</tt></span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"> </span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;">in an array without having to declare 5 different variables, each one with a different identifier. Instead of that, using an array we can store 5 different values of the same type,</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"> </span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><tt>int</tt></span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"> </span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;">for example, with a unique identifier.</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><br />
</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><br />
</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;">For example, an array to contain 5 integer values of type</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"> </span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><tt>int</tt></span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"> </span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;">called</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"> </span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><tt>billy</tt></span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"> </span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;">could be represented like this:</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><br />
</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><br />
</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><img alt="" src="http://www.cplusplus.com/doc/tutorial/arrays/arrays1.gif" style="border-bottom-style: none; border-color: initial; border-left-style: none; border-right-style: none; border-top-style: none; border-width: initial;" /></span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><br />
</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><br />
</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;">where each blank panel represents an element of the array, that in this case are integer values of type</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"> </span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><tt>int</tt></span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;">. These elements are numbered from</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"> </span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><tt>0</tt></span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"> </span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;">to</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"> </span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><tt>4</tt></span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"> </span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;">since in arrays the first index is always</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"> </span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><tt>0</tt></span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;">, independently of its length.</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><br />
</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><br />
</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;">Like a regular variable, an array must be declared before it is used. A typical declaration for an array in C++ is:</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><br />
</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><br />
</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><tt><br />
type name [elements];</tt></span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><br />
</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><br />
</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;">where</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"> </span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><tt>type</tt></span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"> </span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;">is a valid type (like</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"> </span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><tt>int</tt></span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;">,</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"> </span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><tt>float</tt></span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;">...),</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"> </span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><tt>name</tt></span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"> </span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;">is a valid identifier and the</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"> </span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><tt>elements</tt></span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"> </span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;">field (which is always enclosed in square brackets</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"> </span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><tt>[]</tt></span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;">), specifies how many of these elements the array has to contain.</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><br />
</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><br />
</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;">Therefore, in order to declare an array called</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"> </span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><tt>billy</tt></span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"> </span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;">as the one shown in the above diagram it is as simple as:</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><br />
</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><br />
</span><br />
<div class="auto" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"><table class="snippet" style="color: black; font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><tbody>
<tr><td class="rownum" style="color: #a0a0a0; min-width: 20px; text-align: right; vertical-align: top;"><tt> </tt></td><td class="source" style="background-attachment: initial; background-clip: initial; background-color: #efefff; background-image: initial; background-origin: initial; background-position: initial initial; background-repeat: initial initial; border-bottom-color: rgb(192, 192, 208); border-bottom-style: solid; border-bottom-width: 1px; border-left-color: rgb(192, 192, 208); border-left-style: solid; border-left-width: 1px; border-right-color: rgb(192, 192, 208); border-right-style: solid; border-right-width: 1px; border-top-color: rgb(192, 192, 208); border-top-style: solid; border-top-width: 1px; vertical-align: top;"><pre style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"><code><var style="color: #0000b0; font-style: normal;">int</var> billy [5];</code></pre></td></tr>
</tbody></table></div><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><br />
</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><br />
</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><b>NOTE</b></span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;">: The</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"> </span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><tt>elements</tt></span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"> </span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;">field within brackets</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"> </span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><tt>[]</tt></span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"> </span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;">which represents the number of elements the array is going to hold, must be a</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"> </span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><strong>constant</strong></span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"> </span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;">value, since arrays are blocks of non-dynamic memory whose size must be determined before execution. In order to create arrays with a variable length dynamic memory is needed, which is explained later in these tutorials.</span>Adminhttp://www.blogger.com/profile/02962796630033361160noreply@blogger.com0tag:blogger.com,1999:blog-6028771996639635467.post-31756634176928903662010-12-11T02:08:00.000-08:002010-12-11T02:08:31.098-08:00Writing In a File<span class="Apple-style-span" style="color: #222222; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; font-size: 13px; line-height: 18px;">#include<iostream></span><br />
<div style="color: #222222; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; font-size: 13px; line-height: 18px;">#include<fstream> // for file handling</div><div style="color: #222222; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; font-size: 13px; line-height: 18px;">unsing namespace std;</div><div style="color: #222222; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; font-size: 13px; line-height: 18px;">int main()</div><div style="color: #222222; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; font-size: 13px; line-height: 18px;">{</div><div style="color: #222222; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; font-size: 13px; line-height: 18px;"> ofstream out; // declaring a variable</div><div style="color: #222222; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; font-size: 13px; line-height: 18px;"> out.open("file.txt"); // opening the file </div><div style="color: #222222; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; font-size: 13px; line-height: 18px;"> </div><div style="color: #222222; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; font-size: 13px; line-height: 18px;"> </div><div style="color: #222222; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; font-size: 13px; line-height: 18px;"> out.close(); // closes the file</div><div style="color: #222222; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; font-size: 13px; line-height: 18px;"> return 0;</div><div style="color: #222222; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; font-size: 13px; line-height: 18px;">}</div>Adminhttp://www.blogger.com/profile/02962796630033361160noreply@blogger.com0tag:blogger.com,1999:blog-6028771996639635467.post-8722984279680499552010-12-11T02:06:00.000-08:002010-12-11T02:07:18.272-08:00Reading From a File#include<iostream><br />
<div>#include<fstream> // for file handling</div><div>unsing namespace std;</div><div>int main()</div><div>{</div><div> ifstream in; // declaring a variable</div><div> in.open("file.txt"); // opening the file </div><div> if(!in)</div><div> {</div><div> cout<<"No File is Found"<<endl; // if file was not present</div><div> return 0;</div><div> }</div><div> while(in) // this while loop runs till the file is not end</div><div> ............... // this portion includes the work you want to do with the file</div><div> in.close(); // closes the file</div><div> return 0;</div><div>}</div>Adminhttp://www.blogger.com/profile/02962796630033361160noreply@blogger.com0tag:blogger.com,1999:blog-6028771996639635467.post-30970613442756038152010-12-11T01:58:00.001-08:002010-12-11T01:59:35.947-08:00What is File handling? Many real-life problems handle large volumes of data, therefore we need to use some devices such as floppy disk or hard disk to store the data.<br />
<br />
The data is stored in these devices using the concept of files. A file is a collection of related data stored in a particular area on the disk.<br />
<br />
Programs can be designed to perform the read and write operations on these files.<br />
<br />
A program typically involves either or both of the following kinds of data communication:<br />
Data transfer between the console unit and the program.<br />
Data transfer between the program and a disk file.<br />
The input/output system of C++ handles file operations which are very much similar to the console input and output operations.<br />
<br />
It uses file streams as an interface between the programs and the files.<br />
<br />
The stream that supplies data to the program is known as input stream and the one that receives data from the program is known as output stream.<br />
<br />
In other words, the input stream extracts or reads data from the file and the output stream inserts or writes data to the file.Adminhttp://www.blogger.com/profile/02962796630033361160noreply@blogger.com0tag:blogger.com,1999:blog-6028771996639635467.post-89437415976816362942010-12-11T01:54:00.001-08:002010-12-11T01:54:24.993-08:00Object DesignThere are entire books and long college courses that discuss Object design, I will go through the basics. As discussed in the previous section, An object (synonymous with a class in C++) has data that it remembers and actions that it can perform. So once it has been determined how an object fits into a program, it's member data and member functions can be determined. The question becomes how to determine what role within a program an object is will play.<br />
The role that an object plays within a program should be able to be defined in one to three short sentences. If it takes more than this to define (generally) an object's role, then there should be more than one object. For instance, good concise role's for an object would be:<br />
<br />
<ul><li>Manages all requests into a data structure<br />
<center><b>OR</b></center></li>
<li>Arbitrates turns in a multi-user game<br />
<center><b>OR</b></center></li>
<li>does all data type conversions within program<br />
<center><b>OR</b></center></li>
<li>easy interface to reading, writing, and parsing files.</li>
</ul>Drawing pictures that describe the functionality of objects can be a big help when designing a program. Pictures can often describe the relationship between objects better than a paragraph of words. Objects relate to each other in the following ways: ownership (contains), contained-by, knows-about, doesn't-know-about.<br />
Here's a picture of how some of the objects in the text-based medieval video game relate to each other:<br />
<center><img alt="Figure 1" src="http://www.intap.net/~drw/cpp/pictures/pic06_02fig1.gif" /></center>Players and Monsters need to be able to fight each other. Otherwise it wouldn't be a very exciting game. When the Player and a Monster are Battling they battle through a third object, the BattleMgr. The BattleMgr decides which Battler acts first, and eventually, which battler wins. So the Player object and the monster object need to know about the BattleMgr, and the BattleMgr needs to know about both the Player and the Monster.<br />
We have already decided on the attributes of a Battler, so let's add these to the picture.<br />
<center><img alt="Figure 2" src="http://www.intap.net/~drw/cpp/pictures/pic06_02fig2.gif" /></center>Remember our pseudo-code for a battler:<br />
<pre>Player Object:
data:
health
strength
agility
type of weapon
type of armor
actions:
move
attack monster
get treasure
END
</pre>Notice that the actions are not listed in the picture, Just the attributes (data). Weapons and Armor are their own type of objects so players/monsters must know about one of each of these types of objects. The reason that health, strength, and agility are treated differently will become clear later. For now, understand what we are trying to accomplish by drawing a picture: we want to first of all understand the relationships between objects. Secondly we want to talk our way through the program with this picture and make sure that we aren't leaving something out.Let's move to a more complex example. In our game, a player can move through a maze encountering Monsters to battle, and weapons and treasure to pick up.<br />
We still have Players, Monsters, and a BattleMgr. We also have a GameDatabase (GameDB) which keeps track of all the rooms in the maze and what is in them. We also have a RoomManager (RoomMgr) that keeps track of the current room where the player is and interfaces with the GameDB when the player moves to see where the player ends up.<br />
Here is the diagram:<br />
<center><img alt="Figure 3" src="http://www.intap.net/~drw/cpp/pictures/pic06_02fig3.gif" /></center>So let's go through some parts of the game. The player starts the game and decides to go east: so the Player object tells the RoomMgr that the player is going east. The RoomMgr checks that the move is valid and then asks the GameDB for the next room.<br />
We already have a few problems. The RoomMgr needs an instance variable to keep track of the current room. How does the RoomMgr know if "east" is a valid move? Well, each room will have to have 4 instance variables: east, west, north, and south. Their values will be 1 if that is a valid direction and 0 if it is not a valid direction. What about the contents of the room. What can rooms contain? We'll have to create a Treasure object and rooms will have to keep a list of what Treasure they contain and also what Monsters reside in that room.<br />
How will we keep a list of an arbitrary number of items? We will use what is called a linked list. This will be a good example of reusing pieces of code.<br />
So where do we go from here? In the next section, we will try and pseudo-code out our program. We will determine how we want everything to work and then write it down in pseduo-code.<br />
Before going into the next section take a shot at re-drawing the previous picture and figure out how you would organize the objects and what instance variables and member functions they would contain. Your picture will no doubt be different from ours. Not that that means you are right or wrong. One of the interesting things about programming is that there are often many different ways to solve the same problem.Adminhttp://www.blogger.com/profile/02962796630033361160noreply@blogger.com0tag:blogger.com,1999:blog-6028771996639635467.post-91360746414336613012010-12-11T01:53:00.005-08:002010-12-11T01:53:41.876-08:00SummaryObjects are ways of bundling parts of programs into small, manageable pieces. Objects are simply a definition for a type of data to be stored. An instance of an object contains meaningful information, these are manipulated by the program. There can be more than one instance of an object. Instances of objects keep track of information, called member data, or instance variables. This data is kept track of by the instance until it no longer exists. Object instances also know how to perform certain functions, called member functions, or class functions. Every instance of an object performs the same steps when carrying out a member function, although the steps can be influenced by the instances' present member dataAdminhttp://www.blogger.com/profile/02962796630033361160noreply@blogger.com0tag:blogger.com,1999:blog-6028771996639635467.post-80063526794791497532010-12-11T01:53:00.003-08:002010-12-11T01:53:26.358-08:00Objects and InstancesThere is a very important distinction between an <b>object</b> and an <b>instance</b> of an object. An object is actually a definition, or a template for instances of that object. An instance of an object is an actual thing that can be manipulated. For instance, we could define a Person object, which may include such member data as hair color, eye color, height, weight, etc. An instance of this object could be "Dave" and Dave has values for hair color, eye color, etc. This allows for multiple instances of an object to be created. Let's go back to the medieval video game example and define the monster object.<br />
<pre>Monster Object:
data:
health
skin thickness
claws
tail spikes
actions:
move
attack player with claws
attack player with tail
END;
</pre>Now, our game could have one instance of a player:<br />
<pre>Player Instance #1:
data:
health = 16
strength = 12
agility = 14
type of weapon = "mace"
type of armor = "leather"
END;
</pre>and our game could have two instances of monsters:<table><tbody>
<tr><td>a tough one:</td><td>and a weak one:</td></tr>
<tr><td><pre>Monster Instance #1:
data:
health = 21
skin thickness = 20
claws = "sharp"
tail spikes = "razor sharp"
END;
</pre></td><td><pre>Monster Instance#2:
data:
health = 9
skin thickness = 5
claws = "dull"
tail spikes = "quite dull"
END;
</pre></td></tr>
</tbody></table>Notice how an instance of an object contains information on member data, but holds nothing about member functions. Every instance of the Monster object performs "attack player" the same way. There is a series of steps in this member function. But each instance of the monster has its own value for the member data. In the preceding example, we can tell the two monsters in our game apart, because of their member data. One monster is tough and the other monster is weak.Let's say that we had a "Battle" function in our game. The pseudocode for it may go something like the following:<br />
<pre>Function Battle(parameters: _player = the Player Object instance
_monster = the Monster Object instance)
turn = PLAYER;
while ((_player's health > 0) AND (_monster's health > 0)) {
if (turn == PLAYER){
player attack's monster;
turn = MONSTER;
}
else {
monster attack's player
turn = PLAYER;
}
}
} // END FUNCTION Battle
</pre>In the "attack" phase, the attacking person would somehow deduct points from the defending person. Let's say that the player was fighting with the weaker monster. The weaker monster's health value is 9. If the player attacked the monster and did 5 points of damage to the monster, the monster's new health value would be 4. The monster keeps this value as its health value until it is undated again. So if the monster ran away at this point, and later in the game, the player discovered the weaker monster again, it's health value would still be 4.Adminhttp://www.blogger.com/profile/02962796630033361160noreply@blogger.com0tag:blogger.com,1999:blog-6028771996639635467.post-48874866576477375842010-12-11T01:53:00.001-08:002010-12-11T01:53:06.847-08:00Member Data and Member FunctionsData that an object keeps track of is called member data and actions that an object knows how to do are called member functions. Member data is very similar to variables in a regular function in the sense that no other object can get access to that data (unless given permission by the object). Member data keeps its values over the life of an object.Adminhttp://www.blogger.com/profile/02962796630033361160noreply@blogger.com0tag:blogger.com,1999:blog-6028771996639635467.post-21386657843449450692010-12-11T01:52:00.001-08:002010-12-11T01:52:50.039-08:00What is an Object?<h3>Objects defined</h3><div>So what is an object? An object is a component of a program that knows how to perform certain actions and to interact with other pieces of the program. Functions have previously been described as "black boxes" that take an input and spit out an output. Objects can be thought of as "smart" black boxes. That is, objects can know how to do more than one specific task, and they can store their own set of data. Designing a program with objects allows a programmer to model the program after the real world. A program can be broken down into specific parts, and each of these parts can perform fairly simple tasks. When all of these simple pieces are meshed together into a program, it can produce a very complicated and useful application.<br />
Let's say that we are writing a text-based medieval video game. Our video game will have two types of characters: the players and the monsters. A player has to know the values of certain attributes: health, strength, and agility. A player must also know what type of weapon and what type of armor they possess. A player must be able to move through a maze, attack a monster, and pick up treasure. So, to design this "player object", we must first separate <b>data</b> that the player object must know from <b>actions</b> that the player must know how to execute. The definition for a player object could be:<br />
<pre>Player Object:
data:
health
strength
agility
type of weapon
type of armor
actions:
move
attack monster
get treasure
END;</pre></div>Adminhttp://www.blogger.com/profile/02962796630033361160noreply@blogger.com0tag:blogger.com,1999:blog-6028771996639635467.post-57998631151094371552010-12-11T01:50:00.000-08:002010-12-11T01:50:35.325-08:00A Real Function!Enough dilly-dally, let's see a real, working, C++ function that actually does something! Suppose we need a function that, adds two numbers and return their sum..<br />
<br />
int sum(int num1,int num2)<br />
{<br />
return(num1+num2);<br />
}<br />
<br />
<br />
This is our function. Everytime it takes two parameters (integers) and return their sum..<br />
In our main we call it like this..<br />
<br />
<br />
int main()<br />
{<br />
int a=9;<br />
int b=8;<br />
int c=sum(a,b);<br />
cout<<c<<endl; // c=17<br />
return 0;<br />
}Adminhttp://www.blogger.com/profile/02962796630033361160noreply@blogger.com0tag:blogger.com,1999:blog-6028771996639635467.post-20938076465520431852010-12-11T01:46:00.001-08:002010-12-11T01:46:54.773-08:00Function BasicsNow that you know what a function is, let's look at function syntax. We've already seen that a function can take some inputs, do some stuff, and then produce an output.<br />
The basic form of a function <i>definition</i> is this:<br />
<blockquote><pre><i>output</i> <i>function_name</i> (<i>input_1</i>, <i>input_2</i>, <i>input_3</i>, <i>input_...</i>) {
// code to execute inside function
}
</pre></blockquote>It's called a function <i>definition</i> because we are defining the function. We are saying, "This is a function named <i>function_name</i>, whose inputs are <i>input_1</i>, <i>input_2</i>, etc., and whose output is <i>output</i>. When it is called, the function will execute the code in between its curly braces (<code>{}</code>).<br />
At this point, let's refine our sample function definition. When programmers talk about functions, instead of the word <i>input</i> they usually use the word <i>parameter</i>. A parameter to a function is nothing more than an input to a function. At the same time, instead of using the word <i>output</i>, programmers generally refer to the <i>return</i> of a function. A particular function "returns" a value. So, here is our updated function definition:<br />
<blockquote><pre><i>return_type</i> <i>function_name</i> (<i>parameter_1</i>, <i>parameter_2</i>, <i>parameter_3</i>, <i>parameter_...</i>) {
// code to execute inside function
}
</pre></blockquote>Notice that in place of <i><code>output</code></i>, the function definition says <i>return_type</i>. That's because when we are actually writing a function definition, we'll put the return type there, immediately preceding the name of the function. The return type is nothing more than a plain old variable type, such as <code>int</code>, or <code>double</code>, etc.<br />
Similarly, parameters use variable types also. If the first input to a function is an <code>int</code>, then the first parameter will be something like <code>int my_number</code>. We'll see what <code>my_number</code> does in just a moment.Adminhttp://www.blogger.com/profile/02962796630033361160noreply@blogger.com0tag:blogger.com,1999:blog-6028771996639635467.post-82239774700044180842010-12-11T01:45:00.001-08:002010-12-11T01:45:20.843-08:00SummaryThink of a function as a black box.<br />
<center><table border="1" style="width: 100px;"><tbody>
<tr height="100"><td bgcolor="#000000"><br />
<br />
<br />
</td></tr>
</tbody></table></center>A "black box" is a convenient analogy for something that happens by magic. How does the black box work? It takes some inputs, and swirls them around inside the box, and produces some kind of output. Each function can swirl around the inputs in a different way, however that function so chooses. How a function will use the inputs to come up with the outputs is the essence of the function.<br />
We just said that functions are like black boxes because we throw in some inputs, and something happens by magic, and some output comes flying out. The black box metaphor is really only appropriate when you are using other people's functions. When you write the function yourself, you have to know exactly how the function swirls up the inputs. It's not magical for you, because you decided how the function works. However, if you give your function to someone else for them to use, you can tell them it's a black box.<br />
<div><br />
</div>Adminhttp://www.blogger.com/profile/02962796630033361160noreply@blogger.com0tag:blogger.com,1999:blog-6028771996639635467.post-59598709340506615922010-12-11T01:44:00.000-08:002010-12-11T01:44:38.199-08:00What are functions good for?At this point, we've seen one reason why functions are useful. Functions let us create logical groupings of code. If someone is reading your code, and she sees that you call a function <code>bakeCookies</code>, she knows immediately that you are baking cookies. If, on the other hand, she sees that your code places eggs in a bowl, then adds butter, etc., it will not be clear right away that you are trying to bake cookies. Lots of recipes start out with putting eggs in a bowl, and lots of recipes add butter to the eggs. By the time she reads the last line, she might realize that you are baking cookies, but only if she is familiar with the recipe. It's possible that she won't realize that you are baking cookies at all! The point is, functions make your code much easier to read.<br />
There is an even better reason to use functions: they can make your code shorter. Fewer lines of code is not always desirable, but every time you write a line of code, there's the possibility that you are introducing a <a href="http://www.intap.net/~drw/cpp/glossary.htm#bug">bug</a>. Functions start to reduce the number of lines of code when you call them repeatedly.<br />
Suppose that you want to mail out invitations to eight of your friends for a cocktail party. Let's assume that you need to do the following procedure in order to invite your friend Hank.<br />
<pre>write Hank's name on the invitation
write Hank's name and address on the envelope
place the invitation in the envelope
seal and stamp the envelope
drop the envelope in the mail
</pre>It takes five lines of pseudo-code to invite one friend, so it takes 40 lines of pseudo-code to invite eight friends. It would look like this:<pre>write Hank's name on the invitation
write Hank's name and address on the envelope
place the invitation in the envelope
seal and stamp the envelope
drop the envelope in the mail
write Ann's name on the invitation
write Ann's name and address on the envelope
place the invitation in the envelope
seal and stamp the envelope
drop the envelope in the mail
write Alicia's name on the invitation
write Alicia's name and address on the envelope
place the invitation in the envelope
seal and stamp the envelope
drop the envelope in the mail
write Whitney's name on the invitation
write Whitney's name and address on the envelope
place the invitation in the envelope
seal and stamp the envelope
drop the envelope in the mail
write Greg's name on the invitation
write Greg's name and address on the envelope
place the invitation in the envelope
seal and stamp the envelope
drop the envelope in the mail
write Mi Young's name on the invitation
write Mi Young's name and address on the envelope
place the invitation in the envelope
seal and stamp the envelope
drop the envelope in the mail
write Flavio's name on the invitation
write Flavio's name and address on the envelope
place the invitation on the envelope
seal and stamp the envelope
drop the envelope in the mail
write Brian's name on the invitation
write Brian's name and address on the envelope
place the invitation in the envelope
seal and stamp the envelope
drop the envelope in the mail
</pre>That's a lot of repeated code, and any time you repeat code like this, you are more likely to add a bug to your program. For example, look at what we're doing with Flavio's invitation - we are placing it <i>on</i>, not <i>in</i>, the envelope! We sealed his envelope and dropped it in the mail, but there was no invitation inside. Flavio will receive an empty envelope and he'll be mighty confused. That's a mistake that resulted from having to type the same lines of code over and over again.<br />
Functions can substantially reduce the amount of pseudo-code you need to write to invite your eight friends to the party. It seems unlikely that you'd be able to reduce this at all - each of your friends has got to have their own personally addressed invitation, and all of the envelopes have to be sealed and stamped and placed in the mail. How are we going to reduce the number of lines of code? Let's create a function called <code>inviteToParty</code>which does the following procedure:<br />
<pre>write Hank's name on the invitation
write Hank's name and address on the envelope
place the invitation in the envelope
seal and stamp the envelope
drop the envelope in the mail
</pre>Now that we have this function, we can call it eight times to invite our eight friends:<pre>inviteToParty
inviteToParty
inviteToParty
inviteToParty
inviteToParty
inviteToParty
inviteToParty
inviteToParty
</pre>You probably noticed a problem with doing it this way. We're inviting Hank eight times, and none of our other friends are going to receive invitations! Hank will get invited eight times because the function invites <i>Hank</i> to the party, and the function is being called eight times. The solution is to modify the function so that it invites <i>friend</i> to the party, where <i>friend</i> can be any of your friends. We'll change our function so that it looks like this:<pre>write <i>friend</i>'s name on the invitation
write <i>friend</i>'s name and address on the envelope
place the invitation in the envelope
seal and stamp the envelope
drop the envelope in the mail
</pre>and then we'll change the way in which we call the function:<pre>inviteToParty (<i>friend</i> = Hank)
inviteToParty (<i>friend</i> = Ann)
inviteToParty (<i>friend</i> = Alicia)
inviteToParty (<i>friend</i> = Whitney)
inviteToParty (<i>friend</i> = Greg)
inviteToParty (<i>friend</i> = Mi Young)
inviteToParty (<i>friend</i> = Flavio)
inviteToParty (<i>friend</i> = Brian)
</pre>Now, each time we call the function, <code><i>friend</i></code> is a different person, and each of our eight friends will be invited. We've just reduced the number of lines of pseudo-code from 40 to 13 by using a function, and our code became much easier to read. (We also got rid of that bug whereby Flavio received an empty envelope.)<br />
All of the examples on this page were written in pseudo-code, but the next page describes how to write functions in C++.Adminhttp://www.blogger.com/profile/02962796630033361160noreply@blogger.com0tag:blogger.com,1999:blog-6028771996639635467.post-45496198225182264082010-12-11T01:40:00.000-08:002010-12-11T01:42:09.134-08:00What is a Function?Up until this point, every line of code we've shown you has done a simple task, such as performing an arithmetic operation, or checking a boolean condition, or assigning to a variable. Functions allow you to do a whole lot in one line of code. Instead of performing a simple task, a single line of code can display a menu of choices, or compute complicated three-dimensional transformations, or even play Tetris!<br />
How is this possible? Functions allow you to group a series of steps under one name.Adminhttp://www.blogger.com/profile/02962796630033361160noreply@blogger.com0tag:blogger.com,1999:blog-6028771996639635467.post-33099428772999093432010-12-11T01:39:00.001-08:002010-12-11T01:39:09.159-08:00Loops2<h3>the while statement</h3>The <b>while statement</b> has the form:<blockquote><pre><b>while</b>(<i>condition</i>) {
// code to execute
};
</pre></blockquote><br />
<ul><li><i>condition</i> is a boolean statement that is checked each time after the final "}" of the <code>while</code> statement executes. If the <i>condition</i> is true then the <code>while</code> statement executes again. If the <i>condition</i> is false, the <code>while</code>statement does not execute again.</li>
</ul>As an example, let's say that we wanted to write all the even numbers between 11 and 23 to the screen. The following is a full C++ program that does that.<br />
<pre>// include this file for cout
#include <iostream.h>
int main(){
// this variable holds the present number
int current_number = 12;
// while loop that prints all even numbers between
// 11 and 23 to the screen
while (current_number < 23){
cerr << current_number << endl;
current_number += 2;
}
cerr << "all done" << endl;
}
</pre>The preceding example prints the value of <code>current_number</code> to the screen and then adds 2 to its value. As soon as the value of the variable <code>current_number</code> goes above 23, the while loop exits and the next line is executed.<br />
The output of the preceding program would be:<br />
<pre>12
14
16
18
20
22
all done</pre>Adminhttp://www.blogger.com/profile/02962796630033361160noreply@blogger.com0tag:blogger.com,1999:blog-6028771996639635467.post-86576187301846948352010-12-11T01:38:00.001-08:002010-12-11T01:38:30.784-08:00Loops1<h3>the for statement</h3>the <b>for statement</b> has the form:<blockquote><pre><b>for</b>(<i>initial_value</i>,<i>test_condition</i>,<i>step</i>){
// code to execute inside loop
};
</pre></blockquote><br />
<ul><li><i>initial_value</i> sets up the initial value of the loop counter.</li>
<li><i>test_condition</i> this is the condition that is tested to see if the loop is executed again.</li>
<li><i>step</i> this describes how the counter is changed on each execution of the loop.</li>
</ul>Here is an example:<br />
<pre>// The following code adds together the numbers 1 through 10
// this variable keeps the running total
int total=0;
// this loop adds the numbers 1 through 10 to the variable total
for (int i=1; i < 11; i++){
total = total + i;
}
</pre>So in the preceding chunk of code we have:<br />
<ul><li><i>initial_condition</i> is <code>int i=0;</code></li>
<li><i>test_condition</i> is <code>i < 11;</code></li>
<li><i>step</i> is <code>i++;</code></li>
</ul>So, upon initial execution of the loop, the integer variable <code>i</code> is set to 1. The statement <code>total = total + i;</code> is executed and the value of the variable <code>total</code> becomes 1. The <i>step</i> code is now executed and <code>i</code> is incremented by 1, so its new value is 2.<br />
The <i>test_condition</i> is then checked, and since <code>i</code> is less than 11, the loop code is executed and the variable <code>total</code> gets the value 3 (since <code>total</code> was 1, and <code>i</code> was 2. <code>i</code> is then incremented by 1 again.<br />
The loop continues to execute until the condition <code>i<11</code> fails. At that point <code>total</code> will have the value 1+2+3+4+5+6+7+8+9+10 = 55.Adminhttp://www.blogger.com/profile/02962796630033361160noreply@blogger.com0tag:blogger.com,1999:blog-6028771996639635467.post-47008180151015163562010-12-11T01:36:00.000-08:002010-12-11T01:36:37.676-08:00the switch statementThe next branching statement is called a <b>switch statement</b>. A <b>switch statement</b> is used in place of many <b>if statements</b>.<br />
Let's consider the following case: Joel is writing a program that figures interest on money that is held in a bank. The amount of interest that money earns in this bank depends on which type of account the money is in. There are 6 different types of accounts and they earn interest as follows:<br />
<center><table border="1"><tbody>
<tr><th>account type</th><th>interest earned</th></tr>
<tr><td>personal financial</td><td>2.3%</td></tr>
<tr><td>personal homeowner</td><td>2.6%</td></tr>
<tr><td>personal gold</td><td>2.9%</td></tr>
<tr><td>small business</td><td>3.3%</td></tr>
<tr><td>big business</td><td>3.5%</td></tr>
<tr><td>gold business</td><td>3.8%</td></tr>
</tbody></table></center>One way for Programmer to write this program is as follows: (assuming also that Programmer has assigned numbers to the account types starting with personal financial and ending with gold business.)<br />
<pre>// declare a variable to keep track of the interest
float interest = 0.0;
// decide which interest rate to use.
if (account_type == 1){
interest = 2.3;
}
else {
if (account_type == 2) {
interest = 2.6;
}
else {
if (account_type == 3){
interest = 2.9;
}
else {
if (account_type == 4){
interest = 3.3;
}
else {
if (account_type == 5){
interest = 3.5;
}
else {
// account type must be 6
interest = 3.8;
}
}
}
}
}
</pre>That code is hard to read and hard to understand. There is an easier way to write this, using the <b>switch statement</b>. The preceding chunk of code could be written as follows:<br />
<pre>switch (account_value){
case 1:
interest = 2.3;
break;
case 2:
interest = 2.6;
break;
case 3:
interest = 2.9;
break;
case 4:
interest = 3.3;
break;
case 5:
interest = 3.5;
break;
case 6:
interest = 3.8;
break;
default:
interest = 0.0;
}
</pre>The <b>switch statement</b> allows a programmer to compound a group of <b>if statements</b>, provided that the condition being tested is an integer. The switch statement has the form:<br />
<blockquote><pre><b>switch</b>(<i>integer_val</i>){
<b>case</b> <i>val_1</i>:
// code to execute if <i>integer_val</i> is <i>val_1</i>
<b>break</b>;
...
<b>case</b> <i>val_n</i>:
// code to execute if <i>integer_val</i> is <i>val_n</i>
<b>break</b>;
<b>default</b>:
// code to execute if <i>integer_val</i> is none of the above
}
</pre></blockquote>The <b>default</b> clause is optional, but it is good programming practice to use it. The default clause is executed if none of the other clauses have been executed. For example, if my code looked like:<br />
<pre>switch (place) {
case 1:
cout << "we're first" << endl;
break;
case 2:
cout << "we're second" << endl;
break;
default:
cout << "we're not first or second" << endl;
}
</pre>This switch statement will write "we're first" if the variable <b>place</b> is equal to 1, it will write "we're second" if <b>place</b> is equal to 2, and will write "we're not first or second" if <b>place</b> is any other value.<br />
The <b>break</b> keyword means "jump out of the switch statement, and do not execute any more code." To show how this works, examine the following piece of code:<br />
<pre>int value = 0;
switch(input){
case 1:
value+=4;
case 2:
value+=3;
case 3:
value+=2;
default:
value++;
}
</pre>If <i>input</i> is 1 then 4 will be added to <i>value</i>. Since there is no <b>break</b> statement, the program will go on to the next line of code which adds 3, then the line of code that adds 2, and then the line of code that adds 1. So <i>value</i>will be set to 10! The code that was intended was probably:<br />
<pre>int value = 0;
switch(input){
case 1:
value+=4;
break;
case 2:
value+=3;
break;
case 3:
value+=2;
break;
default:
value++;
}
</pre>This feature of <b>switch</b> statements can sometimes be used to a programmers' advantage. In the example with the different types of bank accounts, say that the interest earned was a follows:<br />
<center><table border="1"><tbody>
<tr><th>account type</th><th>interest earned</th></tr>
<tr><td>personal financial</td><td>2.3%</td></tr>
<tr><td>personal homeowner</td><td>2.6%</td></tr>
<tr><td>personal gold</td><td>2.9%</td></tr>
<tr><td>small business</td><td>2.6%</td></tr>
<tr><td>big business</td><td>2.9%</td></tr>
<tr><td>gold business</td><td>3.0%</td></tr>
</tbody></table></center>Now, the code for this could be written as:<br />
<pre>switch (account_value){
case 1:
interest = 2.3;
break;
case 2:
case 4:
interest = 2.6;
break;
case 3:
case 5:
interest = 2.9;
break;
case 6:
interest = 3.8;
break;
default:
interest = 0.0;</pre>Adminhttp://www.blogger.com/profile/02962796630033361160noreply@blogger.com0tag:blogger.com,1999:blog-6028771996639635467.post-26020663743950711212010-12-11T01:34:00.001-08:002010-12-11T01:34:30.412-08:00Example Of If Else StatementHere is a full C++ program as an example:<br />
<pre>//include this file for cout
#include <iostream.h>
int main() {
// define two integers
int x = 3;
int y = 4;
//print out a message telling which is bigger
if (x > y) {
cout << "x is bigger than y" << endl;
}
else {
cout << "x is smaller than y" << endl;
}
return 0;
}
</pre>In this case <i>condition</i> is equal to "(x > y)" which is equal to "(3 > 4)" which is a <i>false</i> statement. So the code within the <b>else</b> clause will be executed. The output of this program will be:<br />
<pre>x is smaller than y
</pre>If instead the value for x was 6 and the value for y was 2, then <i>condition</i> would be "(6 > 2)" which is a <i>true</i> statement and the output of the program would be:<pre>x is bigger than y</pre>Adminhttp://www.blogger.com/profile/02962796630033361160noreply@blogger.com0tag:blogger.com,1999:blog-6028771996639635467.post-50430514775301062232010-12-11T01:33:00.000-08:002010-12-11T01:33:41.496-08:00Branching Statements<h3>the if statement</h3><div><br />
</div><div>The first type of branching statement we will look at is the <b>if statement</b>. An <b>if statement</b> has the form:<br />
<blockquote><pre><b>if</b> (<i>condition</i>)
{
// code to execute if <i>condition</i> is true
}
<b>else</b>
{
// code to execute if <i>condition</i> is false
}</pre></blockquote><br />
In an <b>if statement</b>, <i>condition</i> is a value or an expression that is used to determine which code block is executed, and the curly braces act as "begin" and "end" markers. </div>Adminhttp://www.blogger.com/profile/02962796630033361160noreply@blogger.com0tag:blogger.com,1999:blog-6028771996639635467.post-42989642645039885092010-12-11T01:24:00.000-08:002010-12-11T01:24:17.671-08:00Our Ist Programm..Now, that we have studied the basic of c++ language, lets write our first code..<br />
<br />
<br />
#include<iostream><br />
using namespace std;<br />
int main()<br />
{<br />
cout<<"Hello World"<<endl;<br />
}<br />
<br />
This is out first programm, it displays Hello World on the screen.<br />
";" semicolon indicates the end of the line<br />
"endl" means next line<br />
"cout" is used to display anything.<br />
<br />
We will study them more as we go further.Adminhttp://www.blogger.com/profile/02962796630033361160noreply@blogger.com0tag:blogger.com,1999:blog-6028771996639635467.post-51678170557954818332010-12-10T07:36:00.001-08:002010-12-10T07:36:21.143-08:00Binary representations<span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;">Octal and hexadecimal numbers have a considerable advantage over our decimal numbers in the world of bits, and is that their bases (8 and 16) are perfect multiples of 2 (2</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><sup>3</sup></span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"> </span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;">and 2</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><sup>4</sup></span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;">, respectively), which allows us to make easier conversions from these bases to binary than from decimal numbers (whose base is 2x5). For example, suppose that we want to translate the following binary sequence to numbers of other bases:</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"> </span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><br />
</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><br />
</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><tt><br />
110011111010010100</tt></span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><br />
</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><br />
</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;">In order to translate it to decimal we would need to conduct a mathematical operation similar to the one we have used previously to convert from hexadecimal or octal, which would give us the decimal number 212628.</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><br />
</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><br />
</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;">Nevertheless to pass this sequence to octal it will only take us some seconds and even the less skilled in mathematics can do it just by seeing it: Since 8 is 2</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><sup>3</sup></span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;">, we will separate the binary value in groups of 3 numbers:</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><br />
</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><br />
</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"></span><br />
<pre style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;">110 011 111 010 010 100
</pre><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><br />
</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><br />
</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;">and now we just have to translate to octal numberal radix each group separately:</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"> </span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><br />
</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><br />
</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><pre style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;">110 011 111 010 010 100
6 3 7 2 2 4
</pre></span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><br />
</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><br />
</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;">giving the number 637224 as result. This same process can be inversely performed to pass from octal to binary.</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"> </span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><br />
</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;">In order to conduct the operation with hexadecimal numbers we only have to perform the same process but separating the binary value in groups of 4 numbers, because 16 = 2</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><sup>4</sup></span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;">:</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"> </span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><br />
</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><br />
</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><pre style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;">11 0011 1110 1001 0100
3 3 E 9 4
</pre></span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><br />
</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><br />
</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;">Therefore, the binary expression 110011111010010100 can be represented in C++ either as 212628 (decimal), as 0637224 (octal) or as 0x33e94 (hexadecimal).</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><br />
</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;"><br />
</span><span class="Apple-style-span" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 12px;">The hexadecimal code is specially interesting in computer science since nowadays, computers are based on bytes composed of 8 binary bits and therefore each byte matches with the range that 2 hexadecimal numbers can represent. For that reason it is so frequently used to represent values translated to or from binary base.</span>Adminhttp://www.blogger.com/profile/02962796630033361160noreply@blogger.com0