<?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>C-C++ Programming Encyclopedia &#187; Sample C Programs for Beginners</title>
	<atom:link href="http://www.c-cplusplus.com/category/c-programming/feed" rel="self" type="application/rss+xml" />
	<link>http://www.c-cplusplus.com</link>
	<description>Learn C Programming - C++ Tutorials &#124; Code Snippets &#124; FAQs</description>
	<lastBuildDate>Tue, 03 Jan 2012 22:19:47 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Heap Sort in C: Basics &amp; Code Snippet</title>
		<link>http://www.c-cplusplus.com/heap-sort</link>
		<comments>http://www.c-cplusplus.com/heap-sort#comments</comments>
		<pubDate>Thu, 25 Aug 2011 17:21:59 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Sample C Programs for Beginners]]></category>

		<guid isPermaLink="false">http://www.c-cplusplus.com/?p=324</guid>
		<description><![CDATA[Few of the sorting techniques like the bubble sort, selection or the insertion sort are good enough for sorting small number of element, while heap sort is used to large number of elements, in a faster way. It&#8217;s simple as well, but all it consumes is the additional memory. Before we get into heap sort [...]]]></description>
			<content:encoded><![CDATA[<p>Few of the sorting techniques like the bubble sort, selection or the insertion sort are good enough for sorting small number of element, while <a href="http://www.c-cplusplus.com/heap-sort">heap sort</a> is used to large number of elements, in a faster way. It&#8217;s simple as well, but all it consumes is the additional memory.<br />
<br />
Before we get into heap sort technique, let us learn something about the heap data structure, which is used in heap sort. Top element that is present in the heap indicates the next in the line as in the order either it may be highest or the lowest.<br />
<a href="http://www.c-cplusplus.com/wp-content/uploads/2011/08/heap-sort.gif"><img src="http://www.c-cplusplus.com/wp-content/uploads/2011/08/heap-sort.gif" alt="heap sort in c" title="heap-sort" width="396" height="324" class="alignnone size-full wp-image-371" /></a><br />
Firstly, we will have to create a heap and then go on adding elements to the heap and once you want to delete it then you can easily remove from the heap and in a sorted order as such.<br />
<br />
There are two things here one has to worry about &#8211;<br />
<br />
a. that is the time taken to create the heap, and<br />
b. the time taken to add and remove the element from the heap.</p>
<p>Well, this is quite faster when compared to any of the other sorting techniques.<br />
<br />
One of the major disadvantages associated with this technique is that it&#8217;s not stable for equal elements, as in it does not preserve the original order. F<br />
<br />
or instance if we consider having a list of messages in your inbox then, then you would want the sort by both time and date as well as in an alphabetical order. Then in such cases, you can first sort it as per the date and time and then sort as per the alphabetical order.<br />
<br />
Let us illustrate the above scenario with an example:<br />
<br />
From:  a@qwerty.com       sent: 1/1/2011<br />
From:  b@qwerty.com       sent:  1/1/2011<br />
From:  c@qwerty.com        sent: 2/9/2009<br />
After sorting using heap sort<br />
From:  c@qwerty.com        sent: 2/9/2009<br />
From:  b@qwerty.com       sent:  1/1/2011<br />
From:  a@qwerty.com       sent: 1/1/2011<br />
<br />
As we can see that “b” is ahead of “a” which does not happen in any other sorting techniques as such which indicates that the sort is unstable. A stable sort should look like:<br />
<br />
From:  c@qwerty.com        sent: 2/9/2009<br />
From:  a@qwerty.com       sent: 1/1/2011<br />
From:  b@qwerty.com       sent:  1/1/2011</p>
<p>
<strong>Code Snippet to Implement Heap Sort in C Programming Language</strong><br />
<br />
void HSort(int num[], int size)<br />
{<br />
  int j, temp;<br />
 for (j = (size / 2); j >= 0; j&#8211;)<br />
    shift(num, j, size &#8211; 1);<br />
 for (j = size-1; j >= 1; j&#8211;)<br />
  {<br />
    temp = num[0];<br />
    num[0] = num[i];<br />
    num[i] = temp;<br />
    shift(num, 0, i-1);<br />
  }<br />
}<br />
void shift(int num [], int head, int tail)<br />
{<br />
  int ok, max, temp;<br />
  ok = 0;<br />
  while ((head*2 <= tail) &#038;&#038; (!ok))<br />
  {<br />
    if (head*2 == tail)<br />
      max = head * 2;<br />
    else if (num [head * 2] > num [head * 2 + 1])<br />
      max= head * 2;<br />
    else<br />
      max = head * 2 + 1;</p>
<p>    if (num [head] < num[max])<br />
    {<br />
      temp = num[head];<br />
      num[head] = num [max];<br />
      num[max] = temp;<br />
      head = max;<br />
    }<br />
    else<br />
      ok = 1;<br />
  }<br />
}<br />
<br />
So, that was a simple <a href="http://www.c-cplusplus.com/heap-sort">illustration of heap sort in c</a> &#8211; stay tuned for more useful code snippets!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.c-cplusplus.com/heap-sort/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Fundamentals of Programming Data Structures in C</title>
		<link>http://www.c-cplusplus.com/programming-data-structures-in-c</link>
		<comments>http://www.c-cplusplus.com/programming-data-structures-in-c#comments</comments>
		<pubDate>Thu, 07 Apr 2011 11:00:09 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Sample C Programs for Beginners]]></category>

		<guid isPermaLink="false">http://www.c-cplusplus.com/?p=297</guid>
		<description><![CDATA[Data Structures in C programming language is one of the most important and interesting part. Let us compute the In-Degree and Out-Degree of a node ‘n’ of a Graph using adjacency matrix representation. Computation of In-Degree of a node ‘n’ can be done by using the adjacency matrix representation of a graph. The node number [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.c-cplusplus.com">Data Structures in C</a> programming language is one of the most important and interesting part. Let us compute the In-Degree and Out-Degree of a node ‘n’ of a Graph using adjacency matrix representation.<br />
<span id="more-297"></span><br />
<a href="http://www.c-cplusplus.com/wp-content/uploads/2011/04/data-structures-in-c.jpg"><img src="http://www.c-cplusplus.com/wp-content/uploads/2011/04/data-structures-in-c.jpg" alt="data structures in c" title="data structures in c" width="315" height="314" class="alignleft size-full wp-image-345" /></a>Computation of In-Degree of a node ‘n’ can be done by using the adjacency matrix representation of a graph. The node number ‘n’ can be used as a column index in the adjacency matrix and then you have to count the number of 1’s in that particular column of the adjacency matrix. This constitutes the In-Degree of the node ‘n’.<br />
<br />
The Out-Degree can be computed in a similar manner, where we have to use the node number ‘n’ as the index of row of the same adjacency matrix and then we have to count the number of 1’s in that particular row. This is the way which we can use to compute the <strong>In-Degree</strong> and <strong>Out-Degree</strong> of a graph.<br />
<br />
Here&#8217;s a <a href="http://freeonlineprogrammingtutorials.com">simple code snippet</a> illustrating usage of data structures in c programming language.<br />
<br />
#include < stdio.h ><br />
<br />
#define MAXIMUM 10<br />
<br />
void main()<br />
     {<br />
       int adjacency[ MAXIMUM ] [ MAXIMUM ], node, m, i;<br />
       printf(&#8220;Enter the number of nodes in graph maximum = %d\n&#8221;, MAXIMUM);<br />
        scanf(&#8220;%d&#8221;, &#038;m);<br />
       build_adj_matrix(adjacency, m);<br />
<br />
       for(i=0; i<m; i++)<br />
       {<br />
          printf("The indegree of the node %d is %d\n", i, in_degree(adjacency, i, m));<br />
          printf("The outdegree of the node %d is %d\n", i, out_degree(adjacency, i, m));<br />
       }<br />
}<br />
</p>
<p>void build_adj_matrix( int adjacency[ ] [ MAXIMUM ], int n)<br />
   {<br />
     int x, y;<br />
     for(x=0; x < n; x++)<br />
         for(y=0; y < n; y++)<br />
         {<br />
         printf("Enter 1 if there is an edge from %d to %d, otherwise enter 0 \n", x, y );<br />
         scanf("%d", &#038; adjacency[ x ] [ y ]);<br />
         }<br />
   }<br />
<br />
int out_degree(int adjacency[ ] [ MAXIMUM ], int i, int m)<br />
<br />
    {<br />
       int x, count =0;<br />
       for(x=0; x < m; x++)<br />
           if(adjacency[ i ] [ x ] ==1) count++;<br />
      return( count );<br />
    }<br />
   <br />
int in_degree(int adjacency [ ] [ MAXIMUM ], int i, int m)<br />
<br />
       {<br />
          int x, count =0;<br />
          for(x=0; x < m; x++)<br />
              if(adjacency[ x ] [ i ] ==1) count++;<br />
         return(count);<br />
       }<br /></p>
]]></content:encoded>
			<wfw:commentRss>http://www.c-cplusplus.com/programming-data-structures-in-c/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Biggest C Programming Applications</title>
		<link>http://www.c-cplusplus.com/biggest-c-programming-applications</link>
		<comments>http://www.c-cplusplus.com/biggest-c-programming-applications#comments</comments>
		<pubDate>Thu, 10 Mar 2011 03:07:51 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Sample C Programs for Beginners]]></category>

		<guid isPermaLink="false">http://www.c-cplusplus.com/?p=285</guid>
		<description><![CDATA[C programming language is a very powerful programming language. It is very much compatible with all the hardware types and can manipulate any hardware function in a very easy and efficient way. After Assembly language, the C programming language is the fastest and most powerful programming language known to man. There are various applications of [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.c-cplusplus.com">C programming language</a> is a very powerful programming language. It is very much compatible with all the hardware types and can manipulate any hardware function in a very easy and efficient way.<br />
<br />
After Assembly language, the C programming language is the fastest and most powerful programming language known to man.<br />
<br />
There are various applications of C programming language in either of the scientific or great system applications, and also in programming the drivers for the new hardware releases. Let us discuss some of the greatest applications programmed in C thus far, which you might be even shocked to hear about.<br />
<span id="more-285"></span><br />
<strong>Adobe Systems</strong><br />
<br />
Most of the big projects of Adobe Systems like the Photoshop, Dreamweaver, InDesign and various others are programmed using C programming language.<br />
<br />
<strong>Google</strong><br />
<br />
Google is very much keen on using C or C++ as their primary application programming language. You&#8217;d be amazed to know Google Files System and the Google Chrome software are programmed using C programming language as well.<br />
<br />
<strong>Mozilla</strong><br />
<br />
Mozilla uses C programming language in most of their projects like Firefox and Thunderbird.<br />
<br />
<strong>MySQL</strong><br />
<br />
MySQL uses C programming language in most of their applications along with the C++ programming language for their world’s most powerful database software.<br />
<br />
<strong>Autodesk Maya</strong><br />
<br />
Maya is one resource hog program and it needs immense speed for it to process graphics and that is only possible if they use either the C programming language or C++ programming language.<br />
<br />
<strong>Winamp Media Player</strong><br />
<br />
Winamp media player is the most used player around the globe and the sole reason for its popularity is the faster, efficient and responsive user interface even at the time of high sound quality playback. All the credit goes to the C programming language for its faster and more powerful approach.<br />
<br />
Some of the other software made using C programming language include &#8211;<br />
</p>
<p>•	12D Solutions,<br />
•	Bloomberg,<br />
•	Callas Software,<br />
•	Apple OS X,<br />
•	Windows, and<br />
•	Symbian OS.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.c-cplusplus.com/biggest-c-programming-applications/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Binary Tree Sorting in C</title>
		<link>http://www.c-cplusplus.com/binary-tree-sorting-in-c</link>
		<comments>http://www.c-cplusplus.com/binary-tree-sorting-in-c#comments</comments>
		<pubDate>Sat, 19 Jun 2010 10:04:35 +0000</pubDate>
		<dc:creator>Nanya</dc:creator>
				<category><![CDATA[Sample C Programs for Beginners]]></category>
		<category><![CDATA[Binary Tree Sorting in C]]></category>

		<guid isPermaLink="false">http://www.c-cplusplus.com/?p=128</guid>
		<description><![CDATA[Here&#8217;s the example of Binary Tree Sorting using c. #include < stdio.h > #include < conio.h > #include < alloc.h > struct btreenode { struct btreenode *leftchild ; int data ; struct btreenode *rightchild ; } ; void insert ( struct btreenode **, int ) ; void inorder ( struct btreenode * ) ; void [...]]]></description>
			<content:encoded><![CDATA[<p>Here&#8217;s the example of <a href="http://www.c-cplusplus.com">Binary Tree Sorting </a>using c.<br />
<br />
<span id="more-128"></span></p>
<div style="display:block;float:right;padding:5px;">
<script type="text/javascript"><!--
google_ad_client = "pub-9680100702980837";
/* big_sq_red */
google_ad_slot = "4937024846";
google_ad_width = 336;
google_ad_height = 280;
//-->
</script><br />
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div>
<p>#include  < stdio.h ><br />
#include  < conio.h ><br />
#include  < alloc.h > </p>
<p>struct btreenode<br />
{<br />
	struct btreenode *leftchild ;</p>
<p>	int data ;</p>
<p>	struct btreenode *rightchild ;<br />
} ;</p>
<p>void insert ( struct btreenode **, int ) ;</p>
<p>void inorder ( struct btreenode * ) ;</p>
<p>void main( )<br />
{<br />
	struct btreenode *bt ;</p>
<p>	int arr[10] = { 11, 2, 9, 13, 57, 25, 17, 1, 90, 3 } ;</p>
<p>	int i ;</p>
<p>	bt = NULL ;</p>
<p>	clrscr( ) ;</p>
<p>	printf ( &#8220;Binary tree sort.\n&#8221; ) ;</p>
<p>	printf ( &#8220;\nArray:\n&#8221; ) ;</p>
<p>	for ( i = 0 ; i  < = 9 ; i++ )</p>
<p>		printf ( "%d\t", arr[i] ) ;</p>
<p>	for ( i = 0 ; i  < = 9 ; i++ )</p>
<p>		insert ( &#038;bt, arr[i] ) ;</p>
<p>	printf ( "\nIn-order traversal of binary tree:\n" ) ;</p>
<p>	inorder ( bt ) ;</p>
<p>	getch( ) ;<br />
}</p>
<p>void insert ( struct btreenode **sr, int num )<br />
{<br />
	if ( *sr == NULL )<br />
	{<br />
		*sr = malloc ( sizeof ( struct btreenode ) ) ;</p>
<p>		( *sr ) - >  leftchild = NULL ;</p>
<p>		( *sr ) &#8211; >  data = num ;</p>
<p>		( *sr ) &#8211; >  rightchild = NULL ;<br />
	}<br />
	else<br />
	{<br />
		if ( num  <  ( *sr ) - >  data )</p>
<p>			insert ( &#038;( ( *sr ) &#8211; >  leftchild ), num ) ;<br />
		else<br />
			insert ( &#038;( ( *sr ) &#8211; >  rightchild ), num ) ;<br />
	}<br />
}</p>
<p>void inorder ( struct btreenode *sr )<br />
{<br />
	if ( sr != NULL )<br />
	{<br />
		inorder ( sr &#8211; >  leftchild ) ;</p>
<p>		printf ( &#8220;%d\t&#8221;, sr &#8211; >  data ) ;</p>
<p>		inorder ( sr &#8211; >  rightchild ) ;<br />
	}<br />
}</p>
]]></content:encoded>
			<wfw:commentRss>http://www.c-cplusplus.com/binary-tree-sorting-in-c/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Insertion Sort in C</title>
		<link>http://www.c-cplusplus.com/insertion-sort-in-c</link>
		<comments>http://www.c-cplusplus.com/insertion-sort-in-c#comments</comments>
		<pubDate>Sat, 19 Jun 2010 10:00:19 +0000</pubDate>
		<dc:creator>Nanya</dc:creator>
				<category><![CDATA[Sample C Programs for Beginners]]></category>
		<category><![CDATA[Insertion Sort in C]]></category>

		<guid isPermaLink="false">http://www.c-cplusplus.com/?p=124</guid>
		<description><![CDATA[Let take look of Insertion sort in c. #include < stdio.h > #include < conio.h > void main( ) { int arr[5] = { 25, 17, 31, 13, 2 } ; int i, j, k, temp ; clrscr( ) ; printf ( &#8220;Insertion sort.\n&#8221; ) ; printf ( &#8220;\nArray before sorting:\n&#8221;) ; for ( i [...]]]></description>
			<content:encoded><![CDATA[<p>Let take look of<a href="http://www.c-cplusplus.com"> Insertion sort </a>in c.<br />
<br />
<span id="more-124"></span></p>
<div style="display:block;float:right;padding:5px;">
<script type="text/javascript"><!--
google_ad_client = "pub-9680100702980837";
/* big_sq_red */
google_ad_slot = "4937024846";
google_ad_width = 336;
google_ad_height = 280;
//-->
</script><br />
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div>
<p>#include  < stdio.h ><br />
#include  < conio.h > </p>
<p>void main( )<br />
{<br />
	int arr[5] = { 25, 17, 31, 13, 2 } ;</p>
<p>	int i, j, k, temp ;</p>
<p>	clrscr( ) ;</p>
<p>	printf ( &#8220;Insertion sort.\n&#8221; ) ;</p>
<p>	printf ( &#8220;\nArray before sorting:\n&#8221;) ;</p>
<p>	for ( i = 0 ; i  < = 4 ; i++ )</p>
<p>		printf ( "%d\t", arr[i] ) ;</p>
<p>	for ( i = 1 ; i  < = 4 ; i++ )<br />
	{<br />
		for ( j = 0 ; j  <  i ; j++ )<br />
		{<br />
			if ( arr[j]  >  arr[i] )<br />
			{<br />
				temp = arr[j] ;</p>
<p>				arr[j] = arr[i] ;</p>
<p>				for ( k = i ; k  >  j ; k&#8211; )</p>
<p>					arr[k] = arr[k - 1] ;</p>
<p>				arr[k + 1] = temp ;<br />
			}<br />
		}<br />
	}</p>
<p>	printf ( &#8220;\n\nArray after sorting:\n&#8221;) ;</p>
<p>	for ( i = 0 ; i  < = 4 ; i++ )</p>
<p>		printf ( &#8220;%d\t&#8221;, arr[i] ) ;</p>
<p>	getch( ) ;<br />
}</p>
]]></content:encoded>
			<wfw:commentRss>http://www.c-cplusplus.com/insertion-sort-in-c/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Quick Sort in C</title>
		<link>http://www.c-cplusplus.com/quick-sort-in-c</link>
		<comments>http://www.c-cplusplus.com/quick-sort-in-c#comments</comments>
		<pubDate>Fri, 18 Jun 2010 10:04:48 +0000</pubDate>
		<dc:creator>Nanya</dc:creator>
				<category><![CDATA[Sample C Programs for Beginners]]></category>
		<category><![CDATA[Quick Sort in C]]></category>

		<guid isPermaLink="false">http://www.c-cplusplus.com/?p=121</guid>
		<description><![CDATA[Here&#8217;s the example of Quick sort in c. #include < stdio.h > #include < conio.h > int split ( int*, int, int ) ; void main( ) { int arr[10] = { 11, 2, 9, 13, 57, 25, 17, 1, 90, 3 } ; int i ; void quicksort ( int *, int, int ) [...]]]></description>
			<content:encoded><![CDATA[<p>Here&#8217;s the example of<a href="http://www.c-cplusplus.com"> Quick sort </a>in c.<br />
<br />
<span id="more-121"></span></p>
<div style="display:block;float:right;padding:5px;">
<script type="text/javascript"><!--
google_ad_client = "pub-9680100702980837";
/* big_sq_red */
google_ad_slot = "4937024846";
google_ad_width = 336;
google_ad_height = 280;
//-->
</script><br />
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div>
<p>#include  < stdio.h ><br />
#include  < conio.h > </p>
<p>int split ( int*, int, int ) ;</p>
<p>void main( )<br />
{<br />
	int arr[10] = { 11, 2, 9, 13, 57, 25, 17, 1, 90, 3 } ;<br />
	int i ;</p>
<p>	void quicksort ( int *, int, int ) ;</p>
<p>	clrscr( ) ;</p>
<p>	printf ( &#8220;Quick sort.\n&#8221; ) ;</p>
<p>	printf ( &#8220;\nArray before sorting:\n&#8221;) ;</p>
<p>	for ( i = 0 ; i  < = 9 ; i++ )</p>
<p>		printf ( "%d\t", arr[i] ) ;</p>
<p>	quicksort ( arr, 0, 9 ) ;</p>
<p>	printf ( "\nArray after sorting:\n") ;</p>
<p>	for ( i = 0 ; i  < = 9 ; i++ )</p>
<p>		printf ( "%d\t", arr[i] ) ;</p>
<p>	getch( ) ;<br />
}</p>
<p>void quicksort ( int a[ ], int lower, int upper )<br />
{<br />
	int i ;</p>
<p>	if ( upper  >  lower )<br />
	{<br />
		i = split ( a, lower, upper ) ;</p>
<p>		quicksort ( a, lower, i &#8211; 1 ) ;</p>
<p>		quicksort ( a, i + 1, upper ) ;<br />
	}<br />
}</p>
<p>int split ( int a[ ], int lower, int upper )<br />
{<br />
	int i, p, q, t ;</p>
<p>	p = lower + 1 ;</p>
<p>	q = upper ;</p>
<p>	i = a[lower] ;</p>
<p>	while ( q  > = p )<br />
	{<br />
		while ( a[p]  <  i )</p>
<p>			p++ ;</p>
<p>		while ( a[q]  >  i )</p>
<p>			q&#8211; ;</p>
<p>		if ( q  >  p )<br />
		{<br />
			t = a[p] ;</p>
<p>			a[p] = a[q] ;</p>
<p>			a[q] = t ;<br />
		}<br />
	}</p>
<p>	t = a[lower] ;</p>
<p>	a[lower] = a[q] ;</p>
<p>	a[q] = t ;</p>
<p>	return q ;<br />
}</p>
]]></content:encoded>
			<wfw:commentRss>http://www.c-cplusplus.com/quick-sort-in-c/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to Maintain an AVL tree</title>
		<link>http://www.c-cplusplus.com/how-to-maintain-an-avl-tree</link>
		<comments>http://www.c-cplusplus.com/how-to-maintain-an-avl-tree#comments</comments>
		<pubDate>Wed, 16 Jun 2010 10:02:23 +0000</pubDate>
		<dc:creator>Nanya</dc:creator>
				<category><![CDATA[Sample C Programs for Beginners]]></category>
		<category><![CDATA[How to maintain an AVL tree]]></category>

		<guid isPermaLink="false">http://www.c-cplusplus.com/?p=102</guid>
		<description><![CDATA[Here&#8217;s C program to maintain an AVL tree. #include < stdio.h > #include < conio.h > #include < alloc.h > #define FALSE 0 #define TRUE 1 struct AVLNode { int data ; int balfact ; struct AVLNode *left ; struct AVLNode *right ; } ; struct AVLNode * buildtree ( struct AVLNode *, int, int [...]]]></description>
			<content:encoded><![CDATA[<p>Here&#8217;s C program to maintain an <a href="http://www.c-cplusplus.com">AVL tree</a>.<br />
<br />
<span id="more-102"></span></p>
<p>#include  < stdio.h ><br />
#include  < conio.h ><br />
#include  < alloc.h > </p>
<p>#define FALSE 0<br />
#define TRUE 1</p>
<p>struct AVLNode<br />
{<br />
	int data ;<br />
	int balfact ;<br />
	struct AVLNode *left ;<br />
	struct AVLNode *right ;<br />
} ;</p>
<p>struct AVLNode * buildtree ( struct AVLNode *, int, int * ) ;<br />
struct AVLNode * deldata ( struct AVLNode *, int, int * ) ;<br />
struct AVLNode * del ( struct AVLNode *, struct AVLNode *, int * ) ;<br />
struct AVLNode * balright ( struct AVLNode *, int * ) ;<br />
struct AVLNode * balleft ( struct AVLNode *, int * ) ;<br />
void display ( struct AVLNode * ) ;<br />
void deltree ( struct AVLNode * ) ;</p>
<p>void main( )<br />
{<br />
	struct AVLNode *avl = NULL ;<br />
	int h ;</p>
<p>	clrscr( ) ;</p>
<p>	avl = buildtree ( avl, 20, &#038;h ) ;<br />
	avl = buildtree ( avl, 6, &#038;h ) ;<br />
	avl = buildtree ( avl, 29, &#038;h ) ;<br />
	avl = buildtree ( avl, 5, &#038;h ) ;<br />
	avl = buildtree ( avl, 12, &#038;h ) ;<br />
	avl = buildtree ( avl, 25, &#038;h ) ;<br />
	avl = buildtree ( avl, 32, &#038;h ) ;<br />
	avl = buildtree ( avl, 10, &#038;h ) ;<br />
	avl = buildtree ( avl, 15, &#038;h ) ;<br />
	avl = buildtree ( avl, 27, &#038;h ) ;<br />
	avl = buildtree ( avl, 13, &#038;h ) ;</p>
<p>	printf ( &#8220;\nAVL tree:\n&#8221; ) ;<br />
	display ( avl ) ;</p>
<p>	avl = deldata ( avl, 20, &#038;h ) ;<br />
	avl = deldata ( avl, 12, &#038;h ) ;</p>
<p>	printf ( &#8220;\nAVL tree after deletion of a node:\n&#8221; ) ;<br />
	display ( avl ) ;</p>
<p>	deltree ( avl ) ;</p>
<p>	getch( ) ;<br />
}</p>
<p>/* inserts an element into tree */<br />
struct AVLNode * buildtree ( struct AVLNode *root, int data, int *h )<br />
{<br />
	struct AVLNode *node1, *node2 ;</p>
<p>	if ( !root )<br />
	{<br />
		root = ( struct AVLNode * ) malloc ( sizeof ( struct AVLNode ) ) ;<br />
		root &#8211; >  data = data ;<br />
		root &#8211; >  left = NULL ;<br />
		root &#8211; >  right = NULL ;<br />
		root &#8211; >  balfact = 0 ;<br />
		*h = TRUE ;<br />
		return ( root ) ;<br />
	}</p>
<p>	if ( data  <  root - >  data )<br />
	{<br />
		root &#8211; >  left = buildtree ( root &#8211; >  left, data, h ) ;<br />
		/* If left subtree is higher */<br />
		if ( *h )<br />
		{<br />
			switch ( root &#8211; >  balfact )<br />
			{<br />
				case 1:<br />
					node1 = root &#8211; >  left ;<br />
					if ( node1 &#8211; >  balfact == 1 )<br />
					{<br />
						printf ( &#8220;\nRight rotation along %d.&#8221;, root &#8211; >  data ) ;<br />
						root &#8211; >  left = node1 &#8211; >  right ;<br />
						node1 &#8211; >  right = root ;<br />
						root &#8211; >  balfact = 0 ;<br />
						root = node1 ;<br />
					}<br />
					else<br />
					{<br />
						printf ( &#8220;\nDouble rotation, left along %d&#8221;,<br />
                                                           node1 &#8211; >  data ) ;<br />
						node2 = node1 &#8211; >  right ;<br />
						node1 &#8211; >  right = node2 &#8211; >  left ;<br />
						printf ( &#8221; then right along %d.\n&#8221;, root &#8211; >  data ) ;<br />
						node2 &#8211; >  left = node1 ;<br />
						root &#8211; >  left = node2 &#8211; >  right ;<br />
						node2 &#8211; >  right = root ;<br />
						if ( node2 &#8211; >  balfact == 1 )<br />
							root &#8211; >  balfact = -1 ;<br />
						else<br />
							root &#8211; >  balfact = 0 ;<br />
						if ( node2 &#8211; >  balfact == -1 )<br />
							node1 &#8211; >  balfact = 1 ;<br />
						else<br />
							node1 &#8211; >  balfact = 0 ;<br />
						root = node2 ;<br />
					}<br />
					root &#8211; >  balfact = 0 ;<br />
					*h = FALSE ;<br />
					break ;</p>
<p>				case 0:<br />
					root &#8211; >  balfact = 1 ;<br />
					break ;</p>
<p>				case -1:<br />
					root &#8211; >  balfact = 0 ;<br />
					*h = FALSE ;<br />
			}<br />
		}<br />
	}</p>
<p>	if ( data  >  root &#8211; >  data )<br />
	{<br />
		root &#8211; >  right = buildtree ( root &#8211; >  right, data, h ) ;<br />
		/* If the right subtree is higher */<br />
		if ( *h )<br />
		{<br />
			switch ( root &#8211; >  balfact )<br />
			{<br />
				case 1:<br />
					root &#8211; >  balfact = 0 ;<br />
					*h = FALSE ;<br />
					break ;</p>
<p>				case 0:<br />
					root &#8211; >  balfact = -1 ;<br />
					break;</p>
<p>				case -1:<br />
					node1 = root &#8211; >  right ;<br />
					if ( node1 &#8211; >  balfact == -1 )<br />
					{<br />
						printf ( &#8220;\nLeft rotation along %d.&#8221;, root &#8211; >  data ) ;<br />
						root &#8211; >  right = node1 &#8211; >  left ;<br />
						node1 &#8211; >  left = root ;<br />
						root &#8211; >  balfact = 0 ;<br />
						root = node1 ;<br />
					}<br />
					else<br />
					{<br />
						printf ( &#8220;\nDouble rotation, right along %d&#8221;,<br />
                                                           node1 &#8211; >  data ) ;<br />
						node2 = node1 &#8211; >  left ;<br />
						node1 &#8211; >  left = node2 &#8211; >  right ;<br />
						node2 &#8211; >  right = node1 ;<br />
						printf ( &#8221; then left along %d.\n&#8221;, root &#8211; >  data ) ;<br />
						root &#8211; >  right = node2 &#8211; >  left ;<br />
						node2 &#8211; >  left = root ;</p>
<p>						if ( node2 &#8211; >  balfact == -1 )<br />
							root &#8211; >  balfact = 1 ;<br />
						else<br />
							root &#8211; >  balfact = 0 ;<br />
						if ( node2 &#8211; >  balfact == 1 )<br />
							node1 &#8211; >  balfact = -1 ;<br />
						else<br />
							node1 &#8211; >  balfact = 0 ;<br />
						root = node2 ;<br />
					}<br />
					root &#8211; >  balfact = 0 ;<br />
					*h = FALSE ;<br />
			}<br />
		}<br />
	}<br />
	return ( root ) ;<br />
}</p>
<p>/* deletes an item from the <a href=" http://www.c-cplusplus.com/category/linked-lists ">tree</a> */<br />
struct AVLNode * deldata ( struct AVLNode *root, int data, int *h )<br />
{<br />
	struct AVLNode *node ;</p>
<p>	if ( !root )<br />
	{<br />
		printf ( &#8220;\nNo such data.&#8221; ) ;<br />
		return ( root ) ;<br />
	}<br />
	else<br />
	{<br />
		if ( data  <  root - >  data )<br />
		{<br />
			root &#8211; >  left = deldata ( root &#8211; >  left, data, h ) ;<br />
			if ( *h )<br />
				root = balright ( root, h ) ;<br />
		}<br />
		else<br />
		{<br />
			if ( data  >  root &#8211; >  data )<br />
			{<br />
				root &#8211; >  right = deldata ( root &#8211; >  right, data, h ) ;<br />
				if ( *h )<br />
					root = balleft ( root, h ) ;<br />
			}<br />
			else<br />
			{<br />
				node = root ;<br />
				if ( node &#8211; >  right == NULL )<br />
				{<br />
					root = node &#8211; >  left ;<br />
					*h = TRUE ;<br />
					free ( node ) ;<br />
				}<br />
				else<br />
				{<br />
					if ( node &#8211; >  left == NULL )<br />
					{<br />
						root = node &#8211; >  right ;<br />
						*h = TRUE ;<br />
						free ( node ) ;<br />
					}<br />
					else<br />
					{<br />
						node &#8211; >  right = del ( node &#8211; >  right, node, h ) ;<br />
						if ( *h )<br />
							root = balleft ( root, h ) ;<br />
					}<br />
				}<br />
			}<br />
		}<br />
	}<br />
	return ( root ) ;<br />
}</p>
<p>struct AVLNode * del ( struct AVLNode *succ, struct AVLNode *node, int *h )<br />
{<br />
	struct AVLNode *temp = succ ;<br />
	if ( succ &#8211; >  left != NULL )<br />
	{<br />
		succ &#8211; >  left = del ( succ &#8211; >  left, node, h ) ;<br />
		if ( *h )<br />
			succ = balright ( succ, h ) ;<br />
	}<br />
	else<br />
	{<br />
		temp = succ ;<br />
		node &#8211; >  data = succ &#8211; >  data ;<br />
		succ = succ &#8211; >  right ;<br />
		free ( temp ) ;<br />
		*h = TRUE ;<br />
	}<br />
	return ( succ ) ;<br />
}</p>
<p>/* balances the tree, if right sub-tree is higher */<br />
struct AVLNode * balright ( struct AVLNode *root, int *h )<br />
{<br />
	struct AVLNode *node1, *node2 ;</p>
<p>	switch ( root &#8211; >  balfact )<br />
	{<br />
		case 1:<br />
			root &#8211; >  balfact = 0 ;<br />
			break;</p>
<p>		case 0:<br />
			root &#8211; >  balfact = -1 ;<br />
			*h  = FALSE ;<br />
			break;</p>
<p>		case -1:<br />
			node1 = root &#8211; >  right ;<br />
			if ( node1 &#8211; >  balfact  < = 0 )<br />
			{<br />
				printf ( "\nLeft rotation along %d.", root - >  data ) ;<br />
				root &#8211; >  right = node1 &#8211; >  left ;<br />
				node1 &#8211; >  left = root ;<br />
				if ( node1 &#8211; >  balfact == 0 )<br />
				{<br />
					root &#8211; >  balfact = -1 ;<br />
					node1 &#8211; >  balfact = 1 ;<br />
				   *h = FALSE ;<br />
				}<br />
				else<br />
				{<br />
					root &#8211; >  balfact = node1 &#8211; >  balfact = 0 ;<br />
				}<br />
				root = node1 ;<br />
			}<br />
			else<br />
			{<br />
				printf ( &#8220;\nDouble rotation, right along %d&#8221;, node1 &#8211; >  data );<br />
				node2 = node1 &#8211; >  left ;<br />
				node1 &#8211; >  left = node2 &#8211; >  right ;<br />
				node2 &#8211; >  right = node1 ;<br />
				printf ( &#8221; then left along %d.\n&#8221;, root &#8211; >  data );<br />
				root &#8211; >  right = node2 &#8211; >  left ;<br />
				node2 &#8211; >  left = root ;</p>
<p>				if ( node2 &#8211; >  balfact == -1 )<br />
					root &#8211; >  balfact = 1 ;<br />
				else<br />
					root &#8211; >  balfact = 0 ;<br />
				if ( node2 &#8211; >  balfact == 1 )<br />
					node1 &#8211; >  balfact = -1 ;<br />
				else<br />
					node1 &#8211; >  balfact = 0 ;<br />
				root = node2 ;<br />
				node2 &#8211; >  balfact = 0 ;<br />
			}<br />
	}<br />
	return ( root ) ;<br />
}</p>
<p>/* balances the tree, if left sub-tree is higher */<br />
struct AVLNode * balleft ( struct AVLNode *root, int *h )<br />
{<br />
	struct AVLNode *node1, *node2 ;</p>
<p>	switch ( root &#8211; >  balfact )<br />
	{<br />
		case -1:<br />
			root &#8211; >  balfact = 0 ;<br />
			break ;</p>
<p>		case 0:<br />
			root &#8211; >  balfact = 1 ;<br />
			*h = FALSE ;<br />
			break ;</p>
<p>		case 1:<br />
			node1 = root &#8211; >  left ;<br />
			if ( node1 &#8211; >  balfact  > = 0 )<br />
			{<br />
				printf ( &#8220;\nRight rotation along %d.&#8221;, root &#8211; >  data ) ;<br />
				root &#8211; >  left = node1 &#8211; >  right ;<br />
				node1 &#8211; >  right = root ;<br />
				if ( node1 &#8211; >  balfact == 0 )<br />
				{<br />
					root &#8211; >  balfact = 1 ;<br />
					node1 &#8211; >  balfact = -1 ;<br />
					*h = FALSE ;<br />
				}<br />
				else<br />
				{<br />
					root &#8211; >  balfact = node1 &#8211; >  balfact = 0 ;<br />
				}<br />
				root = node1 ;<br />
			}<br />
			else<br />
			{<br />
				printf ( &#8220;\nDouble rotation, left along %d&#8221;, node1 &#8211; >  data ) ;<br />
				node2 = node1 &#8211; >  right ;<br />
				node1 &#8211; >  right = node2 &#8211; >  left ;<br />
				node2 &#8211; >  left = node1 ;<br />
				printf ( &#8221; then right along %d.\n&#8221;, root &#8211; >  data ) ;<br />
				root &#8211; >  left = node2 &#8211; >  right ;<br />
				node2 &#8211; >  right = root ;</p>
<p>				if ( node2 &#8211; >  balfact == 1 )<br />
					root &#8211; >  balfact = -1 ;<br />
				else<br />
					root &#8211; >  balfact = 0 ;<br />
				if ( node2- >  balfact == -1 )<br />
					node1 &#8211; >  balfact = 1 ;<br />
				else<br />
					node1 &#8211; >  balfact = 0 ;<br />
				root = node2 ;<br />
				node2 &#8211; >  balfact = 0 ;<br />
			}<br />
	}<br />
	return ( root ) ;<br />
}</p>
<p>/* displays the tree in-order */<br />
void display ( struct AVLNode *root )<br />
{<br />
	if ( root != NULL )<br />
	{<br />
		display ( root &#8211; >  left ) ;<br />
		printf ( &#8220;%d\t&#8221;, root &#8211; >  data ) ;<br />
		display ( root &#8211; >  right ) ;<br />
	}<br />
}</p>
<p>/* deletes the tree */<br />
void deltree ( struct AVLNode *root )<br />
{<br />
	if ( root != NULL )<br />
	{<br />
		deltree ( root &#8211; >  left ) ;<br />
		deltree ( root &#8211; >  right ) ;<br />
	}<br />
	free ( root ) ;<br />
}</p>
]]></content:encoded>
			<wfw:commentRss>http://www.c-cplusplus.com/how-to-maintain-an-avl-tree/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>C Program to Perform Basic Matrix Operations</title>
		<link>http://www.c-cplusplus.com/how-to-perform-basic-matrix-operations-like-addition-multiplicaton-etc</link>
		<comments>http://www.c-cplusplus.com/how-to-perform-basic-matrix-operations-like-addition-multiplicaton-etc#comments</comments>
		<pubDate>Wed, 16 Sep 2009 11:04:27 +0000</pubDate>
		<dc:creator>Nanya</dc:creator>
				<category><![CDATA[Sample C Programs for Beginners]]></category>

		<guid isPermaLink="false">http://www.c-cplusplus.com/?p=222</guid>
		<description><![CDATA[Here&#8217;s a tested C program to perform basic matrix operations like Addition, Multiplication etc. #include < stdio.h > #include < conio.h > # define MAX 3 void create ( int [3][3] ) ; void display ( int [3][3] ) ; void matadd ( int [3][3], int [3][3], int [3][3] ) ; void matmul ( int [...]]]></description>
			<content:encoded><![CDATA[<p>Here&#8217;s a tested <a href="http://www.c-cplusplus.com">C program to perform basic matrix operations</a> like Addition, Multiplication etc.<br />
<br />
<span id="more-222"></span><br />
#include  < stdio.h > </p>
<p>#include  < conio.h > </p>
<p># define MAX 3</p>
<p>void create ( int [3][3] ) ;</p>
<p>void display ( int [3][3] ) ;</p>
<p>void matadd ( int [3][3], int [3][3], int [3][3] ) ;</p>
<p>void matmul ( int [3][3], int [3][3], int [3][3] ) ;</p>
<p>void transpose ( int [3][3], int [3][3] ) ;</p>
<p>void main( )</p>
<p>{<br />
	int mat1[3][3], mat2[3][3], mat3[3][3], mat4[3][3], mat5[3][3] ;</p>
<p>	clrscr( ) ;</p>
<p>	printf ( &#8220;\nEnter elements for first array: \n\n&#8221; ) ;</p>
<p>	create ( mat1 ) ;</p>
<p>	printf ( &#8220;\nEnter elements for second array: \n\n&#8221; ) ;</p>
<p>	create ( mat2 ) ;</p>
<p>	printf ( &#8220;\nFirst Array: \n&#8221; ) ;</p>
<p>	display ( mat1 ) ;</p>
<p>	printf ( &#8220;\nSecond Array:\n&#8221; ) ;</p>
<p>	display ( mat2 ) ;</p>
<p>	matadd ( mat1, mat2, mat3 ) ;</p>
<p>	printf ( &#8220;\nAfter Addition: \n&#8221; ) ;</p>
<p>	display ( mat3 ) ;</p>
<p>	matmul ( mat1, mat2, mat4 ) ;</p>
<p>	printf ( &#8220;\nAfter Multiplication: \n&#8221; ) ;</p>
<p>	display ( mat4 ) ;</p>
<p>	transpose ( mat1, mat5 ) ;</p>
<p>	printf ( &#8220;\nTranspose of first matrix: \n&#8221; ) ;</p>
<p>	display ( mat5 ) ;</p>
<p>	getch( ) ;<br />
}</p>
<p><strong>/* creates matrix mat */</strong></p>
<p>void create ( int mat[3][3] )</p>
<p>{</p>
<p>	int i, j ;</p>
<p>	for ( i = 0 ; i  <  MAX ; i++ )</p>
<p>	{</p>
<p>		for ( j = 0 ; j  <  MAX ; j++ )</p>
<p>		{<br />
			printf ( "Enter the element: " ) ;</p>
<p>			scanf ( "%d", &#038;mat[i][j] ) ;<br />
		}<br />
	}<br />
}</p>
<p><strong>/* displays the contents of matrix */</strong></p>
<p>void display ( int mat[3][3] )</p>
<p>{<br />
	int i, j ;</p>
<p>	for ( i = 0 ; i  <  MAX ; i++ )</p>
<p>	{<br />
		for ( j = 0 ; j  <  MAX ; j++ )</p>
<p>			printf ( "%d\t", mat[i][j] ) ;</p>
<p>		printf ( "\n" ) ;<br />
	}<br />
}</p>
<p><strong>/* adds two matrices m1 and m2 */</strong></p>
<p>void matadd ( int m1[3][3], int m2[3][3], int m3[3][3] )</p>
<p>{<br />
	int i, j ;</p>
<p>	for ( i = 0 ; i  <  MAX ; i++ )</p>
<p>	{<br />
		for ( j = 0 ; j  <  MAX ; j++ )</p>
<p>			m3[i][j] = m1[i][j] + m2[i][j] ;<br />
	}<br />
}</p>
<p><strong>/* multiplies two matrices m1 and m2 */</strong></p>
<p>void matmul ( int m1[3][3], int m2[3][3], int m3[3][3] )</p>
<p>{<br />
	int i, j, k ;</p>
<p>	for ( k = 0 ; k  <  MAX ; k++ )</p>
<p>	{<br />
		for ( i = 0 ; i  <  MAX ; i++ )</p>
<p>		{<br />
			m3[k][i] = 0 ;</p>
<p>			for ( j = 0 ; j  <  MAX ; j++ )</p>
<p>				m3[k][i] += m1[k][j] * m2[j][i] ;<br />
		}<br />
	}<br />
}<br />
<strong><br />
/* obtains transpose of matrix m1 */</strong></p>
<p>void transpose ( int m1[3][3], int m2[3][3] )</p>
<p>{<br />
	int i, j ;</p>
<p>	for ( i = 0 ; i  <  MAX ; i++ )</p>
<p>	{<br />
		for ( j = 0 ; j  <  MAX ; j++ )</p>
<p>			m2[i][j] = m1[j][i] ;<br />
	}<br />
}</p>
]]></content:encoded>
			<wfw:commentRss>http://www.c-cplusplus.com/how-to-perform-basic-matrix-operations-like-addition-multiplicaton-etc/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

