<?xml version='1.0' encoding='utf-8'?>
<rss version='2.0'>
   <channel>
      <title>Programming and Web Development Tutorials</title>
      <link>http://tutorials.dreamincode.net</link>
      <description>Web Development, Programming, and Coding Community. Browse forums, snippets, and tutorials to get you on the right track. Experts in C++, PHP, VisualBasic, HTML, JavaScript, ColdFusion, and more!</description>
         <item>
            <link>http://forums.dreamincode.net/showtopic60036.htm</link>
            <title>Accessing Directories in C/C++ Part II in C++ Tutorials</title>
            <description>&lt;!--sizeo:4--&gt;&lt;span style=&quot;font-size:14pt;line-height:100%&quot;&gt;&lt;!--/sizeo--&gt;Working with directories&lt;!--sizec--&gt;&lt;/span&gt;&lt;!--/sizec--&gt; &lt;!--sizeo:2--&gt;&lt;span style=&quot;font-size:10pt;line-height:100%&quot;&gt;&lt;!--/sizeo--&gt;Part II&lt;!--sizec--&gt;&lt;/span&gt;&lt;!--/sizec--&gt;&lt;br /&gt;Creating and removing directories&amp;#33;&lt;br /&gt;&lt;br /&gt;First off, if you haven&amp;#39;t already, I suggest you read &lt;a href=&quot;http://www.dreamincode.net/forums/showtopic59943.htm&quot; target=&quot;_blank&quot;&gt;Part I&lt;/a&gt; of this tutorial, since a function which I have written will utilise everything that is taught in that tutorial.&lt;br /&gt;&lt;br /&gt;Next, take a look at &lt;a href=&quot;http://www.dreamincode.net/code/snippet2295.htm&quot; target=&quot;_blank&quot;&gt;this snippet&lt;/a&gt; which I wrote. It should make sense, if you understood Part I.&lt;br /&gt;&lt;br /&gt;Good, now we&amp;#39;re ready to start. I will be making use of that snippet in this tutorial, to show our progress.&lt;br /&gt;&lt;br /&gt;&lt;!--sizeo:3--&gt;&lt;span style=&quot;font-size:12pt;line-height:100%&quot;&gt;&lt;!--/sizeo--&gt;Let&amp;#39;s begin&amp;#33;&lt;!--sizec--&gt;&lt;/span&gt;&lt;!--/sizec--&gt;&lt;br /&gt;&lt;br /&gt;&lt;b&gt;What are we going to cover in this tutorial?&lt;/b&gt;&lt;br /&gt;Well, in Part I, we learned how to access a directory, using DIR* and dirent*&lt;br /&gt;Now, in this tutorial, we will learn how to use some (simple) functions, which perform the following tasks:&lt;ul&gt;&lt;li&gt;Creating a new directory&lt;/li&gt;&lt;li&gt;Deleting a directory&lt;/li&gt;&lt;li&gt;Changing our active directory&lt;/li&gt;&lt;li&gt;Getting the &lt;i&gt;whole path&lt;/i&gt; of the current directory&lt;/li&gt;&lt;/ul&gt; &lt;br /&gt;As with Part I, this is compatible with both C++ and C, therefore I will provide the main code in C, and convert it to C++ at the end. (Again, the only difference is console output)&lt;br /&gt;&lt;br /&gt;First off, I&amp;#39;m going to give you the bulk of the code. This is the includes and the function which we will use to list the contents of the current directory:&lt;br /&gt;[code=cpp]#include &amp;lt;dirent.h&amp;gt;&lt;br /&gt;#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;&lt;br /&gt;void listdir (const char *path);&lt;br /&gt;&lt;br /&gt;int main ()&lt;br /&gt;{&lt;br /&gt;    // example code will go here&lt;br /&gt;    return 0;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;void listdir (const char *path)&lt;br /&gt;{&lt;br /&gt;    // first off, we need to create a pointer to a directory&lt;br /&gt;    DIR *pdir = NULL; // remember, it&amp;#39;s good practice to initialise a pointer to NULL&amp;#33;&lt;br /&gt;    pdir = opendir (path); // &amp;quot;.&amp;quot; will refer to the current directory&lt;br /&gt;    struct dirent *pent = NULL;&lt;br /&gt;    if (pdir == NULL) // if pdir wasn&amp;#39;t initialised correctly&lt;br /&gt;    { // print an error message and exit the program&lt;br /&gt;        printf (&amp;quot;&amp;#092;nERROR&amp;#33; pdir could not be initialised correctly&amp;quot;);&lt;br /&gt;        return; // exit the function&lt;br /&gt;    } // end if&lt;br /&gt;&lt;br /&gt;    while (pent = readdir (pdir)) // while there is still something in the directory to list&lt;br /&gt;    {&lt;br /&gt;        if (pent == NULL) // if pent has not been initialised correctly&lt;br /&gt;        { // print an error message, and exit the program&lt;br /&gt;            printf (&amp;quot;&amp;#092;nERROR&amp;#33; pent could not be initialised correctly&amp;quot;);&lt;br /&gt;            return; // exit the function&lt;br /&gt;        }&lt;br /&gt;        // otherwise, it was initialised correctly. let&amp;#39;s print it on the console:&lt;br /&gt;        printf (&amp;quot;%s&amp;#092;n&amp;quot;, pent-&amp;gt;d_name);&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    // finally, let&amp;#39;s close the directory&lt;br /&gt;    closedir (pdir);&lt;br /&gt;}[/code]&lt;br /&gt;&lt;b&gt;&lt;!--sizeo:2--&gt;&lt;span style=&quot;font-size:10pt;line-height:100%&quot;&gt;&lt;!--/sizeo--&gt;Creating a new directory&lt;!--sizec--&gt;&lt;/span&gt;&lt;!--/sizec--&gt;&lt;/b&gt;&lt;br /&gt;We can do this with a call to a simple function, call &lt;b&gt;mkdir()&lt;/b&gt; like so:&lt;br /&gt;[code=cpp]mkdir (&amp;quot;.&amp;#092;&amp;#092;dream.in.code&amp;quot;);[/code]&lt;br /&gt;The &lt;b&gt;.&lt;/b&gt; represents our current directory (by default, this is the directory from which the program was launched)&lt;br /&gt;&lt;br /&gt;So, now we have created a new folder called &amp;quot;dream.in.code&amp;quot;&lt;br /&gt;&lt;br /&gt;&lt;b&gt;&lt;!--sizeo:2--&gt;&lt;span style=&quot;font-size:10pt;line-height:100%&quot;&gt;&lt;!--/sizeo--&gt;Getting the current directory&lt;!--sizec--&gt;&lt;/span&gt;&lt;!--/sizec--&gt;&lt;/b&gt;&lt;br /&gt;This can be done with a call to a function called &lt;b&gt;getcwd()&lt;/b&gt; like so:&lt;br /&gt;[code=cpp]printf (&amp;quot;Currently viewing:&amp;#092;n%s&amp;#092;n&amp;quot;, getcwd(NULL,0));[/code]As you can see, we are outputting the return value of the function, which is a string referring to the path of the current directory. Note that the function accepts two parameters. We aren&amp;#39;t going to go into detail right now, since this is just the basics of directory access, but just remember that you can pass NULL,0 to it to get the current path &lt;img src=&quot;style_emoticons/&lt;#EMO_DIR#&gt;/smile.gif&quot; style=&quot;vertical-align:middle&quot; emoid=&quot;:)&quot; border=&quot;0&quot; alt=&quot;smile.gif&quot; /&gt;&lt;br /&gt;&lt;br /&gt;Just to clarify that our dream.in.code folder has been created successfully, let&amp;#39;s list the contents of our current directory, using my snippet:[code=cpp]listdir (&amp;quot;.&amp;quot;);[/code]Remember, &lt;b&gt;.&lt;/b&gt; refers to the current path. Now, if you run the program, you should see a folder called dream.in.code at the bottom of the list. Now let&amp;#39;s move into that folder&amp;#33;&lt;br /&gt;&lt;br /&gt;&lt;b&gt;&lt;!--sizeo:2--&gt;&lt;span style=&quot;font-size:10pt;line-height:100%&quot;&gt;&lt;!--/sizeo--&gt;Changing the active directory&lt;!--sizec--&gt;&lt;/span&gt;&lt;!--/sizec--&gt;&lt;/b&gt;&lt;br /&gt;Again, all that we need to do is make a call to a simple function called &lt;b&gt;chdir()&lt;/b&gt; I&amp;#39;m going to do this, and call the &lt;b&gt;getcwd()&lt;/b&gt; and &lt;b&gt;listdir()&lt;/b&gt; functions for the output, to keep output clear.[code=cpp]    chdir (&amp;quot;.&amp;#092;&amp;#092;dream.in.code&amp;quot;);&lt;br /&gt;    printf (&amp;quot;&amp;#092;nCurrently viewing:&amp;#092;n%s&amp;#092;n&amp;quot;, getcwd(NULL,0));&lt;br /&gt;    listdir (&amp;quot;.&amp;quot;);[/code]&lt;b&gt;NOTE:&lt;/b&gt; At this point, the output from this listdir() call should just be:&lt;br /&gt;&lt;b&gt;.&lt;br /&gt;..&lt;/b&gt;&lt;br /&gt;because &lt;b&gt;.&lt;/b&gt; refers to the current directory, and &lt;b&gt;..&lt;/b&gt; refers to the parent directory. There will be nothing else, since we have not put anything into this folder.&lt;br /&gt;&lt;br /&gt;Now, just for clarity, I&amp;#39;m going to move back a folder, using &lt;b&gt;..&lt;/b&gt; and output the current path, like so:[code=cpp]    chdir (&amp;quot;..&amp;#092;&amp;#092;&amp;quot;); // move back one&lt;br /&gt;    printf (&amp;quot;Currently viewing:&amp;#092;n%s&amp;#092;n&amp;quot;, getcwd(NULL,0));[/code]Finally, let&amp;#39;s remove our &amp;quot;dream.in.code&amp;quot; directory.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;&lt;!--sizeo:2--&gt;&lt;span style=&quot;font-size:10pt;line-height:100%&quot;&gt;&lt;!--/sizeo--&gt;Removing a directory&lt;!--sizec--&gt;&lt;/span&gt;&lt;!--/sizec--&gt;&lt;/b&gt;&lt;br /&gt;Yet another single function call&amp;#33; Isn&amp;#39;t this directory stuff easy? Now, to remove a directory, we can make a call to &lt;b&gt;rmdir()&lt;/b&gt; like so:[code=cpp]    rmdir (&amp;quot;.&amp;#092;dream.in.code&amp;quot;);[/code]And finally, I&amp;#39;ll make another call to my snippet in order to reveal on the console that our directory has gone:[code=cpp]    listdir (&amp;quot;.&amp;quot;);[/code]&lt;br /&gt;&lt;br /&gt;&lt;b&gt;And that&amp;#39;s all there is to it&amp;#33;&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;Why not go down to the local bar and try to pick up some girls with your super cool new directory accessing skills? I&amp;#39;m sure they&amp;#39;ll be impressed&amp;#33; &lt;img src=&quot;style_emoticons/&lt;#EMO_DIR#&gt;/smile.gif&quot; style=&quot;vertical-align:middle&quot; emoid=&quot;:)&quot; border=&quot;0&quot; alt=&quot;smile.gif&quot; /&gt;&lt;br /&gt;&lt;br /&gt;Now for the final code again&amp;#33;&lt;br /&gt;&lt;br /&gt;&lt;!--sizeo:4--&gt;&lt;span style=&quot;font-size:14pt;line-height:100%&quot;&gt;&lt;!--/sizeo--&gt;C CODE&amp;#33;&lt;!--sizec--&gt;&lt;/span&gt;&lt;!--/sizec--&gt;[code=cpp]#include &amp;lt;dirent.h&amp;gt;&lt;br /&gt;#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;&lt;br /&gt;void listdir (const char *path);&lt;br /&gt;&lt;br /&gt;int main ()&lt;br /&gt;{&lt;br /&gt;    if (&amp;#33;mkdir (&amp;quot;.&amp;#092;&amp;#092;dream.in.code&amp;quot;))&lt;br /&gt;    {&lt;br /&gt;        printf (&amp;quot;mkdir() unsuccessful. Terminating...&amp;quot;);&lt;br /&gt;        exit (3); // exit after a 3 second pause&lt;br /&gt;    } // otherwise, it was created, so we can continue&lt;br /&gt;    printf (&amp;quot;dream.in.code folder created successfully&amp;#33;&amp;quot;);&lt;br /&gt;    printf (&amp;quot;Currently viewing:&amp;#092;n%s&amp;#092;n&amp;quot;, getcwd(NULL,0));&lt;br /&gt;    listdir (&amp;quot;.&amp;quot;);&lt;br /&gt;    printf (&amp;quot;&amp;#092;nMoving into our newly created directory&amp;quot;);&lt;br /&gt;    chdir (&amp;quot;.&amp;#092;&amp;#092;dream.in.code&amp;quot;);&lt;br /&gt;    printf (&amp;quot;&amp;#092;nCurrently viewing:&amp;#092;n%s&amp;#092;n&amp;quot;, getcwd(NULL,0));&lt;br /&gt;    listdir (&amp;quot;.&amp;quot;);&lt;br /&gt;    chdir (&amp;quot;..&amp;#092;&amp;#092;&amp;quot;); // move back one&lt;br /&gt;    rmdir (&amp;quot;.&amp;#092;dream.in.code&amp;quot;);&lt;br /&gt;    listdir (&amp;quot;.&amp;quot;);&lt;br /&gt;&lt;br /&gt;    return 0;&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;void listdir (const char *path)&lt;br /&gt;{&lt;br /&gt;    // first off, we need to create a pointer to a directory&lt;br /&gt;    DIR *pdir = NULL; // remember, it&amp;#39;s good practice to initialise a pointer to NULL&amp;#33;&lt;br /&gt;    pdir = opendir (path); // &amp;quot;.&amp;quot; will refer to the current directory&lt;br /&gt;    struct dirent *pent = NULL;&lt;br /&gt;    if (pdir == NULL) // if pdir wasn&amp;#39;t initialised correctly&lt;br /&gt;    { // print an error message and exit the program&lt;br /&gt;        printf (&amp;quot;&amp;#092;nERROR&amp;#33; pdir could not be initialised correctly&amp;quot;);&lt;br /&gt;        return; // exit the function&lt;br /&gt;    } // end if&lt;br /&gt;&lt;br /&gt;    while (pent = readdir (pdir)) // while there is still something in the directory to list&lt;br /&gt;    {&lt;br /&gt;        if (pent == NULL) // if pent has not been initialised correctly&lt;br /&gt;        { // print an error message, and exit the program&lt;br /&gt;            printf (&amp;quot;&amp;#092;nERROR&amp;#33; pent could not be initialised correctly&amp;quot;);&lt;br /&gt;            return; // exit the function&lt;br /&gt;        }&lt;br /&gt;        // otherwise, it was initialised correctly. let&amp;#39;s print it on the console:&lt;br /&gt;        printf (&amp;quot;%s&amp;#092;n&amp;quot;, pent-&amp;gt;d_name);&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    // finally, let&amp;#39;s close the directory&lt;br /&gt;    closedir (pdir);&lt;br /&gt;}[/code]&lt;br /&gt;&lt;!--sizeo:4--&gt;&lt;span style=&quot;font-size:14pt;line-height:100%&quot;&gt;&lt;!--/sizeo--&gt;C++ CODE&amp;#33;&lt;!--sizec--&gt;&lt;/span&gt;&lt;!--/sizec--&gt;[code=cpp]#include &amp;lt;dirent.h&amp;gt;&lt;br /&gt;#include &amp;lt;cstdio&amp;gt; // for the snippet&lt;br /&gt;#include &amp;lt;iostream&amp;gt; // for output&lt;br /&gt;&lt;br /&gt;void listdir (const char *path);&lt;br /&gt;&lt;br /&gt;int main ()&lt;br /&gt;{&lt;br /&gt;    if (&amp;#33;mkdir (&amp;quot;.&amp;#092;&amp;#092;dream.in.code&amp;quot;))&lt;br /&gt;    {&lt;br /&gt;        printf (&amp;quot;mkdir() unsuccessful. Terminating...&amp;quot;);&lt;br /&gt;        exit (3); // exit after a 3 second pause&lt;br /&gt;    } // otherwise, it was created, so we can continue&lt;br /&gt;    std::cout &amp;lt;&amp;lt; &amp;quot;dream.in.code folder created successfully&amp;#33;&amp;quot;&lt;br /&gt;              &amp;lt;&amp;lt; &amp;quot;Currently viewing:&amp;#092;n%s&amp;#092;n&amp;quot; &amp;lt;&amp;lt; getcwd(NULL,0);&lt;br /&gt;    listdir (&amp;quot;.&amp;quot;);&lt;br /&gt;    std::cout &amp;lt;&amp;lt; &amp;quot;&amp;#092;nMoving into our newly created directory&amp;quot;;&lt;br /&gt;    chdir (&amp;quot;.&amp;#092;&amp;#092;dream.in.code&amp;quot;);&lt;br /&gt;    std::cout &amp;lt;&amp;lt; &amp;quot;&amp;#092;nCurrently viewing:&amp;#092;n%s&amp;#092;n&amp;quot; &amp;lt;&amp;lt; getcwd(NULL,0);&lt;br /&gt;    listdir (&amp;quot;.&amp;quot;);&lt;br /&gt;    chdir (&amp;quot;..&amp;#092;&amp;#092;&amp;quot;); // move back one&lt;br /&gt;    rmdir (&amp;quot;.&amp;#092;dream.in.code&amp;quot;);&lt;br /&gt;    listdir (&amp;quot;.&amp;quot;);&lt;br /&gt;&lt;br /&gt;    std::cin.get (); // pause for input&lt;br /&gt;    return EXIT_SUCCESS; // program was executed successfully&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;void listdir (const char *path)&lt;br /&gt;{&lt;br /&gt;    // first off, we need to create a pointer to a directory&lt;br /&gt;    DIR *pdir = NULL; // remember, it&amp;#39;s good practice to initialise a pointer to NULL&amp;#33;&lt;br /&gt;    pdir = opendir (path); // &amp;quot;.&amp;quot; will refer to the current directory&lt;br /&gt;    struct dirent *pent = NULL;&lt;br /&gt;    if (pdir == NULL) // if pdir wasn&amp;#39;t initialised correctly&lt;br /&gt;    { // print an error message and exit the program&lt;br /&gt;        printf (&amp;quot;&amp;#092;nERROR&amp;#33; pdir could not be initialised correctly&amp;quot;);&lt;br /&gt;        return; // exit the function&lt;br /&gt;    } // end if&lt;br /&gt;&lt;br /&gt;    while (pent = readdir (pdir)) // while there is still something in the directory to list&lt;br /&gt;    {&lt;br /&gt;        if (pent == NULL) // if pent has not been initialised correctly&lt;br /&gt;        { // print an error message, and exit the program&lt;br /&gt;            printf (&amp;quot;&amp;#092;nERROR&amp;#33; pent could not be initialised correctly&amp;quot;);&lt;br /&gt;            return; // exit the function&lt;br /&gt;        }&lt;br /&gt;        // otherwise, it was initialised correctly. let&amp;#39;s print it on the console:&lt;br /&gt;        printf (&amp;quot;%s&amp;#092;n&amp;quot;, pent-&amp;gt;d_name);&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    // finally, let&amp;#39;s close the directory&lt;br /&gt;    closedir (pdir);&lt;br /&gt;}[/code]</description>
			<pubDate>Fri, 08 Aug 2008 12:14:43 -0600</pubDate>
			<category>C++ Tutorials</category>
			<author>gabehabe</author>
         </item>
         <item>
            <link>http://forums.dreamincode.net/showtopic59943.htm</link>
            <title>Accessing directories in C/C++ Part I in C++ Tutorials</title>
            <description>&lt;!--quoteo--&gt;&lt;div class='quotetop'&gt;QUOTE&lt;/div&gt;&lt;div class='quotemain'&gt;&lt;!--quotec--&gt; exit (3); // pause for three seconds before exiting&lt;!--QuoteEnd--&gt;&lt;/div&gt;&lt;!--QuoteEEnd--&gt;&lt;br /&gt;Which header file is exit defined in?&lt;br /&gt;&lt;br /&gt;As far as I know, the one defined in stdlib.h takes a &lt;i&gt;status&lt;/i&gt; parameter, and doesn&amp;#39;t pause for &amp;#39;&lt;i&gt;status&lt;/i&gt;&amp;#39; seconds when called.</description>
			<pubDate>Fri, 08 Aug 2008 05:19:22 -0600</pubDate>
			<category>C++ Tutorials</category>
			<author>born2c0de</author>
         </item>
         <item>
            <link>http://forums.dreamincode.net/showtopic59846.htm</link>
            <title>Creating custom .h files and adding them to a project in C++ Tutorials</title>
            <description>&lt;!--sizeo:4--&gt;&lt;span style=&quot;font-size:14pt;line-height:100%&quot;&gt;&lt;!--/sizeo--&gt;Creating a Project in C/C++&lt;!--sizec--&gt;&lt;/span&gt;&lt;!--/sizec--&gt;&lt;br /&gt;Using various filetypes:&lt;br /&gt;&lt;b&gt;.h .cpp&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;This is a little IDE specific but I&amp;#39;ll make it as general as possible.&lt;br /&gt;&lt;br /&gt;This is quite specific, but it can be generalised. Basically, when you create a new project, you should already have a &lt;b&gt;.cpp&lt;/b&gt; file added to it, since this is where the main loop goes. This file is often called &lt;b&gt;main.cpp&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;Now, to add a new file, most IDEs will require you to go to File&amp;gt;New. You should then see an option called something like &amp;quot;Header file.&amp;quot; If not, then it is most likely File&amp;gt;New&amp;gt;file...&lt;br /&gt;&lt;br /&gt;Now, make sure that it is a &lt;b&gt;.h&lt;/b&gt; file that you are creating, give it a name, and click next/OK. Follow the wizard until you are returned to the coding view, and you should now be working in a header file (WhateverYouCalledIt.h)&lt;br /&gt;&lt;br /&gt;First off, I&amp;#39;m going to add some header guards. This is basically to prevent the file being included more than once, which can cause errors (saying that things have already been declared, and are now being redeclared)&lt;br /&gt;&lt;br /&gt;The easiest one is a simple one-line tool. However, it&amp;#39;s not used very often.&lt;br /&gt;[inline]#pragma once[/inline]&lt;br /&gt;&lt;br /&gt;The more common method is to use [inline]#ifndef[/inline] like so:&lt;br /&gt;[code=cpp]#ifndef MYHEADER_H_INCLUDED&lt;br /&gt;#define MYHEADER_H_INCLUDED&lt;br /&gt;// header content goes here&lt;br /&gt;#endif[/code]This looks more confusing than it is. let&amp;#39;s break it down a little.&lt;br /&gt;&lt;br /&gt;A header file is most often used to contain variable declarations, function prototypes, and class declarations. In this example, I&amp;#39;m just going to use a few variables, because you may not yet be at a level of writing functions/classes.&lt;br /&gt;&lt;br /&gt;Here is a &lt;i&gt;very&lt;/i&gt; basic header file: [code=cpp]#ifndef MYHEADER_H_INCLUDED&lt;br /&gt;#define MYHEADER_H_INCLUDED&lt;br /&gt;&lt;br /&gt;int a; // declare an int&lt;br /&gt;double b; // declare a double&lt;br /&gt;char c; // declare a char&lt;br /&gt;&lt;br /&gt;// notice that I haven&amp;#39;t assigned any values here?&lt;br /&gt;// we do that next, in a cpp file&lt;br /&gt;&lt;br /&gt;#endif[/code]Now it&amp;#39;s time to make a &lt;b&gt;.cpp&lt;/b&gt; file to define everything in that header file. So, go to File&amp;gt;New&amp;gt;File... again. This time, however, instead of selecting &lt;b&gt;.h&lt;/b&gt; we&amp;#39;re selecting &lt;b&gt;.cpp&lt;/b&gt;&lt;br /&gt;Give it the same name as the header file. This will make it easier to manage later on. &lt;img src=&quot;style_emoticons/&lt;#EMO_DIR#&gt;/smile.gif&quot; style=&quot;vertical-align:middle&quot; emoid=&quot;:)&quot; border=&quot;0&quot; alt=&quot;smile.gif&quot; /&gt;&lt;br /&gt;&lt;br /&gt;Let&amp;#39;s include our header file, and assign some stuff to our variables:[code=cpp]#include &amp;quot;myheader.h&amp;quot;&lt;br /&gt;&lt;br /&gt;a = 4;&lt;br /&gt;b = 20.2994;&lt;br /&gt;c = &amp;#39;D&amp;#39;;[/code]Pretty simple, huh? That&amp;#39;s because we&amp;#39;re not really learning any new code. All that we&amp;#39;re doing is breaking up some code into manageable files. It seems a little pointless here, but it&amp;#39;s much better to have a few files with a few hundred lines each than one big file with thousands of lines. It simply makes it easier to manage&amp;#33;&lt;br /&gt;&lt;br /&gt;Now to write the code for our &lt;b&gt;main.cpp&lt;/b&gt;&lt;br /&gt;All we need to do is include our header file, and the contents of the cpp file will be included for you.&lt;br /&gt;This is main.cpp:[code=cpp]#include &amp;lt;iostream&amp;gt; // input/output stream&lt;br /&gt;#include &amp;quot;myheader.h&amp;quot; // use &amp;quot; &amp;quot; instead of &amp;lt; &amp;gt; so the compiler knows that it&amp;#39;s a local header file&lt;br /&gt;&lt;br /&gt;using namespace std;&lt;br /&gt;&lt;br /&gt;int main ()&lt;br /&gt;{&lt;br /&gt;    cout &amp;lt;&amp;lt; a &amp;lt;&amp;lt; endl &amp;lt;&amp;lt; b &amp;lt;&amp;lt; endl &amp;lt;&amp;lt; c;&lt;br /&gt;    cin.get ();&lt;br /&gt;    return EXIT_SUCCESS;&lt;br /&gt;}[/code]&lt;br /&gt;There are no scoping issues, since these variables have simply been dragged into the program at compile time.&lt;br /&gt;&lt;br /&gt;Happy coding&amp;#33; &lt;img src=&quot;style_emoticons/&lt;#EMO_DIR#&gt;/smile.gif&quot; style=&quot;vertical-align:middle&quot; emoid=&quot;:)&quot; border=&quot;0&quot; alt=&quot;smile.gif&quot; /&gt;</description>
			<pubDate>Wed, 06 Aug 2008 14:41:45 -0600</pubDate>
			<category>C++ Tutorials</category>
			<author>gabehabe</author>
         </item>
         <item>
            <link>http://forums.dreamincode.net/showtopic59841.htm</link>
            <title>C++ Namespaces Part I in C++ Tutorials</title>
            <description>&lt;!--sizeo:4--&gt;&lt;span style=&quot;font-size:14pt;line-height:100%&quot;&gt;&lt;!--/sizeo--&gt;Working with namespaces&lt;!--sizec--&gt;&lt;/span&gt;&lt;!--/sizec--&gt;&lt;br /&gt;&lt;br /&gt;&lt;b&gt;&lt;i&gt;What is a namespace?&lt;/i&gt;&lt;/b&gt;&lt;br /&gt;A namespace is basically a container for a group of variables/functions etc. I can pretty much guarantee everyone reading this tutorial has already used a namespace in C++ programming. Think of a line that usually comes at the top of a beginner program:&lt;br /&gt;[inline]using namespace std;[/inline]&lt;br /&gt;This is the standard namespace. It&amp;#39;s got the definitions of a lot of neat stuff in there.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;&lt;i&gt;Why should I use a namespace?&lt;/i&gt;&lt;/b&gt;&lt;br /&gt;Namespaces are pretty neat. We use them to contain a group of data. Also, it makes it possible to have more than one variable with the same name. One of which is in the namespace, one which isn&amp;#39;t. This will be explained later on.&lt;br /&gt;&lt;br /&gt;When writing a namespace, there&amp;#39;s nothing you need to include. A namespace is a standard container.&lt;br /&gt;&lt;br /&gt;I&amp;#39;m gonna put my namespace in a seperate header file, just to save confusion.&lt;br /&gt;&lt;br /&gt;Let&amp;#39;s start with our header guards. I&amp;#39;m gonna call the header file &amp;quot;namespace.h&amp;quot; but you can call it whatever you like.&lt;br /&gt;Also, I&amp;#39;m just gonna define it in the .h file. Granted, this is bad habit, but this is just a quick example.&lt;br /&gt;&lt;br /&gt;[code=cpp]#ifndef NAMESPACE_H_INCLUDED&lt;br /&gt;#define NAMESPACE_H_INCLUDED[/code]&lt;br /&gt;Now we&amp;#39;re ready to start writing a namespace. I&amp;#39;m gonna write a simple &amp;quot;math&amp;quot; namespace, since I&amp;#39;ve used something similar in a few posts to explain it already, and the people who I wrote it for found it quite useful.&lt;br /&gt;&lt;br /&gt;There isn&amp;#39;t too much to writing a namespace, but it&amp;#39;s a useful skill. There isn&amp;#39;t too much to break it down into, so I&amp;#39;m gonna post up a simple namespace here. This is namespace.h:&lt;br /&gt;[code=cpp]#ifndef NAMESPACE_H_INCLUDED&lt;br /&gt;#define NAMESPACE_H_INCLUDED&lt;br /&gt;&lt;br /&gt;namespace math // define a namespace called math&lt;br /&gt;{&lt;br /&gt;    namespace numbers // an example of a nested namespace&lt;br /&gt;    { // define some variables within our math::numbers namespace&lt;br /&gt;        int one = 1;&lt;br /&gt;        int two = 2;&lt;br /&gt;        int three = 3;&lt;br /&gt;        int four = 4;&lt;br /&gt;        int five = 5;&lt;br /&gt;        int six = 6;&lt;br /&gt;        int seven = 7;&lt;br /&gt;        int eight = 8;&lt;br /&gt;        int nine = 9;&lt;br /&gt;    } // end of nested namespace&lt;br /&gt;&lt;br /&gt;    // define some functions within our math namespace&lt;br /&gt;    int add (int x, int y) {return x + y;}&lt;br /&gt;    int subtract (int x, int y) {return x - y;}&lt;br /&gt;    int divide (int x, int y) {return x / y;}&lt;br /&gt;    int multiply (int x, int y) {return x * y;}&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;#endif // NAMESPACE_H_INCLUDED[/code]&lt;br /&gt;As you can see, I&amp;#39;ve even made a nested namespace. You can nest namespaces as far as you like, but it seems a little pointless to have something as long as:&lt;br /&gt;[inline]namespace1::namespace2::namespace3::namespace4::x[/inline] just to access a variable named x.&lt;br /&gt;&lt;br /&gt;And now that I&amp;#39;ve said that, you&amp;#39;re probably thinking &amp;quot;what&amp;#39;s this &lt;b&gt;::&lt;/b&gt; operator that this fool uses?&amp;quot;&lt;br /&gt;The &lt;b&gt;::&lt;/b&gt; operator is known as the &lt;b&gt;scope&lt;/b&gt; operator, and it is used to access certain things from within a namespace. The most common, which you &lt;i&gt;may&lt;/i&gt; have seen before is something like:&lt;br /&gt;[inline]std::cout &amp;lt;&amp;lt; &amp;quot;something&amp;quot;;[/inline]&lt;br /&gt;This line basically means that &lt;i&gt;cout&lt;/i&gt; is defined in the &lt;i&gt;std&lt;/i&gt; namespace. Makes sense, right?&lt;br /&gt;&lt;br /&gt;Onwards to the various methods of accessing a namespace&amp;#33; This can be done with various methods.&lt;br /&gt;There are three methods of accessing something from within a namespace. I&amp;#39;m gonna use the std namespace as an example here:&lt;ul&gt;&lt;li&gt;[inline]using namespace std;[/inline] This means that we want to be &lt;b&gt;using&lt;/b&gt; everything which is defined within the std namespace&lt;/li&gt;&lt;li&gt;[inline]using std::cout;[/inline] This means that we only want to use &lt;b&gt;cout&lt;/b&gt; from the std namespace&lt;/li&gt;&lt;li&gt;This last method doesn&amp;#39;t require the &lt;b&gt;using&lt;/b&gt; keyword. We use it whenever we want to access something from the namespace. Example:&lt;br /&gt;[code=cpp]#include &amp;lt;iostream&amp;gt; // input/output stream&lt;br /&gt;&lt;br /&gt;int main ()&lt;br /&gt;{&lt;br /&gt;    std::cout &amp;lt;&amp;lt; &amp;quot;Enter something&amp;#33;&amp;quot; &amp;lt;&amp;lt; std::endl;&lt;br /&gt;    char input;&lt;br /&gt;    std::cin &amp;gt;&amp;gt; input;&lt;br /&gt;    std::cout &amp;lt;&amp;lt; &amp;quot;You entered: &amp;quot; &amp;lt;&amp;lt; input &amp;lt;&amp;lt; &amp;quot;&amp;#33;&amp;quot; &amp;lt;&amp;lt; std::endl;&lt;br /&gt;    std::cin.get (); // pause&lt;br /&gt;    return EXIT_SUCCESS;&lt;br /&gt;}[/code]&lt;/li&gt;&lt;/ul&gt;Notice how we had to prefix every use of any std feature with &lt;b&gt;std::&lt;/b&gt; ? This can become tedious, which is why we have the &lt;b&gt;using&lt;/b&gt; keyword&amp;#33; &lt;img src=&quot;style_emoticons/&lt;#EMO_DIR#&gt;/smile.gif&quot; style=&quot;vertical-align:middle&quot; emoid=&quot;:)&quot; border=&quot;0&quot; alt=&quot;smile.gif&quot; /&gt;&lt;br /&gt;&lt;br /&gt;Now for our main.cpp file, which is used to demonstrate everything we&amp;#39;ve learned so far:&lt;br /&gt;[code=cpp]#include &amp;lt;iostream&amp;gt;&lt;br /&gt;&lt;br /&gt;#include &amp;quot;namespace.h&amp;quot;&lt;br /&gt;&lt;br /&gt;// using a namespace&lt;br /&gt;using namespace math; // using everything from the namespace called numbers&lt;br /&gt;// (our nested namespace)&lt;br /&gt;&lt;br /&gt;// using individual members of a namespace&lt;br /&gt;using std::cout; // use cout from the std namespace specifically&lt;br /&gt;using std::cin;  // use cin from the std namespace specifically&lt;br /&gt;using std::endl; // use endl from the std namespace specifically&lt;br /&gt;&lt;br /&gt;int main ()&lt;br /&gt;{&lt;br /&gt;    cout &amp;lt;&amp;lt; math::add (numbers::three, numbers::two) &amp;lt;&amp;lt; endl;&lt;br /&gt;    // note: if we weren&amp;#39;t using the math namespace, we would have to prefix three and two with:&lt;br /&gt;    // math::numbers::three and math::numbers::two&lt;br /&gt;    // this is why it&amp;#39;s kind of advisable against using huge nested namespaces&amp;#33;&lt;br /&gt;    &lt;br /&gt;    // now, since we need to access two through numbers::two we can still have another variable called two&lt;br /&gt;    // because of the scope.&lt;br /&gt;    int two = 771123;&lt;br /&gt;    // now let&amp;#39;s output both &amp;quot;two&amp;quot; variables and see what we get&lt;br /&gt;    cout &amp;lt;&amp;lt; &amp;quot;two = &amp;quot; &amp;lt;&amp;lt; two &amp;lt;&amp;lt; endl&lt;br /&gt;         &amp;lt;&amp;lt; &amp;quot;numbers::two = &amp;quot; &amp;lt;&amp;lt; numbers::two;&lt;br /&gt;&lt;br /&gt;    cin.get ();&lt;br /&gt;    return EXIT_SUCCESS;&lt;br /&gt;}[/code]&lt;br /&gt;&lt;br /&gt;You can also create namespace with no name, which I will write about in a part II of this tutorial.&lt;br /&gt;&lt;br /&gt;Happy coding&amp;#33;</description>
			<pubDate>Wed, 06 Aug 2008 13:56:08 -0600</pubDate>
			<category>C++ Tutorials</category>
			<author>gabehabe</author>
         </item>
         <item>
            <link>http://forums.dreamincode.net/showtopic59794.htm</link>
            <title>How to create a Mail Merge in Word 2007 in Windows Tutorials</title>
            <description>I had to write a tutorial as a part of my assessment in one of my I.T classes, so I thought I&amp;#39;d share it with you guys. &lt;br /&gt;&lt;br /&gt;The Word Document attached looks a lot better then this tutorial, purely because of the lack of control I have with BBCode. I do suggest you read it as it is formatted a lot clearer than it is with the forum software. For those too lazy to read it, continue reading.&lt;br /&gt;&lt;br /&gt;[attachmentid=7900]&lt;br /&gt;&lt;br /&gt;&lt;!--sizeo:3--&gt;&lt;span style=&quot;font-size:12pt;line-height:100%&quot;&gt;&lt;!--/sizeo--&gt;How to create a Mail Merge&lt;!--sizec--&gt;&lt;/span&gt;&lt;!--/sizec--&gt;&lt;br /&gt;&lt;br /&gt;Microsoft Word has the option of merging information from a database (such as Names, Addresses) into formal letters. This option is called a Mail Merge. This step-by-step tutorial will explain the following Mail Merge concepts:&lt;br /&gt; &lt;ul&gt;&lt;li&gt;How to create a recipient list for use in a Mail Merge&lt;/li&gt;&lt;li&gt;How to import an address list for use in a Mail Merge&lt;/li&gt;&lt;li&gt;How to greet a recipient in a document&lt;/li&gt;&lt;li&gt;How to display a recipient’s address in a letter&lt;/li&gt;&lt;li&gt;How to insert an individual field into a letter&lt;/li&gt;&lt;li&gt;Finishing the Mail Merge&lt;/li&gt;&lt;/ul&gt; &lt;br /&gt;&lt;!--sizeo:2--&gt;&lt;span style=&quot;font-size:10pt;line-height:100%&quot;&gt;&lt;!--/sizeo--&gt;&lt;b&gt;&lt;i&gt;How to create a recipient list for use in a Mail Merge&lt;/i&gt;&lt;/b&gt;&lt;!--sizec--&gt;&lt;/span&gt;&lt;!--/sizec--&gt;&lt;br /&gt;&lt;br /&gt;&lt;img src=&quot;http://img180.imageshack.us/img180/7610/76276084bi3.png&quot; border=&quot;0&quot; alt=&quot;IPB Image&quot; /&gt;&lt;br /&gt;Figure 1.1: The New Address List Dialog&lt;br /&gt;&lt;br /&gt;Creating a recipient list is simply a matter of clicking the ‘Mailings’ tab on the office ribbon, clicking the Select Recipients button and selecting the ‘Type New List’ option from the drop-down list.&lt;br /&gt;&lt;br /&gt;You will be presented with a ‘New Address List’ dialog box (see Figure 1.1). By default, you have the options of providing a wide variation of information about recipients, such as names and contact details.&lt;br /&gt;&lt;br /&gt;Type your list of recipients into the table and click the Ok button to close the window. A dialog will appear, prompting you to tell Microsoft Word where to save your new address list so we can access it easily in all documents. Give it an appropriate name and click the save button.&lt;br /&gt;&lt;br /&gt;Tip: If you need to add additional columns to your address list, select the ‘Customize Columns...’ option in the bottom-left corner of the window.&lt;br /&gt;&lt;br /&gt;&lt;!--sizeo:2--&gt;&lt;span style=&quot;font-size:10pt;line-height:100%&quot;&gt;&lt;!--/sizeo--&gt;&lt;b&gt;&lt;i&gt;How import an address list for use in a Mail Merge&lt;/i&gt;&lt;/b&gt;&lt;!--sizec--&gt;&lt;/span&gt;&lt;!--/sizec--&gt;&lt;br /&gt;To actually use the Mail Merge function, we will need to import a recipient list. We have already created one in the previous step; so again, select the ‘Mailings’ tab on the office ribbon and click the ‘Use Existing List’ button. A dialog will appear prompting you to locate your address list. Select the address list and click Open. &lt;br /&gt;&lt;br /&gt;You will notice some buttons that were previously disabled on the ribbon are now enabled as a result of this operation; this is because Microsoft Word now knows the current document is working with a Data Source.&lt;br /&gt;&lt;br /&gt;Tip: It is not a good idea to import an address list on removable storage devices. When Windows detects a removable storage device, it allocates it a drive letter based on the ones that aren’t being used on the current computer. What this means to you, is that your document may not properly find the address list file if its path keeps changing.&lt;br /&gt;&lt;br /&gt;&lt;!--sizeo:2--&gt;&lt;span style=&quot;font-size:10pt;line-height:100%&quot;&gt;&lt;!--/sizeo--&gt;&lt;b&gt;&lt;i&gt;How to greet a recipient in a document&lt;/i&gt;&lt;/b&gt;&lt;!--sizec--&gt;&lt;/span&gt;&lt;!--/sizec--&gt;&lt;br /&gt;&lt;br /&gt;&lt;img src=&quot;http://img210.imageshack.us/img210/1359/91258306tk9.png&quot; border=&quot;0&quot; alt=&quot;IPB Image&quot; /&gt;&lt;br /&gt;Figure 1.2: The Insert Greeting Line Dialog&lt;br /&gt;&lt;br /&gt;Greeting a recipient in a document is easy. First select the ‘Mailings’ tab on the office ribbon, and then select the Greeting Line button. A dialog will appear titled ‘Insert Greeting Line’ (figure 1.2).&lt;br /&gt;&lt;br /&gt;This dialog will allow you to change the format of the greeting, the greeting line for recipients Microsoft Word doesn’t have data for and a preview of what the greeting will look like in your document.&lt;br /&gt;&lt;br /&gt;&lt;!--sizeo:2--&gt;&lt;span style=&quot;font-size:10pt;line-height:100%&quot;&gt;&lt;!--/sizeo--&gt;&lt;b&gt;&lt;i&gt;How to display a recipient’s address in a letter&lt;/i&gt;&lt;/b&gt;&lt;!--sizec--&gt;&lt;/span&gt;&lt;!--/sizec--&gt;&lt;br /&gt;Displaying a recipient’s address is much the same deal as greeting a recipient in a document. Select the ‘Mailings’ tab on the office ribbon and click on the Address Block button.&lt;br /&gt;&lt;br /&gt;A dialog will appear, prompting you to choose the format of the recipient’s name, whether to insert the company name or not, the option of including the postal address and a preview of how the address block will appear in your document. Modify these options to your liking and click ok.&lt;br /&gt;&lt;br /&gt;&lt;!--sizeo:2--&gt;&lt;span style=&quot;font-size:10pt;line-height:100%&quot;&gt;&lt;!--/sizeo--&gt;&lt;b&gt;&lt;i&gt;How to insert an individual field into a letter&lt;/i&gt;&lt;/b&gt;&lt;!--sizec--&gt;&lt;/span&gt;&lt;!--/sizec--&gt;&lt;br /&gt;&lt;br /&gt;&lt;img src=&quot;http://img227.imageshack.us/img227/2576/45461083ad5.png&quot; border=&quot;0&quot; alt=&quot;IPB Image&quot; /&gt;&lt;br /&gt;Figure 1.3: The Insert Merge Field Dialog&lt;br /&gt;&lt;br /&gt;Sometimes you may need to insert a name or email address out of the blue into your document (or anything else you can think of that you entered into the address list). This can be performed by using the ‘Insert Merge Field’ option. &lt;br /&gt;&lt;br /&gt;Click the ‘Mailings’ tab on the office ribbon, and select the top half of the ‘Insert Merge Field’ button. A dialog will appear (see figure 1.3), giving you an option of selecting a variety of fields.&lt;br /&gt;&lt;br /&gt;When you have found the type of field you would like to insert into your document, select it in the list and click ‘Insert’.&lt;br /&gt;&lt;br /&gt;&lt;!--sizeo:2--&gt;&lt;span style=&quot;font-size:10pt;line-height:100%&quot;&gt;&lt;!--/sizeo--&gt;&lt;b&gt;&lt;i&gt;Finishing the Mail Merge&lt;/i&gt;&lt;/b&gt;&lt;!--sizec--&gt;&lt;/span&gt;&lt;!--/sizec--&gt;&lt;br /&gt;To finish the mail merge, click the ‘Mailings’ tab on the office ribbon and select the ‘Finish &amp;amp; Merge’ button. It will give you the option of editing the individual documents, printing the documents or sending the document as an email.&lt;br /&gt;&lt;br /&gt;Selecting any of the three options will present you with a dialog asking which records in the address list you would like to use. Selecting ‘All’ will perform the operation on all records in the address list.&lt;br /&gt;&lt;br /&gt;&lt;!--sizeo:2--&gt;&lt;span style=&quot;font-size:10pt;line-height:100%&quot;&gt;&lt;!--/sizeo--&gt;&lt;b&gt;&lt;i&gt;Conclusion&lt;/i&gt;&lt;/b&gt;&lt;!--sizec--&gt;&lt;/span&gt;&lt;!--/sizec--&gt;&lt;br /&gt;I hope this tutorial has enlightened you as to the many benefits of using a Mail Merge, and how it can make writing letters to multiple recipients much less work.</description>
			<pubDate>Wed, 06 Aug 2008 02:23:00 -0600</pubDate>
			<category>Windows Tutorials</category>
			<author>RodgerB</author>
         </item>
         <item>
            <link>http://forums.dreamincode.net/showtopic59488.htm</link>
            <title>Determining the distance between two points in PHP Tutorials</title>
            <description>Hello&amp;#33;&lt;br /&gt;&lt;br /&gt;In this tutorial we are going to tackle a rather basic problem.  How to determine the distance between two points on an X,Y grid.  &lt;br /&gt;&lt;br /&gt;Our starting points for the purpose of this tutorial will be (1,-10) and (15,-20).  For our code we will be setting these values into variables but you can also accept them from a form as well as pull them from a MySQL database depending on your project.&lt;br /&gt;&lt;br /&gt;First we&amp;#39;ll set our variables and give them a value:&lt;br /&gt;&lt;br /&gt;&lt;!--c1--&gt;&lt;div class='codetop'&gt;CODE&lt;/div&gt;&lt;div class='codemain'&gt;&lt;!--ec1--&gt;&lt;br /&gt;&lt;br /&gt;&amp;#60;?php&lt;br /&gt;&lt;br /&gt;// first coordinate set &amp;#40;1,-10&amp;#41;&lt;br /&gt;&amp;#036;x1 = 1;&lt;br /&gt;&amp;#036;y1 = -10;&lt;br /&gt;&lt;br /&gt;// second coordinate set &amp;#40;15,-20&amp;#41;&lt;br /&gt;&amp;#036;x2 = 15;&lt;br /&gt;&amp;#036;y2 = -20;&lt;br /&gt;&lt;br /&gt;&lt;!--c2--&gt;&lt;/div&gt;&lt;!--ec2--&gt;&lt;br /&gt;&lt;br /&gt;The next step is to subtract the x coordinate of the first set from the second set.  We also need to do the same for the y coordinates.  Then both values need to be squared.   This is easily accomplished using PHP&amp;#39;s [il]pow(x,y)[/il] function.  X equals the value to be raised, y equals the power to raise it by.  If you want to know the value of 10 to the 9th power it would like [il]pow(10,9)[/il]&lt;br /&gt;&lt;br /&gt;&lt;!--c1--&gt;&lt;div class='codetop'&gt;CODE&lt;/div&gt;&lt;div class='codemain'&gt;&lt;!--ec1--&gt;&lt;br /&gt;&lt;br /&gt;&amp;#036;x = &amp;#40; pow&amp;#40;&amp;#036;x2,2&amp;#41; - pow&amp;#40;&amp;#036;x1,2&amp;#41;&amp;#41;;&lt;br /&gt;&amp;#036;y = &amp;#40; pow&amp;#40;&amp;#036;y2,2&amp;#41; - pow&amp;#40;&amp;#036;y1,2&amp;#41;&amp;#41;;&lt;br /&gt;&lt;br /&gt;&lt;!--c2--&gt;&lt;/div&gt;&lt;!--ec2--&gt;&lt;br /&gt;&lt;br /&gt;The last step is to add these two values and find the square root.&lt;br /&gt;&lt;br /&gt;&lt;!--c1--&gt;&lt;div class='codetop'&gt;CODE&lt;/div&gt;&lt;div class='codemain'&gt;&lt;!--ec1--&gt;&lt;br /&gt;&lt;br /&gt;&amp;#036;distance = &amp;#40; sqrt&amp;#40;&amp;#036;x + &amp;#036;y&amp;#41; &amp;#41;;&lt;br /&gt;&lt;br /&gt;&lt;!--c2--&gt;&lt;/div&gt;&lt;!--ec2--&gt;&lt;br /&gt;&lt;br /&gt;For our example the final answer is 22.891046284519.  If you want to round this value to nearest full number you can use the [il]round()[/il] function, which will round the number up &lt;b&gt;or&lt;/b&gt; down.   If you want to round to a certain number of decimal places, you can specify that such as [il]round(11,3)[/il]&lt;br /&gt;&lt;br /&gt;[il]round(&amp;#036;distance)[/il] returns the value: 23&lt;br /&gt;&lt;br /&gt;[il]round(&amp;#036;distance,2)[/il] returns the value: 22.89&lt;br /&gt;&lt;br /&gt;Putting it all together our code looks like:&lt;br /&gt;&lt;br /&gt;&lt;!--c1--&gt;&lt;div class='codetop'&gt;CODE&lt;/div&gt;&lt;div class='codemain'&gt;&lt;!--ec1--&gt;&lt;br /&gt;&lt;br /&gt;&amp;#60;?php&lt;br /&gt;&lt;br /&gt;// first coordinate set &amp;#40;1,-10&amp;#41;&lt;br /&gt;&amp;#036;x1 = 1;&lt;br /&gt;&amp;#036;y1 = -10;&lt;br /&gt;&lt;br /&gt;// second coordinate set &amp;#40;15,-20&amp;#41;&lt;br /&gt;&amp;#036;x2 = 15;&lt;br /&gt;&amp;#036;y2 = -20;&lt;br /&gt;&lt;br /&gt;&amp;#036;x = &amp;#40; pow&amp;#40;&amp;#036;x2,2&amp;#41; - pow&amp;#40;&amp;#036;x1,2&amp;#41;&amp;#41;;&lt;br /&gt;&amp;#036;y = &amp;#40; pow&amp;#40;&amp;#036;y2,2&amp;#41; - pow&amp;#40;&amp;#036;y1,2&amp;#41;&amp;#41;;&lt;br /&gt;&lt;br /&gt;&amp;#036;distance = &amp;#40; sqrt&amp;#40;&amp;#036;x + &amp;#036;y&amp;#41; &amp;#41;;&lt;br /&gt;&lt;br /&gt;// Round to nearest full number&lt;br /&gt;&lt;br /&gt;&amp;#036;roundtofull = round&amp;#40;&amp;#036;distance&amp;#41;;&lt;br /&gt;&amp;#036;roundto2places = round&amp;#40;&amp;#036;distance,2&amp;#41;;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;?&amp;#62;&lt;br /&gt;&lt;br /&gt;&lt;!--c2--&gt;&lt;/div&gt;&lt;!--ec2--&gt;&lt;br /&gt;&lt;br /&gt;This tutorial is really basic in showing how to determine distance, but I do hope you found it useful. &lt;br /&gt;</description>
			<pubDate>Sun, 03 Aug 2008 03:11:00 -0600</pubDate>
			<category>PHP Tutorials</category>
			<author>rjolitz</author>
         </item>
         <item>
            <link>http://forums.dreamincode.net/showtopic59436.htm</link>
            <title>Make a Bomb in Photoshop Tutorials</title>
            <description>First start by creating a new document (&lt;b&gt;500 x 500&lt;/b&gt;). After that select the type(text) tool. Choose Wingdings for the font and 400 for the font size &lt;i&gt;see figure 1 for more details&lt;/i&gt;.&lt;br /&gt;                    &lt;i&gt;&lt;b&gt;Figure 1&lt;/b&gt;&lt;/i&gt;                &lt;br /&gt;                  &lt;img src=&quot;http://i15.photobucket.com/albums/a353/guidobertoli/Dream%20In%20Code/Tutorials/Photoshop/Bomb/textsettings.png&quot; border=&quot;0&quot; alt=&quot;IPB Image&quot; /&gt;&lt;br /&gt;&lt;br /&gt;          Type a capital M. If you have done this right there should be a bomb shape for your text, &lt;i&gt;see figure 2 for example&lt;/i&gt;. Duplicate the text layer and then rasterize it by going to Layer-&amp;gt;Rasterize-&amp;gt;Type, and name this layer to bomb.&lt;br /&gt;                      &lt;i&gt;&lt;b&gt;Figure 2&lt;/b&gt;&lt;/i&gt;&lt;br /&gt;                  &lt;img src=&quot;http://i15.photobucket.com/albums/a353/guidobertoli/Dream%20In%20Code/Tutorials/Photoshop/Bomb/bombtext.png&quot; border=&quot;0&quot; alt=&quot;IPB Image&quot; /&gt;&lt;br /&gt;                &lt;br /&gt;&lt;br /&gt;Then make a duplicate of the bomb layer two more times, name it fuse, and sparks.&lt;br /&gt;&lt;br /&gt;          Select the bomb layer by ctrl clicking the layer. Delete the sparks with your wand tool, start by alt clicking the black sparks, this will deselect the sparks. Press Shift + Ctrl + I to inverse the selection, then press delete, this will delete the sparks from the layer. Still on the bomb layer Ctrl + Click the layer to select it once again. You can use any way you feel like for this next part. First Alt + Click &amp;amp; Drag a selection around the fuse. If you did this correctly only the round part of the bomb and the piece that connects the bomb with the fuse will be selected. Shift + Ctrl + I again to inverse the selection, press delete to delete the selection. &lt;i&gt;see figures 3 and 4 for details&lt;/i&gt;.&lt;br /&gt;          &lt;br /&gt;                    &lt;i&gt;&lt;b&gt;Figure 3&lt;/b&gt; &lt;/i&gt;&lt;br /&gt;                &lt;img src=&quot;http://i15.photobucket.com/albums/a353/guidobertoli/Dream%20In%20Code/Tutorials/Photoshop/Bomb/nosparks.png&quot; border=&quot;0&quot; alt=&quot;IPB Image&quot; /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;                        &lt;i&gt;&lt;b&gt;Figure 4&lt;/b&gt; &lt;/i&gt;&lt;br /&gt;                &lt;br /&gt;                &lt;img src=&quot;http://i15.photobucket.com/albums/a353/guidobertoli/Dream%20In%20Code/Tutorials/Photoshop/Bomb/nosparksnofuse.png&quot; border=&quot;0&quot; alt=&quot;IPB Image&quot; /&gt;&lt;br /&gt;              &lt;br /&gt;	Then add a gradient overlay to this layer, &lt;i&gt;figures 5 &amp;amp; 6 have gradient settings&lt;/i&gt;. After add an inner glow to the layer, &lt;i&gt;figure 7 has glow settings&lt;/i&gt;.&lt;br /&gt;          &lt;br /&gt;                    &lt;i&gt;&lt;b&gt;Figure 5&lt;/b&gt; &lt;/i&gt;&lt;br /&gt;                &lt;br /&gt;                &lt;img src=&quot;http://i15.photobucket.com/albums/a353/guidobertoli/Dream%20In%20Code/Tutorials/Photoshop/Bomb/gradientoverlay.png&quot; border=&quot;0&quot; alt=&quot;IPB Image&quot; /&gt;&lt;br /&gt;              &lt;br /&gt;&lt;br /&gt;          &lt;i&gt;&lt;b&gt;Figure 6&lt;/b&gt; &lt;/i&gt;&lt;br /&gt;              &lt;br /&gt;                &lt;img src=&quot;http://i15.photobucket.com/albums/a353/guidobertoli/Dream%20In%20Code/Tutorials/Photoshop/Bomb/gradientsettings.png&quot; border=&quot;0&quot; alt=&quot;IPB Image&quot; /&gt;&lt;br /&gt;              &lt;br /&gt;        &lt;i&gt;&lt;b&gt;Figure 7&lt;/b&gt; &lt;/i&gt;&lt;br /&gt;            &lt;br /&gt;              &lt;img src=&quot;http://i15.photobucket.com/albums/a353/guidobertoli/Dream%20In%20Code/Tutorials/Photoshop/Bomb/nosparks.png&quot; border=&quot;0&quot; alt=&quot;IPB Image&quot; /&gt;&lt;br /&gt;            &lt;br /&gt;          This is what you should have.&lt;br /&gt;          &lt;br /&gt;          &lt;i&gt;&lt;b&gt;Figure 8&lt;/b&gt; &lt;/i&gt;&lt;br /&gt;                &lt;br /&gt;                &lt;img src=&quot;http://i15.photobucket.com/albums/a353/guidobertoli/Dream%20In%20Code/Tutorials/Photoshop/Bomb/roundpiecefinished.png&quot; border=&quot;0&quot; alt=&quot;IPB Image&quot; /&gt;&lt;br /&gt;              &lt;br /&gt;&lt;br /&gt;	Do the exact same thing on the spark and the fuse layer only for the fuse layer have only the fuse selected, for the spark layer only the sparks selected, after that we will apply some styles to those layers.&lt;br /&gt;          &lt;br /&gt;                    &lt;i&gt;&lt;b&gt;Figure 9&lt;/b&gt; &lt;/i&gt;&lt;br /&gt;              &lt;br /&gt;                &lt;img src=&quot;http://i15.photobucket.com/albums/a353/guidobertoli/Dream%20In%20Code/Tutorials/Photoshop/Bomb/nostyleonsparkfuse.png&quot; border=&quot;0&quot; alt=&quot;IPB Image&quot; /&gt;&lt;br /&gt;&lt;br /&gt;              Now go to File-&amp;gt;New, set the height &amp;amp; width properties to 8 pixels. Select the rectangular marquee tool and drag four pixel while holding down shift,&lt;i&gt; seen in figure 10&lt;/i&gt;.          &lt;br /&gt;&lt;br /&gt;          &lt;i&gt;&lt;b&gt;Figure 10&lt;/b&gt; &lt;/i&gt;&lt;br /&gt;                &lt;br /&gt;&lt;img src=&quot;http://i15.photobucket.com/albums/a353/guidobertoli/Dream%20In%20Code/Tutorials/Photoshop/Bomb/patternselection.png&quot; border=&quot;0&quot; alt=&quot;IPB Image&quot; /&gt;&lt;br /&gt;              &lt;br /&gt;&lt;br /&gt;          Fill the selection with white, move the selection to the right 4 pixels by hitting the right arrow key 4 times, fill the selection with black, press the down arrow key 4 times and fill it with white, then the left 4 times and fill with black,&lt;i&gt; seen in figure 11&lt;/i&gt;. &lt;br /&gt;          &lt;br /&gt;                    &lt;i&gt;&lt;b&gt;Figure 11&lt;/b&gt; &lt;/i&gt;&lt;br /&gt;                &lt;br /&gt;                &lt;img src=&quot;http://i15.photobucket.com/albums/a353/guidobertoli/Dream%20In%20Code/Tutorials/Photoshop/Bomb/fusepattern.png&quot; border=&quot;0&quot; alt=&quot;IPB Image&quot; /&gt;&lt;br /&gt;              &lt;br /&gt;&lt;br /&gt;          Go to Edit-&amp;gt;Define Pattern. Go back to the bomb document and select the fuse layer. We will add a few layer styles to it. First layer style is a gradient overlay, &lt;i&gt;see figure 12 &amp;amp; 13&lt;/i&gt;. Then we will add a pattern overlay, the pattern will be the pattern that we just created previously, &lt;i&gt;see figure 14&lt;/i&gt;. The last layer style we will add to the fuse layer is an inner glow, &lt;i&gt;see figure 15&lt;/i&gt;.&lt;br /&gt;          &lt;br /&gt;                    &lt;i&gt;&lt;b&gt;Figure 12&lt;/b&gt; &lt;/i&gt;&lt;br /&gt;              &lt;br /&gt;          &lt;img src=&quot;http://i15.photobucket.com/albums/a353/guidobertoli/Dream%20In%20Code/Tutorials/Photoshop/Bomb/fusegradientoverlay.png&quot; border=&quot;0&quot; alt=&quot;IPB Image&quot; /&gt;&lt;br /&gt;              &lt;br /&gt;          &lt;i&gt;&lt;b&gt;Figure 13&lt;/b&gt; &lt;/i&gt;&lt;br /&gt;                &lt;br /&gt;&lt;img src=&quot;http://i15.photobucket.com/albums/a353/guidobertoli/Dream%20In%20Code/Tutorials/Photoshop/Bomb/fusegradientoverlaysettings.png&quot; border=&quot;0&quot; alt=&quot;IPB Image&quot; /&gt;&lt;br /&gt;              &lt;br /&gt;          &lt;i&gt;&lt;b&gt;Figure 14&lt;/b&gt; &lt;/i&gt;&lt;br /&gt;              &lt;br /&gt;                &lt;img src=&quot;http://i15.photobucket.com/albums/a353/guidobertoli/Dream%20In%20Code/Tutorials/Photoshop/Bomb/fusepatternoverlay.png&quot; border=&quot;0&quot; alt=&quot;IPB Image&quot; /&gt;&lt;br /&gt;              &lt;br /&gt;          &lt;i&gt;&lt;b&gt;Figure 15&lt;/b&gt; &lt;/i&gt;&lt;br /&gt;       &lt;br /&gt;       &lt;img src=&quot;http://i15.photobucket.com/albums/a353/guidobertoli/Dream%20In%20Code/Tutorials/Photoshop/Bomb/innerglow.png&quot; border=&quot;0&quot; alt=&quot;IPB Image&quot; /&gt;&lt;br /&gt;              &lt;br /&gt;This is what you should have, &lt;i&gt;see figure 16&lt;/i&gt;.&lt;br /&gt;&lt;br /&gt;                    &lt;i&gt;&lt;b&gt;Figure 16&lt;/b&gt; &lt;/i&gt;&lt;br /&gt;                &lt;br /&gt;              &lt;br /&gt;              &lt;br /&gt;&lt;img src=&quot;http://i15.photobucket.com/albums/a353/guidobertoli/Dream%20In%20Code/Tutorials/Photoshop/Bomb/nostyleonspark.png&quot; border=&quot;0&quot; alt=&quot;IPB Image&quot; /&gt;&lt;br /&gt;              &lt;br /&gt;          We are almost done&amp;#33;&lt;br /&gt;&lt;br /&gt;          Now select the spark layer, we will be adding a gradient overlay, &lt;i&gt;see figures 17 &amp;amp; 18&lt;/i&gt;.&lt;br /&gt;&lt;br /&gt;                    &lt;i&gt;&lt;b&gt;Figure 17&lt;/b&gt; &lt;/i&gt;&lt;br /&gt;              &lt;br /&gt;                &lt;img src=&quot;http://i15.photobucket.com/albums/a353/guidobertoli/Dream%20In%20Code/Tutorials/Photoshop/Bomb/sparkgradientoverlay.png&quot; border=&quot;0&quot; alt=&quot;IPB Image&quot; /&gt;&lt;br /&gt;              &lt;br /&gt;          &lt;i&gt;&lt;b&gt;Figure 18&lt;/b&gt; &lt;/i&gt;&lt;br /&gt;                &lt;br /&gt;                &lt;img src=&quot;http://i15.photobucket.com/albums/a353/guidobertoli/Dream%20In%20Code/Tutorials/Photoshop/Bomb/sparkgradientoverlaysettings.png&quot; border=&quot;0&quot; alt=&quot;IPB Image&quot; /&gt;&lt;br /&gt;          &lt;br /&gt;Finished&amp;#33;&lt;br /&gt;         &lt;br /&gt; This is what you should have.&lt;br /&gt;          &lt;br /&gt;            &lt;img src=&quot;http://i15.photobucket.com/albums/a353/guidobertoli/Dream%20In%20Code/Tutorials/Photoshop/Bomb/Final.png&quot; border=&quot;0&quot; alt=&quot;IPB Image&quot; /&gt;&lt;br /&gt;          &lt;br /&gt;Have Fun&amp;#33;&lt;br /&gt;Don&amp;#39;t Forget I Have Included the PSD File [attachmentid=7850] &amp;amp; [attachmentid=7851].</description>
			<pubDate>Sat, 02 Aug 2008 14:49:18 -0600</pubDate>
			<category>Photoshop Tutorials</category>
			<author>gbertoli3</author>
         </item>
         <item>
            <link>http://forums.dreamincode.net/showtopic59368.htm</link>
            <title>Zen and the Art of Debugging C/C++ Programs in Visual Studio in C++ Tutorials</title>
            <description>Approved.&lt;br /&gt;Well done jwwicks.</description>
			<pubDate>Sun, 03 Aug 2008 01:29:11 -0600</pubDate>
			<category>C++ Tutorials</category>
			<author>born2c0de</author>
         </item>
         <item>
            <link>http://forums.dreamincode.net/showtopic59169.htm</link>
            <title>Looping in Java Tutorials</title>
            <description>Oh&amp;#33;  &lt;!--sizeo:2--&gt;&lt;span style=&quot;font-size:10pt;line-height:100%&quot;&gt;&lt;!--/sizeo--&gt;&lt;b&gt;GOOD CALL&lt;/b&gt;&lt;!--sizec--&gt;&lt;/span&gt;&lt;!--/sizec--&gt;&lt;br /&gt;&lt;br /&gt;I&amp;#39;ll edit it right now, while I&amp;#39;m thinking about it.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;EDIT:&lt;/b&gt; Crap, it&amp;#39;s too old to edit, but I&amp;#39;ve got a plan B. &lt;img src=&quot;style_emoticons/&lt;#EMO_DIR#&gt;/smile.gif&quot; style=&quot;vertical-align:middle&quot; emoid=&quot;:)&quot; border=&quot;0&quot; alt=&quot;smile.gif&quot; /&gt;</description>
			<pubDate>Thu, 07 Aug 2008 17:20:50 -0600</pubDate>
			<category>Java Tutorials</category>
			<author>Locke37</author>
         </item>
         <item>
            <link>http://forums.dreamincode.net/showtopic59152.htm</link>
            <title>Introduction to Designing Classes II in Java Tutorials</title>
            <description>Well, this is me second tutorial on designing classes with static methods.  I&amp;#39;m gonna start you off with a very basic example: the &lt;!--coloro:#663366--&gt;&lt;span style=&quot;color:#663366&quot;&gt;&lt;!--/coloro--&gt;Math&lt;!--colorc--&gt;&lt;/span&gt;&lt;!--/colorc--&gt; class.&lt;br /&gt;&lt;br /&gt;This class has methods that do mathematical calculations...I know...duh.  Well, the methods of the &lt;!--coloro:#663366--&gt;&lt;span style=&quot;color:#663366&quot;&gt;&lt;!--/coloro--&gt;Math&lt;!--colorc--&gt;&lt;/span&gt;&lt;!--/colorc--&gt; class are static, meaning you don&amp;#39;t have to instantiate an object to call them.  You can simply call them like this.&lt;br /&gt;&lt;br /&gt;[code=java]Math.METHOD(parameters_used);[/code]&lt;br /&gt;&lt;br /&gt;You DON&amp;#39;T have to do this:&lt;br /&gt;&lt;br /&gt;[code=java]Math mathObject = new Math();&lt;br /&gt;&lt;br /&gt;mathObject.METHOD();[/code]&lt;br /&gt;&lt;br /&gt;You don&amp;#39;t HAVE to do it that way...but you can, but no one I know does that. &lt;img src=&quot;style_emoticons/&lt;#EMO_DIR#&gt;/smile.gif&quot; style=&quot;vertical-align:middle&quot; emoid=&quot;:)&quot; border=&quot;0&quot; alt=&quot;smile.gif&quot; /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Well, we&amp;#39;ll make a method that does a very simple calculation.  We&amp;#39;ll make an [il]addition()[/il] method.&lt;br /&gt;&lt;br /&gt;[code=java]class OurMath&lt;br /&gt;{&lt;br /&gt;    public static int addition(int a, int &lt;img src=&quot;style_emoticons/&lt;#EMO_DIR#&gt;/cool.gif&quot; style=&quot;vertical-align:middle&quot; emoid=&quot;B)&quot; border=&quot;0&quot; alt=&quot;cool.gif&quot; /&gt;&lt;br /&gt;    {&lt;br /&gt;        int sum = a + b;&lt;br /&gt;&lt;br /&gt;        return sum;&lt;br /&gt;    }&lt;br /&gt;}[/code]&lt;br /&gt;&lt;br /&gt;Notice how we declare our &lt;!--coloro:#3333FF--&gt;&lt;span style=&quot;color:#3333FF&quot;&gt;&lt;!--/coloro--&gt;int&lt;!--colorc--&gt;&lt;/span&gt;&lt;!--/colorc--&gt; return type for the method.  This makes it so we can assign it to an &lt;!--coloro:#3333FF--&gt;&lt;span style=&quot;color:#3333FF&quot;&gt;&lt;!--/coloro--&gt;int&lt;!--colorc--&gt;&lt;/span&gt;&lt;!--/colorc--&gt; variable in a main method or something.&lt;br /&gt;&lt;br /&gt;Also notice the &lt;!--coloro:#3333FF--&gt;&lt;span style=&quot;color:#3333FF&quot;&gt;&lt;!--/coloro--&gt;int&lt;!--colorc--&gt;&lt;/span&gt;&lt;!--/colorc--&gt; variables in the parentheses.  These are called &amp;#39;parameters&amp;#39;.  These are what&amp;#39;s going to be included in the method when it&amp;#39;s called.&lt;br /&gt;&lt;br /&gt;It&amp;#39;s going to be called like this.&lt;br /&gt;&lt;br /&gt;[code=java]int number = OurMath.addition(1, 3);[/code]&lt;br /&gt;&lt;br /&gt;Well, anyone know what [il]number[/il] is going to equal after that?  That&amp;#39;s right&amp;#33;  Four&amp;#33;&lt;br /&gt;&lt;br /&gt;You HAVE to call the method with 2 parameters.  If you call it any other way, you&amp;#39;re going to going to get a compile error saying something along these lines.  [il]cannot find symbol: method addition(int)[/il]&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;!--sizeo:2--&gt;&lt;span style=&quot;font-size:10pt;line-height:100%&quot;&gt;&lt;!--/sizeo--&gt;&lt;b&gt;&lt;!--coloro:#3333FF--&gt;&lt;span style=&quot;color:#3333FF&quot;&gt;&lt;!--/coloro--&gt;static&lt;!--colorc--&gt;&lt;/span&gt;&lt;!--/colorc--&gt; &lt;u&gt;Class Variables&lt;/u&gt;&lt;/b&gt;&lt;!--sizec--&gt;&lt;/span&gt;&lt;!--/sizec--&gt;&lt;br /&gt;&lt;br /&gt;Well, back again to the &lt;!--coloro:#663366--&gt;&lt;span style=&quot;color:#663366&quot;&gt;&lt;!--/coloro--&gt;Math&lt;!--colorc--&gt;&lt;/span&gt;&lt;!--/colorc--&gt; class.  It has 2 &lt;!--coloro:#3333FF--&gt;&lt;span style=&quot;color:#3333FF&quot;&gt;&lt;!--/coloro--&gt;double&lt;!--colorc--&gt;&lt;/span&gt;&lt;!--/colorc--&gt; variables, E, and PI.&lt;br /&gt;&lt;br /&gt;[code=java]class Math&lt;br /&gt;{&lt;br /&gt;    static double E = 2.718...;&lt;br /&gt;    static double PI = 3.141596535...;&lt;br /&gt;}[/code]&lt;br /&gt;&lt;br /&gt;This is how we declare &lt;!--coloro:#3333FF--&gt;&lt;span style=&quot;color:#3333FF&quot;&gt;&lt;!--/coloro--&gt;static&lt;!--colorc--&gt;&lt;/span&gt;&lt;!--/colorc--&gt; (AKA &amp;quot;class&amp;quot;) variables.  Let&amp;#39;s go back to our class...&lt;br /&gt;&lt;br /&gt;[code=java]class OurMath&lt;br /&gt;{&lt;br /&gt;    static int five = 5;&lt;br /&gt;&lt;br /&gt;    public static int addition(int a, int &lt;img src=&quot;style_emoticons/&lt;#EMO_DIR#&gt;/cool.gif&quot; style=&quot;vertical-align:middle&quot; emoid=&quot;B)&quot; border=&quot;0&quot; alt=&quot;cool.gif&quot; /&gt;&lt;br /&gt;    {&lt;br /&gt;        int sum = a + b;&lt;br /&gt;&lt;br /&gt;        return sum;&lt;br /&gt;    }&lt;br /&gt;}[/code]&lt;br /&gt;&lt;br /&gt;Well, now we can access our [il]five[/il] variable through doing this.&lt;br /&gt;&lt;br /&gt;[code=java]int ourVariable = OurMath.five;[/code]&lt;br /&gt;&lt;br /&gt;The main reason of using &lt;!--coloro:#3333FF--&gt;&lt;span style=&quot;color:#3333FF&quot;&gt;&lt;!--/coloro--&gt;static&lt;!--colorc--&gt;&lt;/span&gt;&lt;!--/colorc--&gt; is to make non &amp;quot;object&amp;quot;-ive data or methods.&lt;br /&gt;&lt;br /&gt;Hope this tutorial helped anyone that needed it&amp;#33; &lt;img src=&quot;style_emoticons/&lt;#EMO_DIR#&gt;/biggrin.gif&quot; style=&quot;vertical-align:middle&quot; emoid=&quot;:D&quot; border=&quot;0&quot; alt=&quot;biggrin.gif&quot; /&gt;</description>
			<pubDate>Wed, 30 Jul 2008 17:22:14 -0600</pubDate>
			<category>Java Tutorials</category>
			<author>Locke37</author>
         </item>
         <item>
            <link>http://forums.dreamincode.net/showtopic59043.htm</link>
            <title>Intro to Designing Classes in Java Tutorials</title>
            <description>Well, if you&amp;#39;re reading this, you most likely do not have a lot of Java experience.  No offense to anyone that reads this that DOES have a bit of experience, but this is just a basic concept.&lt;br /&gt;&lt;br /&gt;Well, let&amp;#39;s get started. &lt;img src=&quot;style_emoticons/&lt;#EMO_DIR#&gt;/smile.gif&quot; style=&quot;vertical-align:middle&quot; emoid=&quot;:)&quot; border=&quot;0&quot; alt=&quot;smile.gif&quot; /&gt;&lt;br /&gt;&lt;br /&gt;Now then, classes that have a need of objects...this is what we are going to make.&lt;br /&gt;&lt;br /&gt;I&amp;#39;ll start with an example that some people like, a Character (for a game) class.  Let&amp;#39;s start with the basic header for our class and a constructor.&lt;br /&gt;&lt;br /&gt;[code=java]class Character&lt;br /&gt;{&lt;br /&gt;	public Character()&lt;br /&gt;	{&lt;br /&gt;	}&lt;br /&gt;}[/code]&lt;br /&gt;&lt;br /&gt;Well, this is not a very good class yet, since we don&amp;#39;t have any data...or methods...or anything besides a constructor...so let&amp;#39;s add some&amp;#33;  A Character needs HP and MP, right?&lt;br /&gt;&lt;br /&gt;[code=java]class Character&lt;br /&gt;{&lt;br /&gt;	private int HP;&lt;br /&gt;	private int MP;&lt;br /&gt;&lt;br /&gt;	public Character()&lt;br /&gt;	{&lt;br /&gt;	}&lt;br /&gt;}[/code]&lt;br /&gt;&lt;br /&gt;Notice how we declare these private.  Declaring them private makes it so that they can only be accessed and/or changed by members of it&amp;#39;s own class. (AKA the methods)&lt;br /&gt;&lt;br /&gt;Well, now we have data, but it&amp;#39;s never initialized, let&amp;#39;s add that real quick. &lt;img src=&quot;style_emoticons/&lt;#EMO_DIR#&gt;/smile.gif&quot; style=&quot;vertical-align:middle&quot; emoid=&quot;:)&quot; border=&quot;0&quot; alt=&quot;smile.gif&quot; /&gt;&lt;br /&gt;&lt;br /&gt;[code=java]class Character&lt;br /&gt;{&lt;br /&gt;	private int HP;&lt;br /&gt;	private int MP;&lt;br /&gt;&lt;br /&gt;	public Character(int hp, int mp)&lt;br /&gt;	{&lt;br /&gt;		this.HP = hp;&lt;br /&gt;		this.MP = mp;&lt;br /&gt;	}&lt;br /&gt;}[/code]&lt;br /&gt;&lt;br /&gt;Notice the &lt;!--coloro:#3333FF--&gt;&lt;span style=&quot;color:#3333FF&quot;&gt;&lt;!--/coloro--&gt;this&lt;!--colorc--&gt;&lt;/span&gt;&lt;!--/colorc--&gt; keyword.  This is a pointer to the object that is calling the method.  [il]OBJECT.method();[/il], in that case, the &lt;!--coloro:#3333FF--&gt;&lt;span style=&quot;color:#3333FF&quot;&gt;&lt;!--/coloro--&gt;this&lt;!--colorc--&gt;&lt;/span&gt;&lt;!--/colorc--&gt; keyword will reference [il]OBJECT[/il].&lt;br /&gt;&lt;br /&gt;Now we have some HP and MP.  Hooray&amp;#33;  But now we need some methods to access these variables, right?  RIGHT, Locke&amp;#33;&lt;br /&gt;&lt;br /&gt;[code=java]class Character&lt;br /&gt;{&lt;br /&gt;	private int HP;&lt;br /&gt;	private int MP;&lt;br /&gt;&lt;br /&gt;	public Character(int hp, int mp)&lt;br /&gt;	{&lt;br /&gt;		this.HP = hp;&lt;br /&gt;		this.MP = mp;&lt;br /&gt;	}&lt;br /&gt;&lt;br /&gt;	public int getHP()&lt;br /&gt;	{&lt;br /&gt;		return this.HP;&lt;br /&gt;	}&lt;br /&gt;&lt;br /&gt;	public int getMP()&lt;br /&gt;	{&lt;br /&gt;		return this.MP;&lt;br /&gt;	}&lt;br /&gt;}[/code]&lt;br /&gt;&lt;br /&gt;Well, now we have some methods to access our attributes.  Notice the return type before the method heading...it NEEDS to be there, so that the method knows what it&amp;#39;s supposed to be returning.  The return type is always the same as the variable type in basic return methods like these, that just access and tell you what the variable value is.&lt;br /&gt;&lt;br /&gt;So if we have something like this...&lt;br /&gt;&lt;br /&gt;[code=java]private String name;&lt;br /&gt;&lt;br /&gt;public String getName()&lt;br /&gt;{&lt;br /&gt;	return this.name;&lt;br /&gt;}[/code]&lt;br /&gt;&lt;br /&gt;Well, with our &lt;!--coloro:#3333FF--&gt;&lt;span style=&quot;color:#3333FF&quot;&gt;&lt;!--/coloro--&gt;private&lt;!--colorc--&gt;&lt;/span&gt;&lt;!--/colorc--&gt; declaration, we can&amp;#39;t change our HP or MP values, only access them with our methods, so let&amp;#39;s add some [il]set[/il] methods.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;[code=java]class Character&lt;br /&gt;{&lt;br /&gt;	private int HP;&lt;br /&gt;	private int MP;&lt;br /&gt;&lt;br /&gt;	public Character(int hp, int mp)&lt;br /&gt;	{&lt;br /&gt;		this.HP = hp;&lt;br /&gt;		this.MP = mp;&lt;br /&gt;	}&lt;br /&gt;&lt;br /&gt;	public int getHP()&lt;br /&gt;	{&lt;br /&gt;		return this.HP;&lt;br /&gt;	}&lt;br /&gt;&lt;br /&gt;	public int getMP()&lt;br /&gt;	{&lt;br /&gt;		return this.MP;&lt;br /&gt;	}&lt;br /&gt;&lt;br /&gt;	public void setHP(int newHP)&lt;br /&gt;	{&lt;br /&gt;		this.HP = newHP;&lt;br /&gt;	}&lt;br /&gt;&lt;br /&gt;	public void setMP(int newMP)&lt;br /&gt;	{&lt;br /&gt;		this.MP = newMP;&lt;br /&gt;	}&lt;br /&gt;}[/code]&lt;br /&gt;&lt;br /&gt;We MUST have parameters in our [il]set[/il] methods, or Java doesn&amp;#39;t know what to set the values to.  Notice the &lt;!--coloro:#3333FF--&gt;&lt;span style=&quot;color:#3333FF&quot;&gt;&lt;!--/coloro--&gt;void&lt;!--colorc--&gt;&lt;/span&gt;&lt;!--/colorc--&gt; keyword in the method heading...this means the method doesn&amp;#39;t return anything.  It doesn&amp;#39;t need to, since it&amp;#39;s just changing values.&lt;br /&gt;&lt;br /&gt;Well, this is just a very basic tutorial in designing Object methods/classes.  If you want to know how to make Static classes, you can PM me if my next tutorial does not get approved.&lt;br /&gt;&lt;br /&gt;Bye&amp;#33; &lt;img src=&quot;style_emoticons/&lt;#EMO_DIR#&gt;/biggrin.gif&quot; style=&quot;vertical-align:middle&quot; emoid=&quot;:D&quot; border=&quot;0&quot; alt=&quot;biggrin.gif&quot; /&gt;</description>
			<pubDate>Tue, 29 Jul 2008 15:28:24 -0600</pubDate>
			<category>Java Tutorials</category>
			<author>Locke37</author>
         </item>
         <item>
            <link>http://forums.dreamincode.net/showtopic58940.htm</link>
            <title>AS 2.0 Basics 4 - Functions in Flash Tutorials</title>
            <description>Functions make doing things multiple times with different values very fast and easy, however if you haven&amp;#39;t used functions in the past they can be quite a hassle to get into and understand.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Hello Function&lt;/b&gt; &lt;i&gt;new to the world&lt;/i&gt;&lt;br /&gt;&lt;br /&gt;We will start by creating a simple function that will trace (output) [inline]Hello Function&amp;#33;[/inline]. This is accomplished by creating a function which you define like so [inline]function FUNCTION_NAME()[/inline]. For our current purposes we will name the functio [inline]hello_func[/inline] like so: [inline]function hello_func()[/inline]. The [inline]()[/inline] is where you typically place any parameters (variables) that you are passing into the function for its use, however for the moment we will be passing in no parameters so it is left blank. Now we just need to tell the code where the function starts and ends and what to do. This is accomplished with the following code.&lt;br /&gt;&lt;br /&gt;&lt;!--c1--&gt;&lt;div class='codetop'&gt;CODE&lt;/div&gt;&lt;div class='codemain'&gt;&lt;!--ec1--&gt;&lt;br /&gt;function hello_func&amp;#40;&amp;#41;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;trace&amp;#40;&amp;#34;Hello Function&amp;#33;&amp;#34;&amp;#41;;&lt;br /&gt;}&lt;!--c2--&gt;&lt;/div&gt;&lt;!--ec2--&gt;&lt;br /&gt;&lt;br /&gt;NOTE - This is all the code needed to declare our function&amp;#33;&lt;br /&gt;&lt;br /&gt;Now we have a working function, but how do we get it to run?&lt;br /&gt;This is a fairly simple answer, you call the function. This can be done by typing the function name and then the () with any parameters needed (in this case it is none). [inline]hello_func();[/inline]&lt;br /&gt;&lt;br /&gt;OUTPUT - [inline]Hello Function&amp;#33;[/inline].&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Returning a value&lt;/b&gt;&lt;br /&gt;Functions are also usefule because they can return a value. Say you wanted to know what 2+2 was; a function could tell you. You declare the function in the same way as above, but this time we need to change the contents of the function. We no longer need the [inline]trace(&amp;quot;Hello Function&amp;#33;&amp;quot;);[/inline] so we can delete that. &lt;br /&gt;&lt;br /&gt;NOTE - For our purposes we will call this function adding (because add is a keyword in AS 2.0).&lt;br /&gt;&lt;br /&gt;Then we place a return statement that returns the sum of 2+2 (4).&lt;br /&gt;&lt;br /&gt;&lt;!--c1--&gt;&lt;div class='codetop'&gt;CODE&lt;/div&gt;&lt;div class='codemain'&gt;&lt;!--ec1--&gt;&lt;br /&gt;function adding&amp;#40;&amp;#41;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;return 2+2;&lt;br /&gt;}&lt;br /&gt;trace&amp;#40;adding&amp;#40;&amp;#41;&amp;#41;;&lt;br /&gt;&lt;!--c2--&gt;&lt;/div&gt;&lt;!--ec2--&gt;&lt;br /&gt;&lt;br /&gt;After we have finished  the function [inline]}[/inline] we will have the script trace the value returned.&lt;br /&gt;&lt;br /&gt;OUTPUT - [inline]4[/inline].&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Passing Parameters&lt;/b&gt;&lt;br /&gt;Functions also allow you to pass parameters into them. This can be quite useful (taking adding as an example) if you want to know the sum of 2 numbers that aren&amp;#39;t going to be available hard-coded into the function.&lt;br /&gt;&lt;br /&gt;You need to start off by telling the function what to expect, this is accomplished as such: [inline]function FUNCTION_NAME(PARAM_1, PARAM_2)[/inline]&lt;br /&gt;&lt;br /&gt;We will now begin rewriting our adding function to add different numbers and output the sum. To start out we need to have the function looking for things to add together. Our new function will be coded as so:&lt;br /&gt;&lt;br /&gt;&lt;!--c1--&gt;&lt;div class='codetop'&gt;CODE&lt;/div&gt;&lt;div class='codemain'&gt;&lt;!--ec1--&gt;&lt;br /&gt;function adding&amp;#40;a&amp;#58;Number, b&amp;#58;Number&amp;#41;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;return a+b;&lt;br /&gt;}&lt;br /&gt;&lt;!--c2--&gt;&lt;/div&gt;&lt;!--ec2--&gt;&lt;br /&gt;&lt;br /&gt;This takes 2 numbers (say 1 and 3) and adds them together then returns the value (1+3 = 4). Passing parameters into i is simple too: [inline]adding(1, 3);[/inline].&lt;br /&gt;&lt;br /&gt;Our new code looks like this:&lt;br /&gt;&lt;br /&gt;&lt;!--c1--&gt;&lt;div class='codetop'&gt;CODE&lt;/div&gt;&lt;div class='codemain'&gt;&lt;!--ec1--&gt;&lt;br /&gt;function adding&amp;#40;a&amp;#58;Number, b&amp;#58;Number&amp;#41;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;return a+b;&lt;br /&gt;}&lt;br /&gt;trace&amp;#40;adding&amp;#40;1, 3&amp;#41;&amp;#41;;&lt;br /&gt;&lt;!--c2--&gt;&lt;/div&gt;&lt;!--ec2--&gt;&lt;br /&gt;&lt;br /&gt;OUTPUT - [inline]4[/inline].&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Beginning Defensive Programming&lt;/b&gt; &lt;i&gt;so you don&amp;#39;t error out all the time&lt;/i&gt;&lt;br /&gt;Defensive programming (in this case) is making sure that you don&amp;#39;t allow undefined variables to be sent into the function. If we were to call adding like so: [inline]trace(adding(1, ));[/inline] the output looks like so:&lt;br /&gt;&lt;br /&gt;&lt;!--QuoteBegin-Flash 8 Output window+--&gt;&lt;div class='quotetop'&gt;QUOTE(Flash 8 Output window)&lt;/div&gt;&lt;div class='quotemain'&gt;&lt;!--QuoteEBegin--&gt;**Error** Scene=Scene 1, layer=Layer 1, frame=1:Line 1: Parameter name expected&lt;br /&gt;     function adding(var a:Number, var b:Number){&lt;br /&gt;&lt;br /&gt;**Error** Scene=Scene 1, layer=Layer 1, frame=1:Line 4: Unexpected &amp;#39;)&amp;#39; encountered&lt;br /&gt;     trace(adding(1, ));&lt;br /&gt;&lt;br /&gt;Total ActionScript Errors: 2 	 Reported Errors: 2&lt;!--QuoteEnd--&gt;&lt;/div&gt;&lt;!--QuoteEEnd--&gt;&lt;br /&gt;&lt;br /&gt;This isn&amp;#39;t what you want to have happen if you are going to be putting this out on the web for people to use. So, how do you cover the bases?&lt;br /&gt;&lt;br /&gt;It is a little easier than you would think, however Flash 8 (AS 2.0) requires some differing methods than normally used in programming languages. You have to use if statements (sadly, AS 2.0 doesn&amp;#39;t allow you to set initial values for parameters being sent to functions).&lt;br /&gt;&lt;br /&gt;For our case we will be checking if the numbers are set or not. If they aren&amp;#39;t we will set them to 0, otherwise we will leave them as they are.&lt;br /&gt;&lt;br /&gt;&lt;!--c1--&gt;&lt;div class='codetop'&gt;CODE&lt;/div&gt;&lt;div class='codemain'&gt;&lt;!--ec1--&gt;&lt;br /&gt;if&amp;#40;&amp;#33;a&amp;#41;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;a = 0;&lt;br /&gt;}&lt;br /&gt;if&amp;#40;&amp;#33;b&amp;#41;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;b = 0;&lt;br /&gt;}&lt;!--c2--&gt;&lt;/div&gt;&lt;!--ec2--&gt;&lt;br /&gt;&lt;br /&gt;Now we just place this in the top of our function and we have some basic defensive programming going on.&lt;br /&gt;&lt;br /&gt;&lt;!--c1--&gt;&lt;div class='codetop'&gt;CODE&lt;/div&gt;&lt;div class='codemain'&gt;&lt;!--ec1--&gt;function adding&amp;#40;a&amp;#58;Number, b&amp;#58;Number&amp;#41;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;if&amp;#40;&amp;#33;a&amp;#41;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;a = 0;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;if&amp;#40;&amp;#33;b&amp;#41;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;b = 0;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return a+b;&lt;br /&gt;}&lt;!--c2--&gt;&lt;/div&gt;&lt;!--ec2--&gt;&lt;br /&gt;&lt;br /&gt;If we were to call adding with only 1 variable (or none) it would now return an answer and not error out:&lt;br /&gt;[inline]trace(adding());[/inline] OUTPUT - [inline]0[/inline].&lt;br /&gt;[inline]trace(adding(1));[/inline] OUTPUT - [inline]1[/inline].&lt;br /&gt;[inline]trace(adding(0, 2));[/inline] OUTPUT - [inline]2[/inline];&lt;br /&gt;&lt;br /&gt;REALIZE - This is not perfect, if the user inputs something like ( , 2) it will still error out.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Returning Multiple Values&lt;/b&gt; &lt;i&gt;because one isn&amp;#39;t good enough&lt;/i&gt;&lt;br /&gt;If you have multiple variables that are found in a function and want them all to be returned you may think that comma (,) delimiting them will do the trick (as it does in some languages). However, Flash and AS 2.0 are not other languages and will not allow this. If you attempt to do it you will get the last value in the line of values to be returned and nothing else.&lt;br /&gt;&lt;br /&gt;There is a way to get around this though and it can be quite useful; you must use arrays. In this example we will return a, b, and the sum of the 2.&lt;br /&gt;&lt;br /&gt;&lt;!--c1--&gt;&lt;div class='codetop'&gt;CODE&lt;/div&gt;&lt;div class='codemain'&gt;&lt;!--ec1--&gt;&lt;br /&gt;function adding&amp;#40;a&amp;#58;Number, b&amp;#58;Number&amp;#41;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;if&amp;#40;&amp;#33;a&amp;#41;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;a = 0;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;if&amp;#40;&amp;#33;b&amp;#41;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;b = 0;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return &amp;#91;a, b, a+b&amp;#93;;&lt;br /&gt;}&lt;!--c2--&gt;&lt;/div&gt;&lt;!--ec2--&gt;&lt;br /&gt;&lt;br /&gt;Now if we were to just trace out the returned values it wouldn&amp;#39;t look right:&lt;br /&gt;[inline]trace(adding(1, 2));[/inline] OUTPUT - [inline]1,2,3[/inline].&lt;br /&gt;&lt;br /&gt;So you will need to split the array apart to be able to use various portions of it. This is done like so:&lt;br /&gt;&lt;br /&gt;&lt;!--c1--&gt;&lt;div class='codetop'&gt;CODE&lt;/div&gt;&lt;div class='codemain'&gt;&lt;!--ec1--&gt;&lt;br /&gt;var returned&amp;#58;Array = adding&amp;#40;1, 2&amp;#41;;&lt;br /&gt;trace&amp;#40;&amp;#34;The value &amp;#34;+returned&amp;#91;0&amp;#93;+&amp;#34; plus &amp;#34;+returned&amp;#91;1&amp;#93;+&amp;#34; equals &amp;#34;+returned&amp;#91;2&amp;#93;&amp;#41;;&lt;br /&gt;&lt;!--c2--&gt;&lt;/div&gt;&lt;!--ec2--&gt;&lt;br /&gt;OUTPUT - [inline]The value 1 plus 2 equals 3[/inline].&lt;br /&gt;&lt;br /&gt;The thing to remember when using arrays in AS 2.0 is that you can&amp;#39;t name items in them, you just have to use the number index that thye have, so pay attention to the order you place values into an array.&lt;br /&gt;&lt;br /&gt;NOTE - Arrays always start with index of 0.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Returning Multiple Values&lt;/b&gt; &lt;i&gt;using objects instead of arrays&lt;/i&gt;&lt;br /&gt;I understand that everyone wants to be able to name their values for easy access, I do too. Luckily, with the help of objects you can&amp;#33; Just create a new object and set its values equal to whatever you want (and use whatever name you want too) then return the object and you will be able to call its separate values without needing to memorize the index of each. It can look like so:&lt;br /&gt;&lt;br /&gt;&lt;!--c1--&gt;&lt;div class='codetop'&gt;CODE&lt;/div&gt;&lt;div class='codemain'&gt;&lt;!--ec1--&gt;function adding&amp;#40;a&amp;#58;Number, b&amp;#58;Number&amp;#41;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;if&amp;#40;&amp;#33;a&amp;#41;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;a = 0;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;if&amp;#40;&amp;#33;b&amp;#41;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;b = 0;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;var obj&amp;#58;Object = new Object&amp;#40;&amp;#41;;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;obj.a = a;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;obj.b = b;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;obj.sum = a+b;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return obj;&lt;br /&gt;}&lt;br /&gt;var obj&amp;#58;Object = adding&amp;#40;1, 2&amp;#41;;&lt;br /&gt;trace&amp;#40;&amp;#34;The value &amp;#34;+obj.a+&amp;#34; plus &amp;#34;+obj.b+&amp;#34; equals &amp;#34;+obj.sum&amp;#41;;&lt;!--c2--&gt;&lt;/div&gt;&lt;!--ec2--&gt;&lt;br /&gt;OUTPUT - [inline]The value 1 plus 2 equals 3[/inline].&lt;br /&gt;&lt;br /&gt;NOTICE - I am not using indexes as in the array example above, but am using the varaibels names that I assigned to obj in the function. You can add and rename variables to be returned easily and quickly&amp;#33;&lt;br /&gt;&lt;br /&gt;&lt;b&gt;The End&lt;/b&gt; &lt;i&gt;I hope that helps everyone understand how to use functions a bit more&lt;/i&gt;&lt;br /&gt;See you next time &lt;img src=&quot;style_emoticons/&lt;#EMO_DIR#&gt;/smile.gif&quot; style=&quot;vertical-align:middle&quot; emoid=&quot;:)&quot; border=&quot;0&quot; alt=&quot;smile.gif&quot; /&gt;</description>
			<pubDate>Mon, 28 Jul 2008 17:04:48 -0600</pubDate>
			<category>Flash Tutorials</category>
			<author>BetaWar</author>
         </item>
         <item>
            <link>http://forums.dreamincode.net/showtopic58935.htm</link>
            <title>ArrayList vs. Static Arrays in Java Tutorials</title>
            <description>No problem&amp;#33; &lt;img src=&quot;style_emoticons/&lt;#EMO_DIR#&gt;/biggrin.gif&quot; style=&quot;vertical-align:middle&quot; emoid=&quot;:D&quot; border=&quot;0&quot; alt=&quot;biggrin.gif&quot; /&gt;&lt;br /&gt;&lt;br /&gt;Glad I could help&amp;#33;</description>
			<pubDate>Wed, 30 Jul 2008 16:42:44 -0600</pubDate>
			<category>Java Tutorials</category>
			<author>Locke37</author>
         </item>
         <item>
            <link>http://forums.dreamincode.net/showtopic58898.htm</link>
            <title>32 OpenMP traps for C++ developers in C++ Tutorials</title>
            <description>&lt;!--sizeo:5--&gt;&lt;span style=&quot;font-size:18pt;line-height:100%&quot;&gt;&lt;!--/sizeo--&gt;32 OpenMP traps for C++ developers&lt;!--sizec--&gt;&lt;/span&gt;&lt;!--/sizec--&gt;&lt;br /&gt;&lt;br /&gt;&lt;b&gt;&lt;a href=&quot;http://mailto:kolosov@viva64.com&quot; target=&quot;_blank&quot;&gt;Alexey Kolosov&lt;/a&gt;&lt;br /&gt;OOO &amp;quot;Program Verification Systems&amp;quot;&lt;br /&gt;&lt;a href=&quot;http://mailto:evg@viva64.com&quot; target=&quot;_blank&quot;&gt;Evgeniy Ryzhkov&lt;/a&gt;&lt;br /&gt;OOO &amp;quot;Program Verification Systems&amp;quot;&lt;br /&gt;&lt;a href=&quot;http://mailto:karpov@viva64.com&quot; target=&quot;_blank&quot;&gt;Andrey Karpov&lt;/a&gt;&lt;br /&gt;OOO &amp;quot;Program Verification Systems&amp;quot;&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;&lt;b&gt;&lt;a href=&quot;http://www.viva64.com&quot; target=&quot;_blank&quot;&gt;www.Viva64.com&lt;/a&gt;&lt;/b&gt;&lt;br /&gt;&lt;b&gt;May 2008&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Abstract&lt;/b&gt;&lt;br /&gt;&lt;b&gt;Introduction&lt;/b&gt;&lt;br /&gt;&lt;b&gt;Logical errors&lt;/b&gt;&lt;br /&gt;&lt;b&gt;1. Missing /openmp&lt;/b&gt;&lt;br /&gt;&lt;b&gt;2. Missing parallel&lt;/b&gt;&lt;br /&gt;&lt;b&gt;3. Missing omp&lt;/b&gt;&lt;br /&gt;&lt;b&gt;4. Missing for&lt;/b&gt;&lt;br /&gt;&lt;b&gt;5. Unnecessary parallelization&lt;/b&gt;&lt;br /&gt;&lt;b&gt;6. Incorrect usage of ordered&lt;/b&gt;&lt;br /&gt;&lt;b&gt;7. Redefining the number of threads in a parallel section&lt;/b&gt;&lt;br /&gt;&lt;b&gt;8. Using a lock variable without initializing the variable&lt;/b&gt;&lt;br /&gt;&lt;b&gt; 9. Unsetting a lock from another thread&lt;/b&gt;&lt;br /&gt;&lt;b&gt;10. Using lock as a barrier&lt;/b&gt;&lt;br /&gt;&lt;b&gt;11. Threads number dependency&lt;/b&gt;&lt;br /&gt;&lt;b&gt;12. Incorrect usage of dynamic threads creation&lt;/b&gt;&lt;br /&gt;&lt;b&gt;13. Concurrent usage of a shared resource&lt;/b&gt;&lt;br /&gt;&lt;b&gt;14. Shared memory access unprotected&lt;/b&gt;&lt;br /&gt;&lt;b&gt;15. Using flush with a reference type&lt;/b&gt;&lt;br /&gt;&lt;b&gt;16. Missing flush&lt;/b&gt;&lt;br /&gt;&lt;b&gt;17. Missing synchronization&lt;/b&gt;&lt;br /&gt;&lt;b&gt;18. An external variable is specified as threadprivate not in all units&lt;/b&gt;&lt;br /&gt;&lt;b&gt;19. Uninitialized local variables&lt;/b&gt;&lt;br /&gt;&lt;b&gt;20. Forgotten threadprivate directive&lt;/b&gt;&lt;br /&gt;&lt;b&gt;21. Forgotten private clause&lt;/b&gt;&lt;br /&gt;&lt;b&gt;22. Incorrect worksharing with private variables&lt;/b&gt;&lt;br /&gt;&lt;b&gt;23. Careless usage of the lastprivate clause&lt;/b&gt;&lt;br /&gt;&lt;b&gt;24. Unexpected values of threadprivate variables in the beginning of parallel sections&lt;/b&gt;&lt;br /&gt;&lt;b&gt;25. Some restrictions of private variables&lt;/b&gt;&lt;br /&gt;&lt;b&gt;26. Private variables are not marked as such&lt;/b&gt;&lt;br /&gt;&lt;b&gt;27. Parallel array processing without iteration ordering&lt;/b&gt;&lt;br /&gt;&lt;b&gt;Performance errors&lt;/b&gt;&lt;br /&gt;&lt;b&gt;1. Unnecessary flush&lt;/b&gt;&lt;br /&gt;&lt;b&gt;2. Using critical sections or locks instead of the atomic directive&lt;/b&gt;&lt;br /&gt;&lt;b&gt;3. Unnecessary concurrent memory writing protection&lt;/b&gt;&lt;br /&gt;&lt;b&gt;4. Too much work in a critical section&lt;/b&gt;&lt;br /&gt;&lt;b&gt;5. Too many entries to critical sections&lt;/b&gt;&lt;br /&gt;&lt;b&gt;Conclusion&lt;/b&gt;&lt;br /&gt;&lt;b&gt;References&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;&lt;!--sizeo:3--&gt;&lt;span style=&quot;font-size:12pt;line-height:100%&quot;&gt;&lt;!--/sizeo--&gt;Abstract&lt;!--sizec--&gt;&lt;/span&gt;&lt;!--/sizec--&gt;&lt;br /&gt;&lt;br /&gt;Since multi-core systems are spreading fast, the problem of parallel programming becomes more and more urgent. However, even the majority of experienced developers are new to this sphere. The existing compilers and code analyzers allow finding some bugs, which appear during parallel code development. However, many errors are not diagnosed. The article contains description of a number of errors, which lead to incorrect behavior of parallel programs created with OpenMP.&lt;br /&gt;&lt;br /&gt;&lt;!--sizeo:3--&gt;&lt;span style=&quot;font-size:12pt;line-height:100%&quot;&gt;&lt;!--/sizeo--&gt;Introduction&lt;!--sizec--&gt;&lt;/span&gt;&lt;!--/sizec--&gt;&lt;br /&gt;&lt;br /&gt;Parallel programming appeared a long time ago. The first multiprocessor computer was created in 1960s. However, processors&amp;#39; performance increase has been achieved through clock frequency increment and multiprocessor systems have been rare until recently. The clock frequency increment slows down nowadays and processors&amp;#39; performance increase is achieved through multiple cores. Multi-core processors are spread widely, therefore the problem of parallel programming becomes more and more urgent. Earlier it was enough to install a CPU with a higher clock frequency or larger cash memory to increase a program&amp;#39;s performance. Nowadays this approach is useless and a developer will have to modify the program in order to increase the program&amp;#39;s performance.&lt;br /&gt;Since parallel programming begins gaining in popularity only now, the process of an existing application parallelization or a new parallel program creation may become very problematic even for experienced developers since this sphere is new for them. Currently existing compilers and code analyzers allow finding only some (very few) potential errors. All other errors remain unrecorded and may increase debug and testing time significantly. Besides that, almost all errors of this kind cannot be stably reproduced. The article concerns the C++ language, since C++ programs are usually demanded to work fast. Since Visual Studio 2005 &amp;amp; 2008 support the OpenMP 2.0 standard, we will concern the OpenMP technology. OpenMP allows you to parallelize your code with minimal efforts - all you need to do is to enable the /openmp compiler option and add the needed compiler directives describing how the program&amp;#39;s execution flow should be parallelized to your code. &lt;br /&gt;This article describes only some of the potential errors which are not diagnosed by compilers, static code analyzers and dynamic code analyzers. However, we hope that this paper will help you understand some peculiarities of parallel development and avoid multiple errors.&lt;br /&gt;Also, please note that this paper contains research results, which will be used in the VivaMP static analyzer development (&lt;a href=&quot;http://www.viva64.com/vivamp.php&quot; target=&quot;_blank&quot;&gt;http://www.viva64.com/vivamp.php&lt;/a&gt;). The static analyzer will be designed to find errors in parallel programs created with OpenMP. We are very interested in receiving feedback on this article and learning more patterns of parallel programming errors.&lt;br /&gt;The errors described in this article are split into logical errors and performance errors similar to the approach used in one of the references [&lt;a href=&quot;http://www.viva64.com/go.php?url=100&quot; target=&quot;_blank&quot;&gt;1&lt;/a&gt;]. Logical errors are errors, which cause unexpected results, i.e. incorrect program behavior. Performance errors are errors, which decrease a program&amp;#39;s performance.&lt;br /&gt;First of all, let us define some specific terms which will be used in this article:&lt;br /&gt;Directives are OpenMP directives which define code parallelization means. All OpenMP directives have the appearance of #pragma omp ...&lt;br /&gt;Clauses are auxiliary parts of OpenMP directives. Clauses define how a work is shared between threads, the number of threads, variables access mode, etc.&lt;br /&gt;Parallel section is a code fragment to which the #pragma omp parallel directive is applied.&lt;br /&gt;The article is for developers who are familiar to OpenMP and use the technology in their programs. If you are not familiar with OpenMP, we recommend that you take a look at the document [&lt;a href=&quot;http://www.viva64.com/go.php?url=101&quot; target=&quot;_blank&quot;&gt;2&lt;/a&gt;]. A more detailed description of OpenMP directives, clauses, functions and environment variables can be found in the OpenMP 2.0 specification [&lt;a href=&quot;http://www.viva64.com/go.php?url=102&quot; target=&quot;_blank&quot;&gt;3&lt;/a&gt;]. The specification is duplicated in the MSDN Library and this form of specification is more handy, then the one in the PDF format.&lt;br /&gt;Now, let us describe the potential errors which are badly diagnosed by standard compilers or are not diagnosed at all.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;!--sizeo:3--&gt;&lt;span style=&quot;font-size:12pt;line-height:100%&quot;&gt;&lt;!--/sizeo--&gt;Logical errors&lt;!--sizec--&gt;&lt;/span&gt;&lt;!--/sizec--&gt;&lt;br /&gt;&lt;br /&gt;&lt;!--sizeo:3--&gt;&lt;span style=&quot;font-size:12pt;line-height:100%&quot;&gt;&lt;!--/sizeo--&gt;1. Missing /openmp&lt;!--sizec--&gt;&lt;/span&gt;&lt;!--/sizec--&gt;&lt;br /&gt;&lt;br /&gt;Let&amp;#39;s start with the simplest error: OpenMP directives will be ignored if OpenMP support is not enabled in the compiler settings. The compiler will not report an error or even a warning, the code simply will not work the way the developer expects.&lt;br /&gt;OpenMP support can be enabled in the &amp;quot;Configuration Properties | C/C++ | Language&amp;quot; section of the project properties dialog.&lt;br /&gt;&lt;br /&gt;&lt;!--sizeo:3--&gt;&lt;span style=&quot;font-size:12pt;line-height:100%&quot;&gt;&lt;!--/sizeo--&gt;2. Missing parallel&lt;!--sizec--&gt;&lt;/span&gt;&lt;!--/sizec--&gt;&lt;br /&gt;&lt;br /&gt;OpenMP directives have rather complex format, therefore first of all we are considering the simplest errors caused by incorrect directive format. The listings below show incorrect and correct versions of the same code:&lt;br /&gt;Incorrectly:[CODE=cpp]#pragma omp for&lt;br /&gt;... //your code[/CODE]Correctly:[CODE=cpp]#pragma omp parallel for &lt;br /&gt;... // your code[/CODE][CODE=cpp]#pragma omp parallel&lt;br /&gt;{&lt;br /&gt;  #pragma omp for&lt;br /&gt;  ... //your code&lt;br /&gt;}[/CODE]The first code fragment will be successfully compiled, and the #pragma omp for directive will be simply ignored by the compiler. Therefore, a single thread only will execute the loop, and it will be rather difficult for a developer to find this out. Besides the #pragma omp parallel for directive, the error may also occur with the #pragma omp parallel sections directive. &lt;br /&gt;&lt;br /&gt;&lt;!--sizeo:3--&gt;&lt;span style=&quot;font-size:12pt;line-height:100%&quot;&gt;&lt;!--/sizeo--&gt;3. Missing omp&lt;!--sizec--&gt;&lt;/span&gt;&lt;!--/sizec--&gt;&lt;br /&gt;&lt;br /&gt;A problem similar to the previous one occurs if you omit the omp keyword in an OpenMP directive [&lt;a href=&quot;http://www.viva64.com/go.php?url=103&quot; target=&quot;_blank&quot;&gt;4&lt;/a&gt;]. Let&amp;#39;s take a look at the following simple example:&lt;br /&gt;Incorrectly:[CODE=cpp]#pragma omp parallel num_threads(2)&lt;br /&gt;{&lt;br /&gt;   #pragma single&lt;br /&gt;   {&lt;br /&gt;     printf(&amp;quot;me&amp;#092;n&amp;quot;);&lt;br /&gt;   }&lt;br /&gt;}[/CODE]Correctly:[CODE=cpp]#pragma omp parallel num_threads(2)&lt;br /&gt;{&lt;br /&gt;   #pragma omp single&lt;br /&gt;   {&lt;br /&gt;     printf(&amp;quot;me&amp;#092;n&amp;quot;);&lt;br /&gt;   }&lt;br /&gt;}[/CODE]The &amp;quot;me&amp;quot; string will be printed twice, not once. The compiler will report the &amp;quot;warning C4068: unknown pragma&amp;quot; warning. However, warnings can be disabled in the project&amp;#39;s properties or simply ignored by a developer.&lt;br /&gt;&lt;br /&gt;&lt;!--sizeo:3--&gt;&lt;span style=&quot;font-size:12pt;line-height:100%&quot;&gt;&lt;!--/sizeo--&gt;4. Missing for&lt;!--sizec--&gt;&lt;/span&gt;&lt;!--/sizec--&gt;&lt;br /&gt;&lt;br /&gt;The #pragma omp parallel directive may be applied to a single code line as well as to a code fragment. This fact may cause unexpected behavior of the for loop shown below:[CODE=cpp]#pragma omp parallel num_threads(2)&lt;br /&gt;for (int i = 0; i &amp;lt; 10; i++)&lt;br /&gt; myFunc(); [/CODE]If the developer wanted to share the loop between two threads, he should have used the #pragma omp parallel for directive. In this case the loop would have been executed 10 times indeed. However, the code above will be executed once in every thread. As the result, the myFunc function will be called 20 times. The correct version of the code is provided below:[CODE=cpp]#pragma omp parallel for num_threads(2)&lt;br /&gt;for (int i = 0; i &amp;lt; 10; i++)&lt;br /&gt; myFunc(); [/CODE]&lt;br /&gt;&lt;!--sizeo:3--&gt;&lt;span style=&quot;font-size:12pt;line-height:100%&quot;&gt;&lt;!--/sizeo--&gt;5. Unnecessary parallelization&lt;!--sizec--&gt;&lt;/span&gt;&lt;!--/sizec--&gt;&lt;br /&gt;&lt;br /&gt;Applying the #pragma omp parallel directive to a large code fragment may cause unexpected behavior in cases similar to the one below:[CODE=cpp]#pragma omp parallel num_threads(2)&lt;br /&gt;{        &lt;br /&gt; ... // N code lines&lt;br /&gt; #pragma omp parallel for&lt;br /&gt; for (int i = 0; i &amp;lt; 10; i++)&lt;br /&gt; {   &lt;br /&gt;  myFunc();&lt;br /&gt; }&lt;br /&gt;}[/CODE]In the code above a forgetful or an inexperienced developer who wanted to share the loop execution between two threads placed the parallel keyword inside a parallel section. The result of the code execution will be similar to the previous example: the myFunc function will be called 20 times, not 10. The correct version of the code should look like this:[CODE=cpp]#pragma omp parallel num_threads(2)&lt;br /&gt;{        &lt;br /&gt; ... // N code lines&lt;br /&gt; #pragma omp for&lt;br /&gt; for (int i = 0; i &amp;lt; 10; i++)&lt;br /&gt; {   &lt;br /&gt;  myFunc();&lt;br /&gt; }&lt;br /&gt;}[/CODE]&lt;br /&gt;&lt;!--sizeo:3--&gt;&lt;span style=&quot;font-size:12pt;line-height:100%&quot;&gt;&lt;!--/sizeo--&gt;6. Incorrect usage of ordered&lt;!--sizec--&gt;&lt;/span&gt;&lt;!--/sizec--&gt;&lt;br /&gt;&lt;br /&gt;The ordered directive may cause problems for developers who are new to OpenMP [&lt;a href=&quot;http://www.viva64.com/go.php?url=100&quot; target=&quot;_blank&quot;&gt;1&lt;/a&gt;]. Let&amp;#39;s consider the following sample:&lt;br /&gt;Incorrectly:[CODE=cpp]#pragma omp parallel for ordered&lt;br /&gt;for (int i = 0; i &amp;lt; 10; i++)&lt;br /&gt;{   &lt;br /&gt; myFunc(i);&lt;br /&gt;}[/CODE]Correctly:[CODE=cpp]#pragma omp parallel for ordered&lt;br /&gt;for (int i = 0; i &amp;lt; 10; i++)&lt;br /&gt;{   &lt;br /&gt; #pragma omp ordered&lt;br /&gt; {&lt;br /&gt;  myFunc(i);&lt;br /&gt; }&lt;br /&gt;}[/CODE]In the first code fragment the ordered clause will be simply ignored, because its scope was not specified. The loop will still be executed in a random order (which may sometimes become ascending order if you&amp;#39;re lucky).&lt;br /&gt;&lt;br /&gt;&lt;!--sizeo:3--&gt;&lt;span style=&quot;font-size:12pt;line-height:100%&quot;&gt;&lt;!--/sizeo--&gt;7. Redefining the number of threads in a parallel section&lt;!--sizec--&gt;&lt;/span&gt;&lt;!--/sizec--&gt;&lt;br /&gt;&lt;br /&gt;Now, let us consider more complex errors, which may be caused by insufficient understanding of the OpenMP standard. According to the OpenMP 2.0 specification [&lt;a href=&quot;http://www.viva64.com/go.php?url=102&quot; target=&quot;_blank&quot;&gt;3&lt;/a&gt;] the number of threads cannot be redefined inside a parallel section. Such an attempt will cause run-time errors and program termination of a C++ program. For example:&lt;br /&gt;Incorrectly:[CODE=cpp]#pragma omp parallel&lt;br /&gt;{        &lt;br /&gt; omp_set_num_threads(2);&lt;br /&gt; #pragma omp for&lt;br /&gt; for (int i = 0; i &amp;lt; 10; i++)&lt;br /&gt; {   &lt;br /&gt;  myFunc();&lt;br /&gt; }&lt;br /&gt;}[/CODE]Correctly:[CODE=cpp]#pragma omp parallel num_threads(2)&lt;br /&gt;{        &lt;br /&gt; #pragma omp for&lt;br /&gt; for (int i = 0; i &amp;lt; 10; i++)&lt;br /&gt; {   &lt;br /&gt;  myFunc();&lt;br /&gt; }&lt;br /&gt;}[/CODE]Correctly:[CODE=cpp]omp_set_num_threads(2)&lt;br /&gt;#pragma omp parallel &lt;br /&gt;{        &lt;br /&gt; #pragma omp for&lt;br /&gt; for (int i = 0; i &amp;lt; 10; i++)&lt;br /&gt; {   &lt;br /&gt;  myFunc();&lt;br /&gt; }&lt;br /&gt;}[/CODE]&lt;br /&gt;&lt;!--sizeo:3--&gt;&lt;span style=&quot;font-size:12pt;line-height:100%&quot;&gt;&lt;!--/sizeo--&gt;8. Using a lock variable without initializing the variable&lt;!--sizec--&gt;&lt;/span&gt;&lt;!--/sizec--&gt;&lt;br /&gt;&lt;br /&gt;According to the OpenMP 2.0 specification [&lt;a href=&quot;http://www.viva64.com/go.php?url=102&quot; target=&quot;_blank&quot;&gt;3&lt;/a&gt;] all lock variables must be initialized via the omp_init_lock or omp_init_nest_lock function call (depending on the variable type). A lock variable can be used only after initialization. An attempt to use (set, unset, test) an uninitialized lock variable In a C++ program will cause a run-time error.&lt;br /&gt;Incorrectly:[CODE=cpp]omp_lock_t myLock;&lt;br /&gt;#pragma omp parallel num_threads(2)&lt;br /&gt;{        &lt;br /&gt; ...&lt;br /&gt; omp_set_lock(&amp;amp;myLock);&lt;br /&gt; ...&lt;br /&gt;}[/CODE]Correctly:[CODE=cpp]omp_lock_t myLock;&lt;br /&gt;omp_init_lock(&amp;amp;myLock);&lt;br /&gt;#pragma omp parallel num_threads(2)&lt;br /&gt;{        &lt;br /&gt; ...&lt;br /&gt; omp_set_lock(&amp;amp;myLock);&lt;br /&gt; ...&lt;br /&gt;}[/CODE]&lt;br /&gt;&lt;!--sizeo:3--&gt;&lt;span style=&quot;font-size:12pt;line-height:100%&quot;&gt;&lt;!--/sizeo--&gt; 9. Unsetting a lock from another thread&lt;!--sizec--&gt;&lt;/span&gt;&lt;!--/sizec--&gt;&lt;br /&gt;&lt;br /&gt;If a lock is set in a thread, an attempt to unset this lock in another thread will cause unpredictable behavior [&lt;a href=&quot;http://www.viva64.com/go.php?url=102&quot; target=&quot;_blank&quot;&gt;3&lt;/a&gt;]. Let&amp;#39;s consider the following example:&lt;br /&gt;Incorrectly:[CODE=cpp]omp_lock_t myLock;&lt;br /&gt;omp_init_lock(&amp;amp;myLock);&lt;br /&gt;#pragma omp parallel sections&lt;br /&gt;{        &lt;br /&gt; #pragma omp section&lt;br /&gt; {&lt;br /&gt;  ...&lt;br /&gt;  omp_set_lock(&amp;amp;myLock);&lt;br /&gt;  ...&lt;br /&gt; }&lt;br /&gt; #pragma omp section&lt;br /&gt; {&lt;br /&gt;  ...&lt;br /&gt;  omp_unset_lock(&amp;amp;myLock);&lt;br /&gt;  ...&lt;br /&gt; }&lt;br /&gt;}[/CODE]This code will cause a run-time error in a C++ program. Since lock set and unset operations are similar to entering and leaving a critical section, every thread, which uses locks should perform both operations. Here is a correct version of the code:&lt;br /&gt;Correctly:[CODE=cpp]omp_lock_t myLock;&lt;br /&gt;omp_init_lock(&amp;amp;myLock);&lt;br /&gt;#pragma omp parallel sections&lt;br /&gt;{        &lt;br /&gt; #pragma omp section&lt;br /&gt; {&lt;br /&gt;  ...&lt;br /&gt;  omp_set_lock(&amp;amp;myLock);&lt;br /&gt;  ...&lt;br /&gt;  omp_unset_lock(&amp;amp;myLock);&lt;br /&gt;  ...&lt;br /&gt; }&lt;br /&gt; #pragma omp section&lt;br /&gt; {&lt;br /&gt;  ...&lt;br /&gt;  omp_set_lock(&amp;amp;myLock);&lt;br /&gt;  ...&lt;br /&gt;  omp_unset_lock(&amp;amp;myLock);&lt;br /&gt;  ...&lt;br /&gt; }&lt;br /&gt;}[/CODE]&lt;br /&gt;&lt;!--sizeo:3--&gt;&lt;span style=&quot;font-size:12pt;line-height:100%&quot;&gt;&lt;!--/sizeo--&gt;10. Using lock as a barrier&lt;!--sizec--&gt;&lt;/span&gt;&lt;!--/sizec--&gt;&lt;br /&gt;&lt;br /&gt;The omp_set_lock function blocks execution of a thread until the lock variable becomes available, i.e. until the same thread calls the omp_unset_lock function. Therefore, as it has already been mentioned in the description of the previous error, each of the threads should call both functions. A developer with insufficient understanding of OpenMP may try to use the omp_set_lock function as a barrier, i.e. instead of the #pragma omp barrier directive (since the directive cannot be used inside a parallel section, to which the #pragma omp sections directive is applied). As the result the following code will be created:&lt;br /&gt;Incorrectly: [CODE=cpp]omp_lock_t myLock;&lt;br /&gt;omp_init_lock(&amp;amp;myLock);&lt;br /&gt;#pragma omp parallel sections&lt;br /&gt;{        &lt;br /&gt; #pragma omp section&lt;br /&gt; {  &lt;br /&gt;  ...&lt;br /&gt;  omp_set_lock(&amp;amp;myLock);&lt;br /&gt;  ...&lt;br /&gt; }&lt;br /&gt; #pragma omp section&lt;br /&gt; {&lt;br /&gt;  ...&lt;br /&gt;  omp_set_lock(&amp;amp;myLock);&lt;br /&gt;  omp_unset_lock(&amp;amp;myLock);&lt;br /&gt;  ...&lt;br /&gt; }&lt;br /&gt;}[/CODE]Sometimes the program will be executed successfully. Sometimes it will not. This depends on the thread which finishes its execution first. If the thread which blocks the lock variable without releasing it will be finished first, the program will work as expected. In all other cases the program will infinitely wait for the thread, which works with the lock variable incorrectly, to unset the variable. A similar problem will occur if the developer will place the omp_test_lock function call inside a loop (and that is the way the function is usually used). In this case the loop will make the program hang, because the lock will never be unset.&lt;br /&gt;Since this error is similar to the previous one, the fixed version of the code will remain the same:&lt;br /&gt;Correctly:[CODE=cpp]omp_lock_t myLock;&lt;br /&gt;omp_init_lock(&amp;amp;myLock);&lt;br /&gt;#pragma omp parallel sections&lt;br /&gt;{        &lt;br /&gt; #pragma omp section&lt;br /&gt; {&lt;br /&gt;  ...&lt;br /&gt;  omp_set_lock(&amp;amp;myLock);&lt;br /&gt;  ...&lt;br /&gt;  omp_unset_lock(&amp;amp;myLock);&lt;br /&gt;  ...&lt;br /&gt; }&lt;br /&gt; #pragma omp section&lt;br /&gt; {&lt;br /&gt;  ...&lt;br /&gt;  omp_set_lock(&amp;amp;myLock);&lt;br /&gt;  ...&lt;br /&gt;  omp_unset_lock(&amp;amp;myLock);&lt;br /&gt;  ...&lt;br /&gt; }&lt;br /&gt;}[/CODE]&lt;br /&gt;&lt;!--sizeo:3--&gt;&lt;span style=&quot;font-size:12pt;line-height:100%&quot;&gt;&lt;!--/sizeo--&gt;11. Threads number dependency&lt;!--sizec--&gt;&lt;/span&gt;&lt;!--/sizec--&gt;&lt;br /&gt;&lt;br /&gt;The number of parallel threads created during a program execution is not a constant value in general case. The number is usually equal to the number of processors by default. However, a developer can specify the number of threads explicitly (for example, using the omp_set_num_threads function or the num_threads clause, which has higher priority than the function). The number of threads can also be specified via the OMP_NUM_THREADS environment variable, which has the lowest priority. Therefore, the number of threads, which currently execute a parallel section, is a very unreliable value. Besides, the value may vary from one machine to another. The behavior of your code should not depend on the number of threads, which execute the code, unless you are entirely sure that this is really necessary.&lt;br /&gt;Let&amp;#39;s consider an example from the article [&lt;a href=&quot;http://www.viva64.com/go.php?url=104&quot; target=&quot;_blank&quot;&gt;5&lt;/a&gt;]. The following program should have printed all letters of the English alphabet according to the developer&amp;#39;s plan.&lt;br /&gt;Incorrectly:[CODE=cpp]omp_set_num_threads(4);&lt;br /&gt;#pragma omp parallel private(i)&lt;br /&gt;{&lt;br /&gt; int LettersPerThread = 26 / omp_get_num_threads();&lt;br /&gt; int ThisThreadNum = omp_get_thread_num();&lt;br /&gt; int StartLetter = &amp;#39;a&amp;#39; + ThisThreadNum * LettersPerThread;&lt;br /&gt; int EndLetter = &amp;#39;a&amp;#39; + ThisThreadNum * LettersPerThread + LettersPerThread;&lt;br /&gt; for (int i=StartLetter; i&amp;lt;EndLetter; i++)&lt;br /&gt;  printf (&amp;quot;%c&amp;quot;, i);&lt;br /&gt;}[/CODE]However, only 24 of 26 letters will be printed. The cause of the problem is that 26 (the total number of letters) do not contain 4 (the number of threads). Therefore, the two letters remaining will not be printed. To fix the problem one can either significantly modify the code so that the code will not use the number of threads or share the work between a correct number of threads (e.g. 2 threads). Suppose the developer decided not to use the number of threads in his program and let the compiler share work between threads. In this case the fixed version of the code will be similar to the following one:&lt;br /&gt;Correctly:[CODE=cpp]omp_set_num_threads(4);&lt;br /&gt;#pragma omp parallel for&lt;br /&gt;for (int i = &amp;#39;a&amp;#39;; i &amp;lt;= &amp;#39;z&amp;#39;; i++)&lt;br /&gt;{&lt;br /&gt; printf (&amp;quot;%c&amp;quot;, i);&lt;br /&gt;}[/CODE]All iterations of the loop will surely be executed. One can specify the way the iterations are shared between threads using the schedule clause. Now, the compiler will share work between the threads and he will never forget about the two &amp;quot;additional&amp;quot; iterations. In addition, the resulting code is significantly shorter and readable.&lt;br /&gt;&lt;br /&gt;&lt;!--sizeo:3--&gt;&lt;span style=&quot;font-size:12pt;line-height:100%&quot;&gt;&lt;!--/sizeo--&gt;12. Incorrect usage of dynamic threads creation&lt;!--sizec--&gt;&lt;/span&gt;&lt;!--/sizec--&gt;&lt;br /&gt;&lt;br /&gt;The dynamic keyword may appear in two different contexts in OpenMP: in the schedule(dynamic) clause and in the OMP_DYNAMIC environment variable, which makes a little mess of this. It is important to understand the difference between the two cases. One should not think that the schedule(dynamic) clause can be used only if the OMP_DYNAMIC variable is equal to true. The two cases are not related at all, actually.&lt;br /&gt;The schedule(dynamic) clause means that iterations of a loop are split into chunks, which are dynamically shared between threads. When a thread finishes execution of a chunk, the thread will start executing the following &amp;quot;portion&amp;quot;. If we apply this clause to the previous example, each of the 4 threads will print 6 letters and then the thread, which will become free first, will print the last 2 letters.&lt;br /&gt;The OMP_DYNAMIC variable sets, whether the compiler can define the number of threads dynamically. The cause of a possible problem with this variable is that the variable&amp;#39;s priority is even higher than the one of the num_threads clause. Therefore, if the variable&amp;#39;s value is equal to true, the setting overrides num_threads, omp_set_num_threads and OMP_NUM_THREADS. If a program&amp;#39;s behavior depends on the number of threads, this may cause unexpected results. This is another argument for creating code, which does not depend on the number of threads.&lt;br /&gt;As experience has shown, the value of the OMP_DYNAMIC environment variable is equal to false by default in Visual Studio 2008. However, there is no guarantee that this situation will remain unchanged in the future. The OpenMP specification [&lt;a href=&quot;http://www.viva64.com/go.php?url=102&quot; target=&quot;_blank&quot;&gt;3&lt;/a&gt;] states that the variable&amp;#39;s value is implementation-specific. Therefore, if the developer from the previous example chose an easier way and decided to use the number of threads in his calculations instead of modifying the code significantly, he should make sure that the number of threads would always be equal to the one he needs. Otherwise the code will not work correctly on a four-processor machine.&lt;br /&gt;Correctly:[CODE=cpp]if (omp_get_dynamic())&lt;br /&gt;  omp_set_dynamic(0);&lt;br /&gt;omp_set_num_threads(2);&lt;br /&gt;#pragma omp parallel private(i)&lt;br /&gt;{&lt;br /&gt; int LettersPerThread = 26 / omp_get_num_threads();&lt;br /&gt; int ThisThreadNum = omp_get_thread_num();&lt;br /&gt; int StartLetter = &amp;#39;a&amp;#39; + ThisThreadNum * LettersPerThread;&lt;br /&gt; int EndLetter = &amp;#39;a&amp;#39; + ThisThreadNum * LettersPerThread + LettersPerThread;&lt;br /&gt; for (i=StartLetter; i&amp;lt;EndLetter; i++)&lt;br /&gt;  printf (&amp;quot;%c&amp;quot;, i);&lt;br /&gt;}[/CODE]&lt;br /&gt;&lt;!--sizeo:3--&gt;&lt;span style=&quot;font-size:12pt;line-height:100%&quot;&gt;&lt;!--/sizeo--&gt;13. Concurrent usage of a shared resource&lt;!--sizec--&gt;&lt;/span&gt;&lt;!--/sizec--&gt;&lt;br /&gt;&lt;br /&gt;If we modify the previous example&amp;#39;s code so that the code prints at least two or more letters at a time (not one by one in a random order as it currently does), we will observe one more parallel programming problem, the problem of concurrent shared resource usage. In this case the resource is the application&amp;#39;s console. Let&amp;#39;s consider a slightly modified example from the article [&lt;a href=&quot;http://www.viva64.com/go.php?url=105&quot; target=&quot;_blank&quot;&gt;6&lt;/a&gt;].&lt;br /&gt;Incorrectly:[CODE=cpp]#pragma omp parallel num_threads(2)&lt;br /&gt;{ &lt;br /&gt;    printf(&amp;quot;Hello World&amp;#092;n&amp;quot;);&lt;br /&gt;}[/CODE]In spite of the developer&amp;#39;s expectations, the program&amp;#39;s output on a two-processor machine will be similar to the following two lines:[CODE=cpp]HellHell oo WorWlodrl&lt;br /&gt;d[/CODE]The behavior is caused by the fact that the string output operation is not atomic. Therefore, the two threads will print their characters simultaneously. The same problem will occur if you use the standard output thread (cout) or any other object accessible to the threads as a shared variable.&lt;br /&gt;If it is necessary to perform an action, which changes a shared object&amp;#39;s state, from two threads, one should make sure that the action is performed by a single thread at a time. One can use locks or critical sections to achieve this. The most preferable approach will be discussed further.&lt;br /&gt;Correctly:[CODE=cpp]#pragma omp parallel num_threads(2)&lt;br /&gt;{ &lt;br /&gt; #pragma omp critical&lt;br /&gt; {&lt;br /&gt;  printf(&amp;quot;Hello World&amp;#092;n&amp;quot;);&lt;br /&gt; }&lt;br /&gt;}[/CODE]&lt;br /&gt;&lt;!--sizeo:3--&gt;&lt;span style=&quot;font-size:12pt;line-height:100%&quot;&gt;&lt;!--/sizeo--&gt;14. Shared memory access unprotected&lt;!--sizec--&gt;&lt;/span&gt;&lt;!--/sizec--&gt;&lt;br /&gt;&lt;br /&gt;This error is described in the article [&lt;a href=&quot;http://www.viva64.com/go.php?url=100&quot; target=&quot;_blank&quot;&gt;1&lt;/a&gt;]. The error is similar to the previous one: if several threads are modifying a variable&amp;#39;s value concurrently, the result is unpredictable. However, the error is considered separately from the previous one, because in this case the solution will be slightly different. Since an operation on a variable can be atomic, it is more preferable to use the atomic directive in this case. This approach will provide better performance than critical sections. Detailed recommendations on shared memory protection will be provided further.&lt;br /&gt;Incorrectly:[CODE=cpp]int a = 0;&lt;br /&gt;#pragma omp parallel&lt;br /&gt;{ &lt;br /&gt;    a++;&lt;br /&gt;}[/CODE]Correctly:[CODE=cpp]int a = 0;&lt;br /&gt;#pragma omp parallel&lt;br /&gt;{ &lt;br /&gt; #pragma omp atomic&lt;br /&gt;    a++;&lt;br /&gt;}[/CODE]Another possible solution is to use the reduction clause. In this case every thread will get its own copy of the a variable, perform all the needed actions on this copy and then perform the specified operation to merge all the copies.&lt;br /&gt;Correctly:[CODE=cpp]int a = 0;&lt;br /&gt;#pragma omp parallel reduction(+:a)&lt;br /&gt;{ &lt;br /&gt;    a++;&lt;br /&gt;}&lt;br /&gt;printf(&amp;quot;a=%d&amp;#092;n&amp;quot;, a); [/CODE]The code above, being executed by two threads, will print the &amp;quot;a=2&amp;quot; string.&lt;br /&gt;&lt;br /&gt;&lt;!--sizeo:3--&gt;&lt;span style=&quot;font-size:12pt;line-height:100%&quot;&gt;&lt;!--/sizeo--&gt;15. Using flush with a reference type&lt;!--sizec--&gt;&lt;/span&gt;&lt;!--/sizec--&gt;&lt;br /&gt;&lt;br /&gt;The flush directive makes all the threads refresh values of shared variables. For example, if a thread assigns 1 to a shared variable a, it does not guarantee that another thread reading the variable will get 1. Please note that the directive refreshes only the variables&amp;#39; values. If an application&amp;#39;s code contains a shared reference pointing to an object, the flush directive will refresh only the value of the reference (a memory address), but not the object&amp;#39;s state. In addition, the OpenMP specification [&lt;a href=&quot;http://www.viva64.com/go.php?url=102&quot; target=&quot;_blank&quot;&gt;3&lt;/a&gt;] states explicitly that the flush directive&amp;#39;s argument cannot be a reference.&lt;br /&gt;Incorrectly:[CODE=cpp]MyClass* mc = new MyClass();&lt;br /&gt;#pragma omp parallel sections&lt;br /&gt;{&lt;br /&gt; #pragma omp section&lt;br /&gt; {&lt;br /&gt;  #pragma omp flush(mc)&lt;br /&gt;  mc-&amp;gt;myFunc();&lt;br /&gt;  #pragma omp flush(mc)&lt;br /&gt; }&lt;br /&gt; #pragma omp section&lt;br /&gt; {&lt;br /&gt;  #pragma omp flush(mc)&lt;br /&gt;  mc-&amp;gt;myFunc();&lt;br /&gt;  #pragma omp flush(mc)&lt;br /&gt; }&lt;br /&gt;}[/CODE]The code below actually contains two errors: concurrent access to a shared object, which has already been described above, and usage of the flush directive with a reference type. Therefore, if the myFunc method changes the object&amp;#39;s state, the result of the code execution is unpredictable. To avoid the errors one should get rid of concurrent usage of the shared object. Please note that the flush directive is executed implicitly at entry to and at exit from critical sections (this fact will be discussed later).&lt;br /&gt;Correctly:[CODE=cpp]MyClass* mc = new MyClass();&lt;br /&gt;#pragma omp parallel sections&lt;br /&gt;{&lt;br /&gt; #pragma omp section&lt;br /&gt; {&lt;br /&gt;  #pragma omp critical&lt;br /&gt;  {&lt;br /&gt;   mc-&amp;gt;myFunc();&lt;br /&gt;  }&lt;br /&gt; }&lt;br /&gt; #pragma omp section&lt;br /&gt; {&lt;br /&gt;  #pragma omp critical&lt;br /&gt;  {&lt;br /&gt;   mc-&amp;gt;myFunc();&lt;br /&gt;  }&lt;br /&gt; }&lt;br /&gt;}[/CODE]&lt;br /&gt;&lt;!--sizeo:3--&gt;&lt;span style=&quot;font-size:12pt;line-height:100%&quot;&gt;&lt;!--/sizeo--&gt;16. Missing flush&lt;!--sizec--&gt;&lt;/span&gt;&lt;!--/sizec--&gt;&lt;br /&gt;&lt;br /&gt;According to the OpenMP specification [&lt;a href=&quot;http://www.viva64.com/go.php?url=102&quot; target=&quot;_blank&quot;&gt;3&lt;/a&gt;], the directive is implied in many cases. The full list of such cases will be provided further. A developer may count upon this fact and forget to place the directive in a place where it is really necessary. The flush directive is &lt;b&gt;not&lt;/b&gt; implied in the following cases:&lt;ul&gt;&lt;li&gt;At entry to for.&lt;/li&gt;&lt;li&gt;At entry to or exit from master. &lt;/li&gt;&lt;li&gt;At entry to sections. &lt;/li&gt;&lt;li&gt;At entry to single.&lt;/li&gt;&lt;li&gt;At exit from for, single or sections, if the nowait clause is applied to the directive. The clause removes implicit flush along with the implicit barrier.&lt;/li&gt;&lt;/ul&gt; &lt;br /&gt;Incorrectly:[CODE=cpp]int i = 0;&lt;br /&gt;#pragma omp parallel num_threads(2)&lt;br /&gt;{&lt;br /&gt; a++;&lt;br /&gt; #pragma omp single&lt;br /&gt; {&lt;br /&gt;  cout &amp;lt;&amp;lt; a &amp;lt;&amp;lt; endl;&lt;br /&gt; }&lt;br /&gt;}[/CODE]Correctly:[CODE=cpp]int i = 0;&lt;br /&gt;#pragma omp parallel num_threads(2)&lt;br /&gt;{&lt;br /&gt; a++;&lt;br /&gt; #pragma omp single&lt;br /&gt; {&lt;br /&gt;  #pragma omp flush(a)&lt;br /&gt;  cout &amp;lt;&amp;lt; a &amp;lt;&amp;lt; endl;&lt;br /&gt; }&lt;br /&gt;}[/CODE]The latest version of the code uses the flush directive, but it is not ideal too. This version lacks of synchronization.&lt;br /&gt;&lt;br /&gt;&lt;!--sizeo:3--&gt;&lt;span style=&quot;font-size:12pt;line-height:100%&quot;&gt;&lt;!--/sizeo--&gt;17. Missing synchronization&lt;!--sizec--&gt;&lt;/span&gt;&lt;!--/sizec--&gt;&lt;br /&gt;&lt;br /&gt;Besides the necessity of the flush directive usage, a developer should also keep in mind threads synchronization.&lt;br /&gt;The corrected version of the previous example does not guarantee that the &amp;quot;2&amp;quot; string will be printed to the application&amp;#39;s console window. The thread executing the section will print the value of the a variable which was actual at the moment the output operation was performed. However, there is no guarantee that both threads will reach the single directive simultaneously. In general case the value might be equal to &amp;quot;1&amp;quot; as well as &amp;quot;2&amp;quot;. This behavior is caused by missing threads synchronization. The single directive means that the corresponding section should be executed only by a single thread. However, it is equiprobable that the section will be executed by the thread which finishes its execution first. In this case the &amp;quot;1&amp;quot; string will be printed. A similar error is described in the article [&lt;a href=&quot;http://www.viva64.com/go.php?url=106&quot; target=&quot;_blank&quot;&gt;7&lt;/a&gt;].&lt;br /&gt;Implicit synchronization via an implied barrier directive is performed only at exit from the for, single or sections directive, if the nowait clause is not applied to the directive (the clause removes the implicit barrier). In all other cases the developer should take care of the synchronization.&lt;br /&gt;Correctly:[CODE=cpp]int i = 0;&lt;br /&gt;#pragma omp parallel num_threads(2)&lt;br /&gt;{&lt;br /&gt; a++;&lt;br /&gt; #pragma omp barrier &lt;br /&gt; #pragma omp single&lt;br /&gt; {&lt;br /&gt;  cout&amp;lt;&amp;lt;a&amp;lt;&amp;lt;endl;&lt;br /&gt; }&lt;br /&gt;}[/CODE]This version of the code is entirely correct: the program will always print the &amp;quot;2&amp;quot; string. Please note that this version does not contain the flush directive since it is implicitly included in the barrier directive. &lt;br /&gt;Now, let us consider one more example of missing synchronization. The example is taken from the MSDN Library [&lt;a href=&quot;http://www.viva64.com/go.php?url=107&quot; target=&quot;_blank&quot;&gt;8&lt;/a&gt;].&lt;br /&gt;Incorrectly:[CODE=cpp]struct MyType &lt;br /&gt;{&lt;br /&gt;    ~MyType();&lt;br /&gt;};&lt;br /&gt;MyType threaded_var;&lt;br /&gt;#pragma omp threadprivate(threaded_var)&lt;br /&gt;int main() &lt;br /&gt;{&lt;br /&gt;    #pragma omp parallel&lt;br /&gt;    {&lt;br /&gt;      ...&lt;br /&gt;    }&lt;br /&gt;}[/CODE]The code is incorrect, because there is no synchronization at exit from the parallel section. As the result, when the application&amp;#39;s process execution finishes, some of the threads will still exist and they will not get a notification about the fact that the process execution is finished. The destructor of the threaded_var variable will actually be called only in the main thread. Since the variable is threadprivate, its copies created in other threads will not be destroyed and a memory leak will occur. It is necessary to implement synchronization manually in order to avoid this problem.&lt;br /&gt;Correctly:[CODE=cpp]struct MyType &lt;br /&gt;{&lt;br /&gt;    ~MyType();&lt;br /&gt;};&lt;br /&gt;MyType threaded_var;&lt;br /&gt;#pragma omp threadprivate(threaded_var)&lt;br /&gt;int main() &lt;br /&gt;{&lt;br /&gt; #pragma omp parallel&lt;br /&gt; {&lt;br /&gt;  ...&lt;br /&gt;  #pragma omp barrier&lt;br /&gt; }    &lt;br /&gt;}[/CODE]&lt;br /&gt;&lt;!--sizeo:3--&gt;&lt;span style=&quot;font-size:12pt;line-height:100%&quot;&gt;&lt;!--/sizeo--&gt;18. An external variable is specified as threadprivate not in all units&lt;!--sizec--&gt;&lt;/span&gt;&lt;!--/sizec--&gt;&lt;br /&gt;&lt;br /&gt;We&amp;#39;re beginning to discuss the most troublesome errors: the errors related to the OpenMP memory model. And this one is the first error of this type. The concurrent access to shared memory can also be treated as an error related to the OpenMP memory model since the error is related to shared variables and all global-scope variables are shared by default in OpenMP.&lt;br /&gt;Before we start discussing memory model errors, please note that they all are related to private, firstprivate, lastprivate and threadprivate variables. One can avoid most of the errors if he avoids using the threadprivate directive and the private clause. We recommend declaring the needed variables as local variables in parallel sections instead. &lt;br /&gt;Now, when you&amp;#39;re warned, let&amp;#39;s start discussing the memory model errors. We&amp;#39;ll start with the threadprivate directive. The directive is usually applied to global variables, including external variables declared in another units. In this case the directive should be applied to the variable in all the units in which the variable is used. This rule is described in the abovementioned MSDN Library article [&lt;a href=&quot;http://www.viva64.com/go.php?url=107&quot; target=&quot;_blank&quot;&gt;8&lt;/a&gt;].&lt;br /&gt;A special case of this rule is another rule described in the same article: the threadprivate directive cannot be applied to variables declared in a DLL which will be loaded via the LoadLibrary function or the /DELAYLOAD linker option (since the LoadLibrary function is used implicitly in this case).&lt;br /&gt;&lt;br /&gt;&lt;!--sizeo:3--&gt;&lt;span style=&quot;font-size:12pt;line-height:100%&quot;&gt;&lt;!--/sizeo--&gt;19. Uninitialized local variables&lt;!--sizec--&gt;&lt;/span&gt;&lt;!--/sizec--&gt;&lt;br /&gt;&lt;br /&gt;When a thread starts, local copies of threadprivate, private and lastprivate variables are created for this thread. The copies are unitialized by default. Therefore, any attempt to work with the variables without initializing them, will cause a run-time error.&lt;br /&gt;Incorrectly:[CODE=cpp]int a = 0;&lt;br /&gt;#pragma omp parallel private(a)&lt;br /&gt;{&lt;br /&gt; a++;&lt;br /&gt;}[/CODE]Correctly:[CODE=cpp]int a = 0;&lt;br /&gt;#pragma omp parallel private(a)&lt;br /&gt;{&lt;br /&gt; a = 0;&lt;br /&gt; a++;&lt;br /&gt;}[/CODE]Please note that there is no need to use synchronization and the flush directive since every thread has its own copy of the variable.&lt;br /&gt;&lt;br /&gt;&lt;!--sizeo:3--&gt;&lt;span style=&quot;font-size:12pt;line-height:100%&quot;&gt;&lt;!--/sizeo--&gt;20. Forgotten threadprivate directive&lt;!--sizec--&gt;&lt;/span&gt;&lt;!--/sizec--&gt;&lt;br /&gt;&lt;br /&gt;Since the threadprivate directive is applied only once and used for global variables declared in the beginning of a unit, it&amp;#39;s easy to forget about the directive: for example, when it&amp;#39;s necessary to modify a unit created half a year ago. As the result, the developer will expect a global variable to become shared, as it should be by default. However, the variable will become local for every parallel thread. According to the OpenMP specification [&lt;a href=&quot;http://www.viva64.com/go.php?url=102&quot; target=&quot;_blank&quot;&gt;3&lt;/a&gt;], the variable&amp;#39;s value after a parallel section is unpredictable in this case.&lt;br /&gt;Incorrectly:[CODE=cpp]int a;&lt;br /&gt;#pragma omp threadprivate(a) &lt;br /&gt;int _tmain(int argc, _TCHAR* argv[])&lt;br /&gt;{&lt;br /&gt; ...&lt;br /&gt; a = 0;&lt;br /&gt; #pragma omp parallel&lt;br /&gt; {    &lt;br /&gt;  #pragma omp sections&lt;br /&gt;  {&lt;br /&gt;   #pragma omp section &lt;br /&gt;   {&lt;br /&gt;    a += 3;&lt;br /&gt;   }&lt;br /&gt;   #pragma omp section&lt;br /&gt;   {&lt;br /&gt;    a += 3;&lt;br /&gt;   }&lt;br /&gt;  }&lt;br /&gt;  #pragma omp barrier&lt;br /&gt; }&lt;br /&gt; cout &amp;lt;&amp;lt; &amp;quot;a = &amp;quot; &amp;lt;&amp;lt; a &amp;lt;&amp;lt; endl;&lt;br /&gt;}[/CODE]The program will behave as described in the specification: sometimes &amp;quot;6&amp;quot; (the value the developer expects) will be printed in a console window. Sometimes, however, the program will print &amp;quot;0&amp;quot;. This result is more logical, since 0 is the value assigned to the variable before the parallel section. In theory, the same behavior should be observed if the a variable is declared as private or firstprivate. In practice, however, we have reproduced the behavior only with the threadprivate directive. Therefore, the example above contains this directive. In addition, this case is the most probable. &lt;br /&gt;This fact, however, does not mean that the behavior in the other two cases will be correct in all other implementations; so, one should consider the cases too.&lt;br /&gt;Unfortunately, it is difficult to provide a good solution in this case, because removing the threadprivate directive will change the program&amp;#39;s behavior and declaring a threadprivate variable as shared is forbidden by OpenMP syntax rules. The only possible workaround is to use another variable.&lt;br /&gt;Correctly:[CODE=cpp]int a;&lt;br /&gt;#pragma omp threadprivate(a) &lt;br /&gt;int _tmain(int argc, _TCHAR* argv[])&lt;br /&gt;{&lt;br /&gt; ...&lt;br /&gt; a = 0;&lt;br /&gt; int b = a;&lt;br /&gt; #pragma omp parallel&lt;br /&gt; {    &lt;br /&gt;  #pragma omp sections&lt;br /&gt;  {&lt;br /&gt;   #pragma omp section &lt;br /&gt;   {&lt;br /&gt;    b += 3;&lt;br /&gt;   }&lt;br /&gt;   #pragma omp section&lt;br /&gt;   {&lt;br /&gt;    b += 3;&lt;br /&gt;   }&lt;br /&gt;  }&lt;br /&gt;  #pragma omp barrier&lt;br /&gt; }&lt;br /&gt; a = b;&lt;br /&gt; cout &amp;lt;&amp;lt; &amp;quot;a = &amp;quot; &amp;lt;&amp;lt; a &amp;lt;&amp;lt; endl;&lt;br /&gt;}[/CODE]In this version the a variable becomes a shared variable for the parallel section. Of course, this solution is not the best one. However, this solution guarantees that the old code will not change its behavior.&lt;br /&gt;We recommend that beginners use the default(none) clause to avoid such problems. The clause will make the developer specify access modes for all global variables used in a parallel section. Of course, this will make your code grow, but you will avoid many errors and the code will become more readable.&lt;br /&gt;&lt;br /&gt;&lt;!--sizeo:3--&gt;&lt;span style=&quot;font-size:12pt;line-height:100%&quot;&gt;&lt;!--/sizeo--&gt;21. Forgotten private clause&lt;!--sizec--&gt;&lt;/span&gt;&lt;!--/sizec--&gt;&lt;br /&gt;&lt;br /&gt;Let&amp;#39;s consider a scenario similar to the previous case: a developer needs to modify a unit created some time ago and the clause defining a variable&amp;#39;s access mode is located far enough from the code fragment to be modified.&lt;br /&gt;Incorrectly:[CODE=cpp]int a;&lt;br /&gt;#pragma omp parallel private(a)&lt;br /&gt;{&lt;br /&gt;...&lt;br /&gt;a = 0;&lt;br /&gt;#pragma omp for&lt;br /&gt;for (int i = 0; i &amp;lt; 10; i++)&lt;br /&gt;{&lt;br /&gt; a++;&lt;br /&gt;}&lt;br /&gt;#pragma omp critical&lt;br /&gt;{&lt;br /&gt; cout &amp;lt;&amp;lt; &amp;quot;a = &amp;quot; &amp;lt;&amp;lt; a;&lt;br /&gt;}&lt;br /&gt;}[/CODE]This error seems to be an equivalent of the previous one. However, it is not true. In the previous case the result was printed after a parallel section and in this case the value is printed from a parallel section. As the result, if the variable&amp;#39;s value before the loop is equal to zero, the code will print &amp;quot;5&amp;quot; instead of &amp;quot;10&amp;quot; on a two-processor machine. The cause of the behavior is that the work is shared between two threads. Each thread will get its own local copy of the a variable and increase the variable five times instead of the expected ten times. Moreover, the resulting value will depend on the number of threads executing the parallel section. By the way, the error will also occur if one uses the firstprivate clause instead of the private clause.&lt;br /&gt;Possible solutions are similar to the ones provided for the previous case: one should either significantly modify all older code or modify the new code so that it will be compatible with the behavior of the old code. In this case the second solution is more elegant than the one provided for the previous case.&lt;br /&gt;Correctly:[CODE=cpp]int a;&lt;br /&gt;#pragma omp parallel private(a)&lt;br /&gt;{&lt;br /&gt;...&lt;br /&gt;a = 0;&lt;br /&gt;#pragma omp parallel for&lt;br /&gt;for (int i = 0; i &amp;lt; 10; i++)&lt;br /&gt;{&lt;br /&gt; a++;&lt;br /&gt;}&lt;br /&gt;#pragma omp critical&lt;br /&gt;{&lt;br /&gt; cout &amp;lt;&amp;lt; &amp;quot;a = &amp;quot; &amp;lt;&amp;lt; a;&lt;br /&gt;}&lt;br /&gt;}[/CODE]&lt;br /&gt;&lt;!--sizeo:3--&gt;&lt;span style=&quot;font-size:12pt;line-height:100%&quot;&gt;&lt;!--/sizeo--&gt;22. Incorrect worksharing with private variables&lt;!--sizec--&gt;&lt;/span&gt;&lt;!--/sizec--&gt;&lt;br /&gt;&lt;br /&gt;The error is similar to the previous one and opposite to the &amp;quot;Unnecessary parallelization&amp;quot; error. In this case, however, the error can be caused by another scenario. &lt;br /&gt;Incorrectly:[CODE=cpp]int a;&lt;br /&gt;#pragma omp parallel private(a)&lt;br /&gt;{&lt;br /&gt; a = 0;&lt;br /&gt; #pragma omp barrier&lt;br /&gt; #pragma omp sections &lt;br /&gt; { &lt;br /&gt;  #pragma omp section&lt;br /&gt;  {&lt;br /&gt;   a+=100;  &lt;br /&gt;  }  &lt;br /&gt;  #pragma omp section&lt;br /&gt;  {   &lt;br /&gt;   a+=1;&lt;br /&gt;  }&lt;br /&gt; } &lt;br /&gt; #pragma omp critical&lt;br /&gt;{&lt;br /&gt; cout &amp;lt;&amp;lt; &amp;quot;a = &amp;quot; &amp;lt;&amp;lt; a &amp;lt;&amp;lt; endl;&lt;br /&gt;}&lt;br /&gt;}[/CODE]In this case a developer wanted to increase the value of each local copy of the a variable by 101 and used the sections directive for this purpose. However, since the parallel keyword was not specified in the directive, no additional parallelization was made. The work was shared between the same threads. As the result, on a two-processor machine one thread will print &amp;quot;1&amp;quot; and the other one will print &amp;quot;100&amp;quot;. If the number of threads is increased, the results will be even more unexpected. By the way, if the a variable is not declared as private, the code will become correct.&lt;br /&gt;In the sample above it is necessary to perform additional code parallelization.&lt;br /&gt;Correctly:[CODE=cpp]int a;&lt;br /&gt;#pragma omp parallel private(a)&lt;br /&gt;{&lt;br /&gt; a = 0;&lt;br /&gt; #pragma omp