<?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>electrofriends.com &#187; C</title>
	<atom:link href="http://electrofriends.com/category/articles/computer-science/c-tutorials/feed/" rel="self" type="application/rss+xml" />
	<link>http://electrofriends.com</link>
	<description>...bringing innovative minds together</description>
	<lastBuildDate>Tue, 07 Feb 2012 08:43:06 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.6</generator>
		<item>
		<title>Comments in C language</title>
		<link>http://electrofriends.com/articles/computer-science/c-tutorials/comments-language/</link>
		<comments>http://electrofriends.com/articles/computer-science/c-tutorials/comments-language/#comments</comments>
		<pubDate>Mon, 12 Sep 2011 17:16:47 +0000</pubDate>
		<dc:creator>chitra</dc:creator>
				<category><![CDATA[C]]></category>
		<category><![CDATA[block comment]]></category>
		<category><![CDATA[c comment]]></category>
		<category><![CDATA[c comments]]></category>
		<category><![CDATA[c tutorial]]></category>
		<category><![CDATA[comments in c]]></category>
		<category><![CDATA[line comment]]></category>

		<guid isPermaLink="false">http://electrofriends.com/?p=2168</guid>
		<description><![CDATA[Comments are merely program documentation which helps the reader to understand the code. The compiler ignores these comments when it translates the program into executable code. C language uses two different formats of comment 1. Block comment 2. Line comment 1. Block comment: A block comment is used when the comment will span several lines. [...]]]></description>
			<content:encoded><![CDATA[<p>Comments are merely program documentation which helps the reader to understand the code.<br />
The compiler ignores these comments when it translates the program into executable code.<br />
C language uses two different formats of comment<br />
<strong>1.	Block comment<br />
2.	Line comment</strong></p>
<p><strong>1. Block comment: </strong>A block comment is used when the comment will span several lines.<br />
<strong>Example:</strong></p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"> <span style="color: #808080; font-style: italic;">/*This is a block comment
                    that covers two lines*/</span></pre></div></div>

<p><strong><strong>2. Line comment:</strong></strong> The line comment uses two slashes (//).Programmers generally use this format for short comments.<br />
<strong>Example:</strong></p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">//This is a single line comment</span></pre></div></div>

<p>Example program:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code"><pre class="c" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">/*This is a Greeting program, which prints ‘Hello world’. 
This program demonstrates some of the components of a simple c program*/</span> 
<span style="color: #339933;">#include “stdio.h”</span>
<span style="color: #339933;">#include “conio.h”</span>
<span style="color: #993333;">void</span> main<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
<span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span>“Hello World”<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>  <span style="color: #666666; font-style: italic;">//This statement prints Hello World</span>
getch<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #666666; font-style: italic;">//main()</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://electrofriends.com/articles/computer-science/c-tutorials/comments-language/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>General Structure of C program</title>
		<link>http://electrofriends.com/articles/computer-science/c-tutorials/general-structure-program/</link>
		<comments>http://electrofriends.com/articles/computer-science/c-tutorials/general-structure-program/#comments</comments>
		<pubDate>Mon, 12 Sep 2011 15:26:02 +0000</pubDate>
		<dc:creator>chitra</dc:creator>
				<category><![CDATA[C]]></category>
		<category><![CDATA[basic structure of c]]></category>
		<category><![CDATA[c program tutorial]]></category>
		<category><![CDATA[structure of c program]]></category>

		<guid isPermaLink="false">http://electrofriends.com/?p=2158</guid>
		<description><![CDATA[Basic Structure of a C program contains following sections, Documentation Section Link Section Definition Section Global Declaration Section main&#40;&#41; &#123; Declaration Section Executable part &#125; Subprogram section Function 1 Function 2 . . function n The Documentation Section consists of a set of comment lines giving the name of the program and other details. The [...]]]></description>
			<content:encoded><![CDATA[<p>Basic Structure of a C program contains following sections,</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;">Documentation Section
Link Section
Definition Section
Global Declaration Section
main<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
Declaration Section
Executable part
<span style="color: #009900;">&#125;</span>
Subprogram section
Function <span style="color: #0000dd;">1</span>
Function <span style="color: #0000dd;">2</span>
.
.
<span style="color: #000000; font-weight: bold;">function</span> n</pre></div></div>

<p><strong>The Documentation Section</strong> consists of a set of comment lines giving the name of the program and other details.</p>
<p><strong>The Link Section</strong> provides instructions to the compiler to link functions from the system library. </p>
<p><strong>The Definition Section</strong> defines all symbolic constants.</p>
<p><strong>The Global Declaration Section:</strong> There are some variables and those variables are declared in this section that is outside of all functions. </p>
<p><strong>main() function:</strong> Every C program must have one main function section. This section contains two parts, declaration and executable part.<br />
<strong>Declaration Part</strong> declares all the variables used in the executable part.<br />
There should be at least one statement in the <strong>executable part</strong> which contains instructions to perform certain task.<br />
The declaration and executable part must appear between the opening and closing braces. All statements in the declaration part should end with the semicolon.</p>
<p>The <strong>Subprogram Section</strong> contains all the user defined functions that are called in the main function. </p>
<p><strong>Example Program:</strong></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
</pre></td><td class="code"><pre class="c" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">/*Documentation Section: program to find the area of circle*/</span>
<span style="color: #339933;">#include &quot;stdio.h&quot;   /*link section*/</span>
<span style="color: #339933;">#include &quot;conio.h&quot;  /*link section*/</span>
<span style="color: #339933;">#define PI 3.14     /*definition section*/</span>
<span style="color: #993333;">float</span> area<span style="color: #339933;">;</span>          <span style="color: #808080; font-style: italic;">/*global declaration  section*/</span>
<span style="color: #993333;">void</span> main<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
<span style="color: #993333;">float</span> r<span style="color: #339933;">;</span>                <span style="color: #808080; font-style: italic;">/*declaration part*/</span>
<span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;Enter the radius of the circle<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>   <span style="color: #808080; font-style: italic;">/*executable part starts here*/</span>
scanf<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;%f&quot;</span><span style="color: #339933;">,&amp;</span>r<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
area<span style="color: #339933;">=</span>PI<span style="color: #339933;">*</span>r<span style="color: #339933;">*</span>r<span style="color: #339933;">;</span>
<span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;Area of the circle=%f&quot;</span><span style="color: #339933;">,</span>area<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
getch<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://electrofriends.com/articles/computer-science/c-tutorials/general-structure-program/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Binary Search</title>
		<link>http://electrofriends.com/articles/computer-science/c-tutorials/binary-search/</link>
		<comments>http://electrofriends.com/articles/computer-science/c-tutorials/binary-search/#comments</comments>
		<pubDate>Mon, 15 Aug 2011 17:43:26 +0000</pubDate>
		<dc:creator>chitra</dc:creator>
				<category><![CDATA[Advanced programs]]></category>
		<category><![CDATA[C]]></category>
		<category><![CDATA[binary search]]></category>
		<category><![CDATA[c program]]></category>
		<category><![CDATA[Searching programs]]></category>

		<guid isPermaLink="false">http://electrofriends.com/?p=1591</guid>
		<description><![CDATA[A binary search program locates the position of an item in a sorted array. Logic: The Binary search starts by testing the element at the middle of the array. Let us consider an array ‘a’ with all elements arranged in ascending order. Let low and high are the lower and upper indices of the array ‘a’, respectively. We want to search an [...]]]></description>
			<content:encoded><![CDATA[<p>A binary<strong> </strong>search program locates the position of an item in a sorted array.</p>
<p>Logic: The Binary search starts by testing the element at the middle of the array. Let us consider an array ‘a’ with all elements arranged in ascending order. Let low and high are the lower and upper indices of the array ‘a’, respectively. We want to search an item K in it. To do this, we calculate a middle index with the values of low and high, let it be mid. Then we compare if K=a[mid] or not.</p>
<p>If it is true, then search is done. Otherwise, the search is to be repeated on left or right half depending on the K&lt;a[mid] or K&gt;a[mid], respectively. In case of low =high and K a[mid], the search terminates unsuccessfully.</p>
<p><img class="aligncenter size-full wp-image-1641" src="http://electrofriends.com/wp-content/uploads/2011/08/BinarySearch.png" alt="" width="583" height="207" /></p>
<p>Example 1:</p>
<p>Consider the following elements stored in an array,</p>
<p><img class="aligncenter size-full wp-image-1642" src="http://electrofriends.com/wp-content/uploads/2011/08/BinarySearch_Ex1.png" alt="" width="581" height="402" /></p>
<p>Example 2:</p>
<p>Consider the following elements stored in an array,</p>
<p><img class="aligncenter size-full wp-image-1643" src="http://electrofriends.com/wp-content/uploads/2011/08/BinarySearch_Ex2.png" alt="" width="584" height="430" /></p>
<p>Program:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
</pre></td><td class="code"><pre class="c" style="font-family:monospace;"><span style="color: #339933;">#include&quot;stdio.h&quot;</span>
<span style="color: #339933;">#include&quot;conio.h&quot;</span>
<span style="color: #993333;">void</span> main<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #993333;">int</span> a<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">25</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> i<span style="color: #339933;">,</span> n<span style="color: #339933;">,</span> K<span style="color: #339933;">,</span> flag <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">,</span> low<span style="color: #339933;">,</span> high<span style="color: #339933;">,</span> mid<span style="color: #339933;">;</span>
	clrscr<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;Enter the number of elementsn&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	scanf<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;%d&quot;</span><span style="color: #339933;">,</span> <span style="color: #339933;">&amp;</span>n<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;Enter the elementsn&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span>i <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span> i<span style="color: #339933;">&lt;</span>n<span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		scanf<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;%d&quot;</span><span style="color: #339933;">,&amp;</span>a<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;Enter the key to be searchedn&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	scanf<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;%d&quot;</span><span style="color: #339933;">,&amp;</span>K<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	low <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span>
	high <span style="color: #339933;">=</span> n <span style="color: #339933;">-</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">while</span><span style="color: #009900;">&#40;</span>low <span style="color: #339933;">&lt;=</span> high<span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		mid <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>low<span style="color: #339933;">+</span>high<span style="color: #009900;">&#41;</span><span style="color: #339933;">/</span><span style="color: #0000dd;">2</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>a<span style="color: #009900;">&#91;</span>mid<span style="color: #009900;">&#93;</span> <span style="color: #339933;">==</span> K<span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			flag <span style="color: #339933;">=</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">;</span>
			<span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>K<span style="color: #339933;">&lt;</span>a<span style="color: #009900;">&#91;</span>mid<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span>
			high <span style="color: #339933;">=</span> mid<span style="color: #339933;">-</span><span style="color: #0000dd;">1</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">else</span>
			low <span style="color: #339933;">=</span> mid <span style="color: #339933;">+</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>flag <span style="color: #339933;">==</span> <span style="color: #0000dd;">1</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;Key element is found&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #b1b100;">else</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;Key element not found&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	getch<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://electrofriends.com/articles/computer-science/c-tutorials/binary-search/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Insertion sort in C program</title>
		<link>http://electrofriends.com/source-codes/software-programs/c/sorting-programs/program-to-sort-the-numbers-using-insertion-sort/</link>
		<comments>http://electrofriends.com/source-codes/software-programs/c/sorting-programs/program-to-sort-the-numbers-using-insertion-sort/#comments</comments>
		<pubDate>Thu, 04 Dec 2008 06:12:42 +0000</pubDate>
		<dc:creator>Ranjith</dc:creator>
				<category><![CDATA[C]]></category>
		<category><![CDATA[Sorting Programs]]></category>
		<category><![CDATA[algorithm]]></category>
		<category><![CDATA[C/C++]]></category>
		<category><![CDATA[C/C++ Programms]]></category>
		<category><![CDATA[download]]></category>
		<category><![CDATA[Insertion sort]]></category>
		<category><![CDATA[logic]]></category>
		<category><![CDATA[sort in c]]></category>
		<category><![CDATA[sorting]]></category>
		<category><![CDATA[sorting demo]]></category>
		<category><![CDATA[sorting in c]]></category>
		<category><![CDATA[sorting numbers]]></category>
		<category><![CDATA[Source Codes]]></category>

		<guid isPermaLink="false">http://electrofriends.com/?p=351</guid>
		<description><![CDATA[Here is the program to sort the given integer in ascending order using insertion sort method. Please find the pictorial tutor of the insertion sorting. Logic : Here, sorting takes place by inserting a particular element at the appropriate position, that&#8217;s why the name-  insertion sorting. In the First iteration, second element A[1] is compared [...]]]></description>
			<content:encoded><![CDATA[<p>Here is the program to sort the given integer in ascending order using insertion sort method. Please find the pictorial tutor of the insertion sorting. </p>
<p><strong>Logic :</strong> Here, sorting takes place by inserting a particular element at the appropriate position, that&#8217;s why the name-  insertion sorting. In the First iteration, second element A[1] is compared with the first element A[0]. In the second iteration third element is compared with first and second element. In general, in every iteration an element is compared with all the elements before it. While comparing if it is found that the element can be inserted at a suitable position, then space is created for it by shifting the other elements one position up and inserts the desired element at the suitable position. This procedure is repeated for all the elements in the list.</p>
<p>If we complement the if condition in this program, it will give out the sorted array in descending order. Sorting can also be done in other methods, like selection sorting and bubble sorting, which follows in the next pages.</p>
<div id="attachment_912" class="wp-caption aligncenter" style="width: 590px"><img class="size-full wp-image-912" title="Insertion Sort Demo" src="http://electrofriends.com/wp-content/uploads/2008/12/insertion_sort.jpg" alt="Insertion Sort Demo" width="580" height="1332" /><p class="wp-caption-text">Insertion Sort Demo</p></div>
<p><strong>C program for Insertion Sort:</strong></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
</pre></td><td class="code"><pre class="c" style="font-family:monospace;"><span style="color: #339933;">#include&lt;stdio.h&gt;</span>
<span style="color: #993333;">void</span> main<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #993333;">int</span> A<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">20</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> N<span style="color: #339933;">,</span> Temp<span style="color: #339933;">,</span> i<span style="color: #339933;">,</span> j<span style="color: #339933;">;</span>
	clrscr<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span><span style="color: #000099; font-weight: bold;">\n</span><span style="color: #000099; font-weight: bold;">\t</span> ENTER THE NUMBER OF TERMS...: &quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	scanf<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;%d&quot;</span><span style="color: #339933;">,</span> <span style="color: #339933;">&amp;</span>N<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span><span style="color: #000099; font-weight: bold;">\t</span> ENTER THE ELEMENTS OF THE ARRAY...:&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span>i<span style="color: #339933;">=</span><span style="color: #0000dd;">0</span><span style="color: #339933;">;</span> i<span style="color: #339933;">&lt;</span>N<span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		gotoxy<span style="color: #009900;">&#40;</span><span style="color: #0000dd;">25</span><span style="color: #339933;">,</span><span style="color: #0000dd;">11</span><span style="color: #339933;">+</span>i<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		scanf<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span><span style="color: #000099; font-weight: bold;">\t</span><span style="color: #000099; font-weight: bold;">\t</span>%d&quot;</span><span style="color: #339933;">,</span> <span style="color: #339933;">&amp;</span>A<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span>i<span style="color: #339933;">=</span><span style="color: #0000dd;">1</span><span style="color: #339933;">;</span> i<span style="color: #339933;">&lt;</span>N<span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		Temp <span style="color: #339933;">=</span> A<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
		j <span style="color: #339933;">=</span> i<span style="color: #339933;">-</span><span style="color: #0000dd;">1</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">while</span><span style="color: #009900;">&#40;</span>Temp<span style="color: #339933;">&lt;</span>A<span style="color: #009900;">&#91;</span>j<span style="color: #009900;">&#93;</span> <span style="color: #339933;">&amp;&amp;</span> j<span style="color: #339933;">&gt;=</span><span style="color: #0000dd;">0</span><span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			A<span style="color: #009900;">&#91;</span>j<span style="color: #339933;">+</span><span style="color: #0000dd;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> A<span style="color: #009900;">&#91;</span>j<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
			j <span style="color: #339933;">=</span> j<span style="color: #339933;">-</span><span style="color: #0000dd;">1</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		A<span style="color: #009900;">&#91;</span>j<span style="color: #339933;">+</span><span style="color: #0000dd;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> Temp<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span><span style="color: #000099; font-weight: bold;">\t</span>THE ASCENDING ORDER LIST IS...:<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span>i<span style="color: #339933;">=</span><span style="color: #0000dd;">0</span><span style="color: #339933;">;</span> i<span style="color: #339933;">&lt;</span>N<span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span>
		<span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span><span style="color: #000099; font-weight: bold;">\t</span><span style="color: #000099; font-weight: bold;">\t</span><span style="color: #000099; font-weight: bold;">\t</span>%d&quot;</span><span style="color: #339933;">,</span> A<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	getch<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p><a href="http://electrofriends.com/wp-content/uploads/2008/12/ins_srt.zip">Download exe and source code here.</a></p>
]]></content:encoded>
			<wfw:commentRss>http://electrofriends.com/source-codes/software-programs/c/sorting-programs/program-to-sort-the-numbers-using-insertion-sort/feed/</wfw:commentRss>
		<slash:comments>140</slash:comments>
		</item>
		<item>
		<title>Bubble sort program in C</title>
		<link>http://electrofriends.com/source-codes/software-programs/c/sorting-programs/program-to-sort-the-numbers-using-bubble-sort/</link>
		<comments>http://electrofriends.com/source-codes/software-programs/c/sorting-programs/program-to-sort-the-numbers-using-bubble-sort/#comments</comments>
		<pubDate>Thu, 04 Dec 2008 06:09:14 +0000</pubDate>
		<dc:creator>Ranjith</dc:creator>
				<category><![CDATA[C]]></category>
		<category><![CDATA[Sorting Programs]]></category>
		<category><![CDATA[Bubble sort]]></category>
		<category><![CDATA[bubblesort demo]]></category>
		<category><![CDATA[C/C++]]></category>
		<category><![CDATA[C/C++ Programms]]></category>
		<category><![CDATA[download]]></category>
		<category><![CDATA[Examples]]></category>
		<category><![CDATA[sorting demo]]></category>
		<category><![CDATA[sorting methods]]></category>

		<guid isPermaLink="false">http://electrofriends.com/?p=348</guid>
		<description><![CDATA[Here is the program to sort the given integer in ascending order using bubble sort method. Please find the pictorial tutor of the bubble sorting. Logic :  The entered integers are stored in the array A. Here, to sort the data in ascending order, any number is compared with the next numbers for orderliness. i.e. [...]]]></description>
			<content:encoded><![CDATA[<p>Here is the program to sort the given integer in ascending order using bubble sort method. Please find the pictorial tutor of the bubble sorting. </p>
<p>Logic :  The entered integers are stored in the array A. Here, to sort the data in ascending order, any number is compared with the next numbers for orderliness. i.e. first element A[0] is compared with the second  element A[1]. If forth is greater than the prior element then swapping them, else no change. Then second element is compared with third element, and procedure is continued. Hence, after the first iteration of the outer for loop, largest element is placed at the end of the array. In the second iteration, the comparisons are made till the last but one position and now second largest element is placed at the last but one position. The procedure is traced till the array length.</p>
<p>If we complement the if condition in this program, it will give out the sorted array in descending order. Sorting can also be done in other methods, like <a href="http://electrofriends.com/source-codes/software-programs/c/sorting-programs/program-to-sort-the-numbers-using-selection-sort/">selection sorting</a> and <a href="http://electrofriends.com/articles/computer-science/c-tutorials/source-codes/software-programs/c/sorting-programs/program-to-sort-the-numbers-using-insertion-sort/">insertion sorting</a>, which follows in the next pages.</p>
<div id="attachment_1077" class="wp-caption aligncenter" style="width: 590px"><img src="http://electrofriends.com/wp-content/uploads/2008/12/Bubble_sort.jpg" alt="Bubble sort Image Demo" title="Bubble sort Image Demo" width="580" height="1510" class="size-full wp-image-1077" /><p class="wp-caption-text">Bubble sort Image Demo</p></div>
<p>Here is the C program to sort the numbers using Bubble sort</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #339900;">#include&lt;stdio.h&gt;</span>
<span style="color: #0000ff;">void</span> main<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
	<span style="color: #0000ff;">int</span> A<span style="color: #008000;">&#91;</span><span style="color: #0000dd;">20</span><span style="color: #008000;">&#93;</span>, N, Temp, i, j<span style="color: #008080;">;</span>
	clrscr<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	<span style="color: #0000dd;">printf</span><span style="color: #008000;">&#40;</span>“\n\n\t ENTER THE NUMBER OF TERMS…<span style="color: #008080;">:</span> “<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	<span style="color: #0000dd;">scanf</span><span style="color: #008000;">&#40;</span>“<span style="color: #000040;">%</span>d”,<span style="color: #000040;">&amp;</span>N<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	<span style="color: #0000dd;">printf</span><span style="color: #008000;">&#40;</span>“\n\t ENTER THE ELEMENTS OF THE ARRAY…<span style="color: #008080;">:</span>”<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	<span style="color: #0000ff;">for</span><span style="color: #008000;">&#40;</span>i<span style="color: #000080;">=</span><span style="color: #0000dd;">0</span><span style="color: #008080;">;</span> i<span style="color: #000080;">&lt;</span>N<span style="color: #008080;">;</span> i<span style="color: #000040;">++</span><span style="color: #008000;">&#41;</span>
	<span style="color: #008000;">&#123;</span>
		gotoxy<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">25</span>, <span style="color: #0000dd;">11</span><span style="color: #000040;">+</span>i<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
		<span style="color: #0000dd;">scanf</span><span style="color: #008000;">&#40;</span>“\n\t\t<span style="color: #000040;">%</span>d”, <span style="color: #000040;">&amp;</span>A<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	<span style="color: #008000;">&#125;</span>
	<span style="color: #0000ff;">for</span><span style="color: #008000;">&#40;</span>i<span style="color: #000080;">=</span><span style="color: #0000dd;">0</span><span style="color: #008080;">;</span> i<span style="color: #000080;">&lt;</span>N<span style="color: #000040;">-</span><span style="color: #0000dd;">1</span><span style="color: #008080;">;</span> i<span style="color: #000040;">++</span><span style="color: #008000;">&#41;</span>
		<span style="color: #0000ff;">for</span><span style="color: #008000;">&#40;</span>j<span style="color: #000080;">=</span><span style="color: #0000dd;">0</span><span style="color: #008080;">;</span> j<span style="color: #000080;">&lt;</span>N<span style="color: #000040;">-</span>i<span style="color: #008080;">;</span>j<span style="color: #000040;">++</span><span style="color: #008000;">&#41;</span>
			<span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span>A<span style="color: #008000;">&#91;</span>j<span style="color: #008000;">&#93;</span><span style="color: #000080;">&gt;</span>A<span style="color: #008000;">&#91;</span>j<span style="color: #000040;">+</span><span style="color: #0000dd;">1</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">&#41;</span>
			<span style="color: #008000;">&#123;</span>
				Temp <span style="color: #000080;">=</span> A<span style="color: #008000;">&#91;</span>j<span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span>
				A<span style="color: #008000;">&#91;</span>j<span style="color: #008000;">&#93;</span> <span style="color: #000080;">=</span> A<span style="color: #008000;">&#91;</span>j<span style="color: #000040;">+</span><span style="color: #0000dd;">1</span><span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span>
				A<span style="color: #008000;">&#91;</span>j<span style="color: #000040;">+</span><span style="color: #0000dd;">1</span><span style="color: #008000;">&#93;</span> <span style="color: #000080;">=</span> Temp<span style="color: #008080;">;</span>
			<span style="color: #008000;">&#125;</span>
	<span style="color: #0000dd;">printf</span><span style="color: #008000;">&#40;</span>“\n\tTHE ASCENDING ORDER LIST IS…<span style="color: #008080;">:</span>\n”<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	<span style="color: #0000ff;">for</span><span style="color: #008000;">&#40;</span>i<span style="color: #000080;">=</span><span style="color: #0000dd;">0</span><span style="color: #008080;">;</span> i<span style="color: #000080;">&lt;</span>N<span style="color: #008080;">;</span> i<span style="color: #000040;">++</span><span style="color: #008000;">&#41;</span>
		<span style="color: #0000dd;">printf</span><span style="color: #008000;">&#40;</span>“\n\t\t\t<span style="color: #000040;">%</span>d”,A<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	getch<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p><a href="http://electrofriends.com/wp-content/uploads/2008/12/bubl_srt.zip">Download exe and source code here.</a></p>
]]></content:encoded>
			<wfw:commentRss>http://electrofriends.com/source-codes/software-programs/c/sorting-programs/program-to-sort-the-numbers-using-bubble-sort/feed/</wfw:commentRss>
		<slash:comments>45</slash:comments>
		</item>
		<item>
		<title>Mouse programming in C</title>
		<link>http://electrofriends.com/articles/computer-science/c-tutorials/mouse-programming-in-cc/</link>
		<comments>http://electrofriends.com/articles/computer-science/c-tutorials/mouse-programming-in-cc/#comments</comments>
		<pubDate>Thu, 20 Nov 2008 11:21:41 +0000</pubDate>
		<dc:creator>Ranjith</dc:creator>
				<category><![CDATA[C]]></category>
		<category><![CDATA[C/C++ graphics]]></category>
		<category><![CDATA[Mouse programming]]></category>

		<guid isPermaLink="false">http://electrofriends.com/?p=259</guid>
		<description><![CDATA[Mouse can be used in text mode as well as in graphics mode. Usually it is used in graphics mode. Hence we must first change over to graphics mode. In our program the function initgraph() is responsible for switching the mode from text to graphics .DETECT is a macro defined in &#8216;graphics.h&#8217;. It requests initgraph() [...]]]></description>
			<content:encoded><![CDATA[<p>Mouse can be used in text mode as well as in graphics mode. Usually it is used in graphics mode. Hence we must first change over to graphics mode. In our program the function initgraph() is responsible for switching the mode from text to graphics .DETECT is a macro defined in &#8216;graphics.h&#8217;. It requests initgraph() to automatically determine which graphics driver to load in order to switch to the highest resolution graphics mode. The initgraph() function takes three parameters, the graphics driver, the graphics mode and the path to the driver file.</p>
<p>Once the driver has been loaded, initgraph() sets up the numeric values of the graphics mode chosen in the variables gd and gm respectively. Here we are assuming that the driver files are in the directory &#8216;c:\tc\bgi&#8217;. Hence the path passed to initgraph() is &#8216;c:\tc\bgi&#8217;.</p>
<p>The various mouse functions can be accessed by setting up the AX register with different values (service number) and issuing interrupt number 51. The functions are listed below.</p>
<table id="tbl" style="border-collapse: collapse; text-align: left; height: 507px;" border="1" cellspacing="0" cellpadding="0" width="438" align="center" bordercolor="#c0c0c0">
<tbody>
<tr height="55">
<td width="14%" align="middle"><strong>Interrupt</strong></td>
<td width="14%" align="middle"><strong>Service</strong></td>
<td width="100%" align="middle"><strong>Purpose</strong></td>
</tr>
<tr>
<td width="14%" height="89" align="middle">51</td>
<td width="14%" height="89" align="middle">0  <span style="font-size: 11pt;"> </span></td>
<td width="100%" height="89" align="middle">
<p align="justify">Reset mouse and get status<br />
Call with AX = 0<br />
Returns:  AX = FFFFh If mouse support is available<br />
Ax = 0 If mouse support is not available</td>
</tr>
<tr>
<td width="14%" height="73" align="middle">51</td>
<td width="14%" height="73" align="middle">1</td>
<td width="100%" height="73" align="middle">
<p align="left">Show mouse pointer<br />
Call with AX = 1<br />
Returns: Nothing</td>
</tr>
<tr>
<td width="14%" height="76" align="middle">51</td>
<td width="14%" height="76" align="middle">2</td>
<td width="100%" height="76" align="middle">
<p align="left">Hide mouse pointer<br />
Call with AX = 2<br />
Returns: Nothing</td>
</tr>
<tr>
<td width="14%" height="193" align="middle">51</td>
<td width="14%" height="193" align="middle">3</td>
<td width="100%" height="193" align="middle">
<p align="justify">Get mouse position and button status<br />
Call with AX = 3<br />
Returns: BX = mouse button status<br />
Bit   Significance<br />
0     button not pressed<br />
1     left button is pressed<br />
2     right button is pressed<br />
3     center button is pressed<br />
CX = x coordinate<br />
DX = y coordinate</td>
</tr>
<tr>
<td width="14%" height="114" align="middle">51</td>
<td width="14%" height="114" align="middle">4</td>
<td width="100%" height="114" align="middle">
<p align="left">Set mouse pointer position<br />
Call with AX = 4<br />
CX = x coordinate<br />
DX = y coordinate<br />
Returns: Nothing</td>
</tr>
<tr>
<td width="14%" height="113" align="middle">51</td>
<td width="14%" height="113" align="middle">7</td>
<td width="100%" height="113" align="middle">
<p align="left">Set horizontal limits for pointer<br />
Call with AX = 7<br />
CX = minimum x coordinate<br />
DX = maximum x coordinate<br />
Returns: Nothing</td>
</tr>
<tr>
<td width="14%" height="113" align="middle">51</td>
<td width="14%" height="113" align="middle">8</td>
<td width="100%" height="113" align="middle">
<p align="left">Set vertical limits for pointer<br />
Call with AX = 8<br />
CX = minimum y coordinate<br />
DX = maximum y coordinate<br />
Returns: Nothing</td>
</tr>
</tbody>
</table>
<p>Let us consider a program which makes use of the above functions.</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #339900;">#include&amp;lt;graphics.h&amp;gt;</span>
<span style="color: #339900;">#include&amp;lt;stdio.h&amp;gt;</span>
<span style="color: #339900;">#include&amp;lt;conio.h&amp;gt;</span>
<span style="color: #339900;">#include&amp;lt;dos.h&amp;gt;</span>
<span style="color: #0000ff;">union</span> REGS in,out<span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">int</span> callmouse<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
in.<span style="color: #007788;">x</span>.<span style="color: #007788;">ax</span><span style="color: #000080;">=</span><span style="color: #0000dd;">1</span><span style="color: #008080;">;</span>
int86<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">51</span>,<span style="color: #000040;">&amp;</span>amp<span style="color: #008080;">;</span>in,<span style="color: #000040;">&amp;</span>amp<span style="color: #008080;">;</span>out<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #0000ff;">return</span> <span style="color: #0000dd;">1</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
<span style="color: #0000ff;">void</span> mouseposi<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> <span style="color: #000040;">&amp;</span>amp<span style="color: #008080;">;</span>xpos,<span style="color: #0000ff;">int</span> <span style="color: #000040;">&amp;</span>amp<span style="color: #008080;">;</span>ypos,<span style="color: #0000ff;">int</span> <span style="color: #000040;">&amp;</span>amp<span style="color: #008080;">;</span>click<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
in.<span style="color: #007788;">x</span>.<span style="color: #007788;">ax</span><span style="color: #000080;">=</span><span style="color: #0000dd;">3</span><span style="color: #008080;">;</span>
int86<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">51</span>,<span style="color: #000040;">&amp;</span>amp<span style="color: #008080;">;</span>in,<span style="color: #000040;">&amp;</span>amp<span style="color: #008080;">;</span>out<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
click<span style="color: #000080;">=</span>out.<span style="color: #007788;">x</span>.<span style="color: #007788;">bx</span><span style="color: #008080;">;</span>
xpos<span style="color: #000080;">=</span>out.<span style="color: #007788;">x</span>.<span style="color: #007788;">cx</span><span style="color: #008080;">;</span>
ypos<span style="color: #000080;">=</span>out.<span style="color: #007788;">x</span>.<span style="color: #007788;">dx</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
<span style="color: #0000ff;">int</span> mousehide<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
in.<span style="color: #007788;">x</span>.<span style="color: #007788;">ax</span><span style="color: #000080;">=</span><span style="color: #0000dd;">2</span><span style="color: #008080;">;</span>
int86<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">51</span>,<span style="color: #000040;">&amp;</span>amp<span style="color: #008080;">;</span>in,<span style="color: #000040;">&amp;</span>amp<span style="color: #008080;">;</span>out<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #0000ff;">return</span> <span style="color: #0000dd;">1</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
<span style="color: #0000ff;">void</span> setposi<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> <span style="color: #000040;">&amp;</span>amp<span style="color: #008080;">;</span>xpos,<span style="color: #0000ff;">int</span> <span style="color: #000040;">&amp;</span>amp<span style="color: #008080;">;</span>ypos<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
in.<span style="color: #007788;">x</span>.<span style="color: #007788;">ax</span><span style="color: #000080;">=</span><span style="color: #0000dd;">4</span><span style="color: #008080;">;</span>
in.<span style="color: #007788;">x</span>.<span style="color: #007788;">cx</span><span style="color: #000080;">=</span>xpos<span style="color: #008080;">;</span>
in.<span style="color: #007788;">x</span>.<span style="color: #007788;">dx</span><span style="color: #000080;">=</span>ypos<span style="color: #008080;">;</span>
int86<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">51</span>,<span style="color: #000040;">&amp;</span>amp<span style="color: #008080;">;</span>in,<span style="color: #000040;">&amp;</span>amp<span style="color: #008080;">;</span>out<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
<span style="color: #0000ff;">int</span> main<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
<span style="color: #0000ff;">int</span> x,y,cl,a,b<span style="color: #008080;">;</span>
clrscr<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #0000ff;">int</span> g<span style="color: #000080;">=</span>DETECT,m<span style="color: #008080;">;</span>
initgraph<span style="color: #008000;">&#40;</span><span style="color: #000040;">&amp;</span>amp<span style="color: #008080;">;</span>g,<span style="color: #000040;">&amp;</span>amp<span style="color: #008080;">;</span>m,<span style="color: #FF0000;">&quot;c:<span style="color: #000099; font-weight: bold;">\t</span>c<span style="color: #000099; font-weight: bold;">\b</span>gi&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
a<span style="color: #000080;">=</span><span style="color: #0000dd;">100</span><span style="color: #008080;">;</span>
b<span style="color: #000080;">=</span><span style="color: #0000dd;">400</span><span style="color: #008080;">;</span>
setposi<span style="color: #008000;">&#40;</span>a,b<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
callmouse<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #0000ff;">do</span>
<span style="color: #008000;">&#123;</span>
mouseposi<span style="color: #008000;">&#40;</span>x,y,cl<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
gotoxy<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">10</span>,<span style="color: #0000dd;">9</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #0000dd;">printf</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span><span style="color: #000099; font-weight: bold;">\t</span>Mouse Position is: %d,%d&quot;</span>,x,y<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #0000dd;">printf</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span><span style="color: #000099; font-weight: bold;">\t</span>Click: %d&quot;</span>,cl<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #0000dd;">printf</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span><span style="color: #000099; font-weight: bold;">\t</span>Press any key to hide the mouse&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span><span style="color: #0000ff;">while</span><span style="color: #008000;">&#40;</span><span style="color: #000040;">!</span>kbhit<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
getch<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
mousehide<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #0000dd;">printf</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span><span style="color: #000099; font-weight: bold;">\n</span><span style="color: #000099; font-weight: bold;">\t</span>Press any key to Exit&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
getch<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>The above program makes use of the following functions:</p>
<p>*  callmouse( )<br />
*  mouseposi( )<br />
*  mousehide( )<br />
*  setposi( )</p>
<p><strong> callmouse() </strong> :-  In this function AX is set to &#8220;1&#8243;. When this function is called in main() it displays the mouse pointer. The position of the pointer can be changed by using the mouse.</p>
<p><strong>mousehide() </strong>:-  In this function AX is set to &#8220;2&#8243;.When this function is called in main() it hides the mouse pointer. This function is useful while drawing  figures, first the mouse pointer is kept hidden, then the figure is been drawn and again the mouse pointer is been called.</p>
<p><strong>mouseposi() </strong>:-  In this function AX is set to &#8220;3&#8243;. This function returns the position of the mouse pointer. It contains three parameters,they are xpos,ypos,click. xpos and ypos returns the position of x co-ordinate and y co-ordinate respectively. Click is the integer variable which returns the values 1,2,3 corresponding to the button pressed on the mouse and 0 for buttons being not pressed.</p>
<p><strong> kbhit:</strong> If any key is pressed kbhit returns nonzero integer; if not it returns zero</p>
<p><strong>setposi() </strong>:-      In this function AX is set to &#8220;4&#8243;. This function sets the mouse pointer to specific position . CX is been loaded by x co-ordinate of the mouse pointer and DX is been loaded with the y co-ordinate of the mouse pointer.</p>
<p>Let us consider another program</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #339900;">#include&amp;lt;graphics.h&amp;gt;</span>
<span style="color: #339900;">#include&amp;lt;stdio.h&amp;gt;</span>
<span style="color: #339900;">#include&amp;lt;conio.h&amp;gt;</span>
<span style="color: #339900;">#include&amp;lt;dos.h&amp;gt;</span>
<span style="color: #0000ff;">union</span> REGS in,out<span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">int</span> callmouse<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
in.<span style="color: #007788;">x</span>.<span style="color: #007788;">ax</span><span style="color: #000080;">=</span><span style="color: #0000dd;">1</span><span style="color: #008080;">;</span>
int86<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">51</span>,<span style="color: #000040;">&amp;</span>amp<span style="color: #008080;">;</span>in,<span style="color: #000040;">&amp;</span>amp<span style="color: #008080;">;</span>out<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #0000ff;">return</span> <span style="color: #0000dd;">1</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
<span style="color: #0000ff;">void</span> restrictmouseptr<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> x1,<span style="color: #0000ff;">int</span> y1,<span style="color: #0000ff;">int</span> x2,<span style="color: #0000ff;">int</span> y2<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
in.<span style="color: #007788;">x</span>.<span style="color: #007788;">ax</span><span style="color: #000080;">=</span><span style="color: #0000dd;">7</span><span style="color: #008080;">;</span>
in.<span style="color: #007788;">x</span>.<span style="color: #007788;">cx</span><span style="color: #000080;">=</span>x1<span style="color: #008080;">;</span>
in.<span style="color: #007788;">x</span>.<span style="color: #007788;">dx</span><span style="color: #000080;">=</span>x2<span style="color: #008080;">;</span>
int86<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">51</span>,<span style="color: #000040;">&amp;</span>amp<span style="color: #008080;">;</span>in,<span style="color: #000040;">&amp;</span>amp<span style="color: #008080;">;</span>out<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
in.<span style="color: #007788;">x</span>.<span style="color: #007788;">ax</span><span style="color: #000080;">=</span><span style="color: #0000dd;">8</span><span style="color: #008080;">;</span>
in.<span style="color: #007788;">x</span>.<span style="color: #007788;">cx</span><span style="color: #000080;">=</span>y1<span style="color: #008080;">;</span>
in.<span style="color: #007788;">x</span>.<span style="color: #007788;">dx</span><span style="color: #000080;">=</span>y2<span style="color: #008080;">;</span>
int86<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">51</span>,<span style="color: #000040;">&amp;</span>amp<span style="color: #008080;">;</span>in,<span style="color: #000040;">&amp;</span>amp<span style="color: #008080;">;</span>out<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
<span style="color: #0000ff;">int</span> main<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
<span style="color: #0000ff;">int</span> x,y,cl,a,b<span style="color: #008080;">;</span>
clrscr<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #0000ff;">int</span> g<span style="color: #000080;">=</span>DETECT,m<span style="color: #008080;">;</span>
initgraph<span style="color: #008000;">&#40;</span><span style="color: #000040;">&amp;</span>amp<span style="color: #008080;">;</span>g,<span style="color: #000040;">&amp;</span>amp<span style="color: #008080;">;</span>m,<span style="color: #FF0000;">&quot;c:<span style="color: #000099; font-weight: bold;">\t</span>c<span style="color: #000099; font-weight: bold;">\b</span>gi&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
rectangle<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">100</span>,<span style="color: #0000dd;">100</span>,<span style="color: #0000dd;">550</span>,<span style="color: #0000dd;">400</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
callmouse<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
restrictmouseptr<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">100</span>,<span style="color: #0000dd;">100</span>,<span style="color: #0000dd;">550</span>,<span style="color: #0000dd;">400</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
getch<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>The above program makes use of the following functions:</p>
<p>*   Horizontal ( )<br />
*   Vertical( )</p>
<p><strong>Horizontal()</strong> :- In this function AX is set to &#8220;7&#8243;. Its sets the horizontal barrier for the pointer which restricts the mouse pointer to pass that limit. CX is been loaded with the minimum x co-ordinate and Dx is been loaded with the maximum x co-ordinate.</p>
<p><strong>Vertical()</strong> :-     In this function AX is set to &#8220;8&#8243;.Its sets the vertical barrier for the pointer which restricts the mouse pointer to pass that limit. CX is been loaded with the minimum y co-ordinate and Dx is been loaded with the maximum y co-ordinate.</p>
<p><a href="http://electrofriends.com/wp-content/uploads/2008/11/mouse.zip">Click here to download all example programs. </a></p>
]]></content:encoded>
			<wfw:commentRss>http://electrofriends.com/articles/computer-science/c-tutorials/mouse-programming-in-cc/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Turbo C graphics programming</title>
		<link>http://electrofriends.com/articles/computer-science/c-tutorials/turbo-c-graphics-programming/</link>
		<comments>http://electrofriends.com/articles/computer-science/c-tutorials/turbo-c-graphics-programming/#comments</comments>
		<pubDate>Thu, 20 Nov 2008 10:45:39 +0000</pubDate>
		<dc:creator>Ranjith</dc:creator>
				<category><![CDATA[C]]></category>
		<category><![CDATA[arc]]></category>
		<category><![CDATA[bar]]></category>
		<category><![CDATA[c graphics]]></category>
		<category><![CDATA[circle]]></category>
		<category><![CDATA[drawpoly]]></category>
		<category><![CDATA[ellipse]]></category>
		<category><![CDATA[fillpoly]]></category>
		<category><![CDATA[initgraph]]></category>
		<category><![CDATA[line]]></category>
		<category><![CDATA[outtextxy]]></category>
		<category><![CDATA[pieslice]]></category>
		<category><![CDATA[rectangle]]></category>
		<category><![CDATA[sector]]></category>
		<category><![CDATA[setfillstyle]]></category>
		<category><![CDATA[Turbo C graphics]]></category>

		<guid isPermaLink="false">http://electrofriends.com/?p=254</guid>
		<description><![CDATA[To start with graphics programming, Turbo C is a good choice. Even though DOS has its own limitations,  it is having a large number of  useful functions and is easy to program. To implement graphics algorithms,  To give graphical display of statistics, To view signals from any source, we can use C graphics. Here  is  [...]]]></description>
			<content:encoded><![CDATA[<p>To start with graphics programming, Turbo C is a good choice. Even though DOS has its own limitations,  it is having a large number of  useful functions and is easy to program. To implement graphics algorithms,  To give graphical display of statistics, To view signals from any source, we can use C graphics. Here  is  a article to start programming  with  Turbo  C. &#8216;Run and Learn&#8217; is  our   method.   We  have  used source codes throughout the explanations. Just  execute  them to understand what is happening.</p>
<p>Turbo C has a good collection of graphics libraries. If you know the basics of C, you can easily learn graphics programming. To start programming, let us write a small program that displays a circle on the screen.</p>
<p>/* simple.c<br />
example 1.0<br />
*/<br />
#include&lt;graphics.h&gt;<br />
#include&lt;conio.h&gt;</p>
<p>void main()<br />
{<br />
int gd=DETECT, gm;</p>
<p>initgraph(&amp;gd, &amp;gm, &#8220;c:\\turboc3\\bgi &#8221; );<br />
circle(200,100,150);</p>
<p>getch();<br />
closegraph();<br />
}</p>
<p>To run this program, you need graphics.h header file, graphics.lib library file and Graphics driver (BGI file) in the program folder. These files are part of Turbo C package. In all our programs we used 640&#215;480 VGA monitor. So all the programs are according to that specification. You need to make necessary changes to your programs according to your screen resolution. For VGA monitor, graphics driver used is EGAVGA.BGI.</p>
<p>Here, initgraph() function initializes the graphics mode and clears the screen. We will study the difference between text mode and graphics mode in detail latter.</p>
<p><strong> InitGraph: </strong> Initializes the graphics system.</p>
<p>Declaration:  void far initgraph(int far *graphdriver, int far *graphmode, char far *pathtodriver);</p>
<p>Remarks: To start the graphics system, you must first call initgraph.</p>
<p>initgraph initializes the graphics system by loading a graphics driver from disk (or validating a registered driver) then putting the system into graphics mode.</p>
<p>initgraph also resets all graphics settings (color, palette, current position, viewport, etc.) to their defaults, then resets graphresult to 0.<br />
Arguments:</p>
<p>*graphdriver: Integer that specifies the graphics driver to be used. You can give graphdriver a value using a constant of the graphics drivers enumeration type.</p>
<p>*graphmode : Integer that specifies the initial graphics mode (unless *graphdriver = DETECT). If *graphdriver = DETECT, initgraph sets *graphmode to the highest resolution available for the detected driver. You can give *graphmode a value using a constant of the graphics_modes enumeration type.</p>
<p>pathtodriver : Specifies the directory path where initgraph looks for graphics drivers (*.BGI) first.  If they&#8217;re not there, initgraph looks in the current directory.  If pathtodriver is null, the driver files must be in the current directory.  This is also the path settextstyle searches for the stroked character font files (*.CHR).</p>
<p>closegraph() function switches back the screen from graphcs mode to text mode. It clears the screen also. A graphics program should have a closegraph function at the end of graphics. Otherwise DOS screen will not go to text mode after running the program. Here, closegraph() is called after getch() since screen should not clear until user hits a key.</p>
<p>If you have the BGI file in the same folder of your program, you can just leave it as &#8220;&#8221; only. you need not mention *graphmode if you give *graphdriver as DETECT.</p>
<p>In graphics mode, all the screen co-ordinates are mentioned in terms of pixels. Number of pixels in the screen decides resolution of the screen. In the example 1.0,  circle is drawn with x-coordinate of the center 200, y-coordinate 100 and radius 150 pixels. All the coordinates are mentioned with respect to top-left corner of the screen.</p>
<p><strong>Basic Shapes and Colors:</strong></p>
<p>Now let us write a program to draw some basic shapes.</p>
<p>/*<br />
shapes.c<br />
example 1.1<br />
*/</p>
<p>#include&lt;graphics.h&gt;<br />
#include&lt;conio.h&gt;</p>
<p>void main()<br />
{<br />
int gd=DETECT, gm;<br />
int poly[12]={350,450, 350,410, 430,400, 350,350, 300,430, 350,450 };<br />
initgraph(&amp;gd, &amp;gm, &#8220;&#8221;);</p>
<p>circle(100,100,50);<br />
outtextxy(75,170, &#8220;Circle&#8221;);<br />
rectangle(200,50,350,150);<br />
outtextxy(240, 170, &#8220;Rectangle&#8221;);<br />
ellipse(500, 100,0,360, 100,50);<br />
outtextxy(480, 170, &#8220;Ellipse&#8221;);<br />
line(100,250,540,250);<br />
outtextxy(300,260,&#8221;Line&#8221;);</p>
<p>sector(150, 400, 30, 300, 100,50);<br />
outtextxy(120, 460, &#8220;Sector&#8221;);<br />
drawpoly(6, poly);<br />
outtextxy(340, 460, &#8220;Polygon&#8221;);<br />
getch();<br />
closegraph();<br />
}</p>
<p>Here is the screenshot of output:</p>
<div id="attachment_255" class="wp-caption aligncenter" style="width: 510px"><img class="size-full wp-image-255" title="Output of above program" src="http://electrofriends.com/wp-content/uploads/2008/11/c_grap1.gif" alt="Output of above program" width="500" height="375" /><p class="wp-caption-text">Output of above program</p></div>
<p>Here, circle() function takes x, y coordinates of the circle with respect to left top of the screen and radius of the circle in terms of pixels as arguments. Not that, in graphics, almost all the screen parameters are measured in terms of pixels.</p>
<p>Function outtextxy() displays a string in graphical mode. You can use different fonts, text sizes, alignments, colors and directions of the text that we will study later. Parameters passed are x and y coordinates of the position on the screen where text is to be displayed. There is another function outtext() that displays a text in the current position. Current position is the place where last drawing is ended. These functions are declared as follows:</p>
<p>void far outtextxy(int x, int y, char *text);<br />
void far outtext(char *text);</p>
]]></content:encoded>
			<wfw:commentRss>http://electrofriends.com/articles/computer-science/c-tutorials/turbo-c-graphics-programming/feed/</wfw:commentRss>
		<slash:comments>62</slash:comments>
		</item>
		<item>
		<title>Sorting of numbers</title>
		<link>http://electrofriends.com/articles/computer-science/c-tutorials/sorting-of-numbers/</link>
		<comments>http://electrofriends.com/articles/computer-science/c-tutorials/sorting-of-numbers/#comments</comments>
		<pubDate>Tue, 04 Nov 2008 20:04:34 +0000</pubDate>
		<dc:creator>Ranjith</dc:creator>
				<category><![CDATA[C]]></category>
		<category><![CDATA[Bubble sort]]></category>
		<category><![CDATA[c program]]></category>
		<category><![CDATA[demo in sorting]]></category>
		<category><![CDATA[graphics program]]></category>
		<category><![CDATA[Insertion sort]]></category>
		<category><![CDATA[Selection sort]]></category>

		<guid isPermaLink="false">http://electrofriends.com/?p=44</guid>
		<description><![CDATA[Sorting means arranging a set of data in some order. There are different methods that are used to sort the data in ascending order. Some of the  methods are Selection sort Bubble sort Insertion sort 1. Selection sort Here, to sort the data in ascending order, the 0th element is compared with all other elements. [...]]]></description>
			<content:encoded><![CDATA[<p>Sorting means arranging a set of data in some order. There are different methods that are used to sort the data in ascending order. Some of the  methods are</p>
<ol>
<li> Selection sort</li>
<li> Bubble sort</li>
<li> Insertion sort</li>
</ol>
<p><strong>1. Selection sort</strong></p>
<p>Here, to sort the data in ascending order, the 0th element is compared with all other elements. If it is greater than the compared element then they are interchanged. So after the first iteration smallest element is placed at the 0th position. The same procedure is repeated for the 1st element and so on. <a href="http://electrofriends.com/source-codes/software-programs/c/sorting-programs/program-to-sort-the-numbers-using-selection-sort/">Read more&#8230;</a></p>
<p><strong>2. Bubble sort</strong></p>
<p>Here, to sort the data in ascending order, 0th element is compared with the 1st element. If it is greater than the 1st element then they are interchanged else remains same. Then 1st element is compared with 2nd element and procedure is continued. So after the first iteration largest element is placed at the last position. In the second iteration, the comparisons are made till the last but one position and now second largest element is placed at the second last position. After all the iterations, the list becomes sorted. <a href="http://electrofriends.com/source-codes/software-programs/c/sorting-programs/program-to-sort-the-numbers-using-bubble-sort/">Read more&#8230;</a></p>
<p><strong>3. Insertion sort</strong></p>
<p>Here, sorting takes place by inserting a particular element at the appropriate position. To sort the element in ascending order, in first iteration,1st element is compared with the 0th element. In the second iteration 2nd element is compared with 0th and 1st element. In general, in every iteration an element is compared with all the elements before it. While comparing if it is found that the element in question can be inserted at a suitable position, then space is created for it by shifting the other element one position to the right and inserting the element at the suitable position. This procedure is repeated for all the elements in the list. <a href="../source-codes/software-programs/c/sorting-programs/program-to-sort-the-numbers-using-insertion-sort/">Read more&#8230;</a></p>
<p>In my program, you can enter 5 numbers which are to be sorted by any one of the above methods. After entering the numbers they are shown in red colour box and while comparison is done, the two elements which are compared are shown in green colour box. After sorting the elements, their permanent position is indicated by blue colour box. <a href="http://electrofriends.com/wp-content/uploads/2008/11/sort.zip">Click here to download the program</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://electrofriends.com/articles/computer-science/c-tutorials/sorting-of-numbers/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

