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

<channel>
	<title>Atypical Homeschool&#187; Programming in C</title>
	<atom:link href="http://atypicalhomeschool.net/category/tutorials/programming-in-c/feed/" rel="self" type="application/rss+xml" />
	<link>http://atypicalhomeschool.net</link>
	<description>my personal space</description>
	<lastBuildDate>Thu, 26 Jan 2012 02:34:25 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<item>
		<title>Yet Another Drawing Program</title>
		<link>http://atypicalhomeschool.net/tutorials/yet-another-drawing-program/</link>
		<comments>http://atypicalhomeschool.net/tutorials/yet-another-drawing-program/#comments</comments>
		<pubDate>Fri, 09 Sep 2005 12:53:21 +0000</pubDate>
		<dc:creator>Ron</dc:creator>
				<category><![CDATA[Programming in C]]></category>
		<category><![CDATA[Tutorials]]></category>

		<guid isPermaLink="false">http://atypicalhomeschool.net/wp/tutorials/yet-another-drawing-program/</guid>
		<description><![CDATA[Here is a slightly more complicated drawing program. It will scale a car. You get to choose a scale between 1 and 4. Most home ed families have a minivan. Your challenge is to see if you can change the pattern into a minivan. There is a board in the forum (link on the top) [...]]]></description>
			<content:encoded><![CDATA[<p> Here is a slightly more complicated drawing program. It will scale a car. You get to choose a scale between 1 and 4. Most home ed families have a minivan. Your challenge is to see if you can change the pattern into a minivan.<span id="more-50"></span></p>
<p>There is a board in the forum (link on the top) which we set up to discuss these C Tutorials. Once you register you can post questions or discussion items there.</p>
<p>/* Draw a scaled car on the console or terminal window.<br />
   Takes input value of length of the arm of the X.<br />
   C Tutorials for the home ed student<br />
   Ron R.<br />
   September 2005</p>
<p>Consider an car which is 6 characters high and 19 characters wide</p>
<p>row    column<br />
 0  1234567890123456789<br />
 1       *********<br />
 2      *    *    *<br />
 3   ******************<br />
 4  *                 *<br />
 5  ***  ********  ****<br />
 6     **        **</p>
<p>Note that:<br />
  the top is 2 rows, the body is 3 rows and the wheels are 1 row<br />
  the length is 19 characters<br />
  therefore we can scale this by multiplying the dimensions by 1,2,3 or 4<br />
  and it will still fit on the screen<br />
  normally the front and rear windows are sloped<br />
  and the wheels are round<br />
  the remainder will be calculated as necessary<br />
*/</p>
<p>#include <stdio .h></p>
<p>int main()<br />
{<br />
    int scale; // entered scale of car and height of wheels<br />
    int top; // height of top<br />
    int body; // height of body<br />
    int row,col; // current row and column in printing<br />
    int length; // overall length of car<br />
    int pad; // how much to pad left of car<br />
    int topleft; // top left of top of car<br />
    int topright; // top right of top of car<br />
    int hood; // left of hood of car<br />
    int doorpost; // location of door post<br />
    int bottomleft; // left of left wheel<br />
    int bottomright; // left of right wheel<br />
    int wheel; // width of wheel</p>
<p>    do<br />
    {<br />
        printf(&#8220;Enter the scale of the car (1-4, 0=Exit): &#8220;);<br />
        scanf(&#8220;%i&#8221;,&#038;scale);<br />
    } while(scale < 0 || scale > 4);<br />
    // only draw if they have not entered a 0<br />
    if(scale > 0)<br />
    {<br />
        // this is a good example of the use of a switch<br />
        switch(scale)<br />
        {<br />
            case 1:<br />
                pad = 30;<br />
                break;<br />
            case 2:<br />
                pad = 21;<br />
                break;<br />
            case 3:<br />
                pad = 12;<br />
                break;<br />
            case 4:<br />
                pad = 2;<br />
                break;<br />
        }<br />
        // give it some spacing<br />
        printf(&#8220;\n&#8221;);<br />
        // calculate some values for car<br />
        hood = top = scale * 2;<br />
        bottomleft = body = scale * 3;<br />
        length = scale * 19;<br />
        topleft = scale * 6;<br />
        topright = scale * 14;<br />
        doorpost = scale * 10;<br />
        bottomright = scale * 13;<br />
        wheel = scale * 3 &#8211; 1;<br />
        // loop through each row of top<br />
        for(row=1;row< =top;row++)<br />
        {<br />
            // pad the row to center the car<br />
            for(col=1;col<=pad;col++)<br />
            {<br />
                printf(" ");<br />
            }<br />
            // this will be repeated for each row<br />
            for(col=1;col<=length;col++)<br />
            {<br />
                // if roof<br />
                if(row == 1)<br />
                {    // if between topleft and topright<br />
                    if(col >= topleft &#038;&#038; col < = topright)<br />
                    {<br />
                        printf("*");<br />
                    }<br />
                    else<br />
                    {<br />
                        printf(" ");<br />
                    }<br />
                }<br />
                else<br />
                {<br />
                    if((row + col) == (topleft + 1) || // front window<br />
                       (topright + row) == (col + 1) || // rear window<br />
                       col == doorpost)<br />
                    {<br />
                        printf("*");<br />
                    }<br />
                    else<br />
                    {<br />
                        printf(" ");<br />
                    }<br />
                }<br />
            }<br />
            // goto a new line<br />
            printf("\n");<br />
        }<br />
        // loop through each row of body<br />
        for(row=1;row<=body;row++)<br />
        {<br />
            // pad the row to center the car<br />
            for(col=1;col<=pad;col++)<br />
            {<br />
                printf(" ");<br />
            }<br />
            // this will be repeated for each row<br />
            for(col=1;col<=length;col++)<br />
            {<br />
                if(row == 1) // top line of body<br />
                {<br />
                    if(col >= hood) // round hood a bit<br />
                    {<br />
                        printf(&#8220;*&#8221;);<br />
                    }<br />
                    else<br />
                    {<br />
                        printf(&#8221; &#8220;);<br />
                    }<br />
                }<br />
                else if(row == body) // bottom line of body<br />
                {<br />
                    if(col < = bottomleft || // left of front wheel<br />
                        (col > (bottomleft + wheel) &#038;&#038;<br />
                         col < = bottomright) || // between wheels<br />
                       col > (bottomright + wheel)) // right of rear wheel<br />
                    {<br />
                        printf(&#8220;*&#8221;);<br />
                    }<br />
                    else<br />
                    {<br />
                        printf(&#8221; &#8220;);<br />
                    }<br />
                }<br />
                else if(row < = (((scale - 1) * 2) + 1)) // sloped front<br />
                {<br />
                    if((col + row) == (hood + 1) || // front<br />
                       col == length) // back<br />
                    {<br />
                        printf("*");<br />
                    }<br />
                    else<br />
                    {<br />
                        printf(" ");<br />
                    }<br />
                }<br />
                else // square front<br />
                {<br />
                    if(col == 1 || // front<br />
                       col == length) // back<br />
                    {<br />
                        printf("*");<br />
                    }<br />
                    else<br />
                    {<br />
                        printf(" ");<br />
                    }<br />
                }<br />
            }<br />
            // goto a new line<br />
            printf("\n");<br />
        }<br />
        // loop through each row of wheels<br />
        for(row=1;row<=scale;row++)<br />
        {<br />
            // pad the row to center the car<br />
            for(col=1;col<=pad;col++)<br />
            {<br />
                printf(" ");<br />
            }<br />
            // this will be repeated for each row<br />
            for(col=1;col<=length;col++)<br />
            {<br />
                if(row == scale) // bottom of wheel<br />
                {<br />
                    if((col >= (bottomleft + scale) &#038;&#038; // front wheel<br />
                       col < = (bottomleft + wheel - scale + 1)) ||<br />
                       (col >= (bottomright + scale) &#038;&#038; // rear wheel<br />
                       col <= (bottomright + wheel - scale + 1)))<br />
                    {<br />
                        printf("*");<br />
                    }<br />
                    else<br />
                    {<br />
                        printf(" ");<br />
                    }<br />
                }<br />
                else // slope of wheel<br />
                {<br />
                    if(col == (bottomleft + row) || // front left<br />
                       (col + row) == (bottomleft + wheel + 1) || // front right<br />
                       col == (bottomright + row) || // rear left<br />
                       (col + row) == (bottomright + wheel + 1)) // rear right<br />
                    {<br />
                        printf("*");<br />
                    }<br />
                    else<br />
                    {<br />
                        printf(" ");<br />
                    }<br />
                }<br />
            }<br />
            // goto a new line<br />
            printf("\n");<br />
        }<br />
    }<br />
    return 0;<br />
}<br />
</stdio></p>]]></content:encoded>
			<wfw:commentRss>http://atypicalhomeschool.net/tutorials/yet-another-drawing-program/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Another Drawing Program</title>
		<link>http://atypicalhomeschool.net/tutorials/another-drawing-program/</link>
		<comments>http://atypicalhomeschool.net/tutorials/another-drawing-program/#comments</comments>
		<pubDate>Thu, 01 Sep 2005 12:52:11 +0000</pubDate>
		<dc:creator>Ron</dc:creator>
				<category><![CDATA[Programming in C]]></category>
		<category><![CDATA[Tutorials]]></category>

		<guid isPermaLink="false">http://atypicalhomeschool.net/wp/tutorials/another-drawing-program/</guid>
		<description><![CDATA[Here is a follow-up drawing program similar to the draw X program. After looking at these two programs, try scaling another letter. /* Draw a scaled Y on the console or terminal window. Takes input value of length of the arms of the Y. C Tutorials for the home ed student Ron R. August 2005 [...]]]></description>
			<content:encoded><![CDATA[<p> Here is a follow-up drawing program similar to the draw X program. After looking at these two programs, try scaling another letter.<span id="more-49"></span></p>
<p>    /* Draw a scaled Y on the console or terminal window.<br />
       Takes input value of length of the arms of the Y.<br />
       C Tutorials for the home ed student<br />
       Ron R.<br />
       August 2005</p>
<p>    Consider an Y which is 9 characters high and 9 characters wide</p>
<p>    row    column<br />
     0  123456789<br />
     1  \       /<br />
     2   \     /<br />
     3    \   /<br />
     4     \ /<br />
     5      Y<br />
     6      |<br />
     7      |<br />
     8      |<br />
     9      |</p>
<p>    Note that:<br />
      the \ character appears where row = column<br />
      the / character appears where (row + column) = (2 * (arm + 1))<br />
      the Y appears where column = (arm + 1)<br />
      the | appears for arm length rows where column = (arm + 1)</p>
<p>    therefore we can divide the character into 2 sections:<br />
      the first is the upper arms<br />
      the second is the center and lower arm<br />
    */</p>
<p>    #include <stdio .h></p>
<p>    int main()<br />
    {<br />
        int arm; // length of arm<br />
        int width; // overall width (and height) of X<br />
        int row; // current row in printing<br />
        int col; // current column in printing</p>
<p>    /* this is a good example of the use of a do/while<br />
       we want to collect the value from the user before<br />
       we test to see if it is within an acceptable range<br />
       so that the drawn x will fit in the screen<br />
    */<br />
        do<br />
        {<br />
            printf(&#8220;Enter the length of the arm of the X (1-12, 0=Exit): &#8220;);<br />
            scanf(&#8220;%i&#8221;,&#038;arm);<br />
        } while(arm < 0 || arm > 12);<br />
        // only draw if they have not entered a 0<br />
        if(arm > 0)<br />
        {<br />
            width = 2 * (arm + 1);<br />
            // give it some spacing<br />
            printf(&#8220;\n&#8221;);</p>
<p>            // loop through each row of the top<br />
            for(row=1;row<=arm;row++)<br />
            {<br />
                // this will be repeated for each row<br />
                for(col=1;col<width;col++)<br />
                {<br />
                    // check for back diagonal<br />
                    if(row == col)<br />
                    {<br />
                        printf("\\");<br />
                    }<br />
                    // check for forward diagonal<br />
                    else if((row + col) == width)<br />
                    {<br />
                        printf("/");<br />
                    }<br />
                    // print an empty space<br />
                    else<br />
                    {<br />
                        printf(" "); // try replacing the space with a period<br />
                    }<br />
                }<br />
                // goto a new line<br />
                printf("\n");<br />
            }<br />
            // now draw leg (including centre) of Y<br />
            // change initial value to 0 to get extra row for center Y<br />
            for(row=0;row<=arm;row++)<br />
            {<br />
                // this will be repeated for each row<br />
                // change initial value to 0 to get extra column<br />
                for(col=0;col<=arm;col++)<br />
                {<br />
                    // time to print a character?<br />
                    if(col == arm)<br />
                    {<br />
                        if(row == 0)<br />
                        {<br />
                            printf("Y");<br />
                        }<br />
                        else<br />
                        {<br />
                            printf("|");<br />
                        }<br />
                    }<br />
                    // print an empty space<br />
                    else<br />
                    {<br />
                        printf(" "); // try replacing the space with a period<br />
                    }<br />
                }<br />
                // goto a new line<br />
                printf("\n");<br />
            }</p>
<p>        }<br />
        return 0;<br />
    }</p>
<p></stdio></p>]]></content:encoded>
			<wfw:commentRss>http://atypicalhomeschool.net/tutorials/another-drawing-program/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Tail Whips</title>
		<link>http://atypicalhomeschool.net/tutorials/tail-whips/</link>
		<comments>http://atypicalhomeschool.net/tutorials/tail-whips/#comments</comments>
		<pubDate>Tue, 30 Aug 2005 12:50:15 +0000</pubDate>
		<dc:creator>Ron</dc:creator>
				<category><![CDATA[Programming in C]]></category>
		<category><![CDATA[Tutorials]]></category>

		<guid isPermaLink="false">http://atypicalhomeschool.net/wp/tutorials/tail-whips/</guid>
		<description><![CDATA[Usually, in all of programming courses I taught, I would at some point in the course take about 15 minutes of the beginning of the class to explain something to them. For those who had me for a half dozen or more courses, this talk might have gotten a bit old. But, even then, it [...]]]></description>
			<content:encoded><![CDATA[<p> Usually, in all of programming courses I taught, I would at some point in the course take about 15 minutes of the beginning of the class to explain something to them. For those who had me for a half dozen or more courses, this talk might have gotten a bit old. But, even then, it was worthwhile for them to remember the standpoint I was teaching from.</p>
<p><span id="more-48"></span><br />
As far as I know, all of the students I had in college came from public school. Some had attended other colleges and/or universities. So far in these tutorials, I have covered about the first third of the content/language of a typical first level C programming course. The big difference between these tutorials and my typical college class is that, they had programming assignments to do, which they handed in and I marked. To some degree, the format of all my courses forced the students to do at least some programming.</p>
<p>Based on this background, it is really important that you see the difference between these tutorials and some of the other subjects that you might study for &#8216;school&#8217;. For example, if you study history, you can do quite well on a test solely because you have a good memory. Programming does not work like that.</p>
<p>Programming is something you have to do to learn. The example I frequently used for this was riding a bike. You don&#8217;t learn to ride a bike by having someone else explain what all the parts of the bike are, how to ride it, or by watching someone else ride one. Because you have to train the part of your brain that controls your muscles, you have to get on the bike and ride it. Tail whips and back flips take lots and lots of practice.</p>
<p>In the case of programming, you have to train your mind to think in a way that is at least a little different from anything you have likely done before. In a way, it is very much like playing a game. What I found most difficult for students to grasp was the reality that they were the only intelligence in the equation. When you use your computer, you are looking at a lot of things that look pretty complicated and give the computer the appearance of being smart/intuitive. But every bit of that has been design and developed by other people. When it comes down to it, every computer is something that can do a lot of very simply operations at very high speed. It is the programmer&#8217;s job to organize those simple operations to get the computer to do something they want.</p>
<p>So, the next few tutorials will be programs using the things I have already talked about. In each, I&#8217;ll explain the logic I used to arrive at the program that is implemented in the tutorial. It will be up to you to try similar things to exercise your programming muscles. Here is the first one:</p>
<p>/* Draw a scaled X on the console or terminal window.<br />
   Takes input value of length of the arm of the X.<br />
   C Tutorials for the home ed student<br />
   Ron R.<br />
   August 2005</p>
<p>Consider an X which is 9 characters high and 9 characters wide</p>
<p>row    column<br />
 0  123456789<br />
 1  \       /<br />
 2   \     /<br />
 3    \   /<br />
 4     \ /<br />
 5      X<br />
 6     / \<br />
 7    /   \<br />
 8   /     \<br />
 9  /       \</p>
<p>Note that:<br />
  the \ character appears where row = column<br />
  the / character appears where (row + column) = (2 * (arm + 1))<br />
  the X appears where row = column and (row + column) = (2 * (arm + 1))</p>
<p>*/</p>
<p>#include <stdio .h></p>
<p>int main()<br />
{</p>
<p>        int arm; // length of arm<br />
        int width; // overall width (and height) of X<br />
        int row; // current row in printing<br />
        int col; // current column in printing</p>
<p>    /* this is a good example of the use of a do/while<br />
       we want to collect the value from the user before<br />
       we test to see if it is within an acceptable range<br />
       so that the drawn x will fit in the screen<br />
    */<br />
        do<br />
        {</p>
<p>                printf(&#8220;Enter the length of the arm of the X (1-12, 0=Exit): &#8220;);<br />
                scanf(&#8220;%i&#8221;,&#038;arm);</p>
<p>        } while(arm < 0 || arm > 12);<br />
        // only draw if they have not entered a 0<br />
        if(arm > 0)<br />
        {</p>
<p>                width = 2 * (arm + 1);<br />
                // give it some spacing<br />
                printf(&#8220;\n&#8221;);</p>
<p>                // loop through each row<br />
                for(row=1;row<width;row++)<br />
                {</p>
<p>                        // this will be repeated for each row<br />
                        for(col=1;col<width;col++)<br />
                        {<br />
                            // check for centre<br />
                            if(row == col &#038;&#038; (row + col) == width)<br />
                            {</p>
<p>                                    printf("X");</p>
<p>                            }<br />
                            // check for back diagonal<br />
                            else if(row == col)<br />
                            {</p>
<p>                                    printf("\\");</p>
<p>                            }<br />
                            // check for forward diagonal<br />
                            else if((row + col) == width)<br />
                            {</p>
<p>                                    printf("/");</p>
<p>                            }<br />
                            // print an empty space<br />
                            else<br />
                            {</p>
<p>                                    printf(" "); // try replacing the space with a period</p>
<p>                            }<br />
                        }</p>
<p>                    // goto a new line<br />
                    printf("\n");<br />
                }</p>
<p>        }</p>
<p>        return 0;</p>
<p>}</stdio></p>]]></content:encoded>
			<wfw:commentRss>http://atypicalhomeschool.net/tutorials/tail-whips/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Operators</title>
		<link>http://atypicalhomeschool.net/tutorials/operators/</link>
		<comments>http://atypicalhomeschool.net/tutorials/operators/#comments</comments>
		<pubDate>Wed, 24 Aug 2005 14:40:14 +0000</pubDate>
		<dc:creator>Ron</dc:creator>
				<category><![CDATA[Programming in C]]></category>
		<category><![CDATA[Tutorials]]></category>

		<guid isPermaLink="false">http://atypicalhomeschool.net/wp/tutorials/programming-in-c/operators/</guid>
		<description><![CDATA[In this tutorial, I&#8217;ll decribed most of the commanly used operators in C. There are 2 types of operators: logical and arithematic. Logical operations produce a value of true or false. Arithematic operations return a calculated value. The logical operators are: &#62; - greater than &#8211; x &#62; y means compare x to y and [...]]]></description>
			<content:encoded><![CDATA[<p>In this tutorial, I&#8217;ll decribed most of the commanly used operators in C. There are 2 types of operators: logical and arithematic. Logical operations produce a value of true or false. Arithematic operations return a calculated value.<br />
<span id="more-17"></span><br />
The logical operators are:</p>
<blockquote><p><strong>&gt; </strong>- greater than &#8211; x &gt; y means compare x to y and if x is greater than y then return true otherwise return false</p>
<p> <strong>&gt;=</strong> &#8211; greater than or equal to &#8211; works the same as &gt; except if the values are equal then it also returns true</p>
<p> <strong>&lt;</strong> &#8211; less than &#8211; works the same as &gt; except if x is less than y then it returns true</p>
<p><strong> &lt;=</strong> &#8211; less than or equal to &#8211; works the same as &lt; except if the values are equal then it also returns true</p>
<p><strong>==</strong> &#8211; equal to &#8211; returns true if the values are the same otherwise returns false</p>
<p><strong>!=</strong> &#8211; not equal to &#8211; opposite of equal to</p>
<p><strong>!</strong> &#8211; not &#8211; changes false to true and true to false &#8211; eg. !(x &gt; y) would be the same as &lt;= y. This does have uses, I promise</p>
<p><strong>&amp;&amp;</strong> &#8211; and &#8211; requires that both the value on its left and the value on its right be true to return a true &#8211; eg. (x &gt; y) &amp;&amp; (x &gt; z) means x must be greater than both y and z for this to evaluate to true</p>
<p><strong>||</strong> &#8211; or (2 pipe symbols) &#8211; requires that either the value on its left or the value on its right be true to return a true &#8211; eg. (x &gt; y) || (x &gt; z) means x must be greater than either y or z for this to evaluate to true </p></blockquote>
<p> In the previous post, I used some arithematic operators. Here is a longer list:<br /> <strong><br /> </strong><br />
<blockquote><strong>=</strong> &#8211; assignment operator &#8211; performs all the calculations to the right of it to produce a single value and then assigns that value to the variable to the left of it. eg. x = y + z adds y and z and stores the result in x. The existing value that was in x is replaced.</p>
<p><strong>+</strong> &#8211; add <br /><strong>-</strong> &#8211; subtract <br /><strong>*</strong> &#8211; multiply<br /><strong>/</strong> &#8211; divide<br /><strong>%</strong> &#8211; modulus &#8211; this is an integer calculation. It produces the remainder from integer division. eg. 3 % 3 is 0, 4 % 3 is 1, and 5 % 3&nbsp; is 2</p>
<p><strong>+=</strong> &#8211; add assign &#8211; x += y adds x and y and stores the results in x. The remainder of the assigns work the same way<br /><strong>-=</strong> &#8211; subtract&nbsp; assign<br /><strong>*=</strong> &#8211; multiply assign<br />/= &#8211; divide assign<br /><strong>%=</strong> &#8211; modulus assign</p>
<p><strong>++</strong> &#8211; increment &#8211; can be used in 2 ways: preincrement ++x or postincrement x++. Increment increases the value stored in a variable by 1. The difference between pre and post increment is whether the increment is performed before or after the variable is used in the current line of code. </p>
<p> So, x = y + ++z increases z by 1 and then adds y and z and stores the result in x. <br /> x = y + z++ adds y and z and stores the result in x and then adds increases z by 1.</p>
<p><strong>&#8211;</strong> &#8211; decrement &#8211; works just like increment except that it decreases the value in the variable by 1. </p></blockquote>
<p> Thats not all the operators, but we can do a lot of things with these. I&#8217;ll introduce other operators when I use them in a sample.</p>]]></content:encoded>
			<wfw:commentRss>http://atypicalhomeschool.net/tutorials/operators/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Arrays</title>
		<link>http://atypicalhomeschool.net/tutorials/arrays/</link>
		<comments>http://atypicalhomeschool.net/tutorials/arrays/#comments</comments>
		<pubDate>Wed, 24 Aug 2005 14:38:18 +0000</pubDate>
		<dc:creator>Ron</dc:creator>
				<category><![CDATA[Programming in C]]></category>
		<category><![CDATA[Tutorials]]></category>

		<guid isPermaLink="false">http://atypicalhomeschool.net/wp/tutorials/programming-in-c/arrays/</guid>
		<description><![CDATA[One of the problems programmers face is dealing with a large number of the same datatype. An example of this is this web page. If you view the page source you will see that behind the scenes a web page is a bunch of text. In talking about datatypes, I introduced the char datatype which [...]]]></description>
			<content:encoded><![CDATA[<p>One of the problems programmers face is dealing with a large number of the same datatype. An example of this is this web page. If you view the page source you will see that behind the scenes a web page is a bunch of text. In talking about datatypes, I introduced the char datatype which holds 1 character. But this page consists of a few thousand characters. As a programmer, you would not want to have to create a variable for each character in the page. Programming would be a horribly inefficient means of processing a web page.<br />
<span id="more-16"></span><br />
The solution to this problem in many programming languages is an array. An array is simply a variable which contains space for more than 1 piece of data of a particular datatype.&nbsp; So, in processing this web page, your browser has created an array of the char datatype (assuming it is written in C). Let&#8217;s suppose that the browser needed an array of 20KB characters to hold and process this page. To get the size of the array we would need for 20KB, we do 20 x 1024 = 20,480 Bytes (or characters). To declare a character array of that size we would add the following line of code:</p>
<blockquote><p>char thePage[20480]; </p></blockquote>
<p> If we wanted to look at the first character in the page we would use this in our program:</p>
<blockquote><p>thePage[0]<br />  and the last character in the array would be<br />  thePage[20479] </p></blockquote>
<p> It is important to remember that C uses a zero based index for arrays. The last element of the array is always indexed at 1 less than the size of the array. The next post will be about program control. With it I will do another example program to demonstrate how arrays are used and why they are so useful. Once you start programming, you will find that you frequently use arrays.</p>]]></content:encoded>
			<wfw:commentRss>http://atypicalhomeschool.net/tutorials/arrays/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Conditions &amp; Iterations &#8211; Part 1 of 2</title>
		<link>http://atypicalhomeschool.net/tutorials/conditions-iterations-part-1-of-2/</link>
		<comments>http://atypicalhomeschool.net/tutorials/conditions-iterations-part-1-of-2/#comments</comments>
		<pubDate>Tue, 23 Aug 2005 14:36:50 +0000</pubDate>
		<dc:creator>Ron</dc:creator>
				<category><![CDATA[Programming in C]]></category>
		<category><![CDATA[Tutorials]]></category>

		<guid isPermaLink="false">http://atypicalhomeschool.net/wp/tutorials/programming-in-c/conditions-iterations-part-1-of-2/</guid>
		<description><![CDATA[This is the first of 2 posts on conditions and iterations. Essentially this means conditional execution and repetitive operations. Using arrays and a variable to index into the array we can process an entire array without writing code which specifically accesses each element in the array. Other than using arrays (which I&#8217;ve described in an [...]]]></description>
			<content:encoded><![CDATA[<p>This is the first of 2 posts on conditions and iterations. Essentially this means conditional execution and repetitive operations. Using arrays and a variable to index into the array we can process an entire array without writing code which specifically accesses each element in the array.<br />
<span id="more-15"></span><br />
Other than using arrays (which I&#8217;ve described in an earlier post), there are 2 new things in this post.</p>
<blockquote><p>if(condition to evaluate)<br />
{<br />
&#8230; one or more statements<br />
}<br />
else // can also be else if (condition to evaluate)<br />
{<br />
&#8230; one or more statements<br />
}</p>
<p>and</p>
<p>for(init;condition to evaluate;execute on loop)<br />
{<br />
&#8230; one or more statements<br />
}</p></blockquote>
<p>In both the <em>if</em> and the <em>for</em> statement is <strong>condition to evaluate</strong> which consists of the logical operations described in this <a href="http://www.raasmsoftware.com/wordpress/?p=12">post.</a> The code that is between the first set of braces after the <em>if</em> are executed if the logical operation produces a true result. If there is an <em>else</em> associated with the <em>if</em> then code between the set of braces after the <em>else</em> are executed.</p>
<p>In C, <strong>for</strong> is a looping (or iterating) statement. In the <em>for</em> statement, the <strong>init</strong> is code that is executed before the loop begins. The code between the braces is executed as 1 iteration of the loop. At the end of each iteration, the code in <strong>execute on loop</strong> is executed. Then the <em>for</em> statement evaluates <strong>condition to evaluate</strong> and if it evaluates to true then it re-executes the loop. This continues until the <strong>condition to evaluate</strong> evaluates to false.</p>
<p>Here is the sample code for this tutorial. It is commented as well.</p>
<blockquote><p>
#include &lt;stdio.h&gt;</p>
<p>/*	Sample Program for C Tutorials for the home ed student.<br />
		Demonstrating the use of an integer array and a character <br />
		array.<br />
		Ron R.<br />
		July 2005<br />
		*/</p>
<p>int main()<br />
{
</p></blockquote>
<blockquote><p>	// some variables for iteration<br />
	int i,j;<br />
	// some variables to track integer values<br />
	int max,min,sum;<br />
	// preassign values to integer array<br />
	int example[10] = {45,22,67,987,423,11,45,76,16,91};<br />
	// space for an extra character has to be left on the end<br />
	char sentence[13] = &#8220;Hello World!&#8221;;<br />
	// show the sentence one character at a time<br />
	for(i=0;i&lt;13;i++)<br />
	{
</p></blockquote>
<blockquote><p>		/* %c = print a character<br />
			 then printf needs a piece of data for each control string	<br />
		*/<br />
		printf(&#8220;%c &#8220;,sentence[i]);
</p></blockquote>
<p>	}<br />
	/* print integer values and calculate values<br />
	 	 the next statement is called a compound assignment<br />
		 sum, min, max will all be set to the value in example[0]<br />
	*/<br />
	sum = min = max = example[0];</p>
<p>	printf(&#8220;\n&#8221;); // some spacing</p>
<p>	for(j=0;j&lt;10;j++)<br />
	{</p>
<blockquote><p>		// print it &#8211; %i = print an integer<br />
		printf(&#8220;%i:%i\n&#8221;,j,example[j]);<br />
		/* if this is not the first element in the array we have to do some<br />
			 other processing<br />
		*/<br />
		if(j != 0) <br />
		{
</p></blockquote>
<blockquote><p>			// add to sum<br />
			sum += example[j];<br />
		 	// if its smaller than current min then replace<br />
			if(example[j] < min)<br />
			{
</p></blockquote>
<blockquote><p>				min = example[j];
</p></blockquote>
<p>			}<br />
			// it will only be bigger when it is not smaller than min<br />
			else if (example[j] > max)<br />
			{</p>
<blockquote><p>				max = example[j];
</p></blockquote>
<p>			}<br />
		}<br />
	}<br />
	// print results<br />
	printf(&#8220;smallest #: %i\n&#8221;,min);<br />
	printf(&#8220;largest #: %i\n&#8221;,max);<br />
	printf(&#8220;sum of #s: %i\n&#8221;,sum);<br />
	return 0;<br />
}</p>
<p>Try it. Then change the numbers and see what happens. Put in a different sentence.</p>]]></content:encoded>
			<wfw:commentRss>http://atypicalhomeschool.net/tutorials/conditions-iterations-part-1-of-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Conditions &amp; Iterations &#8211; Part 2 of 2</title>
		<link>http://atypicalhomeschool.net/tutorials/conditions-iterations-part-2-of-2/</link>
		<comments>http://atypicalhomeschool.net/tutorials/conditions-iterations-part-2-of-2/#comments</comments>
		<pubDate>Sat, 20 Aug 2005 14:32:19 +0000</pubDate>
		<dc:creator>Ron</dc:creator>
				<category><![CDATA[Programming in C]]></category>
		<category><![CDATA[Tutorials]]></category>

		<guid isPermaLink="false">http://atypicalhomeschool.net/wp/tutorials/programming-in-c/conditions-iterations-part-2-of-2/</guid>
		<description><![CDATA[This is part 2 of 2 tutorials on conditions and iterations. Essentially this means conditional execution and repetitive operations. Using arrays and a variable to index into the array we can process an entire array without writing code which specifically accesses each element in the array. One of the differences I hope you note here [...]]]></description>
			<content:encoded><![CDATA[<p>This is part 2 of 2 tutorials on conditions and iterations. Essentially this means conditional execution and repetitive operations. Using arrays and a variable to index into the array we can process an entire array without writing code which specifically accesses each element in the array.<br />
<span id="more-14"></span><br />
One of the differences I hope you note here is the indentation of the code samples which tended to not show up in the blog. In the next week, we&#8217;ll move the other posts over here and indent while we are at it.</p>
<p>In addition to if/else if/else there is a second series of conditional execution statements.</p>
<blockquote><p>switch(variable to compare)<br />
{<br />
case literal:<br />
&#8230; one or more statements<br />
break;<br />
case literal:<br />
&#8230; one or more statements<br />
break;<br />
default:<br />
&#8230; one or more statements<br />
break;<br />
}
</p></blockquote>
<p>There really isn&#8217;t a good use for the <em>switch/case/default</em> structure in this tutorial&#8217;s example program. An instance where this is used is when you are offering the user a menu of choices. In that case, the <strong>variable to compare</strong> is the data that your program collected as the user&#8217;s choice from the menu. Each <strong>literal</strong> would be an option from the menu. In the <em>switch/case/default</em>, the <strong>default</strong> is optional. The <strong>default</strong> is the code that gets executed if none of <strong>literals</strong> in the <strong>cases</strong> are the same as the <strong>variable to compare</strong>.</p>
<p>To illustrate this, consider the following menu:</p>
<blockquote><p>Inventory Management</p>
<p>A &#8211; Add an Item<br />
C &#8211; Change an Item&#8217;s Information<br />
D &#8211; Delete an Item<br />
P &#8211; Print All Current Items<br />
Q &#8211; Quit Program
</p></blockquote>
<p>The <strong>switch/case/default</strong> might look like this:</p>
<blockquote><p>
switch(choice) // char choice defined above<br />
{
</p></blockquote>
<blockquote><p>case &#8216;A&#8217;:<<br />
<blockquote>&#8230;code to add an item<br />
break; // ends the switch statement and moves execution to closing brace
</p></blockquote>
<p>case &#8216;C&#8217;:</p>
<blockquote><p>&#8230;code to change an item<br />
break; 
</p></blockquote>
<p>case &#8216;D&#8217;:</p>
<blockquote><p>&#8230;code to delete an item<br />
break; 
</p></blockquote>
<p>case &#8216;P&#8217;:</p>
<blockquote><p>&#8230;code to print items<br />
break; 
</p></blockquote>
<p>case &#8216;Q&#8217;:</p>
<blockquote><p>&#8230;code to tell the program to quit<br />
break; 
</p></blockquote>
<p>default:</p>
<blockquote><p>printf(&#8220;Not a menu option\n&#8221;); // show error message<br />
break;</p></blockquote>
<p>}</p>
<p>And there are also alternate looping structures.</p>
<blockquote><p>while(condition to evaluate)<br />
{<br />
&#8230; one or more statements<br />
}</p>
<p>or</p>
<p>do<br />
{<br />
&#8230; one or more statements<br />
} while(condition to evaluate)
</p></blockquote>
<p>In both the <em>while</em> and the <em>do/while</em> statement is <strong>condition to evaluate</strong> which has the same application as it did in Part 1. Both the <strong>while</strong> and the the <strong>do/while</strong> are looping (or iterating) statements. The code between the braces is executed as 1 iteration of the loop. Then the <strong>while</strong> and the <strong>do/while</strong> statement evaluates <strong>condition to evaluate</strong> and if it evaluates to true then it re-executes the loop. This continues until the <strong>condition to evaluate</strong> evaluates to false. </p>
<p>The only difference between the <strong>while</strong> and the <strong>do/while</strong> is that the <strong>while</strong> test the condition before the first iteration of the loop but the <strong>do/while</strong> does not. So, if the condition is false on the first test a <strong>while</strong> does not execute but a <strong>do/while</strong> does.</p>
<p>Here is the sample code for this tutorial. It is commented as well.</p>
<blockquote><p>
#include &lt;stdio.h&gt;</p>
<p>/*	Sample Program for C Tutorials for the home ed student.<br />
		Demonstrating the use of an integer array and a character <br />
		array.<br />
		Ron R.<br />
		August 2005<br />
		*/</p>
<p>int main()<br />
{
</p></blockquote>
<blockquote><p>// some variables for iteration<br />
	int i,j;<br />
	// some variables to track integer values<br />
	int max,min,sum;<br />
	// preassign values to integer array<br />
	int example[10] = {45,22,67,987,423,11,45,76,16,91};<br />
	// space for an extra character has to be left on the end<br />
	char sentence[13] = &#8220;Hello World!&#8221;;<br />
	// show the sentence one character at a time<br />
	i=0; // set initial value for loop<br />
	while(i&lt;13)<br />
	{
</p></blockquote>
<blockquote><p>/* %c = print a character<br />
			 then printf needs a piece of data for each control string<br />
		*/<br />
		printf(&#8220;%c &#8220;,sentence[i]);<br />
		i++; // increment counter
</p></blockquote>
<p>	}<br />
	/* print integer values and calculate values<br />
	 	 the next statement is called a compound assignment<br />
		 sum, min, max will all be set to the value in example[0]<br />
	*/<br />
	sum = min = max = example[0];</p>
<p>	printf(&#8220;\n&#8221;); // some spacing</p>
<p>	j=0;<br />
	do<br />
	{</p>
<blockquote><p>		// print it &#8211; %i = print an integer<br />
		printf(&#8220;%i:%i\n&#8221;,j,example[j]);<br />
		/* if this is not the first element in the array we have to do some<br />
			 other processing<br />
		*/<br />
		if(j != 0) <br />
		{
</p></blockquote>
<blockquote><p>			// add to sum<br />
			sum += example[j];<br />
		 	// if its smaller than current min then replace<br />
			if(example[j] < min)<br />
			{
</p></blockquote>
<blockquote><p>				min = example[j];
</p></blockquote>
<p>			}<br />
			// it will only be bigger when it is not smaller than min<br />
			else if (example[j] > max)<br />
			{</p>
<blockquote><p>				max = example[j];
</p></blockquote>
<p>			}<br />
		}<br />
		j++;<br />
	} while(j&lt;10)<br />
	// print results<br />
	printf(&#8220;smallest #: %i\n&#8221;,min);<br />
	printf(&#8220;largest #: %i\n&#8221;,max);<br />
	printf(&#8220;sum of #s: %i\n&#8221;,sum);<br />
	return 0;<br />
}</p>]]></content:encoded>
			<wfw:commentRss>http://atypicalhomeschool.net/tutorials/conditions-iterations-part-2-of-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>datatypes &amp; comments example</title>
		<link>http://atypicalhomeschool.net/tutorials/datatypes-comments-example/</link>
		<comments>http://atypicalhomeschool.net/tutorials/datatypes-comments-example/#comments</comments>
		<pubDate>Mon, 18 Jul 2005 12:49:30 +0000</pubDate>
		<dc:creator>Ron</dc:creator>
				<category><![CDATA[Programming in C]]></category>
		<category><![CDATA[Tutorials]]></category>

		<guid isPermaLink="false">http://atypicalhomeschool.net/wp/tutorials/datatypes-comments-example/</guid>
		<description><![CDATA[In this code sample, I&#8217;ve placed lots of comments. To see how well I&#8217;ve done at commenting, I&#8217;ll leave it to you to ask any questions in the forums. #include /* a simple calculator July 19, 2005 Ron R. C Tutorials for the home ed student */ int main() { // declare dataspace int first; [...]]]></description>
			<content:encoded><![CDATA[<p>In this code sample, I&#8217;ve placed lots of comments. To see how well I&#8217;ve done at commenting, I&#8217;ll leave it to you to ask any questions in the forums.<span id="more-47"></span></p>
<p>    #include <stdio .h></p>
<p>    /* a simple calculator<br />
        July 19, 2005<br />
        Ron R.<br />
        C Tutorials for the home ed student<br />
    */<br />
    int main()<br />
    {</p>
<p>            // declare dataspace<br />
            int first;  // first integer<br />
            int second; // second integer<br />
            /* note you can declare 2 variable of the same type<br />
                in the same declaration */<br />
            float firstf,secondf; // first and second float values</p>
<p>            // now get some values from the keyboard<br />
            printf(&#8220;\nEnter a whole number: &#8220;);<br />
            // scanf() scans formatted input<br />
            scanf(&#8220;%i&#8221;,&#038;first); // %i is the formatting for an integer<br />
            printf(&#8220;Enter a second whole number: &#8220;);<br />
            scanf(&#8220;%i&#8221;,&#038;second); // store the value in the variable second<br />
            // now decimals<br />
            printf(&#8220;\nEnter 2 decimal numbers separated by a space: &#8220;);<br />
            /* %f is the formatting for an floating point<br />
                you can also scan for more than 1 value at a time<br />
            */<br />
            scanf(&#8220;%f %f&#8221;,&#038;firstf,&#038;secondf);</p>
<p>            // show some math<br />
            printf(&#8220;\n%i + %i = %i\n&#8221;,first,second,first + second);<br />
            printf(&#8220;%i &#8211; %i = %i\n&#8221;,first,second,first &#8211; second);<br />
            printf(&#8220;%i x %i = %i\n&#8221;,first,second,first * second);<br />
            printf(&#8220;\n%f + %f = %f\n&#8221;,firstf,secondf,firstf + secondf);<br />
            printf(&#8220;%f &#8211; %f = %f\n&#8221;,firstf,secondf,firstf &#8211; secondf);<br />
            printf(&#8220;%f * %f = %f\n&#8221;,firstf,secondf,firstf * secondf);</p>
<p>            return 0;</p>
<p>    }</p>
<p>Give it a try and see what happens with different ranges of values. Also, try giving it some bogus data like letters, etc.</stdio></p>]]></content:encoded>
			<wfw:commentRss>http://atypicalhomeschool.net/tutorials/datatypes-comments-example/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>datatypes</title>
		<link>http://atypicalhomeschool.net/tutorials/datatypes/</link>
		<comments>http://atypicalhomeschool.net/tutorials/datatypes/#comments</comments>
		<pubDate>Sat, 16 Jul 2005 12:44:55 +0000</pubDate>
		<dc:creator>Ron</dc:creator>
				<category><![CDATA[Programming in C]]></category>
		<category><![CDATA[Tutorials]]></category>

		<guid isPermaLink="false">http://atypicalhomeschool.net/wp/tutorials/datatypes/</guid>
		<description><![CDATA[In the The Traditional First Program I talked about the main() function returning an integer to the operating system. int is a datatype. In C, there are a few basic datatypes. Before I talk about those, I&#8217;ll explain a bit about computers and memory. The smallest piece of memory in a computer is called a [...]]]></description>
			<content:encoded><![CDATA[<p>In the <em>The Traditional First Program</em> I talked about the main() function returning an integer to the operating system.<strong> int</strong> is a datatype. In C, there are a few basic datatypes. Before I talk about those, I&#8217;ll explain a bit about computers and memory.<br />
<span id="more-46"></span><br />
 The smallest piece of memory in a computer is called a <strong>bit</strong>. Bit stands for binary digit. In the binary number system there are only two numbers: 0 and 1. So, a bit is like a switch that the computer can turn on and off. The next relevant memory size is called a byte. In a sense, this is really the basic size that computer programs work with. A byte is 8 bits and can contain 256 different values.</p>
<p>  From a byte we get kilobyte (KB), megabyte (MB), gigabyte (GB), etc. A KB is 1024 bytes. A MB is 1024 KB (1,048,576 bytes). A GB is 1024 MB (1,073,741,824 bytes). </p>
<p>  The basic datatypes in C are as follows with the number of bytes used in ():<br /> <br />
<blockquote><strong>char</strong> (1) a single character<br />   <strong>short</strong> (2) a whole number between -32768 and 32767<br />   <strong>int</strong> (2 or 4 depending on processor &#8211; on intel and similar processors it is 4) <br />   <strong>long</strong> (4) a whole number between -2,097,154 and 2,097,153<br />   <strong>float</strong> (4 on intel and similar processors) a floating point decimal value where min and max values depend on processor<br />   <strong>double</strong> (8) a double precision float. A double will hold a wider range of values than a float but it&#8217;s primary use is to provide more precise calculations. </p></blockquote>
<p>  Also, all datatypes except the float and double can have the keyword <strong>unsigned</strong> placed in front of them. What this means is that they only hold positive numbers. For example, and <strong>unsigned short</strong> can have values between 0 and 65,535.</p>
<p>  If I want to create an integer in a program to use to store a whole number value of a high score, I would do it like this:<br /> <br />
<blockquote>int highscore; </p></blockquote>
<p>  If I wanted to give it a starting value of 0, I would do this:<br /> <br />
<blockquote>int highscore=0; </p></blockquote>
<p>  You&#8217;ll see some examples of datatype usage in the next tutorial.</p>]]></content:encoded>
			<wfw:commentRss>http://atypicalhomeschool.net/tutorials/datatypes/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Comments</title>
		<link>http://atypicalhomeschool.net/tutorials/comments/</link>
		<comments>http://atypicalhomeschool.net/tutorials/comments/#comments</comments>
		<pubDate>Tue, 12 Jul 2005 12:44:15 +0000</pubDate>
		<dc:creator>Ron</dc:creator>
				<category><![CDATA[Programming in C]]></category>
		<category><![CDATA[Tutorials]]></category>

		<guid isPermaLink="false">http://atypicalhomeschool.net/wp/tutorials/comments/</guid>
		<description><![CDATA[Every programming language I&#8217;ve worked with allowed the programmer to add comments to the program. Comments are essentially programmer notes which the compiler ignores. In the small programs I&#8217;ve had here so far comments haven&#8217;t really been necessary. However, when you have a program that has 10,000 lines of code and you haven&#8217;t looked at [...]]]></description>
			<content:encoded><![CDATA[<p>Every programming language I&#8217;ve worked with allowed the programmer to add comments to the program. Comments are essentially programmer notes which the compiler ignores. In the small programs I&#8217;ve had here so far comments haven&#8217;t really been necessary. However, when you have a program that has 10,000 lines of code and you haven&#8217;t looked at it in a couple years, comments come in handy. A second instance where comments are handy is when you are trying to make changes to programs someone else wrote. I&#8217;ve modified programs written by other people. Some were well commented, others not. If I had not already been convinced of the value of comments, working with those poorly commented programs would have changed my mind.<span id="more-45"></span></p>
<p>In C, comments can be added to a program in 2 ways. The first way is with a //. When the compiler sees // it ignores the rest of the line. So, you can add a comment that is a full line in the program by starting the line with //. But you can also use the // after the line of code like this:</p>
<p>    printf(&#8220;Hello World!\n&#8221;); // Say Hello</p>
<p>The compiler ignores the Say Hello. The // does not need to have a space before or after it.</p>
<p>The second type of comment is a multiline comment. Multiline comments begin with /* and end with */. The compiler ignores everything between the comments. The /* and the */ do not need to be on separate lines, but can be any number of lines apart. There are sensible restrictions to the use of /* and */. For example</p>
<p>    printf(&#8220;Hello /* Say Hello */World\n&#8221;);<br />
    will print<br />
    Hello /* Say Hello */World</p>
<p>    printf/* Say Hello */(&#8220;Hello World\n&#8221;);<br />
    will give an error</p>
<p>    printf(&#8220;Hello World\n&#8221;)/* Say Hello */;<br />
    will print<br />
    Hello World</p>
<p>    printf(&#8220;Hello World\n&#8221;)<br />
    /*<br />
    Say Hello<br />
    */;<br />
    will print<br />
    Hello World </p>]]></content:encoded>
			<wfw:commentRss>http://atypicalhomeschool.net/tutorials/comments/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

