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

<channel>
	<title>Coding Friends &#187; C / C++</title>
	<atom:link href="http://www.codingfriends.com/index.php/category/programming/c_and_cpp/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.codingfriends.com</link>
	<description>Coding Friends, place for developers.</description>
	<lastBuildDate>Sun, 04 Dec 2011 21:11:08 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Function vs Method</title>
		<link>http://www.codingfriends.com/index.php/2011/03/08/function-vs-method/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=function-vs-method</link>
		<comments>http://www.codingfriends.com/index.php/2011/03/08/function-vs-method/#comments</comments>
		<pubDate>Tue, 08 Mar 2011 23:01:46 +0000</pubDate>
		<dc:creator>genux</dc:creator>
				<category><![CDATA[C / C++]]></category>
		<category><![CDATA[function]]></category>
		<category><![CDATA[method]]></category>

		<guid isPermaLink="false">http://www.codingfriends.com/?p=1575</guid>
		<description><![CDATA[The main difference between a function and a method is that a function can live independently of the any instance of a class, where as a method sits within a class. That is about it, e.g. a function is int func&#40;int value&#41;; ... &#160; int main&#40;&#41; &#123; cout &#60;&#60; funct&#40;3&#41; &#60;&#60; endl; &#125; whereas a [...]]]></description>
			<content:encoded><![CDATA[<p>The main difference between a function and a method is that a function can live independently of the any instance of a class, where as a method sits within a class.</p>
<p>That is about it, e.g. a function is</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">int</span> func<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> value<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
...
&nbsp;
<span style="color: #0000ff;">int</span> main<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
  <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> funct<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">3</span><span style="color: #008000;">&#41;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>whereas a method has to live within a class</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">class</span> myclass
<span style="color: #008000;">&#123;</span>
    <span style="color: #0000ff;">int</span> myMethod<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> it<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
&nbsp;
....
&nbsp;
<span style="color: #0000ff;">int</span> main<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
   myclass theclass<span style="color: #008080;">;</span>
   <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> theclass.<span style="color: #007788;">myMethod</span><span style="color: #008000;">&#40;</span><span style="color: #0000dd;">4</span><span style="color: #008000;">&#41;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.codingfriends.com/index.php/2011/03/08/function-vs-method/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Strings</title>
		<link>http://www.codingfriends.com/index.php/2011/03/08/strings/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=strings</link>
		<comments>http://www.codingfriends.com/index.php/2011/03/08/strings/#comments</comments>
		<pubDate>Tue, 08 Mar 2011 22:59:47 +0000</pubDate>
		<dc:creator>genux</dc:creator>
				<category><![CDATA[C / C++]]></category>

		<guid isPermaLink="false">http://www.codingfriends.com/?p=1572</guid>
		<description><![CDATA[Just a small thing encase anyone else finds this interesting as such.. but you cannot add a set of &#8221; &#8221; strings together in c++ without declaring one being a string.. for example cout &#60;&#60; &#34;hi&#34; + &#34;bye&#34; &#60;&#60; endl; the error would be error: invalid operands of types ‘const char &#91;3&#93;’ and ‘const char [...]]]></description>
			<content:encoded><![CDATA[<p>Just a small thing encase anyone else finds this interesting as such.. but you cannot add a set of &#8221; &#8221; strings together in c++ without declaring one being a string.. for example</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;hi&quot;</span> <span style="color: #000040;">+</span> <span style="color: #FF0000;">&quot;bye&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span></pre></div></div>

<p>the error would be</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;">error<span style="color: #008080;">:</span> invalid operands of types ‘<span style="color: #0000ff;">const</span> <span style="color: #0000ff;">char</span> <span style="color: #008000;">&#91;</span><span style="color: #0000dd;">3</span><span style="color: #008000;">&#93;</span>’ and ‘<span style="color: #0000ff;">const</span> <span style="color: #0000ff;">char</span> <span style="color: #008000;">&#91;</span><span style="color: #0000dd;">4</span><span style="color: #008000;">&#93;</span>’ to binary ‘operator<span style="color: #000040;">+</span>’</pre></div></div>

<p>because c++ treats them both as char[] variables and not as a string, so you need to wrap one up into a string for it to work</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> string<span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;hi&quot;</span><span style="color: #008000;">&#41;</span> <span style="color: #000040;">+</span> <span style="color: #FF0000;">&quot;bye&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.codingfriends.com/index.php/2011/03/08/strings/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Why -&gt; and not *name.type</title>
		<link>http://www.codingfriends.com/index.php/2011/03/08/why-and-not-name-type/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=why-and-not-name-type</link>
		<comments>http://www.codingfriends.com/index.php/2011/03/08/why-and-not-name-type/#comments</comments>
		<pubDate>Tue, 08 Mar 2011 22:55:42 +0000</pubDate>
		<dc:creator>genux</dc:creator>
				<category><![CDATA[C / C++]]></category>

		<guid isPermaLink="false">http://www.codingfriends.com/?p=1568</guid>
		<description><![CDATA[When you are coding with pointers in c++ and you want to access the function/variable from that pointer deferenced, how come you cannot use something like below. struct typeT&#123; int value1; &#125;; &#160; typeT* tt = new typeT; *tt.value1; it is because the access element of the variable tt is higher in the compiler and [...]]]></description>
			<content:encoded><![CDATA[<p>When you are coding with pointers in c++ and you want to access the function/variable from that pointer deferenced, how come you cannot use something like below.</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">struct</span> typeT<span style="color: #008000;">&#123;</span>
   <span style="color: #0000ff;">int</span> value1<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
&nbsp;
typeT<span style="color: #000040;">*</span> tt <span style="color: #000080;">=</span> <span style="color: #0000dd;">new</span> typeT<span style="color: #008080;">;</span>
<span style="color: #000040;">*</span>tt.<span style="color: #007788;">value1</span><span style="color: #008080;">;</span></pre></div></div>

<p>it is because the access element of the variable tt is higher in the compiler and thus it tries to equate</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;">tt.<span style="color: #007788;">value1</span></pre></div></div>

<p>first, which is not a good thing because the tt has not been de-referenced and thus it is just a memory address pointer to the actual object. so you need to do</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #008000;">&#40;</span><span style="color: #000040;">*</span>tt<span style="color: #008000;">&#41;</span>.<span style="color: #007788;">value1</span></pre></div></div>

<p>because like in maths the () will be equated first and then the access element part &#8220;.&#8221; and thus to make it easier</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;">tt<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>value1</pre></div></div>

<p>will change to</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #008000;">&#40;</span><span style="color: #000040;">*</span>tt<span style="color: #008000;">&#41;</span>.<span style="color: #007788;">value1</span></pre></div></div>

<p>within the compiler.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.codingfriends.com/index.php/2011/03/08/why-and-not-name-type/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>CS107 &#8211; Assignment 2 &#8211; Six degrees</title>
		<link>http://www.codingfriends.com/index.php/2010/08/25/cs107-assignment-2-six-degrees/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=cs107-assignment-2-six-degrees</link>
		<comments>http://www.codingfriends.com/index.php/2010/08/25/cs107-assignment-2-six-degrees/#comments</comments>
		<pubDate>Wed, 25 Aug 2010 19:54:32 +0000</pubDate>
		<dc:creator>genux</dc:creator>
				<category><![CDATA[C / C++]]></category>
		<category><![CDATA[Assignment 2]]></category>
		<category><![CDATA[CS107]]></category>

		<guid isPermaLink="false">http://www.codingfriends.com/?p=1167</guid>
		<description><![CDATA[This is assignment 2 from a older CS107 course, because the latest course work is held on a UNIX server which in turn you need access to!!.. kinder does not help when you do not have access too!!.. Here is where I got the files from cs107 link, there is a zip file at the [...]]]></description>
			<content:encoded><![CDATA[<p><span id="zipfile"><a href="http://www.codingfriends.com/wp-content/uploads/2010/08/09-Assignment-2-Six-Degrees.pdf"></a></span>This is assignment 2 from a older <a href="https://courseware.stanford.edu/pg/courses/82252">CS107</a> course, because the latest course work is held on a UNIX server which in turn you need access to!!.. kinder does not help when you do not have access too!!..  Here is where I got the files from <a href="http://see.stanford.edu/see/courseinfo.aspx?coll=2d712634-2bf1-4b55-9a3a-ca9d470755ee">cs107</a> link, there is a zip file at the bottom of that page which has all of the assignments and handouts etc.</p>
<p>I have included the PDF of the assignment in the above file link, but since the actual assignment includes a few massive files you can download them from the CS107 link of the whole course work that I am using from the <a href="http://see.stanford.edu/see/courseinfo.aspx?coll=2d712634-2bf1-4b55-9a3a-ca9d470755ee">cs107</a> link.</p>
<p>As a note, within my linux setup, I need to setup the environment variable to tell the program to use a Linux <a href="http://en.wikipedia.org/wiki/Endianness">little endian</a> by</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #007800;">OSTYPE</span>=linux</pre></div></div>

<p>Basically the assignment is to load in cast/movie details (which the base code from the assignment already does for use) and then we are needed to pull out the data from the structure of the file, the structure of the actors is (as taken from the PDF file)</p>
<ol>
<li>The name of the actor is laid out character by character, as a normal null-terminated C-string. If the length of the actor’s name is even, then the string is<br />
padded with an extra &#8216;\0&#8242; so that the total number of bytes dedicated to the name is always an even number. The information that follows the name is most<br />
easily interpreted as a short integer, and virtually all hardware constrains any address manipulated as a short * to be even.	</li>
<li>The number of movies in which the actor has appeared, expressed as a two-byte short. (Some people have been in more than 255 movies, so a single byte just<br />
isn’t enough.) If the number of bytes dedicated to the actor’s name (always even) and the short (always 2) isn’t a multiple of four, then two additional &#8216;\0&#8242;’s<br />
appear after the two bytes storing the number of movies. This padding is conditionally done so that the 4-byte integers than follow sit at addresses that are<br />
multiples of four.
</li>
<li>An array of offsets into the movieFile image, where each offset identifies one of the actor’s films.</li>
</ol>
<p>with the base of the actors information containing a 4 byte integer value of how many actors are in the block of data and then the following values are in 4 bytes again with the offsets into the block of where the actual data is stored.</p>
<p>So the method that you need to write is to pull in that data from the actorInfo block of memory, and here is my version of it, the method is getCredits and takes the cast member (player) as the method will insert all of the films that cast as been in.</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">bool</span> imdb<span style="color: #008080;">::</span><span style="color: #007788;">getCredits</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> string<span style="color: #000040;">&amp;</span> player, vector<span style="color: #000080;">&lt;</span>film<span style="color: #000080;">&gt;</span><span style="color: #000040;">&amp;</span> films<span style="color: #008000;">&#41;</span> <span style="color: #0000ff;">const</span>
<span style="color: #008000;">&#123;</span>
    <span style="color: #0000ff;">int</span> numberOfActors,data<span style="color: #008080;">;</span>
    <span style="color: #0000ff;">char</span> <span style="color: #000040;">*</span>actorName<span style="color: #008080;">;</span>
    <span style="color: #0000dd;">memcpy</span><span style="color: #008000;">&#40;</span><span style="color: #000040;">&amp;</span>numberOfActors,<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span><span style="color: #000040;">*</span><span style="color: #008000;">&#41;</span>actorInfo.<span style="color: #007788;">fileMap</span>, <span style="color: #0000dd;">4</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    <span style="color: #666666;">// loop though the number of actors</span>
    <span style="color: #0000ff;">for</span> <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> i <span style="color: #000080;">=</span><span style="color: #0000dd;">2</span><span style="color: #008080;">;</span> i <span style="color: #000080;">&lt;</span> numberOfActors<span style="color: #008080;">;</span> i<span style="color: #000040;">++</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        <span style="color: #0000dd;">memcpy</span><span style="color: #008000;">&#40;</span><span style="color: #000040;">&amp;</span>data, <span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span><span style="color: #000040;">*</span><span style="color: #008000;">&#41;</span>actorInfo.<span style="color: #007788;">fileMap</span><span style="color: #008000;">&#41;</span><span style="color: #000040;">+</span>i,<span style="color: #0000dd;">4</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
        actorName <span style="color: #000080;">=</span> <span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">char</span><span style="color: #000040;">*</span><span style="color: #008000;">&#41;</span>actorInfo.<span style="color: #007788;">fileMap</span> <span style="color: #000040;">+</span> <span style="color: #008000;">&#40;</span>data<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
        <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span><span style="color: #0000dd;">strcmp</span><span style="color: #008000;">&#40;</span>actorName,player.<span style="color: #007788;">c_str</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #000080;">==</span><span style="color: #0000dd;">0</span><span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            <span style="color: #666666;">// find the padding lengths</span>
            <span style="color: #0000ff;">int</span> actorNameLength <span style="color: #000080;">=</span> <span style="color: #0000dd;">strlen</span><span style="color: #008000;">&#40;</span>actorName<span style="color: #008000;">&#41;</span><span style="color: #000040;">+</span><span style="color: #0000dd;">1</span><span style="color: #008080;">;</span>
            <span style="color: #0000ff;">int</span> movieNumberPad <span style="color: #000080;">=</span> actorNameLength <span style="color: #000040;">%</span> <span style="color: #0000dd;">2</span><span style="color: #008080;">;</span>
            <span style="color: #0000ff;">int</span> fourOffset <span style="color: #000080;">=</span> <span style="color: #008000;">&#40;</span>actorNameLength <span style="color: #000040;">+</span> movieNumberPad <span style="color: #000040;">+</span> <span style="color: #0000dd;">2</span><span style="color: #008000;">&#41;</span> <span style="color: #000040;">%</span> <span style="color: #0000dd;">4</span><span style="color: #008080;">;</span>
            <span style="color: #0000ff;">short</span> numberMovies<span style="color: #008080;">;</span>
            <span style="color: #0000dd;">memcpy</span><span style="color: #008000;">&#40;</span><span style="color: #000040;">&amp;</span>numberMovies,<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">short</span><span style="color: #000040;">*</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">char</span><span style="color: #000040;">*</span><span style="color: #008000;">&#41;</span>actorInfo.<span style="color: #007788;">fileMap</span> <span style="color: #000040;">+</span> data <span style="color: #000040;">+</span> actorNameLength <span style="color: #000040;">+</span> movieNumberPad<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>,<span style="color: #0000dd;">2</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
            <span style="color: #0000ff;">for</span> <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> j<span style="color: #000080;">=</span><span style="color: #0000dd;">0</span><span style="color: #008080;">;</span> j <span style="color: #000080;">&lt;</span> numberMovies<span style="color: #008080;">;</span> j<span style="color: #000040;">++</span><span style="color: #008000;">&#41;</span>
            <span style="color: #008000;">&#123;</span>
                <span style="color: #0000ff;">int</span> movieOffset<span style="color: #008080;">;</span>
                film theFilm<span style="color: #008080;">;</span>
                <span style="color: #0000dd;">memcpy</span><span style="color: #008000;">&#40;</span><span style="color: #000040;">&amp;</span>movieOffset, <span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span><span style="color: #000040;">*</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">char</span><span style="color: #000040;">*</span><span style="color: #008000;">&#41;</span>actorInfo.<span style="color: #007788;">fileMap</span> <span style="color: #000040;">+</span> data <span style="color: #000040;">+</span> actorNameLength <span style="color: #000040;">+</span> movieNumberPad <span style="color: #000040;">+</span> <span style="color: #008000;">&#40;</span>j<span style="color: #000040;">*</span><span style="color: #0000dd;">4</span><span style="color: #008000;">&#41;</span> <span style="color: #000040;">+</span> fourOffset <span style="color: #000040;">+</span> <span style="color: #0000dd;">2</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>,<span style="color: #0000dd;">4</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
                theFilm.<span style="color: #007788;">title</span> <span style="color: #000080;">=</span> <span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">char</span><span style="color: #000040;">*</span><span style="color: #008000;">&#41;</span>movieInfo.<span style="color: #007788;">fileMap</span> <span style="color: #000040;">+</span> movieOffset<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
                theFilm.<span style="color: #007788;">year</span> <span style="color: #000080;">=</span> <span style="color: #0000dd;">1900</span> <span style="color: #000040;">+</span> <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">char</span><span style="color: #000040;">*</span><span style="color: #008000;">&#41;</span>movieInfo.<span style="color: #007788;">fileMap</span> <span style="color: #000040;">+</span> movieOffset <span style="color: #000040;">+</span> theFilm.<span style="color: #007788;">title</span>.<span style="color: #007788;">length</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #000040;">+</span> <span style="color: #0000dd;">1</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#91;</span><span style="color: #0000dd;">0</span><span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span>
                films.<span style="color: #007788;">push_back</span><span style="color: #008000;">&#40;</span>theFilm<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
            <span style="color: #008000;">&#125;</span>
            <span style="color: #0000ff;">return</span> <span style="color: #0000ff;">true</span><span style="color: #008080;">;</span>
        <span style="color: #008000;">&#125;</span>
    <span style="color: #008000;">&#125;</span>
    <span style="color: #0000ff;">return</span> <span style="color: #0000ff;">false</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>The block of memory for the movie block is similar to the actor block and as taken from the PDF attached file here is the details of that block of memory,</p>
<ol>
<li>The title of the movie, terminated by a &#8216;\0&#8242; so the character array behaves as a normal C-string.</li>
<li>The year the film was released, expressed as a single byte. This byte stores the year – 1900. Since Hollywood is less than 28 years old, it was fine to just store the year as a delta from 1900. If the total number of bytes used to encode the name and year of the movie is odd, then an extra &#8216;\0&#8242; sits in between the one-byte year and the data that follows.</li>
<li>A two-byte short storing the number of actors appearing in the film, padded with two additional bytes of zeroes if needed.	</li>
<li>An array of four-byte integer offsets, where each integer offset identifies one of the actors in the actorFile. The number of offsets here is, of course, equal to<br />
the short integer read during step 3</li>
</ol>
<p>And once again like the actor block there is a 4 byte integer value that stores how many movies within the block of memory with the next 4 bytes storing the offset into that block of memory, here is my implementation of the method getCast that will take a movie as a parameter and find all of the cast (players) and return that vector list.</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">bool</span> imdb<span style="color: #008080;">::</span><span style="color: #007788;">getCast</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> film<span style="color: #000040;">&amp;</span> movie, vector<span style="color: #000080;">&lt;</span>string<span style="color: #000080;">&gt;</span><span style="color: #000040;">&amp;</span> players<span style="color: #008000;">&#41;</span> <span style="color: #0000ff;">const</span>
<span style="color: #008000;">&#123;</span>
    <span style="color: #0000ff;">int</span> numberOfMovies, data<span style="color: #008080;">;</span>
    film theFilm<span style="color: #008080;">;</span>
    <span style="color: #0000ff;">char</span> <span style="color: #000040;">*</span>movieName<span style="color: #008080;">;</span>
    <span style="color: #0000dd;">memcpy</span><span style="color: #008000;">&#40;</span><span style="color: #000040;">&amp;</span>numberOfMovies, <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span><span style="color: #000040;">*</span><span style="color: #008000;">&#41;</span>movieInfo.<span style="color: #007788;">fileMap</span>, <span style="color: #0000dd;">4</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    <span style="color: #0000ff;">for</span> <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> i <span style="color: #000080;">=</span> <span style="color: #0000dd;">1</span><span style="color: #008080;">;</span> i <span style="color: #000080;">&lt;</span> numberOfMovies<span style="color: #008080;">;</span> i<span style="color: #000040;">++</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        <span style="color: #0000dd;">memcpy</span><span style="color: #008000;">&#40;</span><span style="color: #000040;">&amp;</span>data, <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span><span style="color: #000040;">*</span><span style="color: #008000;">&#41;</span>movieInfo.<span style="color: #007788;">fileMap</span><span style="color: #000040;">+</span>i,<span style="color: #0000dd;">4</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
        movieName <span style="color: #000080;">=</span> <span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">char</span><span style="color: #000040;">*</span><span style="color: #008000;">&#41;</span>movieInfo.<span style="color: #007788;">fileMap</span> <span style="color: #000040;">+</span> data<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
        theFilm.<span style="color: #007788;">title</span> <span style="color: #000080;">=</span> movieName<span style="color: #008080;">;</span>
        theFilm.<span style="color: #007788;">year</span> <span style="color: #000080;">=</span> <span style="color: #0000dd;">1900</span> <span style="color: #000040;">+</span> <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">char</span><span style="color: #000040;">*</span><span style="color: #008000;">&#41;</span>movieInfo.<span style="color: #007788;">fileMap</span> <span style="color: #000040;">+</span> data <span style="color: #000040;">+</span> theFilm.<span style="color: #007788;">title</span>.<span style="color: #007788;">length</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #000040;">+</span> <span style="color: #0000dd;">1</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#91;</span><span style="color: #0000dd;">0</span><span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span>
        <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>movie <span style="color: #000080;">==</span> theFilm<span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            <span style="color: #0000ff;">int</span> paddingFirst <span style="color: #000080;">=</span> theFilm.<span style="color: #007788;">title</span>.<span style="color: #007788;">length</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #000040;">%</span> <span style="color: #0000dd;">2</span><span style="color: #008080;">;</span>
            <span style="color: #666666;">// paddingSecond = paddingfirst + length of title + 2 (year + \0) and then + 2 for the actors number</span>
            <span style="color: #0000ff;">int</span> paddingSecond <span style="color: #000080;">=</span> <span style="color: #008000;">&#40;</span>paddingFirst <span style="color: #000040;">+</span> theFilm.<span style="color: #007788;">title</span>.<span style="color: #007788;">length</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #000040;">+</span><span style="color: #0000dd;">2</span> <span style="color: #000040;">+</span> <span style="color: #0000dd;">2</span><span style="color: #008000;">&#41;</span> <span style="color: #000040;">%</span><span style="color:#800080;">4</span><span style="color: #008080;">;</span>
            <span style="color: #0000ff;">short</span> numberOfActors<span style="color: #008080;">;</span>
            <span style="color: #0000dd;">memcpy</span><span style="color: #008000;">&#40;</span><span style="color: #000040;">&amp;</span>numberOfActors, <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">short</span><span style="color: #000040;">*</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">char</span><span style="color: #000040;">*</span><span style="color: #008000;">&#41;</span>movieInfo.<span style="color: #007788;">fileMap</span> <span style="color: #000040;">+</span> data <span style="color: #000040;">+</span> theFilm.<span style="color: #007788;">title</span>.<span style="color: #007788;">length</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #000040;">+</span> <span style="color: #0000dd;">2</span> <span style="color: #000040;">+</span> paddingFirst<span style="color: #008000;">&#41;</span>,<span style="color: #0000dd;">2</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
            <span style="color: #666666;">// get the actors offsets and insert into the players list</span>
            <span style="color: #0000ff;">for</span> <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> k <span style="color: #000080;">=</span><span style="color: #0000dd;">0</span><span style="color: #008080;">;</span> k <span style="color: #000080;">&lt;</span> numberOfActors<span style="color: #008080;">;</span> k<span style="color: #000040;">++</span><span style="color: #008000;">&#41;</span>
            <span style="color: #008000;">&#123;</span>
                <span style="color: #0000ff;">int</span> offset<span style="color: #008080;">;</span>
                <span style="color: #0000dd;">memcpy</span><span style="color: #008000;">&#40;</span><span style="color: #000040;">&amp;</span>offset, <span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span><span style="color: #000040;">*</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">char</span><span style="color: #000040;">*</span><span style="color: #008000;">&#41;</span>movieInfo.<span style="color: #007788;">fileMap</span> <span style="color: #000040;">+</span> data <span style="color: #000040;">+</span> <span style="color: #0000dd;">strlen</span><span style="color: #008000;">&#40;</span>movieName<span style="color: #008000;">&#41;</span> <span style="color: #000040;">+</span> <span style="color: #0000dd;">2</span> <span style="color: #000040;">+</span> paddingFirst <span style="color: #000040;">+</span> <span style="color: #0000dd;">2</span> <span style="color: #000040;">+</span> paddingSecond <span style="color: #000040;">+</span> <span style="color: #008000;">&#40;</span>k<span style="color: #000040;">*</span><span style="color: #0000dd;">4</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>, <span style="color: #0000dd;">4</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
                players.<span style="color: #007788;">push_back</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">char</span><span style="color: #000040;">*</span><span style="color: #008000;">&#41;</span>actorInfo.<span style="color: #007788;">fileMap</span> <span style="color: #000040;">+</span> offset<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
            <span style="color: #008000;">&#125;</span>
            <span style="color: #0000ff;">return</span> <span style="color: #0000ff;">true</span><span style="color: #008080;">;</span>
        <span style="color: #008000;">&#125;</span>
    <span style="color: #008000;">&#125;</span>
    <span style="color: #0000ff;">return</span> <span style="color: #0000ff;">false</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>After you have sorted out reading in the information you have to find out if there is a link from actor1 to another actor2 (six degrees) with linking them together the cast that they have been with with the films that they have stared in, so here is my breath search setup as taken from the assignment.</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #ff0000; font-style: italic;">/**
  * @param actor1 - first actor to look for
  * @param actor2 - second actor to look for to link to the first
  * @param db - the imdb database loaded
  */</span>
<span style="color: #0000ff;">static</span> <span style="color: #0000ff;">bool</span> generateShortestPath<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> string<span style="color: #000040;">&amp;</span> actor1, <span style="color: #0000ff;">const</span> string<span style="color: #000040;">&amp;</span> actor2, <span style="color: #0000ff;">const</span> imdb<span style="color: #000040;">&amp;</span> db<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    list<span style="color: #000080;">&lt;</span>path<span style="color: #000080;">&gt;</span> partialPaths<span style="color: #008080;">;</span>
    set<span style="color: #000080;">&lt;</span>string<span style="color: #000080;">&gt;</span> previouslySeenActors<span style="color: #008080;">;</span>
    set<span style="color: #000080;">&lt;</span>film<span style="color: #000080;">&gt;</span> previouslySeenFilms<span style="color: #008080;">;</span>
&nbsp;
    path actorsFirst<span style="color: #008000;">&#40;</span>actor1<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    partialPaths.<span style="color: #007788;">push_front</span><span style="color: #008000;">&#40;</span>actorsFirst<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
    <span style="color: #0000ff;">while</span> <span style="color: #008000;">&#40;</span><span style="color: #000040;">!</span>partialPaths.<span style="color: #007788;">empty</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #000040;">&amp;&amp;</span> partialPaths.<span style="color: #007788;">front</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>.<span style="color: #007788;">getLength</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #000080;">&lt;=</span><span style="color: #0000dd;">5</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        path frontPath <span style="color: #000080;">=</span> <span style="color: #008000;">&#40;</span>path<span style="color: #008000;">&#41;</span>partialPaths.<span style="color: #007788;">front</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
        partialPaths.<span style="color: #007788;">pop_front</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
        vector<span style="color: #000080;">&lt;</span>film<span style="color: #000080;">&gt;</span> actorsFilms<span style="color: #008080;">;</span>
        db.<span style="color: #007788;">getCredits</span><span style="color: #008000;">&#40;</span>frontPath.<span style="color: #007788;">getLastPlayer</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>, actorsFilms<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
        <span style="color: #0000ff;">for</span> <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> filmI<span style="color: #000080;">=</span><span style="color: #0000dd;">0</span><span style="color: #008080;">;</span> filmI <span style="color: #000080;">&lt;</span> actorsFilms.<span style="color: #007788;">size</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> filmI<span style="color: #000040;">++</span><span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            film filmData<span style="color: #008080;">;</span>
            filmData <span style="color: #000080;">=</span> actorsFilms.<span style="color: #007788;">at</span><span style="color: #008000;">&#40;</span>filmI<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
            <span style="color: #666666;">// not seen the film before</span>
            <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>previouslySeenFilms.<span style="color: #007788;">find</span><span style="color: #008000;">&#40;</span>filmData<span style="color: #008000;">&#41;</span> <span style="color: #000080;">==</span> previouslySeenFilms.<span style="color: #007788;">end</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
            <span style="color: #008000;">&#123;</span>
                previouslySeenFilms.<span style="color: #007788;">insert</span><span style="color: #008000;">&#40;</span>filmData<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
                vector<span style="color: #000080;">&lt;</span>string<span style="color: #000080;">&gt;</span> movieCast<span style="color: #008080;">;</span>
                db.<span style="color: #007788;">getCast</span><span style="color: #008000;">&#40;</span>filmData, movieCast<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
                <span style="color: #0000ff;">for</span> <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> castI <span style="color: #000080;">=</span><span style="color: #0000dd;">0</span><span style="color: #008080;">;</span> castI <span style="color: #000080;">&lt;</span> movieCast.<span style="color: #007788;">size</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> castI<span style="color: #000040;">++</span><span style="color: #008000;">&#41;</span>
                <span style="color: #008000;">&#123;</span>
                    <span style="color: #666666;">// if not already seen</span>
                    <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>previouslySeenActors.<span style="color: #007788;">find</span><span style="color: #008000;">&#40;</span>movieCast.<span style="color: #007788;">at</span><span style="color: #008000;">&#40;</span>castI<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span> <span style="color: #000080;">==</span> previouslySeenActors.<span style="color: #007788;">end</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
                    <span style="color: #008000;">&#123;</span>
                        previouslySeenActors.<span style="color: #007788;">insert</span><span style="color: #008000;">&#40;</span>movieCast.<span style="color: #007788;">at</span><span style="color: #008000;">&#40;</span>castI<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
                        path newfrontPath <span style="color: #000080;">=</span> frontPath<span style="color: #008080;">;</span>
                        newfrontPath.<span style="color: #007788;">addConnection</span><span style="color: #008000;">&#40;</span>filmData, movieCast.<span style="color: #007788;">at</span><span style="color: #008000;">&#40;</span>castI<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
                        <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>movieCast.<span style="color: #007788;">at</span><span style="color: #008000;">&#40;</span>castI<span style="color: #008000;">&#41;</span> <span style="color: #000080;">==</span> actor2<span style="color: #008000;">&#41;</span>
                        <span style="color: #008000;">&#123;</span>
                            <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> newfrontPath <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
                            <span style="color: #0000ff;">return</span> <span style="color: #0000ff;">true</span><span style="color: #008080;">;</span>
                        <span style="color: #008000;">&#125;</span> <span style="color: #0000ff;">else</span>
                            partialPaths.<span style="color: #007788;">push_back</span><span style="color: #008000;">&#40;</span>newfrontPath<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
                    <span style="color: #008000;">&#125;</span>
                <span style="color: #008000;">&#125;</span>
            <span style="color: #008000;">&#125;</span>
        <span style="color: #008000;">&#125;</span>
    <span style="color: #008000;">&#125;</span>
    <span style="color: #0000ff;">return</span> <span style="color: #0000ff;">false</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>You can do some speed ups from the assignments PDF file, but they are mainly the icing on the cake and I thought that I would assignment 3 first, so if I do the extras for this I will update this post.</p>
<p>But this was a fun assignment, because of the memory block and finding out the data within it.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.codingfriends.com/index.php/2010/08/25/cs107-assignment-2-six-degrees/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>struct &#8211; setup and memory locations</title>
		<link>http://www.codingfriends.com/index.php/2010/07/23/struct-setup-and-memory-locations/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=struct-setup-and-memory-locations</link>
		<comments>http://www.codingfriends.com/index.php/2010/07/23/struct-setup-and-memory-locations/#comments</comments>
		<pubDate>Fri, 23 Jul 2010 09:47:17 +0000</pubDate>
		<dc:creator>genux</dc:creator>
				<category><![CDATA[C / C++]]></category>
		<category><![CDATA[memcpy]]></category>
		<category><![CDATA[struct]]></category>

		<guid isPermaLink="false">http://www.codingfriends.com/?p=1109</guid>
		<description><![CDATA[When you setup a struct within c++, it is kinder like having a array of data and if you want to you can access the internal parts by using some memory pointer location fun!. Lets say that you have a struct of struct intvalue &#123; int a; int b; &#125;; Just to say that since [...]]]></description>
			<content:encoded><![CDATA[<p>When you setup a struct within c++, it is kinder like having a array of data and if you want to you can access the internal parts by using some memory pointer location fun!.</p>
<p>Lets say that you have a struct of</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">struct</span> intvalue <span style="color: #008000;">&#123;</span>
    <span style="color: #0000ff;">int</span> a<span style="color: #008080;">;</span>
    <span style="color: #0000ff;">int</span> b<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span></pre></div></div>

<p>Just to say that since I am using int(eger) values so that is what I am incrementing by in the pointer arithmetic which is why I am casting the pointer to a integer value.</p>
<p>So lets say that we create a variable of intvalue and setup the values as below</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;">intvalue testvalue<span style="color: #008080;">;</span>
testvalue.<span style="color: #007788;">a</span> <span style="color: #000080;">=</span> <span style="color: #0000dd;">4</span><span style="color: #008080;">;</span>
testvalue.<span style="color: #007788;">b</span> <span style="color: #000080;">=</span> <span style="color: #0000dd;">5</span><span style="color: #008080;">;</span></pre></div></div>

<p>We can then pull out the value of a or b, but using memcpy and just outputting to a int(eger) variable as below, the key is the ((int*)&#038;testvalue)+1, this will first convert the testvalue variable to a pointer to memory location and then (int*) casts that pointer to a int pointer, because internally that is what it is, and then just add 1 to it, which points to the second value ( in this case the value of b which is 5)</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;">    <span style="color: #0000ff;">int</span> avalue<span style="color: #008080;">;</span>
    <span style="color: #666666;">// convert to a int pointer type and then add one to it (to the next array element as such).</span>
    <span style="color: #0000dd;">memcpy</span><span style="color: #008000;">&#40;</span><span style="color: #000040;">&amp;</span>avalue, <span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span><span style="color: #000040;">*</span><span style="color: #008000;">&#41;</span><span style="color: #000040;">&amp;</span>testvalue<span style="color: #008000;">&#41;</span><span style="color: #000040;">+</span><span style="color: #0000dd;">1</span>,<span style="color: #0000dd;">sizeof</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;a value (or is it the b value :) ) &quot;</span> <span style="color: #000080;">&lt;&lt;</span> avalue <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span></pre></div></div>

<p>The output would be</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">a value <span style="color: #7a0874; font-weight: bold;">&#40;</span>or is it the b value :<span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #000000;">5</span></pre></div></div>

<p>because I am pointing to the second value which is b and thus 5 <img src='http://www.codingfriends.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> .</p>
<p>Of course if you just wanted the value of first int (the value of a in this case) you do not add the 1 to the memory location, for example</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;">    <span style="color: #0000dd;">memcpy</span><span style="color: #008000;">&#40;</span><span style="color: #000040;">&amp;</span>avalue, <span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span><span style="color: #000040;">*</span><span style="color: #008000;">&#41;</span><span style="color: #000040;">&amp;</span>testvalue<span style="color: #008000;">&#41;</span>,<span style="color: #0000dd;">sizeof</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></pre></div></div>

<p>this time I am just converting the testvalue (casting) to a int pointer and thus pointing to the start of the struct and that is where the int a variable is living <img src='http://www.codingfriends.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> .</p>
]]></content:encoded>
			<wfw:commentRss>http://www.codingfriends.com/index.php/2010/07/23/struct-setup-and-memory-locations/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>CS107 &#8211; Assignment 1 &#8211; Context Free Grammar &#8211; Random Sentence Generator</title>
		<link>http://www.codingfriends.com/index.php/2010/07/14/cs107-assignment-1-context-free-grammar-random-sentence-generator/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=cs107-assignment-1-context-free-grammar-random-sentence-generator</link>
		<comments>http://www.codingfriends.com/index.php/2010/07/14/cs107-assignment-1-context-free-grammar-random-sentence-generator/#comments</comments>
		<pubDate>Wed, 14 Jul 2010 14:55:13 +0000</pubDate>
		<dc:creator>genux</dc:creator>
				<category><![CDATA[C / C++]]></category>
		<category><![CDATA[Assignment 1]]></category>
		<category><![CDATA[context free grammar]]></category>
		<category><![CDATA[CS107]]></category>
		<category><![CDATA[random sentence generator]]></category>

		<guid isPermaLink="false">http://www.codingfriends.com/?p=1106</guid>
		<description><![CDATA[This is assignment 1 from a older CS107 course, because the latest course work is held on a UNIX server which in turn you need access to!!.. kinder does not help when you do not have access too!!.. Here is where I got the files from cs107 link, there is a zip file at the [...]]]></description>
			<content:encoded><![CDATA[<p><span id="zipfile"><a href="http://www.codingfriends.com/wp-content/uploads/2010/07/Assignment1.zip"></a></span>This is assignment 1 from a older <a href="https://courseware.stanford.edu/pg/courses/82252">CS107</a> course, because the latest course work is held on a UNIX server which in turn you need access to!!.. kinder does not help when you do not have access too!!..  Here is where I got the files from <a href="http://see.stanford.edu/see/courseinfo.aspx?coll=2d712634-2bf1-4b55-9a3a-ca9d470755ee">cs107</a> link, there is a zip file at the bottom of that page which has all of the assignments and handouts etc.</p>
<p>The basics of a context free grammar is (as taken from the PDF file attached which is the assignment 1 in more details), the tokens as such are the &lt;..&gt; parts of the block, so that you start at &lt;start&gt; and then you print out &#8220;The &#8221; and then goto the &lt;object&gt; block to then print out a randomly picked object to then print out the &lt;verb&gt; if there is any blocks within the object/verb then do them before heading back to the last word on the &lt;start&gt; block which is &#8220;tonight&#8221;.  That is it, it generates random text <img src='http://www.codingfriends.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> .</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;">The Poem grammar
<span style="color: #008000;">&#123;</span>
<span style="color: #000080;">&lt;</span>start<span style="color: #000080;">&gt;</span>
	The <span style="color: #000080;">&lt;</span>object<span style="color: #000080;">&gt;</span> <span style="color: #000080;">&lt;</span>verb<span style="color: #000080;">&gt;</span> tonight. <span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #008000;">&#123;</span>
<span style="color: #000080;">&lt;</span>object<span style="color: #000080;">&gt;</span>
	waves	<span style="color: #008080;">;</span>
	big yellow flowers <span style="color: #008080;">;</span>
	slugs <span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #008000;">&#123;</span>
<span style="color: #000080;">&lt;</span>verb<span style="color: #000080;">&gt;</span>
	sigh <span style="color: #000080;">&lt;</span>adverb<span style="color: #000080;">&gt;</span> <span style="color: #008080;">;</span>
	portend like <span style="color: #000080;">&lt;</span>object<span style="color: #000080;">&gt;</span> <span style="color: #008080;">;</span>
	die <span style="color: #000080;">&lt;</span>adverb<span style="color: #000080;">&gt;</span> <span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #008000;">&#123;</span>
<span style="color: #000080;">&lt;</span>adverb<span style="color: #000080;">&gt;</span>
	warily <span style="color: #008080;">;</span>
	grumpily <span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>So the assignment is mainly to get used to compiling up programs within a Linux/Unix environment, I was using <a href="http://qt.nokia.com/products/developer-tools/">qt-creator</a> IDE (which has placed some files within the zip file attached, but it does not effect the directory structure as such).  To compile on Linux/Unix as long as there is a Makefile you can just</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">make</span></pre></div></div>

<p>So, the assignment 1 base code reads in the textual files and all is required is to output the text within a random sentence and repeat 3 times, so the start of the code does the looping and calls the doRandomText function which requires as parameters the mapped (map<string, Definition>) grammar and where to start (<start>)</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;">  <span style="color: #0000ff;">for</span> <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> i <span style="color: #000080;">=</span><span style="color: #0000dd;">1</span><span style="color: #008080;">;</span>i <span style="color: #000080;">&lt;=</span> <span style="color: #0000dd;">3</span><span style="color: #008080;">;</span> i<span style="color: #000040;">++</span><span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
      <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Version #&quot;</span> <span style="color: #000080;">&lt;&lt;</span> i <span style="color: #000080;">&lt;&lt;</span> endl <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
      doRandomText<span style="color: #008000;">&#40;</span>grammar, <span style="color: #FF0000;">&quot;&lt;start&gt;&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
      <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> endl <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
  <span style="color: #008000;">&#125;</span>
  <span style="color: #0000ff;">return</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #666666;">// recursive loop in the text and produce randomly generated text</span>
<span style="color: #0000ff;">void</span> doRandomText<span style="color: #008000;">&#40;</span>map<span style="color: #000080;">&lt;</span>string, Definition<span style="color: #000080;">&gt;</span> theGrammar, string terminal<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    Definition def <span style="color: #000080;">=</span> theGrammar<span style="color: #008000;">&#91;</span>terminal<span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span>
    <span style="color: #0000dd;">assert</span><span style="color: #008000;">&#40;</span>def.<span style="color: #007788;">getNonterminal</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>.<span style="color: #007788;">size</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #000040;">!</span><span style="color: #000080;">=</span><span style="color: #0000dd;">0</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    Production prod <span style="color: #000080;">=</span> def.<span style="color: #007788;">getRandomProduction</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    <span style="color: #0000ff;">for</span> <span style="color: #008000;">&#40;</span>Production<span style="color: #008080;">::</span><span style="color: #007788;">iterator</span> prodIter <span style="color: #000080;">=</span> prod.<span style="color: #007788;">begin</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> prodIter <span style="color: #000040;">!</span><span style="color: #000080;">=</span> prod.<span style="color: #007788;">end</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> prodIter<span style="color: #000040;">++</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        string theText <span style="color: #000080;">=</span> <span style="color: #000040;">*</span>prodIter<span style="color: #008080;">;</span>
        <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>theText.<span style="color: #007788;">at</span><span style="color: #008000;">&#40;</span><span style="color: #0000dd;">0</span><span style="color: #008000;">&#41;</span><span style="color: #000080;">==</span><span style="color: #FF0000;">'&lt;'</span> <span style="color: #000040;">&amp;&amp;</span> theText.<span style="color: #007788;">at</span><span style="color: #008000;">&#40;</span>theText.<span style="color: #007788;">size</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #000040;">-</span><span style="color: #0000dd;">1</span><span style="color: #008000;">&#41;</span><span style="color: #000080;">==</span> <span style="color: #FF0000;">'&gt;'</span><span style="color: #008000;">&#41;</span>
            doRandomText<span style="color: #008000;">&#40;</span>theGrammar, theText<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
        <span style="color: #0000ff;">else</span>
        <span style="color: #008000;">&#123;</span>
            <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>theText <span style="color: #000080;">==</span> <span style="color: #FF0000;">&quot;,&quot;</span> <span style="color: #000040;">||</span> theText <span style="color: #000080;">==</span> <span style="color: #FF0000;">&quot;.&quot;</span><span style="color: #008000;">&#41;</span>
                <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> theText<span style="color: #008080;">;</span>
            <span style="color: #0000ff;">else</span>
                <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot; &quot;</span> <span style="color: #000080;">&lt;&lt;</span> theText<span style="color: #008080;">;</span>
        <span style="color: #008000;">&#125;</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>The recursive function above basically tries to find the terminal within the grammar definitions and if there is not one, exit the code (assert), else print out the textual data whilst iterating over them, if there is any more terminals within the sentence then goto that terminal by calling this same function.</p>
<p>Here is the output of a run from the program.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">.<span style="color: #000000; font-weight: bold;">/</span>rsg assn-<span style="color: #000000;">1</span>-rsg-data<span style="color: #000000; font-weight: bold;">/</span>excuse.g 
The grammar <span style="color: #c20cb9; font-weight: bold;">file</span> called <span style="color: #ff0000;">&quot;assn-1-rsg-data/excuse.g&quot;</span> contains <span style="color: #000000;">7</span> definitions.
Version <span style="color: #666666; font-style: italic;">#1</span>
&nbsp;
 I need an extension because my disk got erased, and I<span style="color: #ff0000;">'m sure you'</span>ve heard this before, but I had to <span style="color: #c20cb9; font-weight: bold;">make</span> up a lot of documentation <span style="color: #000000; font-weight: bold;">for</span> the Navy <span style="color: #000000; font-weight: bold;">in</span> a big hurry, and I<span style="color: #ff0000;">'m sure you'</span>ve heard this before, but my printout was enshrowded <span style="color: #000000; font-weight: bold;">in</span> a mysterious fog <span style="color: #000000; font-weight: bold;">for</span> three days and <span style="color: #000000; font-weight: bold;">then</span> vanished, and I<span style="color: #ff0000;">'m sure you'</span>ve heard this before, but all my pencils broke, and just <span style="color: #000000; font-weight: bold;">then</span> I had <span style="color: #000000;">7</span> programs <span style="color: #000000; font-weight: bold;">in</span> like, a billion different langauges, and <span style="color: #000000; font-weight: bold;">if</span> you can believe it, I just didn<span style="color: #ff0000;">'t feel like working, and and then if I recall correctly I had to worry about the Winter Olympics, and and then if I recall correctly my disk got erased, and as if that wasn'</span>t enough I forgot how to write.
&nbsp;
Version <span style="color: #666666; font-style: italic;">#2</span>
&nbsp;
 I need an extension because I got stuck <span style="color: #000000; font-weight: bold;">in</span> a blizzard at Tahoe, and <span style="color: #000000; font-weight: bold;">then</span> get this, my Mac was enshrowded <span style="color: #000000; font-weight: bold;">in</span> a mysterious fog <span style="color: #000000; font-weight: bold;">for</span> three days and <span style="color: #000000; font-weight: bold;">then</span> vanished.
&nbsp;
Version <span style="color: #666666; font-style: italic;">#3</span>
&nbsp;
 I need an extension because I didn<span style="color: #ff0000;">'t know I was in this class, and then I just didn'</span>t feel like working, and I<span style="color: #ff0000;">'m sure you'</span>ve heard this before, but I thought I already graduated, and <span style="color: #000000; font-weight: bold;">then</span>, just when my mojo was getting back on its feet, the bookstore was out of erasers, and <span style="color: #000000; font-weight: bold;">if</span> you can believe it, I didn<span style="color: #ff0000;">'t know I was in this class, and and then if I recall correctly my dorm burned down.</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.codingfriends.com/index.php/2010/07/14/cs107-assignment-1-context-free-grammar-random-sentence-generator/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Explicit &#8211; c++ what is it for ?</title>
		<link>http://www.codingfriends.com/index.php/2010/07/14/explicit-c-what-is-it-for/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=explicit-c-what-is-it-for</link>
		<comments>http://www.codingfriends.com/index.php/2010/07/14/explicit-c-what-is-it-for/#comments</comments>
		<pubDate>Wed, 14 Jul 2010 10:19:59 +0000</pubDate>
		<dc:creator>genux</dc:creator>
				<category><![CDATA[C / C++]]></category>
		<category><![CDATA[Explicit]]></category>

		<guid isPermaLink="false">http://www.codingfriends.com/?p=1104</guid>
		<description><![CDATA[The explicit keyword in c++ is make sure that user of your class only creates and uses the class only creates it in the way that you was expecting and not via any references as such. For example in the following code example class mainClassWith &#123; public: // this basically will setup the internal_value equal [...]]]></description>
			<content:encoded><![CDATA[<p>The explicit keyword in c++ is make sure that user of your class only creates and uses the class only creates it in the way that you was expecting and not via any references as such.</p>
<p>For example in the following code example</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">class</span> mainClassWith <span style="color: #008000;">&#123;</span>
<span style="color: #0000ff;">public</span><span style="color: #008080;">:</span>
    <span style="color: #666666;">// this basically will setup the internal_value equal to the value that is passed from the user</span>
    <span style="color: #0000ff;">explicit</span> mainClassWith<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> value<span style="color: #008000;">&#41;</span> <span style="color: #008080;">:</span> internal_value<span style="color: #008000;">&#40;</span>value<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span><span style="color: #008000;">&#125;</span> <span style="color: #008080;">;</span>
&nbsp;
    <span style="color: #0000ff;">int</span> internal_value<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">int</span> functionWith<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> mainClassWith <span style="color: #000040;">&amp;</span>theClass<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;the value with &quot;</span> <span style="color: #000080;">&lt;&lt;</span> theClass.<span style="color: #007788;">internal_value</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>The functionWith will take a parameter of mainClassWith of which has a explicit keyword on its constructor, this means that if we try and do something like</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;">    mainClassWith mainWith<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">4</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
    functionWith<span style="color: #008000;">&#40;</span>mainWith<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></pre></div></div>

<p>then the compiler will compile and also the output would be printed to the screen of &#8220;the value with 4&#8243;, but since we know that the mainClassWith takes a integer value as a constructor and try to bypass any object creation of that mainClassWith and try and do this</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;">functionWith<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">3</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></pre></div></div>

<p>then the compiler will complain because we cannot setup a mainClassWith on the fly like this and let the compiler pass a value of &#8220;3&#8243; to the class and hope that it works!!!, we have explicitly declared that the constructor must be initialised before using.</p>
<p> But if we did take off the explicit keyword as in the class example below.</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">class</span> mainClassWithOut <span style="color: #008000;">&#123;</span>
<span style="color: #0000ff;">public</span><span style="color: #008080;">:</span>
    <span style="color: #666666;">// this basically will setup the internal_value equal to the value that is passed from the user</span>
    mainClassWithOut<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> value<span style="color: #008000;">&#41;</span> <span style="color: #008080;">:</span> internal_value<span style="color: #008000;">&#40;</span>value<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span><span style="color: #008000;">&#125;</span> <span style="color: #008080;">;</span>
&nbsp;
    <span style="color: #0000ff;">int</span> internal_value<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">int</span> functionWithout<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> mainClassWithOut <span style="color: #000040;">&amp;</span>theClass<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;the value without  &quot;</span><span style="color: #000080;">&lt;&lt;</span> theClass.<span style="color: #007788;">internal_value</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>then we are able to call the new function (functionWithout) without actually setting up the object and allow the compiler to make up its own instance of that object and pass to the function.</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;">functionWithout<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">5</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></pre></div></div>

<p>would work and the output would be &#8220;the value without 5&#8243; which is correct, but the compiler and created the mainClassWithout object and setup with the value of 5 via its constructor, which if there was a few different constructors etc then how would you be sure that the correct constructor was called and thus would the value that you want back from the class really be 5, it could be 0 and another value be that 5!!.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.codingfriends.com/index.php/2010/07/14/explicit-c-what-is-it-for/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

