<?xml version="1.0" encoding="UTF-8" standalone="no"?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><rss xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd" version="2.0"><channel><title>Programming Solution</title><description>I am Shahid Hossain and this is a little effort to help people who face different kinds of programming problems. I will try to organize the posts in a way that will be helpful. You will find different categories of problems in this blog. If you have any question mention it in comment section. Don't forget to include any better idea if you have any :)</description><managingEditor>noreply@blogger.com (geteamon)</managingEditor><pubDate>Fri, 25 Oct 2024 03:18:35 +0300</pubDate><generator>Blogger http://www.blogger.com</generator><openSearch:totalResults xmlns:openSearch="http://a9.com/-/spec/opensearchrss/1.0/">51</openSearch:totalResults><openSearch:startIndex xmlns:openSearch="http://a9.com/-/spec/opensearchrss/1.0/">1</openSearch:startIndex><openSearch:itemsPerPage xmlns:openSearch="http://a9.com/-/spec/opensearchrss/1.0/">25</openSearch:itemsPerPage><link>http://programming-eamon.blogspot.com/</link><language>en-us</language><itunes:explicit>no</itunes:explicit><itunes:subtitle>I am Shahid Hossain and this is a little effort to help people who face different kinds of programming problems. I will try to organize the posts in a way that will be helpful. You will find different categories of problems in this blog. If you have any q</itunes:subtitle><itunes:owner><itunes:email>noreply@blogger.com</itunes:email></itunes:owner><item><title>ADT implementation</title><link>http://programming-eamon.blogspot.com/2009/12/adt-implementation.html</link><category>Abstraction</category><author>noreply@blogger.com (geteamon)</author><pubDate>Mon, 28 Dec 2009 01:16:00 +0200</pubDate><guid isPermaLink="false">tag:blogger.com,1999:blog-8130301155776000667.post-1281642049620567982</guid><description>&lt;span style="font-family:Verdana,sans-serif;"&gt;The specification of an ADT (Abstract Data Type) defines abstract data values and abstract operations for the user. Of course, the ADT must implemented in program code.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:Verdana,sans-serif;"&gt;To implement an ADT, the programmer must do two things:&lt;/span&gt;&lt;br /&gt;&lt;ul&gt;&lt;span style="font-family:Verdana,sans-serif;"&gt;&lt;li&gt;choose an concrete data representation of the abstract data, using data type that already exist&lt;/li&gt;&lt;/span&gt;&lt;span style="font-family:Verdana,sans-serif;"&gt;&lt;li&gt;implement each of the allowabel operations in terms of program instructions&lt;/li&gt;&lt;/span&gt;&lt;span style="font-family:Verdana,sans-serif;"&gt;&lt;/span&gt;&lt;/ul&gt;&lt;span style="font-family:Verdana,sans-serif;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:Verdana,sans-serif;"&gt;Our choice may be based on:&lt;/span&gt;&lt;br /&gt;&lt;ul&gt;&lt;span style="font-family:Verdana,sans-serif;"&gt;&lt;li&gt;time efficiency (the speed at which the algorithms execute)&lt;/li&gt;&lt;/span&gt;&lt;span style="font-family:Verdana,sans-serif;"&gt;&lt;li&gt;space efficiency (the economical use of memory space)&lt;/li&gt;&lt;/span&gt;&lt;span style="font-family:Verdana,sans-serif;"&gt;&lt;li&gt;simplicity and readability of the algorithms&lt;/li&gt;&lt;/span&gt;&lt;span style="font-family:Verdana,sans-serif;"&gt;&lt;/span&gt;&lt;/ul&gt;&lt;span style="font-family:Verdana,sans-serif;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:Verdana,sans-serif;"&gt;Categories of ADT operations&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:Verdana,sans-serif;"&gt;The basic operations that are performed on an abstract data type fall into different categories. Such as:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Constructor:&lt;/span&gt; An operation that creates a new instance (variable) of an ADT. For example: creating a new instance of a list&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Transformer:&lt;/span&gt; An operation that builds a new value of the ADT, given one or more previous values of the type. For example: inserting an item in a list or deleting an item from a list.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Observer:&lt;/span&gt; An operation that allows us to observe the state of an instance of an ADT without changing it. For example: checking if a list is empty or searching a list for a target.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Iterator:&lt;/span&gt; This is less common operation, it allows us to process all components in an instance of an ADT. For example: printing the items of a list.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Mutators (setters):&lt;/span&gt; Changing the value of an attribute&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Accessors (getters):&lt;/span&gt; getting the value of an attribute&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Reference: &lt;a href="http://www.google.com/url?sa=t&amp;amp;source=web&amp;amp;ct=res&amp;amp;cd=1&amp;amp;ved=0CAoQFjAA&amp;amp;url=http%3A%2F%2Fen.wikipedia.org%2Fwiki%2FAbstract_data_type&amp;amp;rct=j&amp;amp;q=abstract+data+type+implementation&amp;amp;ei=Z-o3S_vzFoiC_Aadz8yeBA&amp;amp;usg=AFQjCNFssEv_h8kFz0ebC_QDgqeSw3JO9g"&gt;wikipedia&lt;/a&gt;, &lt;a href="http://www.google.com/url?sa=t&amp;amp;source=web&amp;amp;ct=res&amp;amp;cd=3&amp;amp;ved=0CBcQFjAC&amp;amp;url=http%3A%2F%2Fportal.acm.org%2Fcitation.cfm%3Fid%3D802889&amp;amp;rct=j&amp;amp;q=abstract+data+type+implementation&amp;amp;ei=Z-o3S_vzFoiC_Aadz8yeBA&amp;amp;usg=AFQjCNESxVG_M4XCH4IvMRA9B-okySpEgg"&gt;acm&lt;/a&gt;, &lt;a href="http://www.google.com/url?sa=t&amp;amp;source=web&amp;amp;ct=res&amp;amp;cd=5&amp;amp;ved=0CCQQFjAE&amp;amp;url=http%3A%2F%2Fwww.answers.com%2Ftopic%2Fabstract-data-type&amp;amp;rct=j&amp;amp;q=abstract+data+type+implementation&amp;amp;ei=Z-o3S_vzFoiC_Aadz8yeBA&amp;amp;usg=AFQjCNFMlL_80qSv7Ka6WLOCGblrMVBBPA"&gt;answers&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;table width="100%"&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;&lt;div width="150px" align="left"&gt;&lt;a href="http://programming-eamon.blogspot.com/2009/09/abstract-data-types.html"&gt;Previous post&lt;/a&gt;&lt;br /&gt;&lt;/div&gt;&lt;/td&gt;&lt;td&gt;&lt;div width="100px" align="right"&gt;&lt;a href="http://programming-eamon.blogspot.com/"&gt;Next post&lt;/a&gt;&lt;br /&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;</description><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">1</thr:total></item><item><title>Abstract Data Types</title><link>http://programming-eamon.blogspot.com/2009/09/abstract-data-types.html</link><category>Abstraction</category><category>C++</category><author>noreply@blogger.com (geteamon)</author><pubDate>Sat, 19 Sep 2009 02:05:00 +0300</pubDate><guid isPermaLink="false">tag:blogger.com,1999:blog-8130301155776000667.post-2723493644257991451</guid><description>&lt;span style="font-family: Verdana,sans-serif;"&gt;Data abstraction is important because:&lt;/span&gt;&lt;br /&gt;&lt;ul&gt;&lt;span style="font-family: Verdana,sans-serif;"&gt;&lt;li&gt;It allows uns to create data types not otherwise available in a programming language&lt;/li&gt;&lt;/span&gt;&lt;span style="font-family: Verdana,sans-serif;"&gt;&lt;li&gt;It allows us to produce off-the-shelf-software, pieces of software that can be used over and over again in different programs by any programmer&lt;/li&gt;&lt;/span&gt;&lt;span style="font-family: Verdana,sans-serif;"&gt;&lt;/span&gt;&lt;/ul&gt;&lt;span style="font-family: Verdana,sans-serif;"&gt;&lt;/span&gt;&lt;span style="font-family: Verdana,sans-serif;"&gt;The primary tool for practicing data abstraction is the abstract data type(ADT), a new data type that is not built into the programming language, concentrating only on its logical properties and deferring the details of its implementation.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family: Verdana,sans-serif;"&gt;An abstract data type has both a specification (the what) and an implementation (the how)&lt;/span&gt;&lt;b&gt;&lt;span style="font-family: Verdana,sans-serif;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;&lt;b&gt;&lt;span style="font-family: Verdana,sans-serif;"&gt;Properties of abstract data types&lt;/span&gt;&lt;/b&gt;&lt;span style="font-family: Verdana,sans-serif;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;ul&gt;&lt;span style="font-family: Verdana,sans-serif;"&gt;&lt;li&gt;Attributes&lt;br /&gt;&lt;ul&gt;&lt;li&gt;Java: fields&lt;/li&gt;&lt;span style="font-family: Verdana,sans-serif;"&gt;&lt;li&gt;C++: data members or member variables&lt;/li&gt;&lt;/span&gt;&lt;span style="font-family: Verdana,sans-serif;"&gt;&lt;/span&gt;&lt;/ul&gt;&lt;span style="font-family: Verdana,sans-serif;"&gt; &lt;/span&gt;&lt;br /&gt;&lt;/li&gt;&lt;span style="font-family: Verdana,sans-serif;"&gt;&lt;li&gt;Oprations&lt;/li&gt;&lt;/span&gt;&lt;span style="font-family: Verdana,sans-serif;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;ul&gt;&lt;span style="font-family: Verdana,sans-serif;"&gt;&lt;span style="font-family: Verdana,sans-serif;"&gt;&lt;li&gt;Java: methods&lt;/li&gt;&lt;/span&gt;&lt;span style="font-family: Verdana,sans-serif;"&gt;&lt;li&gt;C++: member functions&lt;/li&gt;&lt;/span&gt;&lt;span style="font-family: Verdana,sans-serif;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;/ul&gt;&lt;span style="font-family: Verdana,sans-serif;"&gt;&lt;span style="font-family: Verdana,sans-serif;"&gt;&lt;/span&gt;&lt;span style="font-family: Verdana,sans-serif;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;/ul&gt;&lt;span style="font-family: Verdana,sans-serif;"&gt;&lt;span style="font-family: Verdana,sans-serif;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-family: Verdana,sans-serif;"&gt;&lt;span style="font-family: Verdana,sans-serif;"&gt;Interaction with (usage of) an abstract data type is made through its interface or public interface&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;ul&gt;&lt;span style="font-family: Verdana,sans-serif;"&gt;&lt;span style="font-family: Verdana,sans-serif;"&gt;&lt;li&gt;Public attributes&lt;/li&gt;&lt;/span&gt;&lt;span style="font-family: Verdana,sans-serif;"&gt;&lt;li&gt;Public operations&lt;/li&gt;&lt;/span&gt;&lt;span style="font-family: Verdana,sans-serif;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;/ul&gt;&lt;span style="font-family: Verdana,sans-serif;"&gt;&lt;span style="font-family: Verdana,sans-serif;"&gt;TIP: public interface should be built around public operations.&lt;/span&gt;&lt;b&gt;&lt;span style="font-family: Verdana,sans-serif;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family: Verdana,sans-serif;"&gt;&lt;b&gt;&lt;span style="font-family: Verdana,sans-serif;"&gt;Example of informal specification&lt;/span&gt;&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family: Verdana,sans-serif;"&gt;&lt;span style="font-family: Verdana,sans-serif;"&gt;TYPE&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: Verdana,sans-serif;"&gt;&lt;span style="font-family: Verdana,sans-serif;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-family: Verdana,sans-serif;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-family: Verdana,sans-serif;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-family: Verdana,sans-serif;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-family: Verdana,sans-serif;"&gt;IntList&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: Verdana,sans-serif;"&gt;&lt;span style="font-family: Verdana,sans-serif;"&gt;DOMAIN&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: Verdana,sans-serif;"&gt;&lt;span style="font-family: Verdana,sans-serif;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-family: Verdana,sans-serif;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-family: Verdana,sans-serif;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-family: Verdana,sans-serif;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-family: Verdana,sans-serif;"&gt;Each IntList value is a collection of up to 100 separate integer numbers&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: Verdana,sans-serif;"&gt;&lt;span style="font-family: Verdana,sans-serif;"&gt;OPERATIONS&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: Verdana,sans-serif;"&gt;&lt;span style="font-family: Verdana,sans-serif;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-family: Verdana,sans-serif;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-family: Verdana,sans-serif;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-family: Verdana,sans-serif;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-family: Verdana,sans-serif;"&gt;Insert an item into the list&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: Verdana,sans-serif;"&gt;&lt;span style="font-family: Verdana,sans-serif;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-family: Verdana,sans-serif;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-family: Verdana,sans-serif;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-family: Verdana,sans-serif;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-family: Verdana,sans-serif;"&gt;Delete an item from the list&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: Verdana,sans-serif;"&gt;&lt;span style="font-family: Verdana,sans-serif;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-family: Verdana,sans-serif;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-family: Verdana,sans-serif;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-family: Verdana,sans-serif;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-family: Verdana,sans-serif;"&gt;Return the current length of the list&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: Verdana,sans-serif;"&gt;&lt;span style="font-family: Verdana,sans-serif;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-family: Verdana,sans-serif;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-family: Verdana,sans-serif;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-family: Verdana,sans-serif;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-family: Verdana,sans-serif;"&gt;Print the list&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family: Verdana,sans-serif;"&gt;&lt;span style="font-family: Verdana,sans-serif;"&gt;Notice the complete absense of implementation details&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: Verdana,sans-serif;"&gt;&lt;span style="font-family: Verdana,sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: Verdana,sans-serif;"&gt;&lt;span style="font-family: Verdana,sans-serif;"&gt;Reference: &lt;a href="http://www.google.com/url?sa=t&amp;amp;source=web&amp;amp;ct=res&amp;amp;cd=1&amp;amp;url=http%3A%2F%2Fen.wikipedia.org%2Fwiki%2FAbstract_data_type&amp;amp;ei=JBK0SoStIcv3-AbGuqzeCQ&amp;amp;rct=j&amp;amp;q=abstract+data+types&amp;amp;usg=AFQjCNFssEv_h8kFz0ebC_QDgqeSw3JO9g"&gt;wikipedia&lt;/a&gt;, &lt;a href="http://www.google.com/url?sa=t&amp;amp;source=web&amp;amp;ct=res&amp;amp;cd=5&amp;amp;url=http%3A%2F%2Fbooks.google.com%2Fbooks%3Fid%3DhJ6IOaiHVYUC%26pg%3DPA125%26lpg%3DPA125%26dq%3Dabstract%2Bdata%2Btypes%26source%3Dbl%26ots%3DCykohTqXNO%26sig%3DVMCHoBoje-mRBVeNPHKFbMAX6RI%26hl%3Den%26ei%3DJBK0SoStIcv3-AbGuqzeCQ%26sa%3DX%26oi%3Dbook_result%26ct%3Dresult%26resnum%3D5&amp;amp;ei=JBK0SoStIcv3-AbGuqzeCQ&amp;amp;rct=j&amp;amp;q=abstract+data+types&amp;amp;usg=AFQjCNH81K-mcRipRKS4QNQzVMvPz7jDkg"&gt;google books&lt;/a&gt;, &lt;a href="http://www.google.com/url?sa=t&amp;amp;source=web&amp;amp;ct=res&amp;amp;cd=4&amp;amp;url=http%3A%2F%2Fencyclopedia2.thefreedictionary.com%2FAbstract%2Bdata%2Btype&amp;amp;ei=JBK0SoStIcv3-AbGuqzeCQ&amp;amp;rct=j&amp;amp;q=abstract+data+types&amp;amp;usg=AFQjCNFi4QpiYpYCqV3FuLg02BjIZmEHXg"&gt;thefreedictionary&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;table width="100%"&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;&lt;div align="left" width="150px"&gt;&lt;a href="http://programming-eamon.blogspot.com/2009/09/abstraction.html"&gt;Previous post&lt;/a&gt;&lt;br /&gt;&lt;/div&gt;&lt;/td&gt;&lt;td&gt;&lt;div align="right" width="100px"&gt;&lt;a href="http://programming-eamon.blogspot.com/2009/12/adt-implementation.html"&gt;Next post&lt;/a&gt;&lt;br /&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: Verdana,sans-serif;"&gt;&lt;span style="font-family: Verdana,sans-serif;"&gt;&amp;nbsp;&lt;/span&gt;&lt;br /&gt;&lt;/span&gt;</description><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">2</thr:total></item><item><title>Abstraction</title><link>http://programming-eamon.blogspot.com/2009/09/abstraction.html</link><category>Abstraction</category><category>C++</category><author>noreply@blogger.com (geteamon)</author><pubDate>Fri, 18 Sep 2009 00:02:00 +0300</pubDate><guid isPermaLink="false">tag:blogger.com,1999:blog-8130301155776000667.post-4513111252687994613</guid><description>&lt;span style="font-family: Verdana,sans-serif;"&gt;To cope with complexity, the human mind engages in &lt;a href="http://en.wikipedia.org/wiki/Abstraction"&gt;abstraction&lt;/a&gt;, the act of separating the essential qualities of an idea or object from the details of how it works or is composed. With abstraction, we focus on the what, not the how. Abstraction can be performed from different viewpoints.&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-family: Verdana,sans-serif;"&gt;Examples:&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;ul&gt;&lt;span style="font-family: Verdana,sans-serif;"&gt;
&lt;li&gt;Words of a spoken language&lt;/li&gt;
&lt;li&gt;Metaphysical abstractions&lt;/li&gt;
&lt;li&gt;Objects in a classroom&lt;/li&gt;
&lt;li&gt;Mathematical abstractions&lt;/li&gt;
&lt;li&gt;Abstraction in programming&lt;/li&gt;
&lt;/span&gt;&lt;/ul&gt;&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: left;"&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: left;"&gt;&lt;/div&gt;&lt;div class="separator" style="clear: both; text-align: left;"&gt;&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjKAlAFRTifkT77IzbOHLRj-leaBnAWphZJG-YvSMh4uOGGYcmLIW9_zeWLhwXHf2_9-4ho-5x8hDUUxBHN4qRVEh6F36xlf8y1ddiHLMVZ2EI2UsZj0PgvQAHq5f3uc_onEbYZ-ZNmD8rG/s1600-h/abstraction.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjKAlAFRTifkT77IzbOHLRj-leaBnAWphZJG-YvSMh4uOGGYcmLIW9_zeWLhwXHf2_9-4ho-5x8hDUUxBHN4qRVEh6F36xlf8y1ddiHLMVZ2EI2UsZj0PgvQAHq5f3uc_onEbYZ-ZNmD8rG/s320/abstraction.jpg" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;
&lt;span style="font-family: Verdana,sans-serif;"&gt;Fig: Abstraction in real life&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-family: Verdana,sans-serif;"&gt;&lt;/span&gt;&lt;br /&gt;
&lt;b&gt;&lt;span style="font-family: Verdana,sans-serif;"&gt;Abstraction in Programming&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-family: Verdana,sans-serif;"&gt;&lt;b&gt;Data types:&lt;/b&gt; An int-type represents an integer, a float-type represents a floating point number&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-family: Verdana,sans-serif;"&gt;&lt;b&gt;Functions:&lt;/b&gt; absoluteValue&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-family: Verdana,sans-serif;"&gt;double absoluteValue(double d)&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: Verdana,sans-serif;"&gt;{&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: Verdana,sans-serif;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;if(d&amp;lt;0)&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: Verdana,sans-serif;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: Verdana,sans-serif;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return -d; &lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: Verdana,sans-serif;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: Verdana,sans-serif;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;else&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: Verdana,sans-serif;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: Verdana,sans-serif;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return d;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: Verdana,sans-serif;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: Verdana,sans-serif;"&gt;}&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-family: Verdana,sans-serif;"&gt;#include&amp;lt;cmath&amp;gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: Verdana,sans-serif;"&gt;double absoluteValue(double d)&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: Verdana,sans-serif;"&gt;{&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: Verdana,sans-serif;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return abs(d); &lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: Verdana,sans-serif;"&gt;}&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-family: Verdana,sans-serif;"&gt;Both of the given example functions do the same thing. We don't need to know how they are doing. So we are focusing on what, not how.&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;&lt;span style="font-family: Verdana,sans-serif;"&gt;Types of Abstraction&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-family: Verdana,sans-serif;"&gt;There are two important &lt;a href="http://en.wikipedia.org/wiki/Control_abstraction"&gt;abstraction techniques&lt;/a&gt;:&lt;/span&gt;&lt;br /&gt;
&lt;ul&gt;&lt;span style="font-family: Verdana,sans-serif;"&gt;
&lt;li&gt;control abstraction&lt;/li&gt;
&lt;/span&gt;&lt;span style="font-family: Verdana,sans-serif;"&gt;
&lt;li&gt;data abstraction&lt;/li&gt;
&lt;/span&gt;&lt;span style="font-family: Verdana,sans-serif;"&gt;&lt;/span&gt;&lt;/ul&gt;&lt;span style="font-family: Verdana,sans-serif;"&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: Verdana,sans-serif;"&gt;Control abstraction is the separation of the logical properties of an action from its implementation.&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-family: Verdana,sans-serif;"&gt;Data abstraction is the separation of a data type's logical properties from its implementation.&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-family: Verdana,sans-serif;"&gt;Reference: &lt;a href="http://www.google.com/url?sa=t&amp;amp;source=web&amp;amp;ct=res&amp;amp;cd=1&amp;amp;url=http%3A%2F%2Fen.wikipedia.org%2Fwiki%2FAbstraction_%28computer_science%29&amp;amp;ei=BqOySsngIo3Q-QaOxYXxCQ&amp;amp;rct=j&amp;amp;q=abstraction+in+programming&amp;amp;usg=AFQjCNGTqAVFIdSHIJGZoGUzHDaGvc3mGg"&gt;wikipedia&lt;/a&gt;, &lt;a href="http://www.google.com/url?sa=t&amp;amp;source=web&amp;amp;ct=res&amp;amp;cd=3&amp;amp;url=http%3A%2F%2Fwww.artima.com%2Fintv%2Fabstreffi.html&amp;amp;ei=BqOySsngIo3Q-QaOxYXxCQ&amp;amp;rct=j&amp;amp;q=abstraction+in+programming&amp;amp;usg=AFQjCNHixq0nUgbHSekNOByk03b8podbWg"&gt;artima&lt;/a&gt;, &lt;a href="http://www.google.com/url?sa=t&amp;amp;source=web&amp;amp;ct=res&amp;amp;cd=9&amp;amp;url=http%3A%2F%2Fresearch.microsoft.com%2Fapps%2Fpubs%2Fdefault.aspx%3Fid%3D81293&amp;amp;ei=BqOySsngIo3Q-QaOxYXxCQ&amp;amp;rct=j&amp;amp;q=abstraction+in+programming&amp;amp;usg=AFQjCNFLoQuL_8_9Q6jr6sdph2EDvtNM4A"&gt;microsoft&lt;/a&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;table width="100%"&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td&gt;&lt;div align="left" width="150px"&gt;&lt;a href="http://programming-eamon.blogspot.com/2009/09/array-vs-vector.html"&gt;Previous post&lt;/a&gt;&lt;/div&gt;&lt;/td&gt;&lt;td&gt;&lt;div align="right" width="100px"&gt;&lt;a href="http://programming-eamon.blogspot.com/2009/09/abstract-data-types.html"&gt;Next post&lt;/a&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;&lt;span style="font-family: Verdana,sans-serif;"&gt;&lt;br /&gt;
&lt;/span&gt;</description><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" height="72" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjKAlAFRTifkT77IzbOHLRj-leaBnAWphZJG-YvSMh4uOGGYcmLIW9_zeWLhwXHf2_9-4ho-5x8hDUUxBHN4qRVEh6F36xlf8y1ddiHLMVZ2EI2UsZj0PgvQAHq5f3uc_onEbYZ-ZNmD8rG/s72-c/abstraction.jpg" width="72"/><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">0</thr:total></item><item><title>Array vs vector</title><link>http://programming-eamon.blogspot.com/2009/09/array-vs-vector.html</link><category>Array</category><category>C++</category><category>vector</category><author>noreply@blogger.com (geteamon)</author><pubDate>Sun, 6 Sep 2009 14:05:00 +0300</pubDate><guid isPermaLink="false">tag:blogger.com,1999:blog-8130301155776000667.post-2910681497081285099</guid><description>&lt;span style="font-family:verdana;"&gt;Vectors are intended to replace the C-style arrays, i.e. the static arrays:&lt;ul&gt;&lt;li&gt;Safe to pass as a parameter or assign to&lt;/li&gt;&lt;li&gt;Size increases when necessary means no memory management problem&lt;/li&gt;&lt;li&gt;Comparisons (==, &amp;lt; etc.) works "as it should"&lt;/li&gt;&lt;/ul&gt;vector has its own dangers, though:&lt;ul&gt;&lt;li&gt;No forced boundary limit checks in indexing&lt;/li&gt;&lt;li&gt;Easy to write code that may be inefficient in parameter passing&lt;/li&gt;&lt;li&gt;Passing arrays to libraries that use C arrays is difficult&lt;/li&gt;&lt;/ul&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Comparisons between C arrays and vector:&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;table bgcolor="#ddeeee" border="2"&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;&lt;span style="font-weight: bold;"&gt;operation&lt;/span&gt;&lt;/td&gt;&lt;td&gt;&lt;span style="font-weight: bold;"&gt;array&lt;/span&gt;&lt;/td&gt;&lt;td&gt;&lt;span style="font-weight: bold;"&gt;vector&lt;/span&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Creation&lt;/td&gt;&lt;td&gt;int t[10]&lt;/td&gt;&lt;td&gt;vector&amp;lt;int&amp;gt; t(10); or&lt;br /&gt;vector&amp;lt;int&amp;gt; t; (empty)&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Assignment&lt;/td&gt;&lt;td&gt;in a loop&lt;/td&gt;&lt;td&gt;t1 = t2&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Indexing&lt;/td&gt;&lt;td&gt;t[i]&lt;/td&gt;&lt;td&gt;t[i] or t.at(i)&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Inserting to the end&lt;/td&gt;&lt;td&gt;not possible&lt;/td&gt;&lt;td&gt;t.push_back(item)&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Combining&lt;/td&gt;&lt;td&gt;not possible&lt;/td&gt;&lt;td&gt;t1.insert(t1.end(), t2.begin(), t2.end())&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Size&lt;/td&gt;&lt;td&gt;programmer must know&lt;/td&gt;&lt;td&gt;t.size()&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Emptying&lt;/td&gt;&lt;td&gt;not possible&lt;/td&gt;&lt;td&gt;t.clear()&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Comparison&lt;/td&gt;&lt;td&gt;in a loop&lt;/td&gt;&lt;td&gt;t1 == t2 (likewise &lt;&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Insertion to the middle&lt;/td&gt;&lt;td&gt;not possible&lt;/td&gt;&lt;td&gt;t.insert(iterator, item)&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Searching&lt;/td&gt;&lt;td&gt;in a loop&lt;/td&gt;&lt;td&gt;find(t.begin(), t.end(), item)&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Exchange of values&lt;/td&gt;&lt;td&gt;in a loop&lt;/td&gt;&lt;td&gt;t1.swap(t2)&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;br /&gt;&lt;br /&gt;vector&amp;lt; type &amp;gt; var;&lt;/span&gt;An empty vector&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);font-family:verdana;" &gt;vector&amp;lt; type &amp;gt; var(vector&amp;lt; type &amp;gt;);&lt;/span&gt; &lt;span style="font-family:verdana;"&gt;Initialization with another vector&lt;/span&gt; &lt;br /&gt;&lt;span style="font-family:verdana;"&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;vector&amp;lt; type &amp;gt; var(int);&lt;/span&gt;A vector of the size int with elements of the type&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;vector&amp;lt; type &amp;gt; var(int, element);&lt;/span&gt;A vector with int elements initialized to element&lt;br /&gt;&lt;br /&gt;Operations: &lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;vector[int] // Indexing the vector at int.&lt;br /&gt;vector1 = vector2 // Assignment&lt;br /&gt;vector1 == vector2 // Comparison&lt;br /&gt;vector1 != vector2vector1 &amp;lt; vector2vector1 &amp;lt;= vector2vector1 &amp;gt; vector2 vector1 &amp;gt;= vector2&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Functions:  &lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;swap(vector1, vector2) //  swaps the contents&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Own functions:  &lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;table bgcolor="#ddeeee" border="2"&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;type at( int )&lt;/td&gt;&lt;td&gt;Indexing the vector at int&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;type front()&lt;/td&gt;&lt;td&gt;The first element&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;type back()&lt;/td&gt;&lt;td&gt;Last element&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;vector&amp;lt;type&amp;gt;::size_type size()&lt;/td&gt;&lt;td&gt;number of elements&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;bool empty()&lt;/td&gt;&lt;td&gt;true if vector is empty&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;void push_back(element)&lt;/td&gt;&lt;td&gt;Element is added to the end of vector&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;void pop_back()&lt;/td&gt;&lt;td&gt;Last element is removed, size gets smaller by one&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;void clear()&lt;/td&gt;&lt;td&gt;Empty the vector completely&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;iterator being()&lt;/td&gt;&lt;td&gt;An iterator to the beginning&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;iterator end()&lt;/td&gt;&lt;td&gt;An iterator to the end&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold; color: rgb(0, 153, 0); font-style: italic;"&gt;#include &amp;lt;algorithm&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Functions:&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;&lt;br /&gt;void sort( array, array+size );&lt;br /&gt;void sort( vector.begin(), vector.end() );&lt;br /&gt;    // Sorting the vector or array into an ascending order&lt;br /&gt;bool compare(const item&amp;amp;, const item&amp;amp;);&lt;br /&gt;void sort( array, array+size, compare );&lt;br /&gt;void sort( vector.begin(), vector.end(), compare );&lt;br /&gt;    // Sort can be given a comparison function as a third parameter&lt;br /&gt;    // Function returns true if the first parameter is smaller than the second.&lt;br /&gt;void operations(const item&amp;amp;);&lt;br /&gt;void for_each( array, array+size, operations );&lt;br /&gt;void for_each( vector.begin(), vector.end(), operation );&lt;br /&gt;    // Calls the function operation for each element in the array or vector at a time.&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Reference: &lt;a href="http://www.google.com/url?sa=t&amp;amp;source=web&amp;amp;ct=res&amp;amp;cd=1&amp;amp;url=http%3A%2F%2Fwww.cplusplus.com%2Fforum%2Fbeginner%2F6321%2F&amp;amp;ei=fJejSvOcBsLb-QbRqYzbDw&amp;amp;rct=j&amp;amp;q=array+vs+vector+c%2B%2B&amp;amp;usg=AFQjCNEscN6a4pozsVl7Rd3qozAF1Pi7gg"&gt;cplusplus&lt;/a&gt;, &lt;a href="http://www.google.com/url?sa=t&amp;amp;source=web&amp;amp;ct=res&amp;amp;cd=3&amp;amp;url=http%3A%2F%2Fwww.velocityreviews.com%2Fforums%2Ft374392-c-array-vs-vector.html&amp;amp;ei=fJejSvOcBsLb-QbRqYzbDw&amp;amp;rct=j&amp;amp;q=array+vs+vector+c%2B%2B&amp;amp;usg=AFQjCNEdL9JZR99qfG6XfMPkvXsDU6Jt7w"&gt;velocityreviews&lt;/a&gt;, &lt;a href="http://www.google.com/url?sa=t&amp;amp;source=web&amp;amp;ct=res&amp;amp;cd=5&amp;amp;url=http%3A%2F%2Fforums.devx.com%2Farchive%2Findex.php%2Ft-88641.html&amp;amp;ei=fJejSvOcBsLb-QbRqYzbDw&amp;amp;rct=j&amp;amp;q=array+vs+vector+c%2B%2B&amp;amp;usg=AFQjCNFWtwTjq6s0VN1ADHnu0j5Hg4eZaQ"&gt;devx&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;table width="100%"&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;&lt;p width="150px" align="left"&gt;&lt;a href="http://programming-eamon.blogspot.com/2009/09/vector.html"&gt;Previous post&lt;/a&gt;&lt;/p&gt;&lt;/td&gt;&lt;td&gt;&lt;p width="100px" align="right"&gt;&lt;a href="http://programming-eamon.blogspot.com/2009/09/abstraction.html"&gt;Next post&lt;/a&gt;&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;br /&gt;&lt;/span&gt;</description><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">0</thr:total></item><item><title>Vector</title><link>http://programming-eamon.blogspot.com/2009/09/vector.html</link><category>Array</category><category>C++</category><category>vector</category><author>noreply@blogger.com (geteamon)</author><pubDate>Sun, 6 Sep 2009 00:15:00 +0300</pubDate><guid isPermaLink="false">tag:blogger.com,1999:blog-8130301155776000667.post-5573051391981038164</guid><description>&lt;span style="font-family:verdana;"&gt;The &lt;a href="http://en.wikipedia.org/wiki/Standard_Template_Library"&gt;C++ standard library&lt;/a&gt; offers the programmer plenty of generic data structures and algorithms that are ready for use. The programmer can concentrate on the essential and does not need to invent the wheel all over again. Just like &lt;span style="font-style: italic;"&gt;string&lt;/span&gt; is dynamic and easier to use than static character strings, there is a dynamic data structure corresponding to static &lt;a href="http://en.wikipedia.org/wiki/Array_data_structure"&gt;arrays&lt;/a&gt;. That is &lt;a href="http://en.wikipedia.org/wiki/Vector_%28C%2B%2B%29"&gt;vector&lt;/a&gt;. vector is a part of the C++ standard library. The problems with arrays in C++:&lt;ul&gt;&lt;li&gt;They are static, i.e. the amount of elements cannot be changed without compiling the program again&lt;/li&gt;&lt;li&gt;An array cannot be copied into another away with the = operator&lt;/li&gt;&lt;li&gt;An array cannot be initialized with an another array&lt;/li&gt;&lt;li&gt;There is no way of knowing the current amount of actual elements without keeping constant score of the situation&lt;/li&gt;&lt;/ul&gt;vector makes all of the above possible and has a lot of other useful features. In practice, vector is generic, dynamic array.&lt;br /&gt;&lt;br /&gt;In order to be able to use vector the library vector must be taken into use.&lt;br /&gt;&lt;br /&gt;Any type of data can be put into a vector. All elements must be of the same type (just like with static arrays). vector is defined with &amp;lt;&amp;gt; notation:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;vector&amp;lt; element_type &amp;gt; variable_name;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;for example:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;vector&amp;lt; int &amp;gt; numbers;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;vector&amp;lt; string &amp;gt; names;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;The vector is empty at initialization if no initial values are given:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;vector&amp;lt; double &amp;gt; temperatures1( 5, 20.1 );&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;vector&amp;lt; double &amp;gt; temperatures2( 3 );&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;vector&amp;lt; double &amp;gt; temperatures3( temperatures2 );&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;now there are five elements in the vector temperatures1 with the value 20.1, all three elements in the vector temperatures2 have been initialized to 0.0 and the temperatures3 has the same contents as temperatures2.&lt;br /&gt;&lt;br /&gt;Just like string, vector has its own &lt;a href="http://en.wikipedia.org/wiki/Function_%28computer_science%29"&gt;functions&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;vector &amp;lt;int&amp;gt; numbers(2,3);&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;//The elements in numbers are (3 3)&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;numbers.clear();&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;//numbers now empty&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;numbers.push_back(4);&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;numbers.push_back(10);&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;//Now the elements are (4 10)&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;numbers.push_back( numbers[1] );&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;//(4 10 10)&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;numbers.push_back( numbers.at( 0 ) );&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;//(4 10 10 4)&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;numbers.pop_back();&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;//(4 10 10)&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;// printing the contents&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;if( !numbers.empty() )&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;{&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;unsigned int i;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;for( i = 0; i &amp;lt; numbers.size(); ++i )&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;cout &amp;lt;&amp;lt; "numbers are: " &amp;lt;&amp;lt; numbers[ i ] &amp;lt;&amp;lt; " ";&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;cout &amp;lt;&amp;lt; endl;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;A vector can be assigned another vector of the same type with the = operator. Two vectors of the same type can be compared with ==, !=, &lt;, &gt;, &lt;= and &gt;= &lt;a href="http://en.wikipedia.org/wiki/Operator_%28programming%29"&gt;operators&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;vector can be passed both as a value and a reference &lt;a href="http://en.wikipedia.org/wiki/Parameter_%28computer_science%29"&gt;parameter&lt;/a&gt; like any ordinary &lt;a href="http://en.wikipedia.org/wiki/Variable_%28programming%29"&gt;variable&lt;/a&gt;:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;vector&amp;lt;type&amp;gt; valueparam&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;vector&amp;lt;type&amp;gt;&amp;amp; referenceparam&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;In practice vector should always be passed as a reference parameter due to efficiency issues.&lt;br /&gt;&lt;br /&gt;Reference: &lt;a href="http://www.google.com/url?sa=t&amp;source=web&amp;ct=res&amp;cd=1&amp;url=http%3A%2F%2Fwww.cplusplus.com%2Freference%2Fstl%2Fvector%2F&amp;ei=jdSiSuebKYra-QbCzvDnDw&amp;rct=j&amp;q=vector+C%2B%2B&amp;usg=AFQjCNH3x_ghqOQFDFatqpVZEedTrlAZdw"&gt;cplusplus&lt;/a&gt;, &lt;a href="http://www.google.com/url?sa=t&amp;source=web&amp;ct=res&amp;cd=4&amp;url=http%3A%2F%2Fwww.codersource.net%2Fc%2B%2B_vector_stl.html&amp;ei=jdSiSuebKYra-QbCzvDnDw&amp;rct=j&amp;q=vector+C%2B%2B&amp;usg=AFQjCNHJMA4M_xN19jdJHx8aYdjHsaXTrA"&gt;codesource&lt;/a&gt;, &lt;a href="http://www.google.com/url?sa=t&amp;source=web&amp;ct=res&amp;cd=5&amp;url=http%3A%2F%2Fcppreference.com%2Fwiki%2Fstl%2Fvector%2Fstart&amp;ei=jdSiSuebKYra-QbCzvDnDw&amp;rct=j&amp;q=vector+C%2B%2B&amp;usg=AFQjCNHv2xLEasUDaqG5RAviWn5G432zeg"&gt;cppreference&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;table width="100%"&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;&lt;p width="150px" align="left"&gt;&lt;a href="http://programming-eamon.blogspot.com/2009/09/using-several-code-files.html"&gt;Previous post&lt;/a&gt;&lt;/p&gt;&lt;/td&gt;&lt;td&gt;&lt;p width="100px" align="right"&gt;&lt;a href="http://programming-eamon.blogspot.com/2009/09/array-vs-vector.html"&gt;Next post&lt;/a&gt;&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;br /&gt;&lt;/span&gt;</description><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">2</thr:total></item><item><title>Using several code files</title><link>http://programming-eamon.blogspot.com/2009/09/using-several-code-files.html</link><category>C++</category><author>noreply@blogger.com (geteamon)</author><pubDate>Sat, 5 Sep 2009 22:10:00 +0300</pubDate><guid isPermaLink="false">tag:blogger.com,1999:blog-8130301155776000667.post-8168851770487700699</guid><description>&lt;span style="font-family:verdana;"&gt;Sometimes it is very handy to use several code files for a particular program. For example, if the program is very big and difficult to handle then its necessary to split the whole program into several files and combine together to make the program work properly. Sometimes function can be moved into a separate file. Let's take an example on how to move the functions into a separate file from the main function.&lt;br /&gt;&lt;br /&gt;In the file plus.hh the function is declared:&lt;br /&gt;&lt;br /&gt;&lt;table border="1" width="100%"&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;#ifndef PLUS_HH&lt;br /&gt;#define PLUS_HH&lt;br /&gt;&lt;br /&gt;int plus( int a, int b );&lt;br /&gt;&lt;br /&gt;#endif // PLUS_HH&lt;/span&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;br /&gt;&lt;br /&gt;In the file plus.cc the function is defined:&lt;br /&gt;&lt;br /&gt;&lt;table border="1" width="100%"&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;int plus( int a, int b )&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;{&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;    return a + b;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;}&lt;/span&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;br /&gt;&lt;br /&gt;In the file main.cc the main function is defined:&lt;br /&gt;&lt;br /&gt;&lt;table border="1" width="100%"&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;#include &amp;lt;iostream&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;#include &amp;lt;cstdlib&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;#include "plus.hh"&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;int main( void )&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;{&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;    cout &amp;lt;&amp;lt; "3+4 = " &amp;lt;&amp;lt; plus( 3, 4 ) &amp;lt;&amp;lt; endl;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;    return EXIT_SUCCESS;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;}&lt;/span&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;br /&gt;&lt;br /&gt;The files work together in that sense that the .hh files (&lt;span style="font-style: italic;"&gt;header files&lt;/span&gt;) have the declarations of those functions the &lt;span style="font-style: italic;"&gt;.cc&lt;/span&gt; files (&lt;span style="font-style: italic;"&gt;source files&lt;/span&gt;) define. The actual code, e.g. the function bodies, is in the &lt;span style="font-style: italic;"&gt;.cc&lt;/span&gt; files. The &lt;span style="font-style: italic;"&gt;.hh&lt;/span&gt; files must be added to the program with the &lt;span style="font-style: italic;"&gt;#include&lt;/span&gt; directive if the functions need to be used in that file.&lt;br /&gt;&lt;br /&gt;The command &amp;lt;file&amp;gt; is used when the included file is in some of the systems default directories. The compiler automatically also links these files. "file.hh" means that the header file is in the same directory as the source file. The function definitions need to be included also into the compilation stage to get an executable program. This is done by linking the files together. The compiler also links:&lt;br /&gt;&lt;br /&gt;&lt;table border="1" width="100%"&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;&amp;gt; g++ o plus plus.cc main.cc&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);font-family:verdana;" &gt;&amp;gt;./plus&lt;br /&gt;3+4 = 7&lt;/span&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;br /&gt;&lt;span style="font-family:verdana;"&gt;&lt;br /&gt;If the different stages of compiling and linking ned to be separated, the source files (.cc) can first be compiled into object files (.o) which then are linked into an executable program. For example:&lt;br /&gt;&lt;br /&gt;&lt;table border="1" width="100%"&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;&amp;gt;g++ c plus.cc&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);font-family:verdana;" &gt;&amp;gt;g++ c main.cc&lt;br /&gt;&amp;gt;g++ o plus plus.o main.o&lt;br /&gt;&lt;/span&gt;&lt;span style="font-family:verdana;"&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;&amp;gt;./plus&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;3+4 = 7&lt;/span&gt;&lt;/span&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;br /&gt;&lt;br /&gt;It makes sense to separate the compiling and linking stages when compiling large programs. It is easy to locate which stage the errors are coming from.&lt;br /&gt;&lt;br /&gt;Some important matters:&lt;ul&gt;&lt;li&gt;Always include only header files&lt;/li&gt;&lt;li&gt;Write the actual code into the source files&lt;/li&gt;&lt;li&gt;Since &lt;span style="font-style: italic;"&gt;include&lt;/span&gt; adds the file as a part of the compiled file, it is possible that some file is included more than once. Overlapping definitions can prevent compilation&lt;/li&gt;&lt;li&gt;The directives #ifndef, #define and #endif tell the preprocessor to add the file only once into the compiled file no matter how many times it has been included&lt;/li&gt;&lt;li&gt;The readability of a small program can be increased by moving the &lt;span style="font-style: italic;"&gt;main&lt;/span&gt; to the beginning of the file and adding the definitions of functions after that. The functions must be declared before the &lt;span style="font-style: italic;"&gt;main&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;/span&gt;&lt;span style="font-family:verdana;"&gt;&lt;br /&gt;&lt;br /&gt;Reference: &lt;a href="http://www.google.com/url?sa=t&amp;amp;source=web&amp;amp;ct=res&amp;amp;cd=2&amp;amp;url=http%3A%2F%2Fwww.fredosaurus.com%2Fnotes-cpp%2Fbasics%2Fmultiple-files.html&amp;amp;ei=ereiSqLlHYX4-Aa40KDzDw&amp;amp;rct=j&amp;amp;q=using+several+files+in+C%2B%2B&amp;amp;usg=AFQjCNHJMeeDVSe4RYrJDF2LZ8hjAEj3uw"&gt;fredosaurus&lt;/a&gt;, &lt;a href="http://www.google.com/url?sa=t&amp;amp;source=web&amp;amp;ct=res&amp;amp;cd=3&amp;amp;url=http%3A%2F%2Fwww.gamedev.net%2Freference%2Fprogramming%2Ffeatures%2Forgfiles%2F&amp;amp;ei=ereiSqLlHYX4-Aa40KDzDw&amp;amp;rct=j&amp;amp;q=using+several+files+in+C%2B%2B&amp;amp;usg=AFQjCNHmvtstWIo0Ju50A8iX7a50O8n2zg"&gt;gamedev&lt;/a&gt;, &lt;a href="http://www.google.com/url?sa=t&amp;amp;source=web&amp;amp;ct=res&amp;amp;cd=5&amp;amp;url=http%3A%2F%2Fwww.learncpp.com%2Fcpp-tutorial%2F18-programs-with-multiple-files%2F&amp;amp;ei=ereiSqLlHYX4-Aa40KDzDw&amp;amp;rct=j&amp;amp;q=using+several+files+in+C%2B%2B&amp;amp;usg=AFQjCNGEkIapz-XVsolsukV7l-nJ0ATf8A"&gt;learncpp&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;table width="100%"&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;&lt;p width="150px" align="left"&gt;&lt;a href="http://programming-eamon.blogspot.com/2009/09/typedef.html"&gt;Previous post&lt;/a&gt;&lt;/p&gt;&lt;/td&gt;&lt;td&gt;&lt;p width="100px" align="right"&gt;&lt;a href="http://programming-eamon.blogspot.com/2009/09/vector.html"&gt;Next post&lt;/a&gt;&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;</description><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">3</thr:total></item><item><title>typedef</title><link>http://programming-eamon.blogspot.com/2009/09/typedef.html</link><category>C++</category><category>typedef</category><author>noreply@blogger.com (geteamon)</author><pubDate>Sat, 5 Sep 2009 13:15:00 +0300</pubDate><guid isPermaLink="false">tag:blogger.com,1999:blog-8130301155776000667.post-1866144680464650933</guid><description>&lt;span style="font-family:verdana;"&gt;Sometimes there is a need to have types that cannot operate together in the program. For instance, money and weight should not be added even though both can be saved into variables of the type &lt;span style="font-style: italic;"&gt;double&lt;/span&gt;. In these cases the situation can be made clearer by giving an existing type a new name with the &lt;span style="font-style: italic;"&gt;&lt;a href="http://en.wikipedia.org/wiki/Typedef"&gt;typedef&lt;/a&gt;&lt;/span&gt; definition. For example:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;typedef doube Currency;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;typedef&lt;/span&gt; does not create a new type. For instance the types &lt;span style="font-style: italic;"&gt;double&lt;/span&gt; and &lt;span style="font-style: italic;"&gt;Currency&lt;/span&gt; would be interchangeable.&lt;br /&gt;&lt;br /&gt;Type definitions make the code easier to read. They can also be used to writing more portable code. The type &lt;span style="font-style: italic;"&gt;&lt;a href="http://en.wikipedia.org/wiki/Integer_%28computer_science%29"&gt;int&lt;/a&gt;&lt;/span&gt; is on some machines only 16 bits long, for instance. If the program when moved to this machine needed to operate with large sums, only the typedefinition needs to be changed. All the gazillion points in the code using the alias would remain intact.&lt;br /&gt;&lt;br /&gt;Reference: &lt;a href="http://www.google.com/url?sa=t&amp;source=web&amp;ct=res&amp;cd=3&amp;url=http%3A%2F%2Fwww.cprogramming.com%2Ftutorial%2Ftypedef.html&amp;ei=KTqiStb6IM_I-QaHrY3gDw&amp;rct=j&amp;q=typedef&amp;usg=AFQjCNGjzUHPYFnLIPxN5g8euUBGttXDZg"&gt;cprogramming&lt;/a&gt;, &lt;a href="http://www.google.com/url?sa=t&amp;source=web&amp;ct=res&amp;cd=1&amp;url=http%3A%2F%2Fen.wikipedia.org%2Fwiki%2FTypedef&amp;ei=KTqiStb6IM_I-QaHrY3gDw&amp;rct=j&amp;q=typedef&amp;usg=AFQjCNHJiJv0PrUZ3dgfZcSe2vLbkvG8nQ"&gt;wikipedia&lt;/a&gt;, &lt;a href="http://www.google.com/url?sa=t&amp;source=web&amp;ct=res&amp;cd=5&amp;url=http%3A%2F%2Fwww.google.com%2Fcodesearch%3Fhl%3Den%26q%3Dtypedef%2Bshow%3AAvr5LF7MzIE%3APfJzBA14AJs%3Akda1FrDQeZk%26source%3Duniversal%26cs_p%3Dhttp%3A%2F%2Fwww.mmbase.nl%2Fdownload%2Freleases%2FMMBase-1.4.zip%26cs_f%3Dmmbase-1.4%2Fsrc%2Forg%2Fmmbase%2Fmodule%2Fcorebuilders%2FTypeDef.java&amp;ei=KTqiStb6IM_I-QaHrY3gDw&amp;rct=j&amp;q=typedef&amp;usg=AFQjCNEm81e3ZIVRN0tiubFZac3h56yisA#l31"&gt;mmbase&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;table width="100%"&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;&lt;p width="150px" align="left"&gt;&lt;a href="http://programming-eamon.blogspot.com/2009/09/switch-statement.html"&gt;Previous post&lt;/a&gt;&lt;/p&gt;&lt;/td&gt;&lt;td&gt;&lt;p width="100px" align="right"&gt;&lt;a href="http://programming-eamon.blogspot.com/2009/09/using-several-code-files.html"&gt;Next post&lt;/a&gt;&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;br /&gt;&lt;/span&gt;</description><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">0</thr:total></item><item><title>switch statement</title><link>http://programming-eamon.blogspot.com/2009/09/switch-statement.html</link><category>C++</category><category>switch</category><author>noreply@blogger.com (geteamon)</author><pubDate>Sat, 5 Sep 2009 12:45:00 +0300</pubDate><guid isPermaLink="false">tag:blogger.com,1999:blog-8130301155776000667.post-5267770483701491392</guid><description>&lt;span style="font-family:verdana;"&gt;The &lt;a href="http://en.wikipedia.org/wiki/Switch_statement"&gt;switch&lt;/a&gt; statement can be used to implement a multi-alternative selection statement when:&lt;ul&gt;&lt;li&gt;The tested value can be interpreted as an integer (&lt;span style="font-style: italic;"&gt;&lt;a href="http://en.wikipedia.org/wiki/Integer_%28computer_science%29"&gt;int&lt;/a&gt;&lt;/span&gt;, &lt;span style="font-style: italic;"&gt;&lt;a href="http://en.wikipedia.org/wiki/Character_%28computing%29"&gt;char&lt;/a&gt;&lt;/span&gt; or &lt;span style="font-style: italic;"&gt;&lt;a href="http://en.wikipedia.org/wiki/Enumerated_type"&gt;enum&lt;/a&gt;&lt;/span&gt;)&lt;/li&gt;&lt;li&gt;The alternatives the value is tested are constants that can be interpreted as integers&lt;/li&gt;&lt;/ul&gt;Note: The &lt;span style="font-style: italic;"&gt;switch&lt;/span&gt; statement is not as general as the &lt;span style="font-style: italic;"&gt;&lt;a href="http://en.wikipedia.org/wiki/If_statement"&gt;if&lt;/a&gt;&lt;/span&gt; statement. However, &lt;span style="font-style: italic;"&gt;switch&lt;/span&gt; is more efficient in some situations.&lt;br /&gt;&lt;br /&gt;The general form of the switch statement is:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;switch ( tested_expression )&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;{&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;case alternative1:&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;statementlist1;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;break;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;.&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;.&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;.&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;case alternativeN:&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;statementlistN;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;break;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;default:&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;statementlist def;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;break;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;When the switch statement is executed, the tested expression is evaluated. It is compared to the alternatives one by one. If one of the alternatives matches the tested expression the execution begins in the corresponding &lt;span style="font-style: italic;"&gt;statementlist i&lt;/span&gt;. The execution continues through the &lt;span style="font-style: italic;"&gt;statementlist i&lt;/span&gt; until one of the following is reached:&lt;ul&gt;&lt;li&gt;A break statement&lt;/li&gt;&lt;li&gt;A return statement&lt;/li&gt;&lt;li&gt;The end of the switch statement&lt;/li&gt;&lt;/ul&gt;If the tested expression doesn't match any of the alternatives the &lt;span style="font-style: italic;"&gt;statementlist def&lt;/span&gt; is executed.&lt;br /&gt;&lt;br /&gt;The &lt;span style="font-style: italic;"&gt;default&lt;/span&gt; clause can be omitted. If the value of the tested expression is not in the alternatives execution "falls through" the &lt;span style="font-style: italic;"&gt;switch&lt;/span&gt;.&lt;br /&gt;&lt;br /&gt;If one of the &lt;span style="font-style: italic;"&gt;statementlist i&lt;/span&gt;'s missing a break the execution continues from the next tested expression, otherwise continues from the statement following the &lt;span style="font-style: italic;"&gt;switch&lt;/span&gt;.&lt;br /&gt;&lt;br /&gt;This can be used in situations where several alternatives have same functionality:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;switch( answer )&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;{&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;case 'n':&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;case 'N':&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//the actions performed when&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//the user answers n or N&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;break;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;...&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;The break can also be omitted if the entire statement (or actually the function) is exited with &lt;span style="font-style: italic;"&gt;return&lt;/span&gt;:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;switch( month )&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;{&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;case 1:&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return "January";&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;case 2:&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return "February";&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;etc...&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;case 12:&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return "December";&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;default:&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return "Error";&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Reference: &lt;a href="http://www.google.com/url?sa=t&amp;source=web&amp;ct=res&amp;cd=2&amp;url=http%3A%2F%2Fmsdn.microsoft.com%2Fen-us%2Flibrary%2F66k51h7a(VS.80).aspx&amp;ei=ZDOiSov-Eo_H-QbttsTrDw&amp;rct=j&amp;q=switch+statement+C%2B%2B&amp;usg=AFQjCNEnse_VSmwVnYi5ya2h4YRxtlzlLA"&gt;msdn&lt;/a&gt;, &lt;a href="http://www.google.com/url?sa=t&amp;source=web&amp;ct=res&amp;cd=3&amp;url=http%3A%2F%2Fwww.intap.net%2F~drw%2Fcpp%2Fcpp04_02.htm&amp;ei=ZDOiSov-Eo_H-QbttsTrDw&amp;rct=j&amp;q=switch+statement+C%2B%2B&amp;usg=AFQjCNF4CyibSHkvHMydE9G8NlGURy5vhA"&gt;intap&lt;/a&gt;, &lt;a href="http://www.google.com/url?sa=t&amp;source=web&amp;ct=res&amp;cd=4&amp;url=http%3A%2F%2Fwww.awitness.org%2Fdelphi_pascal_tutorial%2Fc%2B%2B_delphi%2Fswitch.html&amp;ei=ZDOiSov-Eo_H-QbttsTrDw&amp;rct=j&amp;q=switch+statement+C%2B%2B&amp;usg=AFQjCNEEmoAo1eV1EftpBMrrk2N6sGxz-w"&gt;awitness&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;table width="100%"&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;&lt;p width="150px" align="left"&gt;&lt;a href="http://programming-eamon.blogspot.com/2009/09/enumeration.html"&gt;Previous post&lt;/a&gt;&lt;/p&gt;&lt;/td&gt;&lt;td&gt;&lt;p width="100px" align="right"&gt;&lt;a href="http://programming-eamon.blogspot.com/2009/09/typedef.html"&gt;Next post&lt;/a&gt;&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;br /&gt;&lt;/span&gt;</description><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">0</thr:total></item><item><title>Enumeration</title><link>http://programming-eamon.blogspot.com/2009/09/enumeration.html</link><category>C++</category><category>Enumeration</category><author>noreply@blogger.com (geteamon)</author><pubDate>Sat, 5 Sep 2009 01:33:00 +0300</pubDate><guid isPermaLink="false">tag:blogger.com,1999:blog-8130301155776000667.post-2214641423490695289</guid><description>&lt;span style="font-family:verdana;"&gt;An &lt;a href="http://en.wikipedia.org/wiki/Enumerated_type"&gt;enumeration&lt;/a&gt; is a datatype with all the valid values defined by the programmer. In C++ an enumeration is declared.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;enum TypeName { List_of_values };&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;that defines a new data type named &lt;span style="font-style: italic;"&gt;TypeName&lt;/span&gt; whose values are the identifiers in the List of values. Each identifier is associated with an integer constant, i.e. the C++ &lt;a href="http://en.wikipedia.org/wiki/Compiler"&gt;compiler&lt;/a&gt; automatically preforms a identifier-to-integer mapping associating the integer 0 with the first identifier the integer 1 with the second etc.&lt;br /&gt;&lt;br /&gt;C++ also allows the programmer to specify explicitly the values given to the identifiers:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;enum NumberBase { BINARY = 2, OCTAL = 8, DECIMAL = 10, HEX = 16, HEXADECIMAL = 16 };&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;The integer associated with an &lt;a href="http://en.wikipedia.org/wiki/Identifier"&gt;identifier&lt;/a&gt; is, by default, one more than the integer associated with the preceding identifier. The integers can be declared:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;enum Color { RED = 1, BLUE, GREEN, YELLOW, VIOLET };&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Two ro more identifiers can have the same integer value associated with them. The enumerators (the identifiers in the enumeration) can be used wherever &lt;span style="font-style: italic;"&gt;int&lt;/span&gt; or &lt;span style="font-style: italic;"&gt;char&lt;/span&gt; values can be used. The enumerator is interpreted as the integer it is associated with. Enumeration and integer data can operate together as they were of the same type.&lt;br /&gt;&lt;br /&gt;An integer value cannot be used instead of an enumerator in assignments or as a function parameter without explicit type conversion:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;enum Season { SUMMER, FALL, WINTER, SPRING };&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;void print( Season quarter );&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;int figure = SUMMER; // figure &lt;--- 0&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;// equivalent &lt;--- SUMMER&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;Season equivalent = static_cast &lt;season&gt; (figure);&lt;/season&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;// print( SUMMER )&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;print( static_cast &lt;season&gt; (figure) );&lt;/season&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Reference: &lt;a href="http://www.google.com/url?sa=t&amp;source=web&amp;ct=res&amp;cd=1&amp;url=http%3A%2F%2Fenel.ucalgary.ca%2FPeople%2FNorman%2Fenel315_winter1997%2Fenum_types%2F&amp;ei=qpShSqjGGNTI-QaDuODnDw&amp;rct=j&amp;q=enumeration+in+C%2B%2B&amp;usg=AFQjCNFobehZfnNT8nZ7xxbCIQEbcu6RmQ"&gt;ucalgary&lt;/a&gt;, &lt;a href="http://www.google.com/url?sa=t&amp;source=web&amp;ct=res&amp;cd=2&amp;url=http%3A%2F%2Fwww.anyexample.com%2Fprogramming%2Fcplusplus%2Fexample_of_using_enum_in_cplusplus.xml&amp;ei=qpShSqjGGNTI-QaDuODnDw&amp;rct=j&amp;q=enumeration+in+C%2B%2B&amp;usg=AFQjCNEhVxUmH9rBeTiYkt2P6uQUEzFMfg"&gt;anyexample&lt;/a&gt;, &lt;a href="http://www.google.com/url?sa=t&amp;source=web&amp;ct=res&amp;cd=4&amp;url=http%3A%2F%2Fwww.codeproject.com%2FKB%2Fcpp%2FStringizingCppEnums.aspx&amp;ei=qpShSqjGGNTI-QaDuODnDw&amp;rct=j&amp;q=enumeration+in+C%2B%2B&amp;usg=AFQjCNE-m5VTbkMU083Qavm4cMidOt78GA"&gt;codeproject&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;table width="100%"&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;&lt;p width="150px" align="left"&gt;&lt;a href="http://programming-eamon.blogspot.com/2009/09/streams-as-parameters.html"&gt;Previous post&lt;/a&gt;&lt;/p&gt;&lt;/td&gt;&lt;td&gt;&lt;p width="100px" align="right"&gt;&lt;a href="http://programming-eamon.blogspot.com/2009/09/switch-statement.html"&gt;Next post&lt;/a&gt;&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;br /&gt;&lt;/span&gt;</description><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">0</thr:total></item><item><title>Streams as parameters</title><link>http://programming-eamon.blogspot.com/2009/09/streams-as-parameters.html</link><category>C++</category><category>Stream</category><author>noreply@blogger.com (geteamon)</author><pubDate>Sat, 5 Sep 2009 01:05:00 +0300</pubDate><guid isPermaLink="false">tag:blogger.com,1999:blog-8130301155776000667.post-7239980404050793416</guid><description>&lt;span style="font-family:verdana;"&gt;The most important thing to remember when passing a stream to a function as a parameter: &lt;span style="font-weight: bold;"&gt;the formal parameter needs to be a reference parameter&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;It makes sense to require that since the function operates with the stream. The state of the stream changes and the change must be passed back to the actual parameter. As a rule the actual parameter needs to be of the same type as the formal parameter of the function, except when:&lt;br /&gt;&lt;br /&gt;&lt;table bgcolor="#DDEEEE" border="2"&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;&lt;span style="font-weight: bold;"&gt;the type of the formal&lt;br /&gt;parameter is&lt;/span&gt;&lt;/td&gt;&lt;td&gt;&lt;span style="font-weight: bold;"&gt;the actual parameter&lt;br /&gt;can also be&lt;/span&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;sitram&amp;amp;&lt;/td&gt;&lt;td&gt;ifstream or istringstream&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;ostream&amp;amp;&lt;/td&gt;&lt;td&gt;ofstream or ostringstream&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;br /&gt;&lt;br /&gt;In practice that makes writing more general functions possible: the same function can operate with different types of streams.&lt;br /&gt;&lt;br /&gt;A small example function:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;int readNumber( istrea&amp;amp; stream )&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;{&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;int number;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;stream &amp;gt;&amp;gt; number;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;if( !stream )&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return ERROR;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;else&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return number;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Now the same function can be used for reading input from the keyboard:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;int number;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;number = readNumber( cin );&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;of from a file:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;ifstrea database( "input.txt" );&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;int number;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;number = readNumber( database );&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;or from a string:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;istrinigstream numberstring("12345 67890 101112");&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;int number;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;number = readNumber( numberstring );&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Reference: &lt;a href="http://www.google.com/url?sa=t&amp;source=web&amp;ct=res&amp;cd=2&amp;url=http%3A%2F%2Fwww.daniweb.com%2Fforums%2Fthread156888.html&amp;ei=II-hSuHALor5-AaBjuHvDw&amp;rct=j&amp;q=stream+as+parameter+in+C%2B%2B&amp;usg=AFQjCNFoDirRVCFTJSsLF5bWHAj95-1poA"&gt;daniweb&lt;/a&gt;, &lt;a href="http://www.google.com/url?sa=t&amp;source=web&amp;ct=res&amp;cd=3&amp;url=http%3A%2F%2Fbytes.com%2Ftopic%2Fc%2Fanswers%2F684988-setting-class-parameter&amp;ei=II-hSuHALor5-AaBjuHvDw&amp;rct=j&amp;q=stream+as+parameter+in+C%2B%2B&amp;usg=AFQjCNF0FWpnYJfxwQ61F8-ZLxsG82P9PA"&gt;bytes&lt;/a&gt;, &lt;a href="http://www.google.com/url?sa=t&amp;source=web&amp;ct=res&amp;cd=4&amp;url=http%3A%2F%2Fwww.cplusplus.com%2Freference%2Fiostream%2F&amp;ei=II-hSuHALor5-AaBjuHvDw&amp;rct=j&amp;q=stream+as+parameter+in+C%2B%2B&amp;usg=AFQjCNHpsdhCeEEJ4XWwDvain8-XiwL1jg"&gt;cplusplus&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;table width="100%"&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;&lt;p width="150px" align="left"&gt;&lt;a href="http://programming-eamon.blogspot.com/2009/09/problems-with-streams.html"&gt;Previous post&lt;/a&gt;&lt;/p&gt;&lt;/td&gt;&lt;td&gt;&lt;p width="100px" align="right"&gt;&lt;a href="http://programming-eamon.blogspot.com/2009/09/enumeration.html"&gt;Next post&lt;/a&gt;&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;br /&gt;&lt;/span&gt;</description><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">0</thr:total></item><item><title>Problems with streams</title><link>http://programming-eamon.blogspot.com/2009/09/problems-with-streams.html</link><category>C++</category><category>Stream</category><author>noreply@blogger.com (geteamon)</author><pubDate>Sat, 5 Sep 2009 00:30:00 +0300</pubDate><guid isPermaLink="false">tag:blogger.com,1999:blog-8130301155776000667.post-6243439140804223763</guid><description>&lt;span style="font-family:verdana;"&gt;The following problem situations can occur when using streams:&lt;ul&gt;&lt;li&gt;An attempt to write data into a outputstream fails (e.g. the disk is full)&lt;/li&gt;&lt;li&gt;An attempt to read data from an inputstream even though everything has already been read (e.g. the read operation is at the end of file)&lt;/li&gt;&lt;li&gt;Data of the wrong type is attempted to read from an inputstream (e.g. an integer is expected but the user types characters)&lt;/li&gt;&lt;/ul&gt;Noticing the problematic situations is easy with the following details:&lt;ul&gt;&lt;li&gt;All previously introduced stream operations evaluate the stream the operations is directed to ( &lt;span style="font-style: italic;"&gt;peek()&lt;/span&gt; and &lt;span style="font-style: italic;"&gt;get()&lt;/span&gt; without parameters are exceptions)&lt;/li&gt;&lt;li&gt;A stream used as a condition evaluates true only if the operation was successful (initialization included)&lt;/li&gt;&lt;/ul&gt;The following useful examples can be done based on these:&lt;br /&gt;&lt;br /&gt;Connecting an inputstream to a file and reading it a line at a time:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;ifstream file( "products.dat" );&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;if( !file )&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;{&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;cerr &amp;lt;&amp;lt; "open failed!" &amp;lt;&amp;lt; endl;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return ERROR; // depends on the situation&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;string line;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;while( getline( file, line ) )&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;{&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;...&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;// here work with the read data&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;...&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;noticing input of the incorrect type:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;cout &amp;lt;&amp;lt; "give number: ";&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;int number;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;while( !(cin &amp;gt;&amp;gt; number) )&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;{&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;cerr &amp;lt;&amp;lt; "Invalid input, try again!" &amp;lt;&amp;lt; endl;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;cin.clear();&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;cin.ignore( 1000, '\n' );&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;The &lt;span style="font-style: italic;"&gt;cin&lt;/span&gt;'s own function &lt;span style="font-style: italic;"&gt;clear()&lt;/span&gt; has been used to deal with the error situation above. The stream malfunctions every time an error occurs when handling data with it. Not even legal operations work any longer. Sometimes the situation can be fixed with the &lt;span style="font-style: italic;"&gt;clear()&lt;/span&gt; function. Execution can (sometimes) go on normally afterward. Some errors cannot be fixed with &lt;span style="font-style: italic;"&gt;clear()&lt;/span&gt; (e.g. the end of file has been met)&lt;br /&gt;&lt;br /&gt;How to see the difference between the end of stream error and some other error situation:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;while( getline( stream, line ) )&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;{&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;...&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;if( !stream.eof() )&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;{&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;// Read failed for some other reason than&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;// the end of the stream&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;The stream's own function &lt;span style="font-style: italic;"&gt;eof()&lt;/span&gt; returns true only if an attempt has been made to read from the stream after it has already ended. Also peeking the next character in the stream with the &lt;span style="font-style: italic;"&gt;peek()&lt;/span&gt; function is sometimes an useful way to deduct whether the input has been read entirely or not.&lt;br /&gt;&lt;br /&gt;Last, an example on how to read the following input stream:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;tel.number1 name1&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;tel.number2 name2&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;...&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;with the code:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;int number;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;string name;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;while( stream &amp;gt;&amp;gt; number &amp;amp;&amp;amp; getline( stream, name ) )&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;{&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;// do stuff with the name and number&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;if( !stream.eof() )&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;{&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;// invalid line in input&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Reference: &lt;a href="http://www.google.com/url?sa=t&amp;amp;source=web&amp;amp;ct=res&amp;amp;cd=2&amp;amp;url=http%3A%2F%2Fwww.daniweb.com%2Fforums%2Fthread84583.html&amp;amp;ei=OIShSumBFsXy-Qb8vOzfDw&amp;amp;rct=j&amp;amp;q=problems+with+stream+C%2B%2B&amp;amp;usg=AFQjCNH8--XRmpxcjoB5H-6uynpI_69EhQ"&gt;daniweb&lt;/a&gt;, &lt;a href="http://www.google.com/url?sa=t&amp;amp;source=web&amp;amp;ct=res&amp;amp;cd=4&amp;amp;url=http%3A%2F%2Fwww.dreamincode.net%2Fforums%2Fshowtopic35835.htm&amp;amp;ei=OIShSumBFsXy-Qb8vOzfDw&amp;amp;rct=j&amp;amp;q=problems+with+stream+C%2B%2B&amp;amp;usg=AFQjCNEUBGHfvEOcCbP9-TQJ99rMLAGfEw"&gt;dreamincode&lt;/a&gt;, &lt;a href="http://www.google.com/url?sa=t&amp;amp;source=web&amp;amp;ct=res&amp;amp;cd=5&amp;amp;url=http%3A%2F%2Fwww.cplusplus.com%2Fquery%2Fsearch.cgi%3Fq%3Dstream&amp;amp;ei=OIShSumBFsXy-Qb8vOzfDw&amp;amp;rct=j&amp;amp;q=problems+with+stream+C%2B%2B&amp;amp;usg=AFQjCNELsInlEueAHjWILcZsRgFI25lz5g"&gt;cplusplus&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;table width="100%"&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;&lt;p width="150px" align="left"&gt;&lt;a href="http://programming-eamon.blogspot.com/2009/09/operations-with-inputstreams.html"&gt;Previous post&lt;/a&gt;&lt;/p&gt;&lt;/td&gt;&lt;td&gt;&lt;p width="100px" align="right"&gt;&lt;a href="http://programming-eamon.blogspot.com/2009/09/streams-as-parameters.html"&gt;Next post&lt;/a&gt;&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;br /&gt;&lt;/span&gt;</description><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">0</thr:total></item><item><title>Operations with inputstreams</title><link>http://programming-eamon.blogspot.com/2009/09/operations-with-inputstreams.html</link><category>C++</category><category>Stream</category><author>noreply@blogger.com (geteamon)</author><pubDate>Wed, 2 Sep 2009 01:15:00 +0300</pubDate><guid isPermaLink="false">tag:blogger.com,1999:blog-8130301155776000667.post-7184941981560748001</guid><description>&lt;span style="font-family: verdana;"&gt;&lt;span style="font-style: italic; font-weight: bold;"&gt;cin &amp;gt;&amp;gt; variable&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Reads the next word from the stream, interprets it as the same type as the variable and saves the value into the variable. The variable can be of any basic type or &lt;span style="font-style: italic;"&gt;string&lt;/span&gt;.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic; font-weight: bold;"&gt;cin.get()&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Reads and returns the next character in the stream. At the end of input &lt;span style="font-weight: bold;"&gt;EOF&lt;/span&gt; is returned.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic; font-weight: bold;"&gt;cin.get( chr )&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Reads the next character into the parameter that must be of the type &lt;span style="font-style: italic;"&gt;char&lt;/span&gt;.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic; font-weight: bold;"&gt;getline( cin, str )&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Reads a line of input from the stream and saves it into a parameter of the type &lt;span style="font-style: italic;"&gt;string&lt;/span&gt;.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic; font-weight: bold;"&gt;getline( cin, str, chr )&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Like above but reads only until meets the first character matching chr.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic; font-weight: bold;"&gt;cin.ignore( how_many, chr )&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Reads at most &lt;span style="font-style: italic;"&gt;how_many&lt;/span&gt; characters from the stream or until meets the first chr. The read characters are "thrown away". &lt;span style="font-style: italic;"&gt;How_many&lt;/span&gt; is of the type &lt;span style="font-style: italic;"&gt;int&lt;/span&gt; and chr &lt;span style="font-style: italic;"&gt;char&lt;/span&gt;.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic; font-weight: bold;"&gt;cin.peek()&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Returns the next character in the stream. The character is not read from the stream but the next read operations return it also Returns &lt;span style="font-weight: bold;"&gt;EOF&lt;/span&gt; at the end of input.&lt;br /&gt;&lt;br /&gt;Reference: &lt;a href="http://www.google.com/url?sa=t&amp;source=web&amp;ct=res&amp;cd=2&amp;url=http%3A%2F%2Fwww.cplusplus.com%2Freference%2Fiostream%2Fifstream%2F&amp;ei=T52dSsLWMI-L-QaGg5XtAg&amp;rct=j&amp;q=c%2B%2B+input+stream&amp;usg=AFQjCNG3iqZxhs7yJOcHHJqHJopQP44k1A"&gt;cplusplus&lt;/a&gt;, &lt;a href="http://www.google.com/url?sa=t&amp;source=web&amp;ct=res&amp;cd=3&amp;url=http%3A%2F%2Faugustcouncil.com%2F~tgibson%2Ftutorial%2Fiotips.html&amp;ei=T52dSsLWMI-L-QaGg5XtAg&amp;rct=j&amp;q=c%2B%2B+input+stream&amp;usg=AFQjCNHkFuT4IMzTFvI5iaRa-tmSmYYX3w"&gt;augustcouncil&lt;/a&gt;, &lt;a href="http://www.google.com/url?sa=t&amp;source=web&amp;ct=res&amp;cd=4&amp;url=http%3A%2F%2Fwww.elcel.com%2Fdocs%2Fopentop%2FAPI%2Fot%2Fio%2FInputStream.html&amp;ei=T52dSsLWMI-L-QaGg5XtAg&amp;rct=j&amp;q=c%2B%2B+input+stream&amp;usg=AFQjCNG_h1H-XoEIHdGY_Jp4KDz1UFghFg"&gt;elcel&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;table width="100%"&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;&lt;p width="150px" align="left"&gt;&lt;a href="http://programming-eamon.blogspot.com/2009/09/streams-in-c.html"&gt;Previous post&lt;/a&gt;&lt;/p&gt;&lt;/td&gt;&lt;td&gt;&lt;p width="100px" align="right"&gt;&lt;a href="http://programming-eamon.blogspot.com/2009/09/problems-with-streams.html"&gt;Next post&lt;/a&gt;&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;br /&gt;&lt;/span&gt;</description><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">4</thr:total></item><item><title>Streams in C++</title><link>http://programming-eamon.blogspot.com/2009/09/streams-in-c.html</link><category>C++</category><category>Stream</category><author>noreply@blogger.com (geteamon)</author><pubDate>Tue, 1 Sep 2009 20:10:00 +0300</pubDate><guid isPermaLink="false">tag:blogger.com,1999:blog-8130301155776000667.post-3593875079133222349</guid><description>&lt;span style="font-family:verdana;"&gt;In C++ streams are represented with datatypes implemented in a number of libraries:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;#inlcude &amp;lt;iostream&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;table bgcolor="#ddeeee" border="2"&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;istream&lt;br /&gt;&lt;/td&gt;&lt;td&gt;Inputstreams usually already associated with the keyboard (cin).&lt;br /&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;ostream&lt;br /&gt;&lt;/td&gt;&lt;td&gt;Outputstreams usually already associated with the monitor (cout or cerr)&lt;br /&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;#inlcude &amp;lt;fstream&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;table bgcolor="#ddeeee" border="2"&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;ifstream&lt;br /&gt;&lt;/td&gt;&lt;td&gt;Inputstreams the user must associated with a file on the computer disk&lt;br /&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;ofstream&lt;br /&gt;&lt;/td&gt;&lt;td&gt;Outputstreams the user must associated with a file on the computer disk&lt;br /&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;#inlcude &amp;lt;sstream&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;table bgcolor="#ddeeee" border="2"&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;istringstream&lt;br /&gt;&lt;/td&gt;&lt;td&gt;inputstreams the user can use to read from a string using the formatting facilities provided by streams&lt;br /&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;ostringstream&lt;br /&gt;&lt;/td&gt;&lt;td&gt;Outputstreams the user can use to write to a string using the formatting facilities provided by streams&lt;br /&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;br /&gt;A programmer does not need to worry about associating &lt;span style="font-style: italic;"&gt;cout&lt;/span&gt;, &lt;span style="font-style: italic;"&gt;cin&lt;/span&gt; and &lt;span style="font-style: italic;"&gt;cerr&lt;/span&gt;. They can always be used straight after &lt;span style="font-style: italic;"&gt;#include&amp;lt;iostream&amp;gt;&lt;/span&gt;. Using streams usually consist of three phases (&lt;span style="font-style: italic;"&gt;cin, cerr and cout&lt;/span&gt; exceptions):&lt;ul&gt;&lt;li&gt;Initializing (connecting, associating)&lt;/li&gt;&lt;li&gt;Operating with the stream (read, write etc...)&lt;/li&gt;&lt;li&gt;Closing&lt;/li&gt;&lt;/ul&gt;Before handling data with the streams found in &lt;span style="font-style: italic;"&gt;fstream&lt;/span&gt; and &lt;span style="font-style: italic;"&gt;sstream&lt;/span&gt; libraries they must be initialized:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;ifstream:&lt;/span&gt; A variable of the type ifstream is declared and the name of the file to be read is given:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;ifstream stream_name( file_name );&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;ofstream:&lt;/span&gt; A variable of the type ofstream is declared and the name of the file to be written into is given:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;ofstream stream_name( file_name );&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;istringstream:&lt;/span&gt; A variable of the type istringstream is declared and the string from which the input is read is given:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;istringstream stream_name( s_name );&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;ostringstream:&lt;/span&gt; A varible of the type ostringstream is declared.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;ostringstream stream_name;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;The file_name is not of the type string. It is of an older character string type adopted from the C language. The programmer must give the name of the correct type:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;string file_name;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;cin &amp;gt;&amp;gt; file_name;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;ifstream file( file_name.c_str() );&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;the &lt;span style="font-style: italic;"&gt;c_str()&lt;/span&gt; function returns a character string of the c-style string type matching the original string-typed string. The &lt;span style="font-style: italic;"&gt;file_name&lt;/span&gt; can also be a character string literal:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;ifstream file( "inputs.txt" );&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;After being initialized, different operations can be done with the stream depending on its type and whether it is an input or an output stream (phase 2).&lt;br /&gt;&lt;br /&gt;Working with streams can be confusing because of different alternative ways to do things:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;operations: cout &amp;lt;&amp;lt; "Hello!";&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;functions: getline( cin, cstring );&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;own function: cout.put( 'a' );&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Operations that can be used with all kinds of outputstreams (&lt;span style="font-style: italic;"&gt;e.g. cout&lt;/span&gt;):&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;cout &amp;lt;&amp;lt; value &amp;lt;&amp;lt; endl;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Prints the value into the stream: the value can be of any basic datatype or string.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;cout.put( c );&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Prints one character c(char) into the stream&lt;br /&gt;&lt;br /&gt;There are no more essential printing operations. Almost everything can be done with &amp;lt;&amp;lt; operator.&lt;br /&gt;&lt;br /&gt;The stream must be closed when it is not needed any longer (phase 3). If it isn't properly closed it will needlessly keep on using the system's resources. There are two ways to close a stream:&lt;ul&gt;&lt;li&gt;If the stream is a local variable, C++ automatically closes it when the code block the stream was defined in is exited.&lt;/li&gt;&lt;li&gt;The stream can also be "manually" closed if needed with the stream's own function close():&lt;/li&gt;&lt;/ul&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;ifstream database( "phonenumbers.dat" ); // using the stream&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;...&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;database.close();&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;The stream cannot be used after closing unless it is initialized again.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;cin, cout and cerr&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;cin is used for reading from the keyboard and cout for printing on the screen. cerr has the same functionality as cout but it is meant for printing the error messages of the program. All other output is printed into cout.&lt;br /&gt;&lt;br /&gt;Reference: &lt;a href="http://www.google.com/url?sa=t&amp;amp;source=web&amp;amp;ct=res&amp;amp;cd=1&amp;amp;url=http%3A%2F%2Fwww.cplusplus.com%2Freference%2Fiostream%2F&amp;amp;ei=IlWdSrHIM8_D-Qal0KXlAg&amp;amp;rct=j&amp;amp;q=stream+in+c%2B%2B&amp;amp;usg=AFQjCNHpsdhCeEEJ4XWwDvain8-XiwL1jg"&gt;cplusplus&lt;/a&gt;, &lt;a href="http://www.google.com/url?sa=t&amp;amp;source=web&amp;amp;ct=res&amp;amp;cd=3&amp;amp;url=http%3A%2F%2Fwww.exforsys.com%2Ftutorials%2Fc-plus-plus%2Fstandard-input-stream-and-standard-output-stream.html&amp;amp;ei=IlWdSrHIM8_D-Qal0KXlAg&amp;amp;rct=j&amp;amp;q=stream+in+c%2B%2B&amp;amp;usg=AFQjCNEk5dkNLhqIedD3GAsZ-ZH4VEIGhA"&gt;exforsys&lt;/a&gt;, &lt;a href="http://www.google.com/url?sa=t&amp;amp;source=web&amp;amp;ct=res&amp;amp;cd=4&amp;amp;url=http%3A%2F%2Fmsdn.microsoft.com%2Fen-us%2Flibrary%2Ft047d21k%28VS.71%29.aspx&amp;amp;ei=IlWdSrHIM8_D-Qal0KXlAg&amp;amp;rct=j&amp;amp;q=stream+in+c%2B%2B&amp;amp;usg=AFQjCNGSAMfdjxj3ECjFCU7Jf93W8psqdQ"&gt;msdn&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;table width="100%"&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;&lt;p width="150px" align="left"&gt;&lt;a href="http://programming-eamon.blogspot.com/2009/09/streams.html"&gt;Previous post&lt;/a&gt;&lt;/p&gt;&lt;/td&gt;&lt;td&gt;&lt;p width="100px" align="right"&gt;&lt;a href="http://programming-eamon.blogspot.com/2009/09/operations-with-inputstreams.html"&gt;Next post&lt;/a&gt;&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;br /&gt;&lt;/span&gt;</description><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">0</thr:total></item><item><title>Streams</title><link>http://programming-eamon.blogspot.com/2009/09/streams.html</link><category>C++</category><category>Stream</category><author>noreply@blogger.com (geteamon)</author><pubDate>Tue, 1 Sep 2009 01:05:00 +0300</pubDate><guid isPermaLink="false">tag:blogger.com,1999:blog-8130301155776000667.post-2703465193769076686</guid><description>&lt;span style="font-family:verdana;"&gt;Most programs have some kind of &lt;a href="http://en.wikipedia.org/wiki/Input/output"&gt;I/O&lt;/a&gt; operations:&lt;ul&gt;&lt;li&gt;Reading data from a file on the computer's disk into the program&lt;/li&gt;&lt;li&gt;Saving the results of the program into a file on the disk for future use&lt;/li&gt;&lt;/ul&gt;There must be a way in a programming language to represent the real world disk files in such a way that a program can understand them. Usually a special data type that offers operations for reading and writing data is used. In C++ a data type called a &lt;a href="http://en.wikipedia.org/wiki/Stream_%28computing%29"&gt;stream&lt;/a&gt; is used for this purpose. In fact, besides file I/O, all input and output operations in C++ are done with streams.&lt;br /&gt;&lt;br /&gt;The stream can be thought of as a line of characters following in a specific order in one direction without ever changing their order.&lt;ul&gt;&lt;li&gt;Only the first character of the stream can be read from the stream ( with the operation &lt;span style="font-style: italic;"&gt;get()&lt;/span&gt; )&lt;/li&gt;&lt;li&gt;Writing can only be done after the last character in the stream( with the operation &lt;span style="font-style: italic;"&gt;put()&lt;/span&gt; )&lt;/li&gt;&lt;/ul&gt;All other stream operations can be implemented with get and put. Not changing the order of characters in a stream is essential:&lt;ul&gt;&lt;li&gt;If the user types "abcdef" on a keyboard undoubtedly the program gets the same characters in the same order from the input stream(&lt;span style="font-style: italic;"&gt;cin&lt;/span&gt;)&lt;/li&gt;&lt;li&gt;If the program writes characters into a stream connected to a file it is desirable that the characters are saved in the same order they were written into the stream&lt;/li&gt;&lt;/ul&gt;Streams are sequential in nature: data can only be managed in the same order it is in the stream. A stream is simply a sequence of bytes, that means, if a program is interested in the fifth character in the user's input, the preceding four characters must be read first. The same applies to a stream connected to a file: the file must be read from beginning to end. In reality, disk files can be read and written in an arbitrary order.&lt;br /&gt;&lt;br /&gt;A stream is usually connected to a file, the screen or the keyboard. However, streams can also be used to connect two programs to each other. The interesting thing is that streams can always be handled in the same way, regardless of what they happen to be connected to.&lt;br /&gt;&lt;br /&gt;Reference: &lt;a href="http://www.cplusplus.com/reference/iostream/"&gt;cplusplus&lt;/a&gt;, &lt;a href="http://msdn.microsoft.com/en-us/library/t047d21k%28VS.71%29.aspx"&gt;msdn&lt;/a&gt;, &lt;a href="http://www.exforsys.com/tutorials/c-plus-plus/standard-input-stream-and-standard-output-stream.html"&gt;exforsys&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;table width="100%"&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;&lt;p width="150px" align="left"&gt;&lt;a href="http://programming-eamon.blogspot.com/2009/09/sizeof.html"&gt;Previous post&lt;/a&gt;&lt;/p&gt;&lt;/td&gt;&lt;td&gt;&lt;p width="100px" align="right"&gt;&lt;a href="http://programming-eamon.blogspot.com/2009/09/streams-in-c.html"&gt;Next post&lt;/a&gt;&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;br /&gt;&lt;/span&gt;</description><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">0</thr:total></item><item><title>sizeof</title><link>http://programming-eamon.blogspot.com/2009/09/sizeof.html</link><category>C++</category><author>noreply@blogger.com (geteamon)</author><pubDate>Tue, 1 Sep 2009 00:30:00 +0300</pubDate><guid isPermaLink="false">tag:blogger.com,1999:blog-8130301155776000667.post-4834954501280710037</guid><description>&lt;span style="font-family:verdana;"&gt;The &lt;a href="http://en.wikipedia.org/wiki/Sizeof"&gt;sizeof&lt;/a&gt; operator returns the number of bytes required to store a &lt;a href="http://en.wikipedia.org/wiki/Variable_%28programming%29"&gt;variable&lt;/a&gt; or a data (1 byte = 8 bits). The operator can be applied to any type of variable:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;sizeof( variable name );&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;sizeof( data_type_name );&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;sizeof is a handy tool in finding out the amount of elements in an array:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;const int SIZE = sizeof( array ) / sizeof( element_type );&lt;/span&gt;&lt;br /&gt;or&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;const int SIZE = sizeof( array ) / sizeof( array[ 0 ] );&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;When the value of size is always equal to the actual amount of elements in the array even if the size of the array is changed between complications.&lt;br /&gt;&lt;br /&gt;Note that sizeof does not produce a correct answer if they array is a formal parameter of a function.&lt;br /&gt;&lt;br /&gt;Reference: &lt;a href="http://www.cppreference.com/wiki/keywords/sizeof"&gt;cppreference&lt;/a&gt;, &lt;a href="http://www.space.unibe.ch/comp_doc/c_manual/C/SYNTAX/sizeof.html"&gt;space&lt;/a&gt;, &lt;a href="http://msdn.microsoft.com/en-us/library/4s7x1k91.aspx"&gt;msdn&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;table width="100%"&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;&lt;p width="150px" align="left"&gt;&lt;a href="http://programming-eamon.blogspot.com/2009/08/searching-data.html"&gt;Previous post&lt;/a&gt;&lt;/p&gt;&lt;/td&gt;&lt;td&gt;&lt;p width="100px" align="right"&gt;&lt;a href="http://programming-eamon.blogspot.com/2009/09/streams.html"&gt;Next post&lt;/a&gt;&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;br /&gt;&lt;/span&gt;</description><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">0</thr:total></item><item><title>Searching data</title><link>http://programming-eamon.blogspot.com/2009/08/searching-data.html</link><category>C++</category><category>Search</category><author>noreply@blogger.com (geteamon)</author><pubDate>Mon, 31 Aug 2009 19:58:00 +0300</pubDate><guid isPermaLink="false">tag:blogger.com,1999:blog-8130301155776000667.post-463316976817813140</guid><description>&lt;span style="font-family:verdana;"&gt;Searching is the other problem that often occurs when handling data. The search can be done with some part of the actual data called a key. The easiest way to search is the &lt;a style="font-style: italic;" href="http://en.wikipedia.org/wiki/Linear_search"&gt;linear search&lt;/a&gt;: successive elements are examined until either the item is found or the end of the list is reached. This is however an extremely inefficient way to search because in average about n/2 elements must be searched and in the worst case the element is not found but all n elements have been searched any way. A more efficient way to search is to first sort the elements. Searching is then faster because it is known that the item is missing when the correct place in the array is passed.&lt;br /&gt;&lt;br /&gt;A rather simple but efficient searching algorithm is called &lt;a style="font-style: italic;" href="http://en.wikipedia.org/wiki/Binary_search"&gt;binary search&lt;/a&gt;. The elements must be in ascending or descending order.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic; font-weight: bold;"&gt;Algorithm: Binary search&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;left &amp;lt;--- 0&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;right &amp;lt;--- element_amount - 1&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;&lt;span style="font-weight: bold;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;WHILE&lt;/span&gt; left  &amp;lt;= right&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;middle &amp;lt;--- ( left + right )/2&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;&lt;span style="font-weight: bold;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;IF&lt;/span&gt; elements[ middle ].key == searched_key &lt;span style="font-weight: bold;"&gt;THEN&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;item found at location middle&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;&lt;span style="font-weight: bold;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;ELSE IF&lt;/span&gt; elements[ middle ].key &amp;lt; searched_key &lt;span style="font-weight: bold;"&gt;THEN&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;left &amp;lt;--- middle + 1&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; font-weight: bold;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;ELSE&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;right &amp;lt;--- middle - 1&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;item not in the array&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;How does the binary search work with an integer key:&lt;br /&gt;The search key is 4, there are 10 elements with the key 0-10 in the array&lt;br /&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjFPUXFXQltaYC-r70-Jpu6i-cxmyCMJ4Na4_tlAC3OuFLoURtaf926chwJV3i629yYe89pxzFcJ58yYk1MhphurcBEXmwZ-xVn0BpMhyphenhyphenYC8jwXsBnsYlWXM0E9RcwqMoqsRKQfxTIj0XL2/s1600-h/binary_search.jpg"&gt;&lt;img style="cursor: pointer; width: 320px; height: 306px;" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjFPUXFXQltaYC-r70-Jpu6i-cxmyCMJ4Na4_tlAC3OuFLoURtaf926chwJV3i629yYe89pxzFcJ58yYk1MhphurcBEXmwZ-xVn0BpMhyphenhyphenYC8jwXsBnsYlWXM0E9RcwqMoqsRKQfxTIj0XL2/s320/binary_search.jpg" alt="binary search technique" id="BLOGGER_PHOTO_ID_5376169446368948098" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;Fig: Binary search mechanism&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Note that Binary Search can be used to work with other types than string by changing some points in the code to match the type. string must be changed to match the type of the searched data. The comparisons in the algorithms need to compare the wanted type.&lt;br /&gt;&lt;br /&gt;Reference: &lt;a href="http://en.allexperts.com/q/C-1040/Sorting-searching-data.htm"&gt;allexperts&lt;/a&gt;, &lt;a href="http://www.onesmartclick.com/engineering/data-structure.html"&gt;onesmartclick&lt;/a&gt;, &lt;a href="http://www.informit.com/store/product.aspx?isbn=0201350882"&gt;informit&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;table width="100%"&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;&lt;p width="150px" align="left"&gt;&lt;a href="http://programming-eamon.blogspot.com/2009/08/sorting-in-c.html"&gt;Previous post&lt;/a&gt;&lt;/p&gt;&lt;/td&gt;&lt;td&gt;&lt;p width="100px" align="right"&gt;&lt;a href="http://programming-eamon.blogspot.com/2009/09/sizeof.html"&gt;Next post&lt;/a&gt;&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;br /&gt;&lt;/span&gt;</description><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" height="72" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjFPUXFXQltaYC-r70-Jpu6i-cxmyCMJ4Na4_tlAC3OuFLoURtaf926chwJV3i629yYe89pxzFcJ58yYk1MhphurcBEXmwZ-xVn0BpMhyphenhyphenYC8jwXsBnsYlWXM0E9RcwqMoqsRKQfxTIj0XL2/s72-c/binary_search.jpg" width="72"/><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">0</thr:total></item><item><title>Sorting in C++</title><link>http://programming-eamon.blogspot.com/2009/08/sorting-in-c.html</link><category>C++</category><category>Sorting</category><author>noreply@blogger.com (geteamon)</author><pubDate>Mon, 31 Aug 2009 01:35:00 +0300</pubDate><guid isPermaLink="false">tag:blogger.com,1999:blog-8130301155776000667.post-7165513702731668783</guid><description>&lt;span style="font-family: verdana;"&gt;in C++ the library &lt;span style="font-style: italic;"&gt;algorithm&lt;/span&gt; provides many easy to use algorithms. The sorting algorithm available is called &lt;span style="font-style: italic;"&gt;sort&lt;/span&gt;. The contents of an array are sorted with&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;sort( array_name, array_name+array_size );&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Sorting an integer array with sort:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;#include &amp;lt;iostream&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;#include &amp;lt;algorithm&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;using namespace std;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;void printArray( int array[], int size );&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;int main( void )&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;{&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;const int SIZE = 8;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;int array[ SIZE ] = { 4, 6, 5, 7, 8, 1, 2, 3 };&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;printArray( array, SIZE );&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;sort( array, array+SIZE );&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;printArray( array, SIZE );&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;void printArray( int array[], int size )&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;{&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;int i = 0;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;while( i &amp;lt; size )&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;cout &amp;lt;&amp;lt; array[ i ] &amp;lt;&amp;lt; " ";&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;++i;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;cout &amp;lt;&amp;lt; endl;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;The algorithm requires that the smaller than relation, &lt;, has been defined for the elements of the array. To sort an array of structure, the &lt; operator needs to be defined manually to the sorting function:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;struct Student&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;{&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;string name;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;int st_number;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;};&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;bool compare( const Student&amp;amp; s1, const Student&amp;amp; s2 );&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;const int SIZE = 20;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;int main()&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;{&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Student array[SIZE];&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;// ...&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;sort( array, array+SIZE, compare );&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;// ...&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;bool compare( const Student&amp;amp; s1, const Student&amp;amp; s2 )&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;{&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;if( s1.st_number &amp;lt; s2.st_number )&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return true;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return false;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Reference: &lt;a href="http://www.google.com/url?sa=t&amp;source=web&amp;ct=res&amp;cd=3&amp;url=http%3A%2F%2Fen.wikipedia.org%2Fwiki%2FSort_(C%252B%252B)&amp;ei=iv6aSsujC4bw-QbP7YmPBA&amp;rct=j&amp;q=sorting+in+C%2B%2B&amp;usg=AFQjCNES3j-VT9lEXLXV5lLAjDLXPzbolg"&gt;wikipedia&lt;/a&gt;, &lt;a href="http://www.google.com/url?sa=t&amp;source=web&amp;ct=res&amp;cd=1&amp;url=http%3A%2F%2Fmathbits.com%2FMathBits%2FCompSci%2FArrays%2FSorting.htm&amp;ei=iv6aSsujC4bw-QbP7YmPBA&amp;rct=j&amp;q=sorting+in+C%2B%2B&amp;usg=AFQjCNFs2RZBOOAF4BHFI8bJHHDYw_m1CQ"&gt;mathbits&lt;/a&gt;, &lt;a href="http://www.google.com/url?sa=t&amp;source=web&amp;ct=res&amp;cd=2&amp;url=http%3A%2F%2Fwww.daniweb.com%2Fforums%2Fthread41830.html&amp;ei=iv6aSsujC4bw-QbP7YmPBA&amp;rct=j&amp;q=sorting+in+C%2B%2B&amp;usg=AFQjCNGCs03Xl_x31z5sZBZapxKFbzoWMg"&gt;daniweb&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;table width="100%"&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;&lt;p width="150px" align="left"&gt;&lt;a href="http://programming-eamon.blogspot.com/2009/08/sorting.html"&gt;Previous post&lt;/a&gt;&lt;/p&gt;&lt;/td&gt;&lt;td&gt;&lt;p width="100px" align="right"&gt;&lt;a href="http://programming-eamon.blogspot.com/2009/08/searching-data.html"&gt;Next post&lt;/a&gt;&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;br /&gt;&lt;/span&gt;</description><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">0</thr:total></item><item><title>Sorting</title><link>http://programming-eamon.blogspot.com/2009/08/sorting.html</link><category>C++</category><category>Sorting</category><author>noreply@blogger.com (geteamon)</author><pubDate>Mon, 31 Aug 2009 00:59:00 +0300</pubDate><guid isPermaLink="false">tag:blogger.com,1999:blog-8130301155776000667.post-5602328942989310236</guid><description>&lt;span style="font-family: verdana;"&gt;Arranging the elements of any array (or some key fields in them) into an ascending or descending order is called &lt;a href="http://en.wikipedia.org/wiki/Sorting"&gt;sorting&lt;/a&gt;. Since sorting is a field of coputer science that has been studied a lot, there are dozens of sorting &lt;a href="http://programming-eamon.blogspot.com/2009/08/what-is-algorithm.html"&gt;algorithms&lt;/a&gt; available. Different algorithms and their efficiency is usually covered on the course Utilization of Data Structures. &lt;a href="http://en.wikipedia.org/wiki/Pseudocode"&gt;Pseudocode&lt;/a&gt; for these algorithms can be found in the literature. Cormen, Leiserson, Rivest, Stein: Introduction to Algorithms for instance. An efficient function for sorting data can often be found in one of the libraries.&lt;br /&gt;&lt;br /&gt;Reference: &lt;a href="http://www.google.com/url?sa=t&amp;source=web&amp;ct=res&amp;cd=1&amp;url=http%3A%2F%2Fen.wikipedia.org%2Fwiki%2FSorting_algorithm&amp;ei=8_WaSqfRD5T2-Ab9zcUj&amp;rct=j&amp;q=sorting&amp;usg=AFQjCNFjQz40S9PSPBTvmgiNDGtPawySTQ"&gt;wikipedia&lt;/a&gt;, &lt;a href="http://www.google.com/url?sa=t&amp;source=web&amp;ct=res&amp;cd=4&amp;url=http%3A%2F%2Fwww.sorting-algorithms.com%2F&amp;ei=8_WaSqfRD5T2-Ab9zcUj&amp;rct=j&amp;q=sorting&amp;usg=AFQjCNH5OLiUWJjJKVRGc9eyHZ9aeZHdHw"&gt;sorting-algorithm&lt;/a&gt;, &lt;a href="http://www.google.com/url?sa=t&amp;source=web&amp;ct=res&amp;cd=5&amp;url=http%3A%2F%2Fmaven.smith.edu%2F~thiebaut%2Fjava%2Fsort%2Fdemo.html&amp;ei=8_WaSqfRD5T2-Ab9zcUj&amp;rct=j&amp;q=sorting&amp;usg=AFQjCNEH15lriEQe3zhL_dPy1TKia5IlNA"&gt;smith&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;table width="100%"&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;&lt;p width="150px" align="left"&gt;&lt;a href="http://programming-eamon.blogspot.com/2009/08/modifiers-unsigned-short-and-long.html"&gt;Previous post&lt;/a&gt;&lt;/p&gt;&lt;/td&gt;&lt;td&gt;&lt;p width="100px" align="right"&gt;&lt;a href="http://programming-eamon.blogspot.com/2009/08/sorting-in-c.html"&gt;Next post&lt;/a&gt;&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;</description><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">0</thr:total></item><item><title>Modifiers: unsigned, short and long</title><link>http://programming-eamon.blogspot.com/2009/08/modifiers-unsigned-short-and-long.html</link><category>C++</category><author>noreply@blogger.com (geteamon)</author><pubDate>Sun, 30 Aug 2009 18:10:00 +0300</pubDate><guid isPermaLink="false">tag:blogger.com,1999:blog-8130301155776000667.post-8923729292846294472</guid><description>&lt;span style="font-family:verdana;"&gt;Integers (and in some cases characters) can be adjusted with the modifiers: unsigned, short and long. These can be used to either restrict or widen the used data type:&lt;br /&gt;&lt;br /&gt;&lt;table bgcolor="#ddeeee" border="2"&gt;&lt;tr&gt;&lt;td&gt;&amp;nbsp;unsigned int&amp;nbsp;&lt;/td&gt;&lt;td&gt;An integer value represented with as many bits as int.&lt;br /&gt;range: 0 and all positives&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&amp;nbsp;short int&amp;nbsp;&lt;/td&gt;&lt;td&gt;An integer value represented with at most as many bits as int.&lt;br /&gt;range: negatives and positives&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&amp;nbsp;long int&amp;nbsp;&lt;/td&gt;&lt;td&gt;An integer value represented with at least as many bits as int.&lt;br /&gt;range: negatives and positives&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&amp;nbsp;unsigned short int&amp;nbsp;&lt;/td&gt;&lt;td&gt;like short int&lt;br /&gt;range: 0 and positives&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&amp;nbsp;unsigned long int&amp;nbsp;&lt;/td&gt;&lt;td&gt;like long int&lt;br /&gt;range: 0 and positives&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;it depends on the processor used with how many bits are used to represent each type.&lt;br /&gt;&lt;br /&gt;Reference: &lt;a href="http://www.google.com/url?sa=t&amp;source=web&amp;ct=res&amp;cd=2&amp;url=http%3A%2F%2Fwww.bilmuh.gyte.edu.tr%2Fgokturk%2Fintrocpp%2Ficpp_mods.html&amp;ei=vpWaSu7AHM7S-QaAlOyOBA&amp;rct=j&amp;q=C%2B%2B+modifiers&amp;usg=AFQjCNGx5fwIAj875xYifJGJMnQM-ZtFyA"&gt;bilmuh&lt;/a&gt;, &lt;a href="http://www.google.com/url?sa=t&amp;source=web&amp;ct=res&amp;cd=3&amp;url=http%3A%2F%2Fmsdn.microsoft.com%2Fen-us%2Flibrary%2F6bh0054z(VS.80).aspx&amp;ei=vpWaSu7AHM7S-QaAlOyOBA&amp;rct=j&amp;q=C%2B%2B+modifiers&amp;usg=AFQjCNHAAw1YFClFRv-5aaW3ZdVrAmejYg"&gt;msdn&lt;/a&gt;, &lt;a href="http://www.google.com/url?sa=t&amp;source=web&amp;ct=res&amp;cd=5&amp;url=http%3A%2F%2Fwww.faqts.com%2Fknowledge_base%2Fview.phtml%2Faid%2F24762%2Ffid%2F163&amp;ei=vpWaSu7AHM7S-QaAlOyOBA&amp;rct=j&amp;q=C%2B%2B+modifiers&amp;usg=AFQjCNFc_jpNKkS3nx7JnitfzGYlzkKLmg"&gt;faqts&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;table width="100%"&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;&lt;p width="150px" align="left"&gt;&lt;a href="http://programming-eamon.blogspot.com/2009/08/structures.html"&gt;Previous post&lt;/a&gt;&lt;/p&gt;&lt;/td&gt;&lt;td&gt;&lt;p width="100px" align="right"&gt;&lt;a href="http://programming-eamon.blogspot.com/2009/08/sorting.html"&gt;Next post&lt;/a&gt;&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;br /&gt;&lt;/span&gt;</description><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">0</thr:total></item><item><title>Structures</title><link>http://programming-eamon.blogspot.com/2009/08/structures.html</link><category>C++</category><category>Structure</category><author>noreply@blogger.com (geteamon)</author><pubDate>Sun, 30 Aug 2009 13:30:00 +0300</pubDate><guid isPermaLink="false">tag:blogger.com,1999:blog-8130301155776000667.post-3286098423823853364</guid><description>&lt;span style="font-family:verdana;"&gt;&lt;a href="http://en.wikipedia.org/wiki/Array_data_structure"&gt;Arrays&lt;/a&gt; can be used to handle several elements of the same type together. However, sometimes it would be useful to handle several data elements of different types as a unit. In C++ &lt;a href="http://en.wikipedia.org/wiki/Struct_%28C_programming_language%29"&gt;structures&lt;/a&gt; are used for this purposes.&lt;br /&gt;&lt;br /&gt;Structures (like arrays) are called structured data types, i.e. they have a structure defined by the programmer. A structure is declared:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;struct type_name&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;{&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;data_type member_name1;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;...&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;data_type member_nameN;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;};&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;This defines a new type called type_name which can be used as any other type in C++. Note the semicolon (;) at the end. It is a mandatory while declaring a structure.&lt;br /&gt;&lt;br /&gt;The individual members in the structure act like any other &lt;a href="http://en.wikipedia.org/wiki/Variable_%28programming%29"&gt;variable&lt;/a&gt; with the type data_type. The members can be accessed using . (dot) &lt;a href="http://en.wikipedia.org/wiki/Operator_%28programming%29"&gt;operator&lt;/a&gt;:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;struct Student&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;{&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;string name;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;int student_number;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;};&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;...&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;Student st; // Declare a new variable of type Student&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;...&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;st.name = "Harry Potter";&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;cin &amp;gt;&amp;gt; st.student_number;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;The members of a structure can also be initialized with literals of the same type as the members:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;Student harry=&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;{&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;"Harry Potter", 12345&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;};&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;The members are initialized in the order they appeared at the declaration. A new value can be assigned to a structure with =, but comparison ( == and != ) is not possible.&lt;br /&gt;&lt;br /&gt;The members can be of any type, including arrays:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;struct Grade&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;{&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;string name,&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;int problem_points[4];&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;};&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;...&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;Grade grade1;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;...&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;grade1.problem_points[2] = 5;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;On the other hand, the elements of any array can be of any type, too, including structures:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;Student students[200];&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;...&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;students[0].name = "Ron Weasley";&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;cin &gt;&gt; students[54].student_number;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;A struct is an aggregate of elements of (nearly) arbitrary types. Structures allow the programmer to define a new data type and group related components together. When structure is passed as a parameter of a function, by default it is passed by value but it can be passed by reference as well.&lt;br /&gt;&lt;br /&gt;Reference: &lt;a href="http://www.google.com/url?sa=t&amp;source=web&amp;ct=res&amp;cd=1&amp;url=http%3A%2F%2Fwww.fredosaurus.com%2Fnotes-cpp%2Fstructs%2Fstructs.html&amp;ei=klaaSoeNDozz-QbnjpyPBA&amp;rct=j&amp;q=struct+in+C%2B%2B&amp;usg=AFQjCNFF3MmQos5psO9JvmT8ajYq4nkz2w"&gt;fredosaurus&lt;/a&gt;, &lt;a href="http://www.google.com/url?sa=t&amp;source=web&amp;ct=res&amp;cd=3&amp;url=http%3A%2F%2Fen.wikipedia.org%2Fwiki%2FC%252B%252B_classes&amp;ei=klaaSoeNDozz-QbnjpyPBA&amp;rct=j&amp;q=struct+in+C%2B%2B&amp;usg=AFQjCNE84_biGB1s8ZzOoGaPV38GS4PzZg"&gt;wikipedia&lt;/a&gt;, &lt;a href="http://www.google.com/url?sa=t&amp;source=web&amp;ct=res&amp;cd=4&amp;url=http%3A%2F%2Fmsdn.microsoft.com%2Fen-us%2Flibrary%2F64973255(VS.80).aspx&amp;ei=klaaSoeNDozz-QbnjpyPBA&amp;rct=j&amp;q=struct+in+C%2B%2B&amp;usg=AFQjCNHcabV5YYspVsVMlVOX1Q0ROowRmQ"&gt;msdn&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;table width="100%"&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;&lt;p width="150px" align="left"&gt;&lt;a href="http://programming-eamon.blogspot.com/2009/08/arrays-as-parameters.html"&gt;Previous post&lt;/a&gt;&lt;/p&gt;&lt;/td&gt;&lt;td&gt;&lt;p width="100px" align="right"&gt;&lt;a href="http://programming-eamon.blogspot.com/2009/08/modifiers-unsigned-short-and-long.html"&gt;Next post&lt;/a&gt;&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;</description><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">0</thr:total></item><item><title>Arrays as parameters</title><link>http://programming-eamon.blogspot.com/2009/08/arrays-as-parameters.html</link><category>Array</category><category>C++</category><category>Function</category><author>noreply@blogger.com (geteamon)</author><pubDate>Sat, 29 Aug 2009 21:18:00 +0300</pubDate><guid isPermaLink="false">tag:blogger.com,1999:blog-8130301155776000667.post-3720943560738306769</guid><description>&lt;span style="font-family:verdana;"&gt;Even though call-by-value is the default parameter-passing mechanism in C++, arrays as parameters have some special features. Placing a pair of brackets ( [] ) after the name of a parameter indicates that the parameter is an &lt;a href="http://en.wikipedia.org/wiki/Array_data_type"&gt;array&lt;/a&gt;:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;double arraySum( double array[ ], int array_size )&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;{&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;double sum = 0.0;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;int i = 0;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;while( i &amp;lt; array_size )&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;sum = array[ i ] + sum;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return sum;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;It is not necessary to specify the capacity of the array. There is no restriction on the capacity of the array passed to the function. It is necessary to use another parameter or a constant to pass the size of the array to the function.&lt;br /&gt;&lt;br /&gt;Arrays are automatically passed by reference, i.e. specifying a parameter as an array makes it a reference parameter without the ampersand ( &amp;amp; ). If the function modifies the array, the corresponding argument will also be modified. This can be avoided by declaring the array as a constant reference parameter.&lt;br /&gt;&lt;br /&gt;Let's take a larger example:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;#include &amp;lt;cstdlib&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;#include &amp;lt;iostream&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;using namespace std;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;const int MAX_NUMBER = 1000;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;const int ERROR = -1;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;const double END_NUMBER = -1.0;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;int readNumbers( double numbers[ ] );&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;double calculateMean( const double numbers[ ], int how_many );&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;int main( void )&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;{&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;double numbers[ MAX_NUMBERS ];&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;int how_many_read = 0;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;how_many_read = readNumbers( numbers );&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;if( how_many_read == ERROR )&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return EXIT_FAILURE;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;cout &amp;lt;&amp;lt; "Mean value is: "&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;lt;&amp;lt; calculateMean( numbers, how_many_read )&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;lt;&amp;lt; endl;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return EXIT_SUCCESS;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;int readNumbers( double numbers[ ] )&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;{&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;int how_many = 0;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//we know that there is room for MAX_NUMBERS&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//amount of space in teh array&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;while( how_many &amp;lt; MAX_NUMBERS )&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;cout &amp;lt;&amp;lt; "Give "&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;lt;&amp;lt; how_many + 1&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;lt;&amp;lt; ". number: ";&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//The number is read from the input into&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//the array the change modifies the&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//coresponding argument&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;cin &amp;gt;&amp;gt; numbers[ how_many ];&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;if( numbers[ how_many ] == END_NUMBER )&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return how_many;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;++how_many;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//if how_many got too large, and error has occured&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return ERROR;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;double calculateMean( const double numbers[ ], int how_many )&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;{&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;double sum = 0.0;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;int i = 0;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;if( how_many == 0 )&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return sum;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//in this function the actual size of&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//the array doesn't matter. It is important&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//to know fo how many numbers the mean is&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//supposed to be calculated from&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;while( i &amp;lt; how_many )&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;sum = sum + numbers[ i ];&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;++i;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return sum / how_many;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The other alternative woule have been:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;int readNumbers( const double numbers[ ], int size )&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;{&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;int how_many = 0;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//now the size of the array is given as parameter&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;while( how_many &amp;lt; size )&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;cout &amp;lt;&amp;lt; "Give "&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;lt;&amp;lt; how_many + 1&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;lt;&amp;lt; ". number: ";&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//The number is read from the input&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//into the array the change modifies&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//the corresponding argument&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;cin &amp;gt;&amp;gt; numbers[ how_many ];&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;if( numbers[ how_many ] == END_NUMBER )&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return how_many;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;++how_many;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//if how_many got too large, an error has occured&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return ERROR;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;It would have been called in main:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;how_many_read = readNumbers( numbers, MAX_NUMBERS );&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;The latter is recommended since the result can be used more widely.&lt;/span&gt;&lt;span style="font-family:verdana;"&gt;&lt;span style="font-family:verdana;"&gt;&lt;br /&gt;&lt;br /&gt;Reference: &lt;a href="http://www.cplusplus.com/doc/tutorial/arrays/"&gt;cplusplus&lt;/a&gt;, &lt;a href="http://www.macs.hw.ac.uk/%7Epjbk/pathways/cpp1/node182.html"&gt;macs&lt;/a&gt;, &lt;a href="http://msdn.microsoft.com/en-us/library/538f81ec%28VS.80%29.aspx"&gt;msdn&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;table width="100%"&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;&lt;p width="150px" align="left"&gt;&lt;a href="http://programming-eamon.blogspot.com/2009/08/functions-parameter-passing-mechanisms.html"&gt;Previous post&lt;/a&gt;&lt;/p&gt;&lt;/td&gt;&lt;td&gt;&lt;p width="100px" align="right"&gt;&lt;a href="http://programming-eamon.blogspot.com/2009/08/structures.html"&gt;Next post&lt;/a&gt;&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;</description><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">0</thr:total></item><item><title>Functions: Parameter-passing mechanisms</title><link>http://programming-eamon.blogspot.com/2009/08/functions-parameter-passing-mechanisms.html</link><category>C++</category><category>Function</category><author>noreply@blogger.com (geteamon)</author><pubDate>Sat, 29 Aug 2009 14:30:00 +0300</pubDate><guid isPermaLink="false">tag:blogger.com,1999:blog-8130301155776000667.post-729300769083844812</guid><description>&lt;span style="font-family:verdana;"&gt;The simplest parameter-passing mechanism in C++ occurs by default. It is called &lt;span style="font-weight: bold;"&gt;call-by-value&lt;/span&gt;. Value passed using this method are called value parameters. Any modification of a value parameter within the function body has &lt;span style="font-weight: bold;"&gt;no effect&lt;/span&gt; on the value of its corresponding &lt;a href="http://en.wikipedia.org/wiki/Function_argument"&gt;argument&lt;/a&gt;:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;void divideByTwo( int parameter )&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;{&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;parameter = parameter / 2;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;int main( void )&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;{&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;int figure = 42;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;divideByTwo( figure );&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;cout &amp;lt;&amp;lt; figure &amp;lt;&amp;lt; endl;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);font-family:verdana;" &gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return EXIT_SUCCESS;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:verdana;"&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;prints 42 as a result.&lt;br /&gt;&lt;br /&gt;It would be often useful to be able to change the value of the corresponding argument. For example, if the function needs to return more than one value, data could be returned by changing the value of the parameters. There is also &lt;span style="font-weight: bold;"&gt;call-by-reference&lt;/span&gt; mechanism in C++. These parameters are called &lt;a href="http://en.wikipedia.org/wiki/Reference_%28C%2B%2B%29"&gt;reference parameters&lt;/a&gt;. Reference parameters are aliases of their corresponding arguments, i.e. any change to the value of a reference parameter within the function body changes the value of its corresponding argument. When using call-by-reference, the values are passed by adding an ampersand (&amp;amp;) between the type and the name of the parameter.&lt;br /&gt;&lt;br /&gt;The example mentioned before:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;void divideByTwo( int&amp;amp; parameter )&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;{&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;parameter = parameter / 2;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;int main( void )&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;{&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;int figure = 42;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;divideByTwo( figure );&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;cout &amp;lt;&amp;lt; figure &amp;lt;&amp;lt; endl;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return EXIT_SUCCESS;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;prints now 21, i.e. function divideByTwo was able to change the actual value of the parameter.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Reference: &lt;a href="http://www.google.com/url?sa=t&amp;amp;source=web&amp;amp;ct=res&amp;amp;cd=4&amp;amp;url=http%3A%2F%2Fmsdn.microsoft.com%2Fen-us%2Flibrary%2Fzthk2dkh%28VS.80%29.aspx&amp;amp;ei=TBKZStHjHJT6-AbK84mPBA&amp;amp;rct=j&amp;amp;q=parameter+passing+in+C%2B%2B&amp;amp;usg=AFQjCNERa_n5X7buCeiU2eIHyDxGl-Bmzg"&gt;msdn&lt;/a&gt;, &lt;a href="http://www.google.com/url?sa=t&amp;amp;source=web&amp;amp;ct=res&amp;amp;cd=2&amp;amp;url=http%3A%2F%2Fwww.comp.nus.edu.sg%2F%7Eesim%2Ffaq%2Fnode12.html&amp;amp;ei=TBKZStHjHJT6-AbK84mPBA&amp;amp;rct=j&amp;amp;q=parameter+passing+in+C%2B%2B&amp;amp;usg=AFQjCNFdTXh5IT20rnX2M30UFy0YdAzbOw"&gt;comp&lt;/a&gt;, &lt;a href="http://www.google.com/url?sa=t&amp;amp;source=web&amp;amp;ct=res&amp;amp;cd=3&amp;amp;url=http%3A%2F%2Fwww.brpreiss.com%2Fbooks%2Fopus4%2Fhtml%2Fpage591.html&amp;amp;ei=TBKZStHjHJT6-AbK84mPBA&amp;amp;rct=j&amp;amp;q=parameter+passing+in+C%2B%2B&amp;amp;usg=AFQjCNHFB13aT9ML-d0mPa49MFgr8xiohA"&gt;brpreiss&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;table width="100%"&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;&lt;p width="150px" align="left"&gt;&lt;a href="http://programming-eamon.blogspot.com/2009/08/function-to-deal-with-character.html"&gt;Previous post&lt;/a&gt;&lt;/p&gt;&lt;/td&gt;&lt;td&gt;&lt;p width="100px" align="right"&gt;&lt;a href="http://programming-eamon.blogspot.com/2009/08/arrays-as-parameters.html"&gt;Next post&lt;/a&gt;&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;</description><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">0</thr:total></item><item><title>Functions to deal with character</title><link>http://programming-eamon.blogspot.com/2009/08/function-to-deal-with-character.html</link><category>C++</category><category>Character</category><author>noreply@blogger.com (geteamon)</author><pubDate>Fri, 28 Aug 2009 01:17:00 +0300</pubDate><guid isPermaLink="false">tag:blogger.com,1999:blog-8130301155776000667.post-4380900620764556872</guid><description>&lt;span style="font-family:verdana;"&gt;A lot of useful functions for dealing with characters can be found in the library cctype:&lt;br /&gt;&lt;br /&gt;&lt;table bgcolor="#ddeeee" border="2"&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;&lt;b&gt;&amp;nbsp;Funstion&amp;nbsp;&lt;/b&gt;&lt;/td&gt;&lt;td&gt;&lt;b&gt;&amp;nbsp;Funcionality&amp;nbsp;&lt;/b&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&amp;nbsp;isalpha(c);&amp;nbsp;&lt;/td&gt;&lt;td&gt;&amp;nbsp;Returns true if c is a letter (a-z, A-Z)&amp;nbsp;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&amp;nbsp;isupper(c);&amp;nbsp;&lt;/td&gt;&lt;td&gt;&amp;nbsp;Returns true if c is an upper-case letter&amp;nbsp;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&amp;nbsp;islower(c);&amp;nbsp;&lt;/td&gt;&lt;td&gt;&amp;nbsp;Returns true if c is a lower-case letter&amp;nbsp;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&amp;nbsp;isdigit(c);&amp;nbsp;&lt;/td&gt;&lt;td&gt;&amp;nbsp;Returns trud if c is a number(0-9)&amp;nbsp;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&amp;nbsp;isspace(c);&amp;nbsp;&lt;/td&gt;&lt;td&gt;&amp;nbsp;Returns true if c is a white space character&amp;nbsp;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&amp;nbsp;ispunct(c);&amp;nbsp;&lt;/td&gt;&lt;td&gt;&amp;nbsp;Returns true if c is a punctuation mark&amp;nbsp;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&amp;nbsp;toupper(c);&amp;nbsp;&lt;/td&gt;&lt;td&gt;&amp;nbsp;If c is a lower-case letter a upper-case letter is returned, otherwise c is returned&amp;nbsp;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&amp;nbsp;tolower(c);&amp;nbsp;&lt;/td&gt;&lt;td&gt;&amp;nbsp;If c is a upper-case letter a lower-case letter is returned, otherwise c is returned&amp;nbsp;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;br /&gt;&lt;br /&gt;These can be used by adding #include &amp;lt;cctype&amp;gt; to the includes.&lt;br /&gt;&lt;br /&gt;However, the return type and the type of the parameter of these functions is &lt;span style="font-style: italic;"&gt;int&lt;/span&gt;. This can cause problems in some systems. A safe way to use the functions is always explicitly cast the parameter to &lt;span style="font-style: italic;"&gt;int&lt;/span&gt; with &lt;span style="font-style: italic;"&gt;static_cast&amp;lt;int&amp;gt;( c );&lt;/span&gt; The same can be done to the return type of the &lt;span style="font-style: italic;"&gt;toupper&lt;/span&gt; and &lt;span style="font-style: italic;"&gt;tolower&lt;/span&gt; to make sure the character value is correct.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;table width="100%"&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;&lt;p width="150px" align="left"&gt;&lt;a href="http://programming-eamon.blogspot.com/2009/08/character-string-type-string.html"&gt;Previous post&lt;/a&gt;&lt;/p&gt;&lt;/td&gt;&lt;td&gt;&lt;p width="100px" align="right"&gt;&lt;a href="http://programming-eamon.blogspot.com/2009/08/functions-parameter-passing-mechanisms.html"&gt;Next post&lt;/a&gt;&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;</description><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">0</thr:total></item><item><title>Character string type string</title><link>http://programming-eamon.blogspot.com/2009/08/character-string-type-string.html</link><category>C++</category><category>string</category><author>noreply@blogger.com (geteamon)</author><pubDate>Thu, 27 Aug 2009 22:22:00 +0300</pubDate><guid isPermaLink="false">tag:blogger.com,1999:blog-8130301155776000667.post-4605455738535954944</guid><description>&lt;span style="font-family:verdana;"&gt;Since words consist of sequences or strings of characters, there is naturally also the problem of storing and processing strings in C++. One of the solutions to the problem in C++ is the &lt;span style="font-style: italic;"&gt;&lt;a href="http://en.wikipedia.org/wiki/String_%28computer_science%29"&gt;string&lt;/a&gt;&lt;/span&gt; type. &lt;span style="font-style: italic;"&gt;string&lt;/span&gt; is part of the C++ standard library. When it is used #include &amp;lt;string&amp;gt; must be added at the beginning of the code file. After that string behaves in the same way as a basic data type:&lt;ul&gt;&lt;li&gt;Defining &lt;a href="http://en.wikipedia.org/wiki/Variable_%28programming%29"&gt;variables&lt;/a&gt;&lt;/li&gt;&lt;li&gt;Parameters and return types of &lt;a href="http://en.wikipedia.org/wiki/Function_%28computer_science%29"&gt;functions&lt;/a&gt;&lt;/li&gt;&lt;li&gt;Assignment and initialization from another string variable or &lt;a href="http://en.wikipedia.org/wiki/Literal_%28computer_science%29"&gt;literal&lt;/a&gt; with =&lt;/li&gt;&lt;li&gt;Printing with cout and reading input with cin&lt;/li&gt;&lt;li&gt;Comparison with ==, !=, &lt;=, &gt;=, &lt;&gt;&lt;/li&gt;&lt;/ul&gt;In addition, string has other useful properties. Let's take a closer look at the string type.&lt;br /&gt;&lt;br /&gt;A string variable is defined:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;string variable_name = "init_value";&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Any text inside the quotes can be assigned to a string variable. string is a special type that does not necessarily need the &lt;span style="font-style: italic;"&gt;"init_value"&lt;/span&gt; but it is usually initialized for consistency.&lt;br /&gt;&lt;br /&gt;Reading a string:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;int main( void )&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;{&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;    string name = "";&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;    cout &amp;lt;&amp;lt; "What's your name?" &amp;lt;&amp;lt; endl;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;    cin &amp;gt;&amp;gt; name;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;    cout &amp;lt;&amp;lt; "Hello" &amp;lt;&amp;lt; name &amp;lt;&amp;lt; endl;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;The output of the program:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;What's your name?&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;Harry Potter&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;Hello Harry&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;So cin stops reading the input when it encounters the first white space character. The entire line can be read by using the function &lt;span style="font-style: italic;"&gt;getline()&lt;/span&gt;:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;int main( void )&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;{&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;    string name = "";&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;    cout &amp;lt;&amp;lt; "What's your name?" &amp;lt;&amp;lt; endl;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;    getline( cin, name );&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;    cout &amp;lt;&amp;lt; "Hello" &amp;lt;&amp;lt; name &amp;lt;&amp;lt; endl;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Now the output will be:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;What's your name?&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;Harry Potter&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;Hello Harry Potter&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;getline&lt;/span&gt; always reads the entire line (including white spaces). It has two arguments, first the input stream (cin) and second which string you wish to write to.&lt;br /&gt;&lt;br /&gt;A string variable can contain as many characters as needed. string has a lot of functions of its own that can be used to get information about the string. The size of the string can be obtained with the &lt;span style="font-style: italic;"&gt;length()&lt;/span&gt; function. The function returns the amount of characters in the string.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;cout &amp;lt;&amp;lt; name.length();&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;would print 12 after the getline. The individual characters in a string can be accessed either with the &lt;span style="font-style: italic;"&gt;at()&lt;/span&gt; function or &lt;span style="font-style: italic;"&gt;with []&lt;/span&gt;.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;name.at( 1 ) = name[ 10 ];&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;There are several functions for finding substrings:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;name.find( "ot" );&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;name.find( "ot", 3 );&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;find()&lt;/span&gt; functions return the position of the substring &lt;span style="font-style: italic;"&gt;"ot"&lt;/span&gt; in the string &lt;span style="font-style: italic;"&gt;name&lt;/span&gt;. If the substring is not found, the function returns a special value &lt;span style="font-style: italic;"&gt;string::npos&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;A part of a stirng can be accessed with the &lt;span style="font-style: italic;"&gt;substr()&lt;/span&gt; function. On the other hand &lt;span style="font-style: italic;"&gt;replace()&lt;/span&gt; function lets you to write to a substring.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;name.substr( 0, 5 ); //equal Harry&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;name.replace( 0, 5, "Ron" ); //equals Ron Potter&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;A part of a string can be removed with &lt;span style="font-style: italic;"&gt;erase()&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;name.erase( 0, 4); // leaves just potter&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;append()&lt;/span&gt; adds a substring to the end of the string:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;name.append( " the wizard" );&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;The length of the string changes if needed. The size can also be changed explicitly with the &lt;span style="font-style: italic;"&gt;resize()&lt;/span&gt; function:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;name.resize( 6 ); //leaves again just potter&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Reference: &lt;a href="http://www.cplusplus.com/reference/string/string/"&gt;cplusplus&lt;/a&gt;, &lt;a href="http://anaturb.net/C/string_exapm.htm"&gt;anaturb&lt;/a&gt;, &lt;a href="http://www.cppreference.com/wiki/string/start"&gt;cppreference&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;table width="100%"&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;&lt;p width="150px" align="left"&gt;&lt;a href="http://programming-eamon.blogspot.com/2009/08/array.html"&gt;Previous post&lt;/a&gt;&lt;/p&gt;&lt;/td&gt;&lt;td&gt;&lt;p width="100px" align="right"&gt;&lt;a href="http://programming-eamon.blogspot.com/2009/08/function-to-deal-with-character.html"&gt;Next post&lt;/a&gt;&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;</description><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">0</thr:total></item><item><title>Array</title><link>http://programming-eamon.blogspot.com/2009/08/array.html</link><category>Array</category><category>C++</category><author>noreply@blogger.com (geteamon)</author><pubDate>Wed, 26 Aug 2009 18:47:00 +0300</pubDate><guid isPermaLink="false">tag:blogger.com,1999:blog-8130301155776000667.post-936463916449677380</guid><description>&lt;span style="font-family:verdana;"&gt;A variable of the type int or double can contain one value at a time. It would often be useful of the able to handle an aggregate of elements of the same type. An &lt;a href="http://en.wikipedia.org/wiki/Array_data_structure"&gt;array&lt;/a&gt; is a structured datatype that:&lt;ul&gt;&lt;li&gt;has a specified size&lt;/li&gt;&lt;li&gt;consists of data elements of the same type&lt;/li&gt;&lt;li&gt;the elements are identified by their position in the array&lt;/li&gt;&lt;/ul&gt;In C++ array variables are declared:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;type array_name[ capacity ];&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;The result is an array type &lt;a href="http://en.wikipedia.org/wiki/Variable_(programming)"&gt;variable&lt;/a&gt; named &lt;span style="font-style: italic;"&gt;array_name&lt;/span&gt; in which data elements of the type &lt;span style="font-style: italic;"&gt;type&lt;/span&gt; can be stored. The number of elements the array can contain is determined by &lt;span style="font-style: italic;"&gt;capacity&lt;/span&gt;. &lt;span style="font-style: italic;"&gt;type&lt;/span&gt; can be any defined type (predefined or programmer-defined). &lt;span style="font-style: italic;"&gt;capacity&lt;/span&gt; needs to be a constant or a literal. For example, an integer array with nine elements:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;int numbers[ 9 ];&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;An array can be seen as a row of numbered boxes, whose contents can be checked and changed. Every box fits one data element of the type &lt;span style="font-style: italic;"&gt;type&lt;/span&gt;. In C++ arrays are indexed from zero through size-1.&lt;br /&gt;&lt;br /&gt;int numbers[ 9 ] looks like this:&lt;br /&gt;&lt;br /&gt;numbers: &lt;table bgcolor="#ddeeee" border="2"&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;&lt;br /&gt;&lt;/td&gt;&lt;td&gt;&lt;br /&gt;&lt;/td&gt;&lt;td&gt;&lt;br /&gt;&lt;/td&gt;&lt;td&gt;&lt;br /&gt;&lt;/td&gt;&lt;td&gt;&lt;br /&gt;&lt;/td&gt;&lt;td&gt;&lt;br /&gt;&lt;/td&gt;&lt;td&gt;&lt;br /&gt;&lt;/td&gt;&lt;td&gt;&lt;br /&gt;&lt;/td&gt;&lt;td&gt;&lt;br /&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&amp;nbsp;0&amp;nbsp;&lt;/td&gt;&lt;td&gt;&amp;nbsp;1&amp;nbsp;&lt;/td&gt;&lt;td&gt;&amp;nbsp;2&amp;nbsp;&lt;/td&gt;&lt;td&gt;&amp;nbsp;3&amp;nbsp;&lt;/td&gt;&lt;td&gt;&amp;nbsp;4&amp;nbsp;&lt;/td&gt;&lt;td&gt;&amp;nbsp;5&amp;nbsp;&lt;/td&gt;&lt;td&gt;&amp;nbsp;6&amp;nbsp;&lt;/td&gt;&lt;td&gt;&amp;nbsp;7&amp;nbsp;&lt;/td&gt;&lt;td&gt;&amp;nbsp;8&amp;nbsp;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;br /&gt;&lt;br /&gt;the index of the last element is one smaller than the size of the array.&lt;br /&gt;&lt;br /&gt;An element at index i can be accessed using [ ]:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;numbers[8] = 2*numbers[0];&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;would give the last element the value of the first element times two. Any expression of the type integer can be used as an operand for the brackets.&lt;br /&gt;&lt;br /&gt;For example,&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;const int SIZE = 10;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;int maxValue( )&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;{&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;int array[ SIZE ];&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;int i = 0;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;int max = 0;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;while( i &lt;&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;cin &gt;&gt; array[ i ];&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;if( array[ i ] &gt; max )&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;max = array[ i ];&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;++i;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return max;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;No checking is done ot ensure that the indices stay within the range determined by the declaration of the array:&lt;/span&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;int index = 1000;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;numbers[ index ] = 10; // logical error!&lt;/span&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&lt;br /&gt;A &lt;a href="http://en.wikipedia.org/wiki/Compiler"&gt;compiler&lt;/a&gt; compiles the code above just fine. It is still incorrect because it refers to a non-existent element.&lt;br /&gt;&lt;br /&gt;Like all variables, an array needs to be initialized with some sensible values before it can be used. An array can be initialized at declaration:&lt;/span&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;int numbers[ 9 ] = {1, 2, 3, 4, 5, 6, 7};&lt;/span&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&lt;br /&gt;results in an array:&lt;br /&gt;&lt;br /&gt;numbers:&lt;table bgcolor="#ddeeee" border="2"&gt;&lt;tr&gt;&lt;td&gt;&amp;nbsp;1&amp;nbsp;&lt;/td&gt;&lt;td&gt;&amp;nbsp;2&amp;nbsp;&lt;/td&gt;&lt;td&gt;&amp;nbsp;3&amp;nbsp;&lt;/td&gt;&lt;td&gt;&amp;nbsp;4&amp;nbsp;&lt;/td&gt;&lt;td&gt;&amp;nbsp;5&amp;nbsp;&lt;/td&gt;&lt;td&gt;&amp;nbsp;6&amp;nbsp;&lt;/td&gt;&lt;td&gt;&amp;nbsp;7&amp;nbsp;&lt;/td&gt;&lt;td&gt;&amp;nbsp;0&amp;nbsp;&lt;/td&gt;&lt;td&gt;&amp;nbsp;0&amp;nbsp;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&amp;nbsp;0&lt;/td&gt;&lt;td&gt;&amp;nbsp;1&lt;/td&gt;&lt;td&gt;&amp;nbsp;2&lt;/td&gt;&lt;td&gt;&amp;nbsp;3&lt;/td&gt;&lt;td&gt;&amp;nbsp;4&lt;/td&gt;&lt;td&gt;&amp;nbsp;5&lt;/td&gt;&lt;td&gt;&amp;nbsp;6&lt;/td&gt;&lt;td&gt;&amp;nbsp;7&lt;/td&gt;&lt;td&gt;&amp;nbsp;8&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;br /&gt;&lt;br /&gt;If there are less elements at initialization than the size of the array can contain, the rest of the elements are initialized to zero.&lt;br /&gt;&lt;br /&gt;It is not necessary to tell the size of the array when initialized. It is automatically determined from the number of elements.&lt;br /&gt;&lt;/span&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;int numbers[ ] = {1, 2, 3, 4, 5, 6, 7, 8, 9};&lt;/span&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&lt;br /&gt;This is true only at initialization and cannot be used as a general method of handling array data. An array cannot be assigned another with =. Moreover, arrays cannot be compared with != and == operators. In array everything must be done one element at a time.&lt;br /&gt;&lt;br /&gt;Arrays are static &lt;a href="http://en.wikipedia.org/wiki/Data_structure"&gt;data structures&lt;/a&gt;. Their capacity cannot change during program execution. The capacity must always be fixed when the program is compiled:&lt;br /&gt;&lt;/span&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;int capacity = 0;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;cin &gt;&gt; capacity;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;int array[ capacity ]; // Error&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;results in a compilation error.&lt;/span&gt;&lt;span style="font-style: italic; color: rgb(0, 153, 0);"&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Reference: &lt;a href="http://www.google.com/url?sa=t&amp;source=web&amp;ct=res&amp;cd=1&amp;url=http%3A%2F%2Fwww.cplusplus.com%2Fdoc%2Ftutorial%2Farrays%2F&amp;ei=MluVSvy1L9PI-Qbe2vzyDQ&amp;rct=j&amp;q=C%2B%2B+array&amp;usg=AFQjCNGsxpld2a4Ab8KgoQXwON5CR2bEaw"&gt;cplusplus&lt;/a&gt;, &lt;a href="http://www.google.com/url?sa=t&amp;source=web&amp;ct=res&amp;cd=2&amp;url=http%3A%2F%2Fwww.codersource.net%2Fc%2B%2B_arrays_tutorial.html&amp;ei=MluVSvy1L9PI-Qbe2vzyDQ&amp;rct=j&amp;q=C%2B%2B+array&amp;usg=AFQjCNFaSTjvTEbF_8ctXOBN2cqAEPbIDg"&gt;codesource&lt;/a&gt;, &lt;a href="http://www.google.com/url?sa=t&amp;source=web&amp;ct=res&amp;cd=3&amp;url=http%3A%2F%2Fwww.fredosaurus.com%2Fnotes-cpp%2Farrayptr%2Farray-initialization.html&amp;ei=MluVSvy1L9PI-Qbe2vzyDQ&amp;rct=j&amp;q=C%2B%2B+array&amp;usg=AFQjCNGo6Df5uiR16jDTKw4irhG5huhIXg"&gt;fredosaurus&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;table width="100%"&gt;&lt;tr&gt;&lt;td&gt;&lt;p align="left" width="150px"&gt;&lt;a href="http://programming-eamon.blogspot.com/2009/08/comparing-real-numbers-with-and.html"&gt;Previous post&lt;/a&gt;&lt;/p&gt;&lt;/td&gt;&lt;td&gt;&lt;p align="right" width="100px"&gt;&lt;a href="http://programming-eamon.blogspot.com/2009/08/character-string-type-string.html"&gt;Next post&lt;/a&gt;&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;</description><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">0</thr:total></item></channel></rss>