<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:blogger='http://schemas.google.com/blogger/2008' xmlns:georss='http://www.georss.org/georss' xmlns:gd="http://schemas.google.com/g/2005" xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-997202589550114592</id><updated>2026-08-16T15:39:17.549+05:30</updated><category term="C concepts"/><category term="C tutorial"/><category term="Programming in C"/><category term="Simple C programs"/><category term="C program"/><category term="Linked list"/><category term="queue"/><category term="C-free"/><category term="array"/><category term="data structure in C"/><category term="fibonacci series"/><category term="getting execution time of C program"/><category term="getting running time"/><category term="getting running time of C program"/><category term="time.h in C"/><category term="user-defined functions in C"/><category term="10000th term of fibonacci series"/><category term="2012th term of fibonacci series"/><category term="Armstrong number"/><category term="Armstrong number C"/><category term="Base conversion in C"/><category term="C program running time in windows"/><category term="C program to print linked list"/><category term="C program to reverse order of words"/><category term="C program to reverse words"/><category term="C programing made easy"/><category term="Decimal to any base"/><category term="Narcissistic number"/><category term="UDF"/><category term="arrange linked list"/><category term="binary recursion"/><category term="c prog to print triangles of abcd"/><category term="circular queue in C"/><category term="clock() function in C"/><category term="concept of heap"/><category term="concepts in C"/><category term="counting the nodes of a tree"/><category term="delete duplicate entries"/><category term="delete element in linked list"/><category term="dequeue"/><category term="enqueue"/><category term="fibonacci number"/><category term="fibonacci recursion"/><category term="fibonacci series unlimited"/><category term="finding whether a number is armstrong"/><category term="first in first out"/><category term="free C tutorial"/><category term="frequency of string"/><category term="function accepting variable number of arguments"/><category term="function returning structure"/><category term="function with varargs"/><category term="heap and array"/><category term="heap sample program"/><category term="heap sort"/><category term="how to reutrn multiple values"/><category term="implementation of heap"/><category term="in place insertion sort"/><category term="insert sort in C"/><category term="insertion sort in C"/><category term="insertion sort user defined"/><category term="integer array queue"/><category term="k&#39;th largest number"/><category term="n&#39;th root of a number"/><category term="n&#39;th root of a number C program"/><category term="nodes"/><category term="nth term of fibonacci series"/><category term="pop in C"/><category term="print linked list in reverse"/><category term="program to find nth fibonacci number"/><category term="push in C"/><category term="queue concept"/><category term="queue concept in C"/><category term="queue in array"/><category term="queue in linked list"/><category term="recursion in linked list"/><category term="returning a structure"/><category term="returning multiple values in C"/><category term="rev_word"/><category term="reverse order of words in a file"/><category term="reverse the list"/><category term="reverse words in a sentence"/><category term="reversing the character array"/><category term="running time in micro seconds"/><category term="running time in milli seconds"/><category term="running time in nano seconds"/><category term="size of a tree"/><category term="stack"/><category term="stack implementation on linked list"/><category term="stdarg.h in C"/><category term="strdup function in C"/><category term="strings in C"/><category term="strtok function in C"/><category term="timespec structure"/><category term="trees in C"/><category term="unlimited fibonacci c"/><category term="use of stack to reverse"/><category term="va_list in C"/><category term="vararg in C"/><category term="variable number of arguments"/><title type='text'>Getting started with C</title><subtitle type='html'>This is a learner&#39;s blog. Comments are always invited. If you found this useful, share it so that it can be useful to others too.</subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://programsimply.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/997202589550114592/posts/default?redirect=false'/><link rel='alternate' type='text/html' href='http://programsimply.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><link rel='next' type='application/atom+xml' href='http://www.blogger.com/feeds/997202589550114592/posts/default?start-index=26&amp;max-results=25&amp;redirect=false'/><author><name>Ranu Pratap Singh</name><uri>http://www.blogger.com/profile/15360265618857473433</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>124</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-997202589550114592.post-7113166111765298300</id><published>2014-01-21T18:09:00.000+05:30</published><updated>2014-01-21T18:09:00.982+05:30</updated><title type='text'>this is a blinking christmas tree. </title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
#include&amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;time.h&amp;gt;&lt;br /&gt;
void sleep(time_t delay)&lt;br /&gt;
&amp;nbsp;{&lt;br /&gt;
&amp;nbsp;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;time_t timer0,timer1;&lt;br /&gt;
&amp;nbsp;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;time(&amp;amp;timer0);&lt;br /&gt;
&amp;nbsp;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;do{&lt;br /&gt;
&amp;nbsp;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;time(&amp;amp;timer1);&lt;br /&gt;
&amp;nbsp;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;}while((timer1-timer0)&amp;lt;delay);&lt;br /&gt;
&amp;nbsp;}&lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;int i=5,j,k,s=0;&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;printf(&quot;your christmas tree is here\n&quot;);&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;for(k=0;k&amp;lt;5;k++)&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;printf(&quot; &quot;);&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;sleep(1);&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;printf(&quot;@@\n&quot;);&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;for(j=0;j&amp;lt;10;j++)&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;{&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;   &lt;/span&gt;for(k=i;k&amp;lt;10;k++)&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;    &lt;/span&gt;printf(&quot; &quot;);&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;   &lt;/span&gt;i++;&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;   &lt;/span&gt;if(i&amp;gt;10)&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;   &lt;/span&gt;{&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;   &lt;/span&gt;for(k=0;k&amp;lt;3;k++)&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;   &lt;/span&gt;printf(&quot; &quot;);&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;   &lt;/span&gt;}&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;   &lt;/span&gt;sleep(1);&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;   &lt;/span&gt;printf(&quot;*&quot;);&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;   &lt;/span&gt;for(k=0;k&amp;lt;2*s;k++)&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;   &lt;/span&gt;{if(2*s&amp;lt;10){&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;   &lt;/span&gt;sleep(1);&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;   &lt;/span&gt;printf(&quot;*&quot;);&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;   &lt;/span&gt;}&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;   &lt;/span&gt;}&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;   &lt;/span&gt;if(2*s&amp;gt;=10)&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;   &lt;/span&gt;{for(k=0;k&amp;lt;4;k++)&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;    &lt;/span&gt;{sleep(1);&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;    &lt;/span&gt;printf(&quot;*&quot;);}&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;   &lt;/span&gt;}&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;   &lt;/span&gt;sleep(1);&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;   &lt;/span&gt;printf(&quot;*&quot;);&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;   &lt;/span&gt;s++;&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;    &lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;printf(&quot;\n&quot;);&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;}&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;return 0;&lt;br /&gt;
}&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;   &lt;/span&gt;&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://programsimply.blogspot.com/feeds/7113166111765298300/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://programsimply.blogspot.com/2014/01/this-is-blinking-christmas-tree.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/997202589550114592/posts/default/7113166111765298300'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/997202589550114592/posts/default/7113166111765298300'/><link rel='alternate' type='text/html' href='http://programsimply.blogspot.com/2014/01/this-is-blinking-christmas-tree.html' title='this is a blinking christmas tree. '/><author><name>Unknown</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-997202589550114592.post-7164633070004421637</id><published>2014-01-21T18:06:00.002+05:30</published><updated>2014-01-21T18:06:35.785+05:30</updated><title type='text'>program that prints inorder traversal of tree and count the leaf nodes.</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
#include&amp;lt;stdio.h&amp;gt;&lt;br /&gt;
struct x{&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;int data;&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;struct x *right;&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;struct x *left;&lt;br /&gt;
};&lt;br /&gt;
typedef struct x node;&lt;br /&gt;
node* createTree(node * ,int );&lt;br /&gt;
node* createNode(int x);&lt;br /&gt;
void inorder(node** );&lt;br /&gt;
int countLeaf(node** );&lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;int i,x,j,n;&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;node *root=NULL;&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;printf(&quot;how many nodes you want to create\n&quot;);&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;scanf(&quot;%d&quot;,&amp;amp;n);&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;if(n&amp;gt;0)&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;{&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;printf(&quot;enter the data for nodes\n&quot;);&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;for(i=0;i&amp;lt;n;i++)&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;{&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;scanf(&quot;%d&quot;,&amp;amp;x);&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;root=createTree(root,x);&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;}&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;}&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;inorder(&amp;amp;root);&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;j=countLeaf(&amp;amp;root);&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;printf(&quot;\nthe leaf nodes are %d\n&quot;,j);&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;return 0;&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;&lt;br /&gt;
}&lt;br /&gt;
node* createNode(int x)&lt;br /&gt;
{&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;node* a;&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;a=(node*)malloc(sizeof(node));&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;a-&amp;gt;data=x;&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;a-&amp;gt;right=NULL;&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;a-&amp;gt;left=NULL;&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;return a;&lt;br /&gt;
}&lt;br /&gt;
node* createTree(node* root,int x)&lt;br /&gt;
{&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;if(root==NULL)&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;{&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;root=createNode(x);&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;return root;&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;}&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;else if(root-&amp;gt;data&amp;lt;x)&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;root-&amp;gt;right=createTree(root-&amp;gt;right,x);&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;else&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;root-&amp;gt;left=createTree(root-&amp;gt;left,x);&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;return root;&lt;br /&gt;
}&lt;br /&gt;
void inorder(node **root)&lt;br /&gt;
{&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;if(*root==NULL)&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;return;&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;else{&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;inorder(&amp;amp;((*root)-&amp;gt;left));&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;printf(&quot;%d-&amp;gt;&quot;,(*root)-&amp;gt;data);&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;inorder(&amp;amp;((*root)-&amp;gt;right));&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;}&lt;br /&gt;
}&lt;br /&gt;
int countLeaf(node **root)&lt;br /&gt;
{&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;static int count=0;&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;if(*root==NULL)&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;return count;&lt;br /&gt;
&amp;nbsp;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;else if((*root)-&amp;gt;left==NULL &amp;amp;&amp;amp; (*root)-&amp;gt;right==NULL)&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt; &lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;{&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt; count++;&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;return count;&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt; &lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;}&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;else{&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;countLeaf(&amp;amp;((*root)-&amp;gt;left));&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;countLeaf(&amp;amp;((*root)-&amp;gt;right));&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;}&lt;br /&gt;
}&lt;br /&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://programsimply.blogspot.com/feeds/7164633070004421637/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://programsimply.blogspot.com/2014/01/program-that-prints-inorder-traversal.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/997202589550114592/posts/default/7164633070004421637'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/997202589550114592/posts/default/7164633070004421637'/><link rel='alternate' type='text/html' href='http://programsimply.blogspot.com/2014/01/program-that-prints-inorder-traversal.html' title='program that prints inorder traversal of tree and count the leaf nodes.'/><author><name>Unknown</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-997202589550114592.post-5150961404848089394</id><published>2013-11-28T02:05:00.002+05:30</published><updated>2013-11-28T02:05:39.704+05:30</updated><title type='text'>conversion of an infix expression to a prefix expression using stack.</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
THE CODE:&lt;br /&gt;
#include&amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include&amp;lt;ctype.h&amp;gt;&lt;br /&gt;
#define max 50&lt;br /&gt;
struct x{&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;char data[max];&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;int top;&lt;br /&gt;
};&lt;br /&gt;
typedef struct x stack;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;&lt;br /&gt;
void infix_prefix(char [],char []);&lt;br /&gt;
void infix_postfix(char [],char []);&lt;br /&gt;
void push(stack *,char );&lt;br /&gt;
char pop(stack *);&lt;br /&gt;
int precedence(char );&lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;char infix[max],prefix[max];&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;printf(&quot;\nenter an infix expression\n&quot;);&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;gets(infix);&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;infix_prefix(infix,prefix);&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;printf(&quot;\n prefix:\n %s &quot;,prefix);&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;return 0;&lt;br /&gt;
}&lt;br /&gt;
void infix_prefix(char infix[],char prefix[])&lt;br /&gt;
{&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;int i,j;&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;char a[max],temp;&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;for(j=strlen(infix)-1,i=0;j&amp;gt;=0;j--,i++)&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;a[i]=infix[j];&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;a[i]=&#39;\0&#39;;&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;for(i=0;a[i]!=&#39;\0&#39;;i++)&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;{&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;if(a[i]==&#39;(&#39;)&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;a[i]=&#39;)&#39;;&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;else if(a[i]==&#39;)&#39;)&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;a[i]=&#39;(&#39;;&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;}&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;infix_postfix(a,prefix);&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;for(i=0,j=strlen(prefix)-1;i&amp;lt;j;i++,j--)&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;{&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;temp=prefix[j];&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;prefix[j]=prefix[i];&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;prefix[i]=temp;&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
void infix_postfix(char infix[],char postfix[])&lt;br /&gt;
{&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;int i,j;&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;stack s;&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;char x,token;&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;s.top=-1;&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;j=0;&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;for(i=0;infix[i]!=&#39;\0&#39;;i++)&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;{&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;token=infix[i];&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;if(isalnum(token)){&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;   &lt;/span&gt;postfix[j]=token;&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;   &lt;/span&gt;j++;&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;}&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;else if(token==&#39;(&#39;)&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;   &lt;/span&gt;push(&amp;amp;s,&#39;(&#39;);&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;else if(token==&#39;)&#39;)&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;{&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;   &lt;/span&gt;while((x=pop(&amp;amp;s))!=&#39;(&#39;){&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;   &lt;/span&gt;postfix[j]=x;&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;   &lt;/span&gt;j++;&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;   &lt;/span&gt;}&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;}&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;else&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;{&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;   &lt;/span&gt;while( s.top!=-1 &amp;amp;&amp;amp; precedence(token)&amp;lt;=precedence(s.data[s.top]))&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;   &lt;/span&gt;{&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;    &lt;/span&gt;x=pop(&amp;amp;s);&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;    &lt;/span&gt;postfix[j++]=x;&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;   &lt;/span&gt;}&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;   &lt;/span&gt;push(&amp;amp;s,token);&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;}&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;}&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;while(s.top!=-1)&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;{&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;x=pop(&amp;amp;s);&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;postfix[j]=x;&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;j++;&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;}&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;postfix[j]=&#39;\0&#39;;&lt;br /&gt;
}&lt;br /&gt;
int precedence(char x)&lt;br /&gt;
{&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;if(x==&#39;(&#39;)&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;return 0;&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;else if(x==&#39;+&#39;||x==&#39;-&#39;)&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;return 1;&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;else if(x==&#39;*&#39;||x==&#39;/&#39;||x==&#39;%&#39;)&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;return 2;&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;else&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;return 3;&lt;br /&gt;
}&lt;br /&gt;
void push(stack *s,char x)&lt;br /&gt;
{&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;s-&amp;gt;top=s-&amp;gt;top+1;&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;s-&amp;gt;data[s-&amp;gt;top]=x;&lt;br /&gt;
}&lt;br /&gt;
char pop(stack *s)&lt;br /&gt;
{&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;int x;&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;x=s-&amp;gt;data[s-&amp;gt;top];&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;s-&amp;gt;top=s-&amp;gt;top-1;&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;return x;&lt;br /&gt;
}&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;   &lt;/span&gt;&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://programsimply.blogspot.com/feeds/5150961404848089394/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://programsimply.blogspot.com/2013/11/conversion-of-infix-expression-to.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/997202589550114592/posts/default/5150961404848089394'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/997202589550114592/posts/default/5150961404848089394'/><link rel='alternate' type='text/html' href='http://programsimply.blogspot.com/2013/11/conversion-of-infix-expression-to.html' title='conversion of an infix expression to a prefix expression using stack.'/><author><name>Unknown</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-997202589550114592.post-7585104087941306766</id><published>2013-10-24T10:23:00.001+05:30</published><updated>2013-11-11T22:16:51.296+05:30</updated><title type='text'>sorting using quicksort method(divide and conqour method ).</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;b&gt;&lt;u&gt;THE NEED:&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&lt;u&gt;quicksort&lt;/u&gt;&lt;/b&gt; is the fastest sort in best average case of complexity O(n logn) in worst average case it is O(&lt;i style=&quot;background-color: white; font-family: sans-serif; font-size: 13px; line-height: 19.1875px;&quot;&gt;n&lt;/i&gt;&lt;sup style=&quot;background-color: white; font-family: sans-serif; line-height: 1em;&quot;&gt;2&lt;/sup&gt;).&lt;br /&gt;
the method is to choose&lt;b&gt;&lt;u&gt; pivot&lt;/u&gt;&lt;/b&gt;, which in my program is the index such that all number in the left of array[pivot],should be less than array[pivot]. And all number in right should be greater than array[pivot].&lt;br /&gt;
Rest will be done by recursion.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;&lt;u&gt;CODE:&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;
#include&amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#define max 10&lt;br /&gt;
void quicksort(int a[],int ,int );&lt;br /&gt;
int choosepivot(int a[],int ,int );&lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;int a[max],first=0,last,i,n;&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;printf(&quot;enter lenght of array you want to sort\n&quot;);&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;scanf(&quot;%d&quot;,&amp;amp;last);&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;printf(&quot;enter the array\n&quot;);&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;for(i=0;i&amp;lt;last;i++){&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;scanf(&quot;%d&quot;,&amp;amp;a[i]);&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;}&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;quicksort(a,first,last-1);&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;for(i=0;i&amp;lt;last;i++)&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;{&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;printf(&quot;%d &quot;,a[i]);&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;}&lt;br /&gt;
}&lt;br /&gt;
void quicksort(int a[max],int first,int last)&lt;br /&gt;
{&lt;br /&gt;
int pivot;&lt;br /&gt;
if(first&amp;lt;last)&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;{&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;pivot=choosepivot(a,first,last);&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;quicksort(a,first,pivot-1);&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;quicksort(a,pivot+1,last);&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;}&lt;br /&gt;
}&lt;br /&gt;
int choosepivot(int a[max],int first,int last)&lt;br /&gt;
{&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;int j,temp;&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;int pivot=first;&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;j=last;&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;while(j&amp;gt;pivot)&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;{&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;if(a[pivot]&amp;lt;a[pivot+1]){&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;   &lt;/span&gt;pivot++;&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;}&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;else if(a[j]&amp;lt;a[pivot]){&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;   &lt;/span&gt;temp=a[pivot];&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;   &lt;/span&gt;a[pivot]=a[j];&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;   &lt;/span&gt;a[j]=temp;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;}&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;else&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;   &lt;/span&gt;j--;&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;}&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;return pivot;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;   &lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;   &lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;   &lt;/span&gt;&lt;br /&gt;
&lt;div&gt;
&lt;b&gt;&lt;u&gt;APPROACH:&lt;/u&gt;&lt;/b&gt;&lt;/div&gt;
&lt;div&gt;
the function quicksort() sorts the array by dividing them on the basis of index(pivot) that&#39;s why this is called as divide and conquer approach.&lt;/div&gt;
&lt;div&gt;
the function choose pivot ,takes the pivot as first element of the array that is being called.And then check &amp;nbsp;weather the next to it is greater than the number in a[pivot],if not pivot(index) will move to the next number else it will rotate the next number with the last and moves pivot to the next index.&lt;/div&gt;
&lt;div&gt;
at last the function returns the pivot .&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://programsimply.blogspot.com/feeds/7585104087941306766/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://programsimply.blogspot.com/2013/10/sorting-using-quicksort-methoddivide.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/997202589550114592/posts/default/7585104087941306766'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/997202589550114592/posts/default/7585104087941306766'/><link rel='alternate' type='text/html' href='http://programsimply.blogspot.com/2013/10/sorting-using-quicksort-methoddivide.html' title='sorting using quicksort method(divide and conqour method ).'/><author><name>Unknown</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-997202589550114592.post-8751857137288462212</id><published>2013-09-05T00:25:00.000+05:30</published><updated>2013-09-05T00:25:57.021+05:30</updated><title type='text'></title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;h2 style=&quot;text-align: left;&quot;&gt;
Vector &amp;amp; Matrix Algebra&lt;/h2&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;#include&amp;lt;iostream.h&amp;gt; &amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;#include&amp;lt;conio.h&amp;gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;#include &amp;lt;cstdlib&amp;gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;#include &amp;lt;math.h&amp;gt; &amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;class vector &amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;&amp;nbsp;{ &amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;&amp;nbsp;public: &amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;&amp;nbsp;int x,y,z; &amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;&amp;nbsp;void read() &amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;&amp;nbsp;{ &amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;&amp;nbsp;cout&amp;lt;&amp;lt;&quot;\n\nEnter the magnitude of i : &quot;; &amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;&amp;nbsp;cin&amp;gt;&amp;gt;x; &amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;&amp;nbsp;cout&amp;lt;&amp;lt;&quot;\n\nEnter the magnitude of j : &quot;; &amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;&amp;nbsp;cin&amp;gt;&amp;gt;y; &amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;&amp;nbsp;cout&amp;lt;&amp;lt;&quot;\n\nEnter the magnitude of k : &quot;; &amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;&amp;nbsp;cin&amp;gt;&amp;gt;z; &amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;&amp;nbsp;} &amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;&amp;nbsp;vector operator -() &amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;&amp;nbsp;{ &amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;&amp;nbsp;x=-x; &amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;&amp;nbsp;y=-y; &amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;&amp;nbsp;z=-z; &amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;&amp;nbsp;} &amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;&amp;nbsp;vector operator +(vector b) &amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;&amp;nbsp;{ &amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;&amp;nbsp;vector c; &amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;&amp;nbsp;c.x=x+b.x; &amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;&amp;nbsp;c.y=y+b.y; &amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;&amp;nbsp;c.z=z+b.z; &amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;&amp;nbsp;return c; &amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;&amp;nbsp;} &amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;&amp;nbsp;vector operator *(vector b) &amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;&amp;nbsp;{ &amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;&amp;nbsp;int prod; &amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;&amp;nbsp;vector c; &amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;&amp;nbsp;c.x=x*b.x; &amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;&amp;nbsp;c.y=y*b.y; &amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;&amp;nbsp;c.z=z*b.z; &amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;&amp;nbsp;prod=c.x+c.y+c.z;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;&amp;nbsp;cout&amp;lt;&amp;lt;&quot;\nInner Product = &quot;; &amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;&amp;nbsp;cout&amp;lt;&amp;lt;prod;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;&amp;nbsp;vector d; &amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;&amp;nbsp;d.x=(y*b.z)-(z*b.y); &amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;&amp;nbsp;d.y=(x*b.z)-(z*b.x); &amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;&amp;nbsp;d.z=(x*b.y)-(y*b.x); &amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;&amp;nbsp;return d; &amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;&amp;nbsp;} &amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;&amp;nbsp;void display() &amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;&amp;nbsp;{ &amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;&amp;nbsp;cout&amp;lt;&amp;lt;x&amp;lt;&amp;lt;&quot;i&quot;&amp;lt;&amp;lt;&quot;+&quot;&amp;lt;&amp;lt;y&amp;lt;&amp;lt;&quot;j&quot;&amp;lt;&amp;lt;&quot;+&quot;&amp;lt;&amp;lt;z&amp;lt;&amp;lt;&quot;k&quot;; &amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;&amp;nbsp;} &amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;&amp;nbsp;};&amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;using namespace std;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;void vecmat_mult()&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;{&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;&amp;nbsp; &amp;nbsp; int mat[10][10];&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;&amp;nbsp; &amp;nbsp; int vec[3];&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;&amp;nbsp; &amp;nbsp; int out[10];&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;&amp;nbsp; &amp;nbsp; int c,d,m,n,i,j;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;cout&amp;lt;&amp;lt;&quot;Enter the number of rows and columns of matrix&quot;&amp;lt;&amp;lt;endl;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;&amp;nbsp; &lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;cin&amp;gt;&amp;gt;m;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;&amp;nbsp; &lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;cin&amp;gt;&amp;gt;n;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;&amp;nbsp; &lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;cout&amp;lt;&amp;lt;&quot;Enter the elements of matrix&quot;&amp;lt;&amp;lt;endl;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;&amp;nbsp; &lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;for ( c = 0 ; c &amp;lt; m ; c++ )&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;&amp;nbsp; &amp;nbsp; &lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;{&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt; &amp;nbsp;for ( d = 0 ; d &amp;lt; n ; d++ )&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt; &amp;nbsp;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;{&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;   &lt;/span&gt; &amp;nbsp;cout&amp;lt;&amp;lt;&quot;Enter elements mat&quot;&amp;lt;&amp;lt;c+1&amp;lt;&amp;lt;d+1&amp;lt;&amp;lt;&quot; &quot;;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;   &lt;/span&gt; &amp;nbsp;cin&amp;gt;&amp;gt;mat[c][d];&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt; &amp;nbsp;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;}&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;&amp;nbsp; &amp;nbsp; &lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;}&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;&amp;nbsp; &amp;nbsp; cout&amp;lt;&amp;lt;&quot;Enter Vector Component&quot;&amp;lt;&amp;lt;endl;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;&amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;&amp;nbsp;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;for(i=0;i&amp;lt;3;i++)&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt; &lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;{&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt; &lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;cin&amp;gt;&amp;gt;vec[i];&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt; &lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;}&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;&amp;nbsp;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;&amp;nbsp; &amp;nbsp; for (i = 0; i &amp;lt; m; i++ )&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;{&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;   &lt;/span&gt;out[i] = 0;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;for ( int j = 0; j &amp;lt; n; j++ )&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;   &lt;/span&gt; &amp;nbsp;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;{&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;    &lt;/span&gt; &amp;nbsp;out[i] += mat[i][j] * vec[j];&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt; &amp;nbsp;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;   &lt;/span&gt;}&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;}&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;cout&amp;lt;&amp;lt;&quot;Result Product is\n\n&quot;;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;&amp;nbsp; &amp;nbsp; for (i=0;i&amp;lt;m;i++)&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;{&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;   &lt;/span&gt;cout&amp;lt;&amp;lt;out[i]&amp;lt;&amp;lt;&quot; &amp;nbsp;&quot;&amp;lt;&amp;lt;endl;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;&amp;nbsp; &amp;nbsp; &lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;}&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;}&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;void matrixmult()&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;{&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;&amp;nbsp; &amp;nbsp; int m1[10][10],i,j,k,m2[10][10],mult[10][10],r1,c1,r2,c2;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;&amp;nbsp; &amp;nbsp; cout&amp;lt;&amp;lt;&quot;Enter number of rows and columns of first matrix\n&quot;;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;&amp;nbsp; &amp;nbsp; cin&amp;gt;&amp;gt;r1&amp;gt;&amp;gt;c1;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;&amp;nbsp; &amp;nbsp; cout&amp;lt;&amp;lt;&quot;Enter number of rows and columns of second matrix\n&quot;;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;&amp;nbsp; &amp;nbsp; cin&amp;gt;&amp;gt;r2&amp;gt;&amp;gt;c2;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;&amp;nbsp; &amp;nbsp; if(r2==c1)&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;&amp;nbsp; &amp;nbsp; {&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; cout&amp;lt;&amp;lt;&quot;Enter rows and columns of First matrix \n&quot;;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; for(i=0;i&amp;lt;r1;i++)&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; for(j=0;j&amp;lt;c1;j++)&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; cin&amp;gt;&amp;gt;m1[i][j];&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; cout&amp;lt;&amp;lt;&quot;First Matrix is :\n&quot;;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; for(i=0;i&amp;lt;r1;i++)&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; {&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; for(j=0;j&amp;lt;c1;j++)&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; cout&amp;lt;&amp;lt;m1[i][j]&amp;lt;&amp;lt;&quot;\t&quot;;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;cout&amp;lt;&amp;lt;&quot;\n&quot;;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; }&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; cout&amp;lt;&amp;lt;&quot;Enter rows and columns of Second matrix \n&quot;;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; for(i=0;i&amp;lt;r2;i++)&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; for(j=0;j&amp;lt;c2;j++)&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; cin&amp;gt;&amp;gt;m2[i][j];&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; cout&amp;lt;&amp;lt;&quot;Second Matrix is:\n&quot;;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; for(i=0;i&amp;lt;r2;i++)&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; {&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; for(j=0;j&amp;lt;c2;j++)&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; cout&amp;lt;&amp;lt;m2[i][j]&amp;lt;&amp;lt;&quot;\t&quot;;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; cout&amp;lt;&amp;lt;&quot;\n&quot;;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; }&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; cout&amp;lt;&amp;lt;&quot;Multiplication of the Matrices:\n&quot;;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; for(i=0;i&amp;lt;r1;i++)&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; {&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; for(j=0;j&amp;lt;c2;j++)&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; {&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; mult[i][j]=0;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; for(k=0;k&amp;lt;r1;k++)&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; mult[i][j]+=m1[i][k]*m2[k][j];&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; cout&amp;lt;&amp;lt;mult[i][j]&amp;lt;&amp;lt;&quot;\t&quot;;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; }&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; cout&amp;lt;&amp;lt;&quot;\n&quot;;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; }&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;&amp;nbsp; &amp;nbsp; }&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;&amp;nbsp; &amp;nbsp; else&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;&amp;nbsp; &amp;nbsp; {&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; cout&amp;lt;&amp;lt;&quot;Matrix multiplication cannot be done&quot;;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;&amp;nbsp; &amp;nbsp; }&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;}&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;void matrixtranspose()&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;{&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;int a[10][10], trans[10][10], r, c, i, j;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;cout&amp;lt;&amp;lt;&quot;Enter rows and column of matrix: &quot;&amp;lt;&amp;lt;endl;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;cin&amp;gt;&amp;gt;r&amp;gt;&amp;gt;c;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;cout&amp;lt;&amp;lt;&quot;\nEnter elements of matrix:\n&quot;;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;for(i=0; i&amp;lt;r; ++i)&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;{&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt; &amp;nbsp;for(j=0; j&amp;lt;c; ++j)&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt; &amp;nbsp;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;{&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;   &lt;/span&gt; &amp;nbsp;cout&amp;lt;&amp;lt;&quot;Enter elements a&quot;&amp;lt;&amp;lt;i+1&amp;lt;&amp;lt;j+1&amp;lt;&amp;lt;&quot; &quot;;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;   &lt;/span&gt; &amp;nbsp;cin&amp;gt;&amp;gt;a[i][j];&amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;   &lt;/span&gt;}&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt; }&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt; &amp;nbsp;/*Displaying the matrix a[][] */&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt; &amp;nbsp;cout&amp;lt;&amp;lt;&quot;\nEntered Matrix: \n&quot;;&amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt; &amp;nbsp;for(i=0;i&amp;lt;r; ++i)&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt; &amp;nbsp;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;{&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt; &amp;nbsp;for(j=0; j&amp;lt;c; ++j)&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;{&amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;   &lt;/span&gt; &amp;nbsp;cout&amp;lt;&amp;lt;a[i][j]&amp;lt;&amp;lt;&quot;\t&quot;;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;   &lt;/span&gt; &amp;nbsp;if(j==c-1)&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;   &lt;/span&gt; &amp;nbsp;cout&amp;lt;&amp;lt;&quot;\n\n&quot;;&amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt; &amp;nbsp; &amp;nbsp; &lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;}&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt; &amp;nbsp;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;}&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt; &amp;nbsp;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;/* Finding transpose of matrix a[]&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;[] and storing it in array trans[][]. */&amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;for(i=0; i&amp;lt;r; ++i)&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;{&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt; &amp;nbsp;for(j=0;j&amp;lt;c; ++j)&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt; &amp;nbsp; {&amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt; &amp;nbsp; &amp;nbsp; trans[j][i]=a[i][j];&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;&amp;nbsp; &amp;nbsp; }&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;/* Displaying the transpose,i.e, Displaying array trans[][].*/&amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;cout&amp;lt;&amp;lt;&quot;\nTranspose of Matrix:\n&quot;;&amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;for(i=0; i&amp;lt;c; ++i)&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;{&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt; &amp;nbsp;for(j=0; j&amp;lt;r; ++j)&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt; &amp;nbsp;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;{&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt; &amp;nbsp;printf(&quot;%d &quot;,trans[i][j]); if(j==r-1) printf(&quot;\n\n&quot;);&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;&amp;nbsp; &lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;}&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;}&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;}&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;int main() &amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;&amp;nbsp;{ &amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;&amp;nbsp;vector v1,v2,v3; &amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;&amp;nbsp;int choice,cont; &amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;&amp;nbsp;do &amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;&amp;nbsp;{ &amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;&amp;nbsp;cout&amp;lt;&amp;lt;&quot;\n\tVECTORS &amp;amp; MaTRIX Algebra\n\n1.Vector Addition\n\n2.Vector Product\n\n3.Product Matrix-Vector\n\n4.Product Matrix-Matrix\n\n5.Transpose Matrix\n\n6.Exit&quot;; &amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;&amp;nbsp;cout&amp;lt;&amp;lt;&quot;\n\nEnter your choice : &quot;; &amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;&amp;nbsp;cin&amp;gt;&amp;gt;choice; &amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;&amp;nbsp;switch(choice) &amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;&amp;nbsp;{ &amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;&amp;nbsp;case 1 : cout&amp;lt;&amp;lt;&quot;\n\nEnter the First Vector : &quot;; &amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; v1.read(); &amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; cout&amp;lt;&amp;lt;&quot;\n\nEnter the Second Vector : &quot;; &amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; v2.read(); &amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; v3=v1+v2; &amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; cout&amp;lt;&amp;lt;&quot;\n\nThe Sum of Vectors : &quot;; &amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; v3.display(); &amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; break; &amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;&amp;nbsp;case 2 : cout&amp;lt;&amp;lt;&quot;\n\nEnter the First Vector : &quot;; &amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; v1.read(); &amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; cout&amp;lt;&amp;lt;&quot;\n\nEnter the Second Vector : &quot;; &amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; v2.read(); &amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; v3=v1*v2; &amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; cout&amp;lt;&amp;lt;&quot;\n\nCross Product = &quot;; &amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; v3.display();&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; break; &amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;&amp;nbsp;case 3 : vecmat_mult();&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;&amp;nbsp;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt; &amp;nbsp;break;&amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;&amp;nbsp;case 4 : matrixmult();&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp;break;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;&amp;nbsp;case 5 : matrixtranspose();&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp;break;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;&amp;nbsp;case 6 : exit(0);&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;&amp;nbsp;default : cout&amp;lt;&amp;lt;&quot;\n\nInvalid Choice&quot;; &amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;&amp;nbsp;} &amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;&amp;nbsp;cout&amp;lt;&amp;lt;&quot;\n\n\nDo you want to continue?(1-YES,0-NO)\n&quot;; &amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;&amp;nbsp;cin&amp;gt;&amp;gt;cont; &amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;&amp;nbsp;}while(cont==1); &amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;&amp;nbsp;return 0;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Helvetica Neue, Arial, Helvetica, sans-serif;&quot;&gt;&amp;nbsp;}&lt;/span&gt;&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://programsimply.blogspot.com/feeds/8751857137288462212/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://programsimply.blogspot.com/2013/09/vector-matrix-algebra-include-include.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/997202589550114592/posts/default/8751857137288462212'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/997202589550114592/posts/default/8751857137288462212'/><link rel='alternate' type='text/html' href='http://programsimply.blogspot.com/2013/09/vector-matrix-algebra-include-include.html' title=''/><author><name>Unknown</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-997202589550114592.post-3023967928284670858</id><published>2012-01-10T23:05:00.002+05:30</published><updated>2012-01-10T23:13:33.294+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="function returning structure"/><category scheme="http://www.blogger.com/atom/ns#" term="how to reutrn multiple values"/><category scheme="http://www.blogger.com/atom/ns#" term="returning a structure"/><category scheme="http://www.blogger.com/atom/ns#" term="returning multiple values in C"/><title type='text'>Returning a structure in C</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot;&gt;&lt;div style=&quot;font-family: Arial,Helvetica,sans-serif;&quot;&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;u&gt;&lt;b&gt;The need:&lt;/b&gt;&lt;/u&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;font-family: Arial,Helvetica,sans-serif;&quot;&gt;&amp;nbsp;      &amp;nbsp;&amp;nbsp; This program is basically solution of a problem I found on internet. Problem statement is:&lt;br /&gt;
There is a purse filled with some coins of gold, silver ans copper. A person draws coins one by one and notes down the type of coin each time. He writes g for gold, s for silver and c for copper coin. Thus a character string is formed. You have to write a function countCoins() which takes that string as parameter and returns count each type of coin. As we know that a function can return only single value, we have 2 options left to return all three counts with one function:&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; 1. store 3 answers in some contiguous location and return a pointer to that location.&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; 2. If possible, club all three variables to make a single variable and return that single variable. Yes it is possible, we can make a structure variable containing all three counts and return that structure variable.&lt;br /&gt;
&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Arial,Helvetica,sans-serif;&quot;&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;u&gt;&lt;b&gt;The code:&lt;/b&gt;&lt;/u&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;----------------------------------------------------------&lt;/div&gt;&lt;pre class=&quot;brush: c&quot;&gt;#include&amp;lt;stdio.h&amp;gt;
typedef struct
{
&amp;nbsp;&amp;nbsp; &amp;nbsp;int silver;
&amp;nbsp;&amp;nbsp; &amp;nbsp;int copper;
&amp;nbsp;&amp;nbsp; &amp;nbsp;int gold;
}CoinPurse;
CoinPurse countCoin(char *);

int main()
{
&amp;nbsp;&amp;nbsp; &amp;nbsp;int i;
&amp;nbsp;&amp;nbsp; &amp;nbsp;CoinPurse a,*b;
&amp;nbsp;&amp;nbsp; &amp;nbsp;char *coins=&quot;gscgcc&quot;;
&amp;nbsp;&amp;nbsp; &amp;nbsp;a=countCoin(coins);
&amp;nbsp;&amp;nbsp; &amp;nbsp;printf(&quot;Coppper coins are %d\n&quot;,a.copper);
&amp;nbsp;&amp;nbsp; &amp;nbsp;printf(&quot;Gold coins are %d\n&quot;,a.gold);
&amp;nbsp;&amp;nbsp; &amp;nbsp;printf(&quot;Silver coins are %d\n&quot;,a.silver);
&amp;nbsp;&amp;nbsp; &amp;nbsp;return 0;
}

CoinPurse countCoin(char *coins)
{
&amp;nbsp;&amp;nbsp; &amp;nbsp;CoinPurse x;
&amp;nbsp;&amp;nbsp; &amp;nbsp;x.copper=0;
&amp;nbsp;&amp;nbsp; &amp;nbsp;x.gold=0;
&amp;nbsp;&amp;nbsp; &amp;nbsp;x.silver=0;
&amp;nbsp;&amp;nbsp; &amp;nbsp;while(*coins)
&amp;nbsp;&amp;nbsp; &amp;nbsp;{
&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;switch(*coins)
&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;{
&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;case &#39;c&#39;:&amp;nbsp;&amp;nbsp; &amp;nbsp;x.copper++;
&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;break;
&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;case &#39;g&#39;:&amp;nbsp;&amp;nbsp; &amp;nbsp;x.gold++;
&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;break;
&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;case &#39;s&#39;:&amp;nbsp;&amp;nbsp; &amp;nbsp;x.silver++;
&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;break;
&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;default:&amp;nbsp;&amp;nbsp; &amp;nbsp;break;
&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;}
&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;coins++;
&amp;nbsp;&amp;nbsp; &amp;nbsp;}
&amp;nbsp;&amp;nbsp; &amp;nbsp;return x;
}&lt;/pre&gt;&lt;div style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;----------------------------------------------------------&lt;/div&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;u&gt;&lt;b&gt;Approach:&lt;/b&gt;&lt;/u&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;&lt;span style=&quot;font-family: Arial,Helvetica,sans-serif;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; The approach is very simple. The structure CoinPurse contains counter for each type of coin. the function countCoin() takes a character string , examines each character and based on that increments the correct counter. after the string is finished, the structure containing the counts is returned by the function countCoin(). In this program i have given the character string *coins as &lt;/span&gt;&lt;/span&gt; &lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;&quot;gscgcc&quot;&lt;/span&gt;&lt;span style=&quot;font-size: small;&quot;&gt;&lt;span style=&quot;font-family: Arial,Helvetica,sans-serif;&quot;&gt; soit is clear that there are 2 gold, 1 silver and 3 copper coins which can be verified by the program output.&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programsimply.blogspot.com/feeds/3023967928284670858/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://programsimply.blogspot.com/2012/01/returning-structure-in-c.html#comment-form' title='3 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/997202589550114592/posts/default/3023967928284670858'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/997202589550114592/posts/default/3023967928284670858'/><link rel='alternate' type='text/html' href='http://programsimply.blogspot.com/2012/01/returning-structure-in-c.html' title='Returning a structure in C'/><author><name>Ranu Pratap Singh</name><uri>http://www.blogger.com/profile/15360265618857473433</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>3</thr:total></entry><entry><id>tag:blogger.com,1999:blog-997202589550114592.post-3002817023146280914</id><published>2012-01-08T01:45:00.003+05:30</published><updated>2012-01-08T11:50:49.184+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="counting the nodes of a tree"/><category scheme="http://www.blogger.com/atom/ns#" term="size of a tree"/><title type='text'>Counting total number of nodes in a tree</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;&lt;div style=&quot;font-family: Arial,Helvetica,sans-serif;&quot;&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;u&gt;&lt;b&gt;The need:&lt;/b&gt;&lt;/u&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;font-family: Arial,Helvetica,sans-serif;&quot;&gt;&amp;nbsp;     &amp;nbsp;&amp;nbsp; This program counts the total number of nodes in a binary tree and prints that for the user. This count is also known as tree size.&lt;/div&gt;&lt;div style=&quot;font-family: Arial,Helvetica,sans-serif;&quot;&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;u&gt;&lt;b&gt;The code:&lt;/b&gt;&lt;/u&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;----------------------------------------------------------&lt;/div&gt;&lt;pre class=&quot;brush: c&quot;&gt;#include&amp;lt;stdio.h&amp;gt;
#include&amp;lt;stdlib.h&amp;gt;
struct tree_node
{
 int data;
 struct tree_node *left,*right,*parent;
};
typedef struct tree_node node;
node * new_node(int x);
node * insert_node(node *root, int x);
int count_nodes(node *root);

int main()
{
 node *root=NULL;
 int i,x,max;
 printf(&quot;how many numbers do you want to enter in tree?\n&quot;);
 scanf(&quot;%d&quot;,&amp;amp;max);
 printf(&quot;Enter %d numbers \n&quot;,max);
 for(i=0;i&amp;lt;max;i++)
 {
  scanf(&quot;%d&quot;,&amp;amp;x);
  root = insert_node(root, x);
 }
 printf(&quot;all numbers inserted into the tree\t press enter to count&quot;);
 fflush(stdin);
 getchar();
 printf(&quot;\nThere are total %d nodes in this tree\n&quot;,count_nodes(root));
    return 0;
}

node * insert_node(node *root, int x)
{
 if(!root)
 {
  root = new_node(x);
  return root;
 }
 if(root-&amp;gt;data &amp;gt; x)
  root-&amp;gt;left = insert_node(root-&amp;gt;left,x);
 else
  root-&amp;gt;right = insert_node(root-&amp;gt;right,x);
 return root;
}

int count_nodes(node *root)
{
 if(!root)
 return 0;
 else
 return(count_nodes(root-&amp;gt;left) + 1 + count_nodes(root-&amp;gt;right));
}

node * new_node(int x)
{
 node *furnished;
 furnished = (node*)malloc(sizeof(node));
 furnished-&amp;gt;data=x;
 furnished-&amp;gt;left=NULL;
 furnished-&amp;gt;right=NULL;
 furnished-&amp;gt;parent=NULL;
 return furnished;
}&lt;/pre&gt;&lt;div style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;----------------------------------------------------------&lt;/div&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;span style=&quot;font-family: Arial,Helvetica,sans-serif;&quot;&gt;&lt;u&gt;&lt;b&gt;Approach:&lt;/b&gt;&lt;/u&gt;&lt;/span&gt;&lt;/span&gt; &lt;span style=&quot;font-size: x-small;&quot;&gt;&lt;span style=&quot;font-family: Arial,Helvetica,sans-serif;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style=&quot;font-size: small;&quot;&gt;The approach is simple. We have to go by recursion. if the root is null(means there is no tree) then number of nodes are 0. Otherwise if root is not null(the tree exists), the total number of nodes will be&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;&lt;span style=&quot;font-family: Arial,Helvetica,sans-serif;&quot;&gt;&lt;span style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; (number of nodes in left subtree + number of nodes in right subtree + 1 )&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;&lt;span style=&quot;font-family: Arial,Helvetica,sans-serif;&quot;&gt;&lt;span style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;&lt;span style=&quot;font-family: Arial,Helvetica,sans-serif;&quot;&gt;&lt;span style=&quot;font-size: small;&quot;&gt;Here recursive function count_nodes does the job very well.&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programsimply.blogspot.com/feeds/3002817023146280914/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://programsimply.blogspot.com/2012/01/counting-total-number-of-nodes-in-tree.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/997202589550114592/posts/default/3002817023146280914'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/997202589550114592/posts/default/3002817023146280914'/><link rel='alternate' type='text/html' href='http://programsimply.blogspot.com/2012/01/counting-total-number-of-nodes-in-tree.html' title='Counting total number of nodes in a tree'/><author><name>Ranu Pratap Singh</name><uri>http://www.blogger.com/profile/15360265618857473433</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-997202589550114592.post-3946778418940188843</id><published>2011-12-12T18:06:00.004+05:30</published><updated>2011-12-14T09:09:26.051+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="reverse order of words in a file"/><category scheme="http://www.blogger.com/atom/ns#" term="stack implementation on linked list"/><category scheme="http://www.blogger.com/atom/ns#" term="strdup function in C"/><category scheme="http://www.blogger.com/atom/ns#" term="strtok function in C"/><category scheme="http://www.blogger.com/atom/ns#" term="use of stack to reverse"/><title type='text'>Reversing order of words in a sentence using stack</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;&lt;div style=&quot;font-family: Arial,Helvetica,sans-serif;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;&lt;u&gt;&lt;b&gt;The need:&lt;/b&gt;&lt;/u&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;font-family: Arial,Helvetica,sans-serif;&quot;&gt;&amp;nbsp;     &amp;nbsp;&amp;nbsp; This is another way ( a layman&#39;s approach) to write &lt;a href=&quot;http://programsimply.blogspot.com/2011/12/program-to-reverse-order-of-words-in.html&quot; target=&quot;_blank&quot;&gt;previous program&lt;/a&gt; about this topic. I have written the approach there too.&lt;/div&gt;&lt;div style=&quot;font-family: Arial,Helvetica,sans-serif;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;&lt;u&gt;&lt;b&gt;The code:&lt;/b&gt;&lt;/u&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;----------------------------------------------------------&lt;/div&gt;&lt;pre class=&quot;brush: c&quot;&gt;#include&amp;lt;stdio.h&amp;gt;
#include&amp;lt;string.h&amp;gt;
#include&amp;lt;stdlib.h&amp;gt;

struct linked_list
{
&amp;nbsp;&amp;nbsp;&amp;nbsp; char *chr;
&amp;nbsp;&amp;nbsp;&amp;nbsp; struct linked_list *next;
};
typedef struct linked_list node;
void push(node **p,char *x);
char *pop(node **p);

int main()
{
&amp;nbsp;&amp;nbsp;&amp;nbsp; char a[50],*temp,*dup;
&amp;nbsp;&amp;nbsp;&amp;nbsp; strcpy(a,&quot;I dont know why useless programs are asked&quot;);
&amp;nbsp;&amp;nbsp;&amp;nbsp; node * head=NULL;
&amp;nbsp;&amp;nbsp;&amp;nbsp; temp=strtok(a,&quot; &quot;);
&amp;nbsp;&amp;nbsp;&amp;nbsp; while(temp != NULL)
&amp;nbsp;&amp;nbsp;&amp;nbsp; {
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; dup=strdup(temp);
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; temp=strtok(NULL,&quot; &quot;);
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; push(&amp;amp;head,dup);
&amp;nbsp;&amp;nbsp;&amp;nbsp; }
&amp;nbsp;&amp;nbsp;&amp;nbsp; strcpy(a,&quot;&quot;);
&amp;nbsp;&amp;nbsp;&amp;nbsp; while((temp=pop(&amp;amp;head))!=NULL)
&amp;nbsp;&amp;nbsp;&amp;nbsp; {
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; strcat(a,temp);
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; strcat(a,&quot; &quot;);
&amp;nbsp;&amp;nbsp;&amp;nbsp; }
&amp;nbsp;&amp;nbsp;&amp;nbsp; a[strlen(a)-1]=&#39;\0&#39;;
&amp;nbsp;&amp;nbsp;&amp;nbsp; printf(&quot;%s\n&quot;,a);
&amp;nbsp;&amp;nbsp;&amp;nbsp; return 0;
}

void push(node **p,char *x)
{
&amp;nbsp;&amp;nbsp;&amp;nbsp; node *new_node;
&amp;nbsp;&amp;nbsp;&amp;nbsp; new_node=(node *)malloc(sizeof(node));
&amp;nbsp;&amp;nbsp;&amp;nbsp; new_node-&amp;gt;chr=x;
&amp;nbsp;&amp;nbsp;&amp;nbsp; new_node-&amp;gt;next=*p;
&amp;nbsp;&amp;nbsp;&amp;nbsp; *p=new_node;
}

char *pop(node **p)
{
&amp;nbsp;&amp;nbsp;&amp;nbsp; char *temp=NULL;
&amp;nbsp;&amp;nbsp;&amp;nbsp; node *tmp;
&amp;nbsp;&amp;nbsp;&amp;nbsp; if(*p==NULL)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return (NULL);
&amp;nbsp;&amp;nbsp;&amp;nbsp; else
&amp;nbsp;&amp;nbsp;&amp;nbsp; {
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; temp=(*p)-&amp;gt;chr;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; tmp=*p;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; (*p)=(*p)-&amp;gt;next;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; free(tmp);
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return (temp);
&amp;nbsp;&amp;nbsp;&amp;nbsp; }
}&lt;/pre&gt;&lt;div style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;----------------------------------------------------------&lt;/div&gt;&lt;span style=&quot;font-family: Arial,Helvetica,sans-serif; font-size: medium;&quot;&gt;&lt;u&gt;&lt;b&gt;Approach:&lt;/b&gt;&lt;/u&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Arial,Helvetica,sans-serif;&quot;&gt; &lt;/span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; In that case I have made use of my stack concepts and using   almost no brain I have got it done. Have a look how simple is   this. Steps are:&lt;br /&gt;
1. read word by word (strtok has been used to read words delimiter is space)&lt;br /&gt;
2. keep on pushing all words on a stack till last word.&lt;br /&gt;
3. Now pop each word and print. :-)&lt;br /&gt;
&lt;u style=&quot;color: #990000;&quot;&gt;&lt;b&gt;&lt;span style=&quot;font-family: Arial,Helvetica,sans-serif; font-size: medium;&quot;&gt;Remarks:&lt;/span&gt;&lt;/b&gt;&lt;/u&gt; &lt;br /&gt;
1. This is not the most efficient method to do this. For more efficient method have a look at &lt;a href=&quot;http://programsimply.blogspot.com/2011/12/program-to-reverse-order-of-words-in.html&quot; target=&quot;_blank&quot;&gt;previous program&lt;/a&gt; about this topic.&lt;br /&gt;
2. This program can also be used to reverse the order of words in a file. In that case steps are:&lt;br /&gt;
&lt;ol ,=&quot;&quot; style=&quot;text-align: left;&quot; type=&quot;a&quot;&gt;&lt;li&gt;read word by word (using fscanf(FilePointer,&quot;%s&quot;,&amp;amp;TempString)).&lt;/li&gt;
&lt;li&gt;keep on pushing all words on a stack till last word.&lt;/li&gt;
&lt;li&gt;Now pop each word and print to another file. :-)&lt;/li&gt;
&lt;/ol&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programsimply.blogspot.com/feeds/3946778418940188843/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://programsimply.blogspot.com/2011/12/reversing-order-of-words-in-sentence.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/997202589550114592/posts/default/3946778418940188843'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/997202589550114592/posts/default/3946778418940188843'/><link rel='alternate' type='text/html' href='http://programsimply.blogspot.com/2011/12/reversing-order-of-words-in-sentence.html' title='Reversing order of words in a sentence using stack'/><author><name>Ranu Pratap Singh</name><uri>http://www.blogger.com/profile/15360265618857473433</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-997202589550114592.post-5099225089913958260</id><published>2011-12-11T00:13:00.003+05:30</published><updated>2011-12-13T18:53:03.853+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="n&#39;th root of a number"/><category scheme="http://www.blogger.com/atom/ns#" term="n&#39;th root of a number C program"/><title type='text'>Finding k&#39;th root of a number</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;&lt;u&gt;&lt;b&gt;&lt;span style=&quot;font-family: Arial,Helvetica,sans-serif; font-size: large;&quot;&gt;The need:&lt;/span&gt;&lt;/b&gt;&lt;/u&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Arial,Helvetica,sans-serif; font-size: small;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; The need is nothing but to find k&#39;th root of a number.&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: large;&quot;&gt;&lt;u&gt;&lt;span style=&quot;font-family: Arial,Helvetica,sans-serif;&quot;&gt;&lt;b&gt;The code:&lt;/b&gt; &lt;/span&gt;&lt;/u&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;----------------------------------------------------------------&lt;/span&gt;&lt;br /&gt;
&lt;pre class=&quot;brush: c&quot;&gt;#include&amp;lt;stdio.h&amp;gt;
#include&amp;lt;math.h&amp;gt;
main()
{
&amp;nbsp;&amp;nbsp;&amp;nbsp; printf (&quot;*********a program to find k&#39;th root of x ....\n\n&quot;);
&amp;nbsp;&amp;nbsp;&amp;nbsp; char c;
&amp;nbsp;&amp;nbsp;&amp;nbsp; float i,j,k,m;
&amp;nbsp;&amp;nbsp;&amp;nbsp; do
&amp;nbsp;&amp;nbsp;&amp;nbsp; {
&amp;nbsp;&amp;nbsp;&amp;nbsp; printf (&quot;To get k&#39;th root of x ....\n&quot;);
&amp;nbsp;&amp;nbsp;&amp;nbsp; printf (&quot;type the values .....\n\n&quot;);
&amp;nbsp;&amp;nbsp;&amp;nbsp; printf (&quot;x= &quot;); 
&amp;nbsp;&amp;nbsp;&amp;nbsp; scanf(&quot;%f&quot;,&amp;amp;i);
&amp;nbsp;&amp;nbsp;&amp;nbsp; printf (&quot;k=&amp;nbsp; &quot;);
&amp;nbsp;&amp;nbsp;&amp;nbsp; scanf(&quot;%f&quot;,&amp;amp;k);
&amp;nbsp;&amp;nbsp;&amp;nbsp; j=pow(i,(1/k));
&amp;nbsp;&amp;nbsp;&amp;nbsp; printf(&quot;\n %dth root of%f = %f\n\n\n&quot;,(int)k,i,j);
&amp;nbsp;&amp;nbsp;&amp;nbsp; printf(&quot;do you want to try more? y/n \t&quot;);
&amp;nbsp;&amp;nbsp;&amp;nbsp; fflush(stdin);
&amp;nbsp;&amp;nbsp;&amp;nbsp; c=getchar();
&amp;nbsp;&amp;nbsp;&amp;nbsp; }while(c==&#39;y&#39;);
}&lt;/pre&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;----------------------------------------------------------------&amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;&lt;u&gt;&lt;b&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;span style=&quot;font-family: Arial,Helvetica,sans-serif;&quot;&gt;The Approach:&lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;/u&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;&lt;span style=&quot;font-size: small;&quot;&gt;&lt;span style=&quot;font-family: Arial,Helvetica,sans-serif;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; The approach is very simple. Lets apply simple math&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;&lt;span style=&quot;font-size: small;&quot;&gt;&lt;span style=&quot;font-family: Arial,Helvetica,sans-serif;&quot;&gt; If y^k = x&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;&lt;span style=&quot;font-size: small;&quot;&gt;&lt;span style=&quot;font-family: Arial,Helvetica,sans-serif;&quot;&gt;then y =&amp;nbsp; x^(1/k)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;&lt;span style=&quot;font-size: small;&quot;&gt;&lt;span style=&quot;font-family: Arial,Helvetica,sans-serif;&quot;&gt;and the problem is solved because we have built-in pow() function for a power. &lt;/span&gt;&lt;/span&gt;&lt;u&gt;&lt;b&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;span style=&quot;font-family: Arial,Helvetica,sans-serif;&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;/u&gt;&lt;/span&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programsimply.blogspot.com/feeds/5099225089913958260/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://programsimply.blogspot.com/2011/12/finding-kth-root-of-number.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/997202589550114592/posts/default/5099225089913958260'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/997202589550114592/posts/default/5099225089913958260'/><link rel='alternate' type='text/html' href='http://programsimply.blogspot.com/2011/12/finding-kth-root-of-number.html' title='Finding k&#39;th root of a number'/><author><name>Ranu Pratap Singh</name><uri>http://www.blogger.com/profile/15360265618857473433</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-997202589550114592.post-1615689419850698045</id><published>2011-12-10T15:11:00.005+05:30</published><updated>2011-12-12T09:38:38.446+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="function accepting variable number of arguments"/><category scheme="http://www.blogger.com/atom/ns#" term="function with varargs"/><category scheme="http://www.blogger.com/atom/ns#" term="stdarg.h in C"/><category scheme="http://www.blogger.com/atom/ns#" term="va_list in C"/><category scheme="http://www.blogger.com/atom/ns#" term="vararg in C"/><category scheme="http://www.blogger.com/atom/ns#" term="variable number of arguments"/><title type='text'>Variable number of arguments to a function</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;&lt;div style=&quot;font-family: Arial,Helvetica,sans-serif;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;&lt;u&gt;&lt;b&gt;Variable number of arguments:&lt;/b&gt;&lt;/u&gt;&lt;/span&gt;&lt;/div&gt;&amp;nbsp;      &amp;nbsp;&amp;nbsp; &lt;span style=&quot;font-family: Arial,Helvetica,sans-serif;&quot;&gt;Till now we have seen various types of functions in C. Some them were built-in and some user defined. There are a lot of noticeable point about them. Intuitively We can say that all the functions should possess same characteristics&amp;nbsp; whether it is user defined or built-in. We have also seen function returning some value, not returning any value, taking no argument and taking certain fixed number of arguments with certain data-types.&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Arial,Helvetica,sans-serif;&quot;&gt;&amp;nbsp; &amp;nbsp; Some-times a question can come in your mind if it is possible for a function to accept any number of arguments in C language. Off course it is possible !!! In fact we have used such function many times. Don&#39;t remember??? Recall our most commonly used functions printf() and scanf(). How many arguments does printf() function take??&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Arial,Helvetica,sans-serif;&quot;&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; Let me remind you when we pass arguments to a function while calling it, the arguments are separated by a comma(,). So printf() function takes variable number of arguments. First argument to printf() is a character string and after that some other arguments of various types may or may not come. This fact encourages us to create our own user defined function which would take variable number of arguments.&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: medium;&quot;&gt;&lt;u&gt;&lt;b&gt;Problem and solution:&lt;/b&gt;&lt;/u&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Arial,Helvetica,sans-serif;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; There may be some problems while implementing variable arguments. One such problem is to determine how many arguments have been passed during a particular call. To overcome that problem there is a rule that at least one argument must be specified in the declaration of function. For more ease I will be giving that argument as a number equal to the count of following arguments so that I can easily know that how many arguments are being passed.&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: medium;&quot;&gt;&lt;u&gt;&lt;b&gt;Sample program:&lt;/b&gt;&lt;/u&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Arial,Helvetica,sans-serif;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; This program is one of the simplest programs for illustrating the use of variable number of arguments. There is a function sum which can take may arguments and print the sum of those.&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Arial,Helvetica,sans-serif;&quot;&gt;Have a look.&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;------------------------------------------------------------&lt;/span&gt;&lt;br /&gt;
&lt;pre class=&quot;brush: c&quot;&gt;#include &amp;lt;stdio.h&amp;gt;
#include &amp;lt;stdarg.h&amp;gt;
int sum(int num, ... );
int main( void )
{
&amp;nbsp;&amp;nbsp;&amp;nbsp; printf(&quot;This is program to show the use of variable numbers of arguments to a function\n&quot;);
&amp;nbsp;&amp;nbsp;&amp;nbsp; printf( &quot;Sum of 1,2,3,4 is %d\n&quot;, sum(4,1,2,3,4) );
&amp;nbsp;&amp;nbsp;&amp;nbsp; printf( &quot;Sum of 4,5,3 is %d\n&quot;, sum(3,4,5,3) );
&amp;nbsp;&amp;nbsp;&amp;nbsp; printf( &quot;Sum of 7,3,4,9,1,5 is %d\n&quot;, sum(6,7,3,4,9,1,5) );
&amp;nbsp;&amp;nbsp;&amp;nbsp; return( 0 );
&amp;nbsp;}
int sum(int num, ... )
{
&amp;nbsp;&amp;nbsp;&amp;nbsp; int answer = 0;
&amp;nbsp;&amp;nbsp;&amp;nbsp; va_list argptr;
&amp;nbsp;&amp;nbsp;&amp;nbsp; va_start( argptr, num );
&amp;nbsp;&amp;nbsp;&amp;nbsp; while(num-- &amp;gt; 0)
&amp;nbsp;&amp;nbsp;&amp;nbsp; answer += va_arg( argptr, int );
&amp;nbsp;&amp;nbsp;&amp;nbsp; va_end( argptr );
&amp;nbsp;&amp;nbsp;&amp;nbsp; return( answer );
} &lt;/pre&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;------------------------------------------------------------&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: large;&quot;&gt;&lt;u&gt;&lt;b&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;&lt;span style=&quot;font-family: Arial,Helvetica,sans-serif;&quot;&gt;Explanation:&lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;/u&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;&lt;span style=&quot;font-family: Arial,Helvetica,sans-serif;&quot;&gt;&amp;nbsp; &amp;nbsp; Declaration of the function is clear as shown. Here argptr is a variable of type va_list. After va_start function is executed here, argptr will contain list of rest of the variables. After that each value of each argument can be accessed with the help of va_arg() function. When all the arguments are finished va_end() function should be put at the end. This va_end() is analogous to fclose() in file handling.&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;&lt;span style=&quot;font-family: Arial,Helvetica,sans-serif;&quot;&gt;&amp;nbsp;&lt;/span&gt; &lt;/span&gt;&lt;br /&gt;
&lt;div style=&quot;color: black;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;&lt;span style=&quot;font-family: Arial,Helvetica,sans-serif;&quot;&gt;&lt;span style=&quot;color: #990000; font-size: large;&quot;&gt;&lt;u&gt;&lt;b&gt;&lt;/b&gt;&lt;/u&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;&lt;span style=&quot;font-family: Arial,Helvetica,sans-serif;&quot;&gt;&lt;span style=&quot;color: #990000; font-size: large;&quot;&gt;&lt;u&gt;&lt;b&gt;Remarks:&lt;/b&gt;&lt;/u&gt;&lt;/span&gt;&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;&lt;span style=&quot;font-family: Arial,Helvetica,sans-serif;&quot;&gt;1. variable number of arguments means the function can take some random(but finite) number of arguments. This does not mean that the function can take infinite number of arguments. Maximum number of arguments to a function are limited and depend on the compiler.&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programsimply.blogspot.com/feeds/1615689419850698045/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://programsimply.blogspot.com/2011/12/variable-number-of-arguments-to.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/997202589550114592/posts/default/1615689419850698045'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/997202589550114592/posts/default/1615689419850698045'/><link rel='alternate' type='text/html' href='http://programsimply.blogspot.com/2011/12/variable-number-of-arguments-to.html' title='Variable number of arguments to a function'/><author><name>Ranu Pratap Singh</name><uri>http://www.blogger.com/profile/15360265618857473433</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-997202589550114592.post-1289686276862777431</id><published>2011-12-10T13:40:00.002+05:30</published><updated>2013-01-15T00:36:38.237+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="in place insertion sort"/><category scheme="http://www.blogger.com/atom/ns#" term="insert sort in C"/><category scheme="http://www.blogger.com/atom/ns#" term="insertion sort in C"/><category scheme="http://www.blogger.com/atom/ns#" term="insertion sort user defined"/><title type='text'>Insertion sort in C</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;h3 class=&quot;post-title entry-title&quot;&gt;
&lt;/h3&gt;
&lt;div class=&quot;post-header&quot;&gt;
&lt;/div&gt;
&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot;&gt;
&lt;div style=&quot;font-family: Arial,Helvetica,sans-serif;&quot;&gt;
&lt;span style=&quot;font-size: large;&quot;&gt;&lt;u&gt;&lt;b&gt;The need:&lt;/b&gt;&lt;/u&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;font-family: Arial,Helvetica,sans-serif;&quot;&gt;
&amp;nbsp;     &amp;nbsp;&amp;nbsp; This is a program to sort a given array of numbers using insertion sort. This program sorts numbers in increasing order. To read more about insert sort read &lt;a href=&quot;http://en.wikipedia.org/wiki/Insert_sort&quot; target=&quot;_blank&quot;&gt;wikipedia&lt;/a&gt;.&lt;/div&gt;
&lt;div style=&quot;font-family: Arial,Helvetica,sans-serif;&quot;&gt;
&lt;span style=&quot;font-size: large;&quot;&gt;&lt;u&gt;&lt;b&gt;The code:&lt;/b&gt;&lt;/u&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;
----------------------------------------------------------&lt;br /&gt;
&lt;pre class=&quot;brush: c&quot;&gt;#include&amp;lt;stdio.h&amp;gt;
void insert_sort(int a[],int size);
int main()
{
&amp;nbsp;&amp;nbsp;&amp;nbsp; int i,size;
&amp;nbsp;&amp;nbsp;&amp;nbsp; printf(&quot;program for sorting using insert sort\nHow many numbers do you want to sort\n&quot;);
&amp;nbsp;&amp;nbsp;&amp;nbsp; scanf(&quot;%d&quot;,&amp;amp;size);
&amp;nbsp;&amp;nbsp;&amp;nbsp; int arr[size];
&amp;nbsp;&amp;nbsp;&amp;nbsp; printf(&quot;enter numbers\n&quot;);
&amp;nbsp;&amp;nbsp;&amp;nbsp; for(i=0;i&amp;lt;size;i++)
&amp;nbsp;&amp;nbsp;&amp;nbsp; scanf(&quot;%d&quot;,&amp;amp;arr[i]);
&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp; printf(&quot;the array before sort is\n&quot;);
&amp;nbsp;&amp;nbsp;&amp;nbsp; for(i=0;i&amp;lt;size;i++)
&amp;nbsp;&amp;nbsp;&amp;nbsp; printf(&quot;%d &quot;,arr[i]);
&amp;nbsp;&amp;nbsp;&amp;nbsp; printf(&quot;\n&quot;);
&amp;nbsp;&amp;nbsp;&amp;nbsp; insert_sort(arr,size);
&amp;nbsp;&amp;nbsp;&amp;nbsp; printf(&quot;the array after sort is\n&quot;);
&amp;nbsp;&amp;nbsp;&amp;nbsp; for(i=0;i&amp;lt;size;i++)
&amp;nbsp;&amp;nbsp;&amp;nbsp; printf(&quot;%d &quot;,arr[i]);
&amp;nbsp;&amp;nbsp;&amp;nbsp; printf(&quot;\n&quot;);
&amp;nbsp;&amp;nbsp;&amp;nbsp; return 0;
}

void insert_sort(int a[],int size)
{
&amp;nbsp;&amp;nbsp;&amp;nbsp; int i,j,k,temp;
&amp;nbsp;&amp;nbsp;&amp;nbsp; for(i=1;i&amp;lt;size;i++)
&amp;nbsp;&amp;nbsp;&amp;nbsp; {
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; j=0;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; temp=a[i];
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; while(temp&amp;gt;a[j]&amp;amp;&amp;amp;j&amp;lt;i)
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; j++;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; for(k=i;k&amp;gt;j;k--)
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; a[k]=a[k-1];
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; a[k]=temp;
&amp;nbsp;&amp;nbsp;&amp;nbsp; }
}&lt;/pre&gt;
&lt;/div&gt;
&lt;div style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;
---------------------------------------------------------- &lt;/div&gt;
&lt;/div&gt;
&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;font-family: Arial, Helvetica, sans-serif; font-size: large;&quot;&gt;&lt;u&gt;&lt;b&gt;Approach:&lt;/b&gt;&lt;/u&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt; &lt;/span&gt;&amp;nbsp;&amp;nbsp; &lt;span style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;The   approach is to follow the existing algorithm. Read algorithm on&lt;/span&gt;&lt;span style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt; &lt;/span&gt;&lt;a href=&quot;http://en.wikipedia.org/wiki/Insert_sort&quot; style=&quot;font-family: Arial,Helvetica,sans-serif;&quot; target=&quot;_blank&quot;&gt;wikipedia&lt;/a&gt;&lt;span style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;. The basic idea is traverse the array form start, temporarily store value of current element and search for the place to the left of that element where the value fits. When you find the place, shift the elements after that position by one to make the void to place temporarily stored element.&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://programsimply.blogspot.com/feeds/1289686276862777431/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://programsimply.blogspot.com/2011/12/insertion-sort-in-c.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/997202589550114592/posts/default/1289686276862777431'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/997202589550114592/posts/default/1289686276862777431'/><link rel='alternate' type='text/html' href='http://programsimply.blogspot.com/2011/12/insertion-sort-in-c.html' title='Insertion sort in C'/><author><name>Ranu Pratap Singh</name><uri>http://www.blogger.com/profile/15360265618857473433</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-997202589550114592.post-3523126266016903117</id><published>2011-12-09T13:12:00.003+05:30</published><updated>2011-12-09T14:42:37.888+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="k&#39;th largest number"/><title type='text'>finding kth largest number out of n numbers</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot;&gt;&lt;div style=&quot;font-family: Arial,Helvetica,sans-serif;&quot;&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;u&gt;&lt;b&gt;The need:&lt;/b&gt;&lt;/u&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;font-family: Arial,Helvetica,sans-serif;&quot;&gt;&amp;nbsp;      &amp;nbsp;&amp;nbsp; This program gives the kth largest number from the given  unsorted list of n numbers. I don&#39;t know what could be the application  of that but some sites tell that it is asked as interview question  sometimes.&lt;/div&gt;&lt;div style=&quot;font-family: Arial,Helvetica,sans-serif;&quot;&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;u&gt;&lt;b&gt;The code:&lt;/b&gt;&lt;/u&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;----------------------------------------------------------&lt;/div&gt;&lt;pre class=&quot;brush: c&quot;&gt;/*find kth largest number from a list of n numbers*/
#include&amp;lt;stdio.h&amp;gt;
#include&amp;lt;stdlib.h&amp;gt;
void getMaxToFront(int *p,int front, int end);
void swap(int *, int *);
int main()
{
&amp;nbsp;&amp;nbsp;&amp;nbsp; int i,*num,n,k;
&amp;nbsp;&amp;nbsp;&amp;nbsp; printf(&quot;This is a program to find kth largest number from a list of numbers\n&quot;);
&amp;nbsp;&amp;nbsp;&amp;nbsp; printf(&quot;how many are total numbers\n n = \t&quot;);
&amp;nbsp;&amp;nbsp;&amp;nbsp; scanf(&quot;%d&quot;,&amp;amp;n);
&amp;nbsp;&amp;nbsp;&amp;nbsp; if(1&amp;gt;n)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; exit(printf(&quot;n cannot be less than 1\n&quot;));
&amp;nbsp;&amp;nbsp;&amp;nbsp; printf(&quot;for kth largest give value of k\n k = \t&quot;);
&amp;nbsp;&amp;nbsp;&amp;nbsp; scanf(&quot;%d&quot;,&amp;amp;k);
&amp;nbsp;&amp;nbsp;&amp;nbsp; if(k&amp;lt;1)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; exit(printf(&quot;k cannot be less than 1\n&quot;));
&amp;nbsp;&amp;nbsp;&amp;nbsp; if(k&amp;gt;n)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; exit(printf(&quot;k must be less then n\n&quot;));
&amp;nbsp;&amp;nbsp;&amp;nbsp; num=(int *)malloc(n*sizeof(int));
&amp;nbsp;&amp;nbsp;&amp;nbsp; printf(&quot;now enter %d integers\n&quot;,n);
&amp;nbsp;&amp;nbsp;&amp;nbsp; for(i=0;i&amp;lt;n;i++)
&amp;nbsp;&amp;nbsp;&amp;nbsp; scanf(&quot;%d&quot;,num+i);
&amp;nbsp;&amp;nbsp;&amp;nbsp; for(i=0;i&amp;lt;k;i++)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; getMaxToFront(num,i,n-1);
&amp;nbsp;&amp;nbsp;&amp;nbsp; printf(&quot;\n%d is %dth largest\n&quot;,*(num+k-1),k);
&amp;nbsp;&amp;nbsp;&amp;nbsp; getchar();
&amp;nbsp;&amp;nbsp;&amp;nbsp; return 0;
}

void getMaxToFront(int *p,int front, int end)
{&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp; int i;
&amp;nbsp;&amp;nbsp;&amp;nbsp; for(i=front+1;i&amp;lt;=end;i++)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if( *(p+front) &amp;lt; *(p+i) )
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; swap(p+front,p+i);
}

void swap(int *a,int *b)
{
&amp;nbsp;&amp;nbsp;&amp;nbsp; int temp;
&amp;nbsp;&amp;nbsp;&amp;nbsp; temp = *a;
&amp;nbsp;&amp;nbsp;&amp;nbsp; *a = *b;
&amp;nbsp;&amp;nbsp;&amp;nbsp; *b = temp;
}&lt;/pre&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt; &lt;/span&gt;&lt;br /&gt;
&lt;div style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;----------------------------------------------------------&lt;/div&gt;&lt;span style=&quot;font-family: Arial,Helvetica,sans-serif; font-size: large;&quot;&gt;&lt;u&gt;&lt;b&gt;Approach:&lt;/b&gt;&lt;/u&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Arial,Helvetica,sans-serif;&quot;&gt; &lt;/span&gt;&amp;nbsp;&amp;nbsp; first    approach is simple. Pick the largest bring it to 1st position, then  pick largest from the rest of list and bring on 2nd position and so on  upto kth :-).&lt;br /&gt;
&lt;br /&gt;
&lt;div style=&quot;color: #990000; font-family: Arial,Helvetica,sans-serif;&quot;&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;b&gt;&lt;u&gt;Remarks:&lt;/u&gt;&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;&lt;span style=&quot;font-family: Arial,Helvetica,sans-serif;&quot;&gt;&lt;/span&gt;&lt;div style=&quot;font-family: Arial,Helvetica,sans-serif;&quot;&gt;Analyze the case:&lt;/div&gt;&lt;div style=&quot;font-family: Arial,Helvetica,sans-serif;&quot;&gt;n=100, k=5;&lt;/div&gt;&lt;div style=&quot;font-family: Arial,Helvetica,sans-serif;&quot;&gt;The above program is fine with that. Just find out biggest 5 numbers and done.&lt;/div&gt;&lt;div style=&quot;font-family: Arial,Helvetica,sans-serif;&quot;&gt;Now have a look at this case:&lt;/div&gt;&lt;div style=&quot;font-family: Arial,Helvetica,sans-serif;&quot;&gt;n=100, k=95;&lt;/div&gt;&lt;div style=&quot;font-family: Arial,Helvetica,sans-serif;&quot;&gt;In this case you will not want to go from front to end because you will have to iterate the loop in main 95 time. Instead we&#39;ll find out 6th smallest and go end to front... :-)&lt;/div&gt;&lt;div style=&quot;font-family: Arial,Helvetica,sans-serif;&quot;&gt;so better modify the above program a little bit which is like below:&lt;/div&gt;&lt;div style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;-------------------------------------------------------------&lt;/div&gt;&lt;pre class=&quot;brush: c&quot;&gt;/*find kth largest number from a list of n numbers*/
#include&amp;lt;stdio.h&amp;gt;
#include&amp;lt;stdlib.h&amp;gt;
void getMaxToFront(int *p,int front, int end);
void getMinToEnd(int *p,int front, int end);
void swap(int *, int *);
int main()
{
&amp;nbsp;&amp;nbsp;&amp;nbsp; int i,*num,n,k;
&amp;nbsp;&amp;nbsp;&amp;nbsp; printf(&quot;This is a program to find kth largest number from a list of numbers\n&quot;);
&amp;nbsp;&amp;nbsp;&amp;nbsp; printf(&quot;how many are total numbers\n n = \t&quot;);
&amp;nbsp;&amp;nbsp;&amp;nbsp; scanf(&quot;%d&quot;,&amp;amp;n);
&amp;nbsp;&amp;nbsp;&amp;nbsp; if(1&amp;gt;n)
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; exit(printf(&quot;n cannot be less than 1\n&quot;));
&amp;nbsp;&amp;nbsp;&amp;nbsp; printf(&quot;for kth largest give value of k\n k = \t&quot;);
&amp;nbsp;&amp;nbsp;&amp;nbsp; scanf(&quot;%d&quot;,&amp;amp;k);
&amp;nbsp;&amp;nbsp;&amp;nbsp; if(k&amp;lt;1)
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; exit(printf(&quot;k cannot be less than 1\n&quot;));
&amp;nbsp;&amp;nbsp;&amp;nbsp; if(k&amp;gt;n)
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; exit(printf(&quot;k must be less then n\n&quot;));
&amp;nbsp;&amp;nbsp;&amp;nbsp; num=(int *)malloc(n*sizeof(int));
&amp;nbsp;&amp;nbsp;&amp;nbsp; printf(&quot;now enter %d integers\n&quot;,n);
&amp;nbsp;&amp;nbsp;&amp;nbsp; for(i=0;i&amp;lt;n;i++)
&amp;nbsp;&amp;nbsp;&amp;nbsp; scanf(&quot;%d&quot;,num+i);
&amp;nbsp;&amp;nbsp;&amp;nbsp; if(k&amp;lt;=n/2){
&amp;nbsp;&amp;nbsp;&amp;nbsp; printf(&quot;\nusing getMaxToFront\n&quot;);
&amp;nbsp;&amp;nbsp;&amp;nbsp; for(i=0;i&amp;lt;k;i++)
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; getMaxToFront(num,i,n-1);
&amp;nbsp;&amp;nbsp;&amp;nbsp; }
&amp;nbsp;&amp;nbsp;&amp;nbsp; else{
&amp;nbsp;&amp;nbsp;&amp;nbsp; printf(&quot;\nusing getMinToEnd\n&quot;);
&amp;nbsp;&amp;nbsp;&amp;nbsp; for(i=n;i&amp;gt;=k;i--)
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; getMinToEnd(num,0,i-1);
&amp;nbsp;&amp;nbsp;&amp;nbsp; }
&amp;nbsp;&amp;nbsp;&amp;nbsp; printf(&quot;\n%d is %dth largest\n&quot;,*(num+k-1),k);
&amp;nbsp;&amp;nbsp;&amp;nbsp; getchar();
&amp;nbsp;&amp;nbsp;&amp;nbsp; return 0;
}

void getMaxToFront(int *p,int front, int end)
{&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; //end is needed as an argument here because we need to use the length of list
&amp;nbsp;&amp;nbsp;&amp;nbsp; int i;
&amp;nbsp;&amp;nbsp;&amp;nbsp; for(i=front+1;i&amp;lt;=end;i++)
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; if( *(p+front) &amp;lt; *(p+i) )
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; swap(p+front,p+i);
}

void getMinToEnd(int *p,int front, int end)
{&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; //actually we could have skipped front as an argument here observe each time I am passing 0 as front
&amp;nbsp;&amp;nbsp;&amp;nbsp; int i;
&amp;nbsp;&amp;nbsp;&amp;nbsp; for(i=end;i&amp;gt;=front;i--)
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; if( *(p+end) &amp;gt; *(p+i) )
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; swap(p+end,p+i);
}

void swap(int *a,int *b)
{
&amp;nbsp;&amp;nbsp;&amp;nbsp; int temp;
&amp;nbsp;&amp;nbsp;&amp;nbsp; temp = *a;
&amp;nbsp;&amp;nbsp;&amp;nbsp; *a = *b;
&amp;nbsp;&amp;nbsp;&amp;nbsp; *b = temp;
}&lt;/pre&gt;&lt;div style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;-------------------------------------------------------------&lt;br /&gt;
&lt;span style=&quot;font-family: Arial,Helvetica,sans-serif;&quot;&gt;This program uses the method/function which gives less number of loop run&lt;/span&gt;&lt;span style=&quot;font-family: Arial,Helvetica,sans-serif;&quot;&gt;s&lt;/span&gt;&lt;span style=&quot;font-family: Arial,Helvetica,sans-serif;&quot;&gt;.&lt;/span&gt; &lt;/div&gt;&lt;/div&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programsimply.blogspot.com/feeds/3523126266016903117/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://programsimply.blogspot.com/2011/12/finding-kth-largest-number-out-of-n.html#comment-form' title='5 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/997202589550114592/posts/default/3523126266016903117'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/997202589550114592/posts/default/3523126266016903117'/><link rel='alternate' type='text/html' href='http://programsimply.blogspot.com/2011/12/finding-kth-largest-number-out-of-n.html' title='finding kth largest number out of n numbers'/><author><name>Ranu Pratap Singh</name><uri>http://www.blogger.com/profile/15360265618857473433</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>5</thr:total></entry><entry><id>tag:blogger.com,1999:blog-997202589550114592.post-5590639062047346309</id><published>2011-12-09T13:10:00.003+05:30</published><updated>2011-12-10T13:13:07.063+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="Armstrong number"/><category scheme="http://www.blogger.com/atom/ns#" term="Armstrong number C"/><category scheme="http://www.blogger.com/atom/ns#" term="finding whether a number is armstrong"/><category scheme="http://www.blogger.com/atom/ns#" term="Narcissistic number"/><title type='text'>Finding whether a number is armstrong number of not</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;&lt;div style=&quot;font-family: Arial,Helvetica,sans-serif;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;&lt;u&gt;&lt;b&gt;The need:&lt;/b&gt;&lt;/u&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;font-family: Arial,Helvetica,sans-serif;&quot;&gt;&amp;nbsp;    &amp;nbsp;&amp;nbsp; This one is trivial program at this point of time. Still putting on demand of Rahul Devra.For those who dont know what is Armstrong number (Narcissistic number) first have a look at &lt;a href=&quot;http://en.wikipedia.org/wiki/Narcissistic_number&quot; target=&quot;_blank&quot;&gt;Narcissistic number&lt;/a&gt; on wikipedia.&lt;/div&gt;&lt;div style=&quot;font-family: Arial,Helvetica,sans-serif;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;&lt;u&gt;&lt;b&gt;The code:&lt;/b&gt;&lt;/u&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;----------------------------------------------------------&lt;/div&gt;&lt;pre class=&quot;brush: c&quot;&gt;/*Find whether entered number is armstrong number or not*/
#include&amp;lt;stdio.h&amp;gt;
#include&amp;lt;math.h&amp;gt;
int digit_count(int x);
int isArmstrong(int x);

int main()
{
&amp;nbsp;&amp;nbsp;&amp;nbsp; int flg=0,num;
&amp;nbsp;&amp;nbsp;&amp;nbsp; printf(&quot;This is a program to find whether a number is armstrong number\n&quot;);
&amp;nbsp;&amp;nbsp;&amp;nbsp; printf(&quot;enter a natural number\n&quot;);
&amp;nbsp;&amp;nbsp;&amp;nbsp; scanf(&quot;%d&quot;,&amp;amp;num);
&amp;nbsp;&amp;nbsp;&amp;nbsp; if(isArmstrong(num))
&amp;nbsp;&amp;nbsp;&amp;nbsp; printf(&quot;%d is an armstrong number\n&quot;,num);
&amp;nbsp;&amp;nbsp;&amp;nbsp; else
&amp;nbsp;&amp;nbsp;&amp;nbsp; printf(&quot;%d is not an armstrong number\n&quot;,num);
&amp;nbsp;&amp;nbsp;&amp;nbsp; getchar();
&amp;nbsp;&amp;nbsp;&amp;nbsp; return 0;
}

int digit_count(int x)
{
&amp;nbsp;&amp;nbsp;&amp;nbsp; int count=0;
&amp;nbsp;&amp;nbsp;&amp;nbsp; if(x==0)
&amp;nbsp;&amp;nbsp;&amp;nbsp; return 0;
&amp;nbsp;&amp;nbsp;&amp;nbsp; while(x/=10)
&amp;nbsp;&amp;nbsp;&amp;nbsp; count++;
&amp;nbsp;&amp;nbsp;&amp;nbsp; return count+1;
}

int isArmstrong(int x)
{
&amp;nbsp;&amp;nbsp;&amp;nbsp; int i,num_digit,sum=0,current_digit,holder;
&amp;nbsp;&amp;nbsp;&amp;nbsp; num_digit = digit_count(x);
&amp;nbsp;&amp;nbsp;&amp;nbsp; holder=x;
&amp;nbsp;&amp;nbsp;&amp;nbsp; for(i=0;i&amp;lt;num_digit;i++)
&amp;nbsp;&amp;nbsp;&amp;nbsp; {
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; current_digit = x%10;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; sum+=pow(current_digit,num_digit);
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; x/=10;
&amp;nbsp;&amp;nbsp;&amp;nbsp; }
&amp;nbsp;&amp;nbsp;&amp;nbsp; if(sum==holder)
&amp;nbsp;&amp;nbsp;&amp;nbsp; return 1;
&amp;nbsp;&amp;nbsp;&amp;nbsp; else
&amp;nbsp;&amp;nbsp;&amp;nbsp; return 0;
}&lt;/pre&gt;&lt;div style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;----------------------------------------------------------&lt;/div&gt;&lt;span style=&quot;font-family: Arial,Helvetica,sans-serif; font-size: medium;&quot;&gt;&lt;u&gt;&lt;b&gt;Approach:&lt;/b&gt;&lt;/u&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Arial,Helvetica,sans-serif;&quot;&gt; &lt;/span&gt;&amp;nbsp;&amp;nbsp; The  approach is simple. Go by the definition, take digit by digit check for the condition for Armstrong number and that&#39;s it.&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programsimply.blogspot.com/feeds/5590639062047346309/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://programsimply.blogspot.com/2011/12/finding-whether-number-is-armstrong.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/997202589550114592/posts/default/5590639062047346309'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/997202589550114592/posts/default/5590639062047346309'/><link rel='alternate' type='text/html' href='http://programsimply.blogspot.com/2011/12/finding-whether-number-is-armstrong.html' title='Finding whether a number is armstrong number of not'/><author><name>Ranu Pratap Singh</name><uri>http://www.blogger.com/profile/15360265618857473433</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-997202589550114592.post-576087094744367215</id><published>2011-12-07T13:06:00.002+05:30</published><updated>2011-12-09T15:13:02.853+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="10000th term of fibonacci series"/><category scheme="http://www.blogger.com/atom/ns#" term="2012th term of fibonacci series"/><category scheme="http://www.blogger.com/atom/ns#" term="fibonacci series"/><category scheme="http://www.blogger.com/atom/ns#" term="fibonacci series unlimited"/><category scheme="http://www.blogger.com/atom/ns#" term="nth term of fibonacci series"/><category scheme="http://www.blogger.com/atom/ns#" term="unlimited fibonacci c"/><title type='text'>getting nth term of fibonacci series unlimited number of terms</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;&lt;div style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;&lt;u&gt;&lt;b&gt;&lt;span style=&quot;font-family: Arial,Helvetica,sans-serif;&quot;&gt;The need:&lt;/span&gt;&lt;/b&gt;&lt;/u&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;&lt;span style=&quot;font-family: Arial,Helvetica,sans-serif;&quot;&gt;&amp;nbsp;      &amp;nbsp;&amp;nbsp; The program for fibonacci series is very simple but those programs give you terms upto the limit of integer size. This is a program to print Fibonacci series upto 10000th term. I wrote this program when I noticed people coming on &lt;a href=&quot;http://programsimply.blogspot.com/2011/10/getting-nth-term-of-fibonacci-series.html&quot; target=&quot;_blank&quot;&gt;previous fibonacci program&lt;/a&gt; in search of 2011th term or so.&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;&lt;u&gt;&lt;b&gt;&lt;span style=&quot;font-family: Arial,Helvetica,sans-serif;&quot;&gt;The code:&amp;nbsp;&lt;/span&gt;&lt;/b&gt;&lt;/u&gt; &lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;--------------------------------------------&lt;/span&gt;&lt;br /&gt;
&lt;pre class=&quot;brush: c&quot;&gt;#define MAX 2100 
#include&amp;lt;stdio.h&amp;gt; 
#include&amp;lt;string.h&amp;gt; 
#include&amp;lt;stdlib.h&amp;gt; 
void print(char x[]); 
void add(char x[], char y[], char z[]); 

int main() 
{ 
&amp;nbsp;&amp;nbsp;&amp;nbsp; int i=0,j,num; 
&amp;nbsp;&amp;nbsp;&amp;nbsp; char n_minus1[MAX]=&quot;1&quot;,n_minus2[MAX]=&quot;1&quot;,nth[MAX]=&quot;&quot;; 
&amp;nbsp;&amp;nbsp;&amp;nbsp; printf(&quot;This is a program to print fibonacci sequence upto desired term.(max 10000)\n&quot;); 
&amp;nbsp;&amp;nbsp;&amp;nbsp; printf(&quot;\nenter a natural number upto which do you want fibonacci series\n&quot;);
&amp;nbsp;&amp;nbsp;&amp;nbsp; scanf(&quot;%d&quot;,&amp;amp;num); 
&amp;nbsp;&amp;nbsp;&amp;nbsp; if(&amp;nbsp;&amp;nbsp;&amp;nbsp; num&amp;lt;1||num&amp;gt;10000) 
&amp;nbsp;&amp;nbsp;&amp;nbsp; { 
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; printf(&quot;The desired term index was either invalid or greater than 10000\n&quot;); 
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; exit(0); 
&amp;nbsp;&amp;nbsp;&amp;nbsp; }
&amp;nbsp;&amp;nbsp;&amp;nbsp; printf(&quot;\noptions:\n1. print only %dth term\n2. print all terms\n&quot;,num);
&amp;nbsp;&amp;nbsp;&amp;nbsp; scanf(&quot;%d&quot;,&amp;amp;j);
&amp;nbsp;&amp;nbsp;&amp;nbsp; switch(j)
&amp;nbsp;&amp;nbsp;&amp;nbsp; {
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; case 1: 
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; if(num&amp;lt;2){
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; printf(&quot;The first term is 1\n&quot;);
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; break;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; }
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; if(num&amp;lt;3){
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; printf(&quot;The second term is 1\n&quot;);
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; break;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; }
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; for(i=2;i&amp;lt;num;i++)
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; {
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp; add(nth,n_minus1,n_minus2);
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp; strcpy(n_minus2,n_minus1);
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp; strcpy(n_minus1,nth);
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; }
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; printf(&quot;The %dth term is &quot;,num);
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; print(nth);
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; break;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; case 2:&amp;nbsp;&amp;nbsp;&amp;nbsp; printf(&quot;\nFibonacci series upto %dth term\n\n&quot;,num);
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; printf(&quot;term \tvalue\n&quot;,num);
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; printf(&quot;%4d \t1\n&quot;,1);
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; if(num&amp;lt;2)
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; break;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; printf(&quot;%4d \t1\n&quot;,2);
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; if(num&amp;lt;3)
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; break;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; for(i=2;i&amp;lt;num;i++)
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; {
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp; add(nth,n_minus1,n_minus2);
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp; printf(&quot;%4d\t&quot;,i+1);
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp; print(nth);
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp; strcpy(n_minus2,n_minus1);
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp; strcpy(n_minus1,nth);
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; }
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; break;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; default: printf(&quot;Invalid choice...!!! \tExiting\n&quot;);
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; exit(0);
&amp;nbsp;&amp;nbsp;&amp;nbsp; }
&amp;nbsp;&amp;nbsp;&amp;nbsp; printf(&quot;\n\nprgram finished successfully.\t press any key to continue\n&quot;);
&amp;nbsp;&amp;nbsp;&amp;nbsp; getchar(); 
&amp;nbsp;&amp;nbsp;&amp;nbsp; return 0; 
} 

void print(char x[]) 
{ 
&amp;nbsp;&amp;nbsp;&amp;nbsp; int i; 
&amp;nbsp;&amp;nbsp;&amp;nbsp; for(i=strlen(x)-1;i&amp;gt;=0;i--) 
&amp;nbsp;&amp;nbsp;&amp;nbsp; { 
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; putchar(x[i]); 
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; if(i%5==0) 
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; putchar(&#39; &#39;); 
&amp;nbsp;&amp;nbsp;&amp;nbsp; } 
&amp;nbsp;&amp;nbsp;&amp;nbsp; putchar(&#39;\n&#39;); 
&amp;nbsp;&amp;nbsp;&amp;nbsp; return; 
} 

void add(char x[],char y[],char z[]) 
{ 
&amp;nbsp;&amp;nbsp;&amp;nbsp; int i,ylen,zlen,equal;
&amp;nbsp;&amp;nbsp;&amp;nbsp; char c = 0,temp;
&amp;nbsp;&amp;nbsp;&amp;nbsp; ylen = strlen(y);
&amp;nbsp;&amp;nbsp;&amp;nbsp; zlen = strlen(z);
&amp;nbsp;&amp;nbsp;&amp;nbsp; equal = (ylen == zlen) ? 1 : 0; 
&amp;nbsp;&amp;nbsp;&amp;nbsp; for(i=0;i&amp;lt;zlen;i++) 
&amp;nbsp;&amp;nbsp;&amp;nbsp; { 
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; x[i]=c + y[i] + z[i] - &#39;0&#39;; 
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; if( x[i]-&#39;0&#39; &amp;gt; 9)
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; {
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; x[i]-=10;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; c=1;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; }
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; else
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; c=0; 
&amp;nbsp;&amp;nbsp;&amp;nbsp; }
&amp;nbsp;&amp;nbsp;&amp;nbsp; if(equal)
&amp;nbsp;&amp;nbsp;&amp;nbsp; {
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; if(c!=0)
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; {
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; x[i]=c + &#39;0&#39;;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; x[i+1]=&#39;\0&#39;;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; }
&amp;nbsp;&amp;nbsp;&amp;nbsp; }
&amp;nbsp;&amp;nbsp;&amp;nbsp; else
&amp;nbsp;&amp;nbsp;&amp;nbsp; {
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; x[i]=c + y[i];
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; x[i+1]=&#39;\0&#39;;
&amp;nbsp;&amp;nbsp;&amp;nbsp; } 
&amp;nbsp;&amp;nbsp;&amp;nbsp; return; 
}&lt;/pre&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;--------------------------------------------&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;&amp;nbsp; &lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: large;&quot;&gt; &lt;u style=&quot;font-family: Arial,Helvetica,sans-serif;&quot;&gt;&lt;b&gt;The approach:&lt;/b&gt;&lt;/u&gt;&amp;nbsp; &lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;&lt;span style=&quot;font-family: Arial,Helvetica,sans-serif;&quot;&gt;This  program creates a Fibonacci series upto the terms given by the user.  The logic is same. Just go with the definition of Fibonacci that any  term is the sum of its two preceding terms. Here nth always hold current  term and n_minus1 and n_minus2 hold previous 2 terms(except for first 2 terms case). I have already told that integers are very small to work when we have more than 10digits in the scene so these 3 variables are taken as strings. add() is UDF defined by me which takes 3 arguments x,y,z. add puts sum of y and z into x. This add() is the only twist here to differentiate this program from &lt;a href=&quot;http://programsimply.blogspot.com/2011/05/program-38-fibonacci-series.html&quot; target=&quot;_blank&quot;&gt;program38&lt;/a&gt; of this blog.&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot;&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: large;&quot;&gt;&lt;span style=&quot;font-family: Arial,Helvetica,sans-serif;&quot;&gt;&lt;b style=&quot;color: #660000;&quot;&gt;&lt;u&gt;Remarks:&lt;/u&gt;&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Arial,Helvetica,sans-serif;&quot;&gt;1. In this   program is capable to give you upto 10000th term of fibonacci easily.&lt;/span&gt;&lt;/div&gt;&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot;&gt;&lt;span style=&quot;font-family: Arial,Helvetica,sans-serif;&quot;&gt;2. You can get upto more terms. all you have to do is to increase the value of max in #define statement. This is why I wrote unlimited in the title.&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programsimply.blogspot.com/feeds/576087094744367215/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://programsimply.blogspot.com/2011/12/getting-nth-term-of-fibonacci-series.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/997202589550114592/posts/default/576087094744367215'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/997202589550114592/posts/default/576087094744367215'/><link rel='alternate' type='text/html' href='http://programsimply.blogspot.com/2011/12/getting-nth-term-of-fibonacci-series.html' title='getting nth term of fibonacci series unlimited number of terms'/><author><name>Ranu Pratap Singh</name><uri>http://www.blogger.com/profile/15360265618857473433</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-997202589550114592.post-6112085969197666595</id><published>2011-12-06T19:52:00.000+05:30</published><updated>2011-12-06T19:52:50.492+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="C program to reverse order of words"/><category scheme="http://www.blogger.com/atom/ns#" term="rev_word"/><category scheme="http://www.blogger.com/atom/ns#" term="UDF"/><title type='text'>program to reverse the spelling of words in a sentence</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot;&gt;&lt;div style=&quot;font-family: Arial,Helvetica,sans-serif;&quot;&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;u&gt;&lt;b&gt;The need:&lt;/b&gt;&lt;/u&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;font-family: Arial,Helvetica,sans-serif;&quot;&gt;&amp;nbsp;   &amp;nbsp;&amp;nbsp; I don&#39;t know what could be the need of this program but I found this question on many sites and thats why I wrote this program. &lt;/div&gt;&lt;div style=&quot;font-family: Arial,Helvetica,sans-serif;&quot;&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;u&gt;&lt;b&gt;The code:&lt;/b&gt;&lt;/u&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;----------------------------------------------------------&lt;/div&gt;&lt;pre class=&quot;brush: c&quot;&gt;#include&amp;lt;stdio.h&amp;gt;
#include&amp;lt;string.h&amp;gt;

void reverse(char *str,int start, int end)
{
&amp;nbsp;&amp;nbsp; &amp;nbsp;char temp;
&amp;nbsp;&amp;nbsp; &amp;nbsp;int i,j;
&amp;nbsp;&amp;nbsp; &amp;nbsp;i=end-start;
&amp;nbsp;&amp;nbsp; &amp;nbsp;for(j=0;j&amp;lt;=i/2;j++)
&amp;nbsp;&amp;nbsp; &amp;nbsp;{&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;temp=*(str+start+j);
&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;*(str+start+j)=*(str+start+i-j);
&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;*(str+start+i-j)=temp;
&amp;nbsp;&amp;nbsp; &amp;nbsp;}
}

rev_word(char *str)
{
&amp;nbsp;&amp;nbsp; &amp;nbsp;int start=0,end=0,i=0;&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp; &amp;nbsp;while(*(str+i)!=&#39;\0&#39;)
&amp;nbsp;&amp;nbsp; &amp;nbsp;{
&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;if(*(str+i)==&#39; &#39;)
&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;{
&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;end=i-1;
&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;reverse(str, start, end);
&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;i++;
&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;start=i;
&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;}
&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;else
&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;i++;
&amp;nbsp;&amp;nbsp; &amp;nbsp;}
&amp;nbsp;&amp;nbsp; &amp;nbsp;end=i-1;
&amp;nbsp;&amp;nbsp; &amp;nbsp;reverse(str, start, end);
}

int main()
{
&amp;nbsp;&amp;nbsp; &amp;nbsp;char a[]=&quot;I dont know why useless programs are asked&quot;;
&amp;nbsp;&amp;nbsp; &amp;nbsp;printf(&quot;before\n%s\n&quot;,a);
&amp;nbsp;&amp;nbsp; &amp;nbsp;rev_word(a);
&amp;nbsp;&amp;nbsp; &amp;nbsp;printf(&quot;after\n%s\n&quot;,a);
&amp;nbsp;&amp;nbsp; &amp;nbsp;return 0;
}&lt;/pre&gt;&lt;div style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;----------------------------------------------------------&lt;/div&gt;&lt;span style=&quot;font-family: Arial, Helvetica, sans-serif; font-size: large;&quot;&gt;&lt;u&gt;&lt;b&gt;Approach:&lt;/b&gt;&lt;/u&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt; &lt;/span&gt;&amp;nbsp;&amp;nbsp; The approach is simple. make a note of start and end points (indices) of each word and reverse with the help of those points. The UDF reverse() does the job here.&lt;span style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programsimply.blogspot.com/feeds/6112085969197666595/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://programsimply.blogspot.com/2011/12/program-to-reverse-spelling-of-words-in.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/997202589550114592/posts/default/6112085969197666595'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/997202589550114592/posts/default/6112085969197666595'/><link rel='alternate' type='text/html' href='http://programsimply.blogspot.com/2011/12/program-to-reverse-spelling-of-words-in.html' title='program to reverse the spelling of words in a sentence'/><author><name>Ranu Pratap Singh</name><uri>http://www.blogger.com/profile/15360265618857473433</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-997202589550114592.post-587019398385514321</id><published>2011-12-06T19:39:00.007+05:30</published><updated>2012-03-06T03:01:40.189+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="C program to reverse words"/><category scheme="http://www.blogger.com/atom/ns#" term="reverse words in a sentence"/><category scheme="http://www.blogger.com/atom/ns#" term="reversing the character array"/><title type='text'>program to reverse the order of words in a sentence</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;&lt;div style=&quot;font-family: Arial,Helvetica,sans-serif;&quot;&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;u&gt;&lt;b&gt;The need:&lt;/b&gt;&lt;/u&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;font-family: Arial,Helvetica,sans-serif;&quot;&gt;&amp;nbsp;    &amp;nbsp;&amp;nbsp; I don&#39;t know what could be the need of this program but I found  this question on many sites and thats why I wrote this program.This program is extension of previous program.&lt;/div&gt;&lt;div style=&quot;font-family: Arial,Helvetica,sans-serif;&quot;&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;u&gt;&lt;b&gt;The code:&lt;/b&gt;&lt;/u&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;----------------------------------------------------------&lt;/div&gt;&lt;pre class=&quot;brush: c&quot;&gt;#include&amp;lt;stdio.h&amp;gt;
#include&amp;lt;string.h&amp;gt;

void reverse(char *str,int start, int end)
{
&amp;nbsp;&amp;nbsp;&amp;nbsp; char temp;
&amp;nbsp;&amp;nbsp;&amp;nbsp; int i,j;
&amp;nbsp;&amp;nbsp;&amp;nbsp; i=end-start;
&amp;nbsp;&amp;nbsp;&amp;nbsp; for(j=0;j&amp;lt;=i/2;j++)
&amp;nbsp;&amp;nbsp;&amp;nbsp; {&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; temp=*(str+start+j);
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; *(str+start+j)=*(str+start+i-j);
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; *(str+start+i-j)=temp;
&amp;nbsp;&amp;nbsp;&amp;nbsp; }
}

rev_word(char *str)
{
&amp;nbsp;&amp;nbsp;&amp;nbsp; int start=0,end=0,i=0;&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp; while(*(str+i)!=&#39;\0&#39;)
&amp;nbsp;&amp;nbsp;&amp;nbsp; {
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if(*(str+i)==&#39; &#39;)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; end=i-1;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; reverse(str, start, end);
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; i++;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; start=i;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; else
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; i++;
&amp;nbsp;&amp;nbsp;&amp;nbsp; }
&amp;nbsp;&amp;nbsp;&amp;nbsp; end=i-1;
&amp;nbsp;&amp;nbsp;&amp;nbsp; reverse(str, start, end);
}

int main()
{
&amp;nbsp;&amp;nbsp;&amp;nbsp; char a[]=&quot;I dont know why useless programs are asked&quot;;
&amp;nbsp;&amp;nbsp;&amp;nbsp; printf(&quot;before\n%s\n&quot;,a);
&amp;nbsp;&amp;nbsp;&amp;nbsp; rev_word(a);
&amp;nbsp;&amp;nbsp;&amp;nbsp; reverse(a, 0, strlen(a)-1);
&amp;nbsp;&amp;nbsp;&amp;nbsp; printf(&quot;after\n%s\n&quot;,a);
&amp;nbsp;&amp;nbsp;&amp;nbsp; return 0;
}&lt;/pre&gt;&lt;div style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;----------------------------------------------------------&lt;/div&gt;&lt;span style=&quot;font-family: Arial,Helvetica,sans-serif; font-size: large;&quot;&gt;&lt;u&gt;&lt;b&gt;Approach:&lt;/b&gt;&lt;/u&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Arial,Helvetica,sans-serif;&quot;&gt; &lt;/span&gt;&amp;nbsp;&amp;nbsp; The  approach is simple. make a note of start and end points (indices) of  each word and reverse with the help of those points. The UDF reverse()  does the job here. After that reverse the whole sentence character by character. I have used my UDF here because its existing and I dont need to write or search for another function though one could also use strrev() built-in function to reverse the whole sentence character by character.&lt;br /&gt;
&lt;u style=&quot;color: #990000;&quot;&gt;&lt;b&gt;&lt;span style=&quot;font-family: Arial,Helvetica,sans-serif; font-size: large;&quot;&gt;Remarks:&lt;/span&gt;&lt;/b&gt;&lt;/u&gt; &lt;br /&gt;
1. You can modify reverse() function here into ordinary string_reverse (same as built-in strrev) function. For that remove arguments start and end from the function definition and use 0 in place of start and (length of string-1) as end.&lt;br /&gt;
&lt;span style=&quot;font-size: large;&quot;&gt;Other approach:&lt;/span&gt; &lt;br /&gt;
2. Here for doing the whole thing I have used only one extra character other than the provided string (off course I have also used counters etc. but that is different thing). If I was &lt;b&gt;&lt;i&gt;allowed to use as much space as I wanted&lt;/i&gt;&lt;/b&gt;, I would have not thought this much like I have thought here --&amp;gt; reversing the spelling of each word and then reversing the whole string. In that case I would have made use of my stack concepts and using almost no brain I could have got it done. Have a look how simple is this. Steps are:&lt;br /&gt;
1. read word by word (using scanf with %s)&lt;br /&gt;
2. keep on pushing all words on a stack till last word.&lt;br /&gt;
3. Now pop each word and print. :-)&lt;br /&gt;
For this approach have a look at &lt;a href=&quot;http://programsimply.blogspot.com/2011/12/reversing-order-of-words-in-sentence.html&quot; target=&quot;_blank&quot;&gt;next program&lt;/a&gt; on this topic. &lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programsimply.blogspot.com/feeds/587019398385514321/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://programsimply.blogspot.com/2011/12/program-to-reverse-order-of-words-in.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/997202589550114592/posts/default/587019398385514321'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/997202589550114592/posts/default/587019398385514321'/><link rel='alternate' type='text/html' href='http://programsimply.blogspot.com/2011/12/program-to-reverse-order-of-words-in.html' title='program to reverse the order of words in a sentence'/><author><name>Ranu Pratap Singh</name><uri>http://www.blogger.com/profile/15360265618857473433</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-997202589550114592.post-7623382756333677582</id><published>2011-11-23T11:38:00.003+05:30</published><updated>2011-11-23T12:22:05.925+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="C program running time in windows"/><category scheme="http://www.blogger.com/atom/ns#" term="clock() function in C"/><category scheme="http://www.blogger.com/atom/ns#" term="getting execution time of C program"/><category scheme="http://www.blogger.com/atom/ns#" term="getting running time"/><category scheme="http://www.blogger.com/atom/ns#" term="getting running time of C program"/><category scheme="http://www.blogger.com/atom/ns#" term="running time in milli seconds"/><category scheme="http://www.blogger.com/atom/ns#" term="time.h in C"/><title type='text'>Get the execution time of a C program</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;&lt;div style=&quot;font-family: Arial,Helvetica,sans-serif;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;&lt;u&gt;&lt;b&gt;The need:&lt;/b&gt;&lt;/u&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;font-family: Arial,Helvetica,sans-serif;&quot;&gt;&amp;nbsp;  &amp;nbsp;&amp;nbsp; Need is same as previous program which gives you precision of nanoseconds but that program would work only on linux. This program will work on windows also but will give you precision of milli seconds only.&amp;nbsp; All you  need to do is to place your test program code (the code whose execution time is to be measured) in between the comments which indicate the start  and end of the program. &lt;/div&gt;&lt;div style=&quot;font-family: Arial,Helvetica,sans-serif;&quot;&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;&lt;u&gt;&lt;b&gt;The code:&lt;/b&gt;&lt;/u&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;----------------------------------------------------------&lt;/div&gt;&lt;pre class=&quot;brush: c&quot;&gt;/*This program should work on both windows and linux*/
#include&amp;lt;time.h&amp;gt; 
#include&amp;lt;stdio.h&amp;gt;
#include&amp;lt;stdlib.h&amp;gt; 
#define START if ( (start_time = clock()) == -1) {printf(&quot;Could not call clock&quot;);exit(1);} 
#define STOP if ( (stop_time = clock()) == -1) {printf(&quot;Could call clock&quot;);exit(1);} 
#define PRINT_TIME_DIFF printf( &quot;Your program took %.3f seconds.\n&quot;, ((double)stop_time-start_time)/CLOCKS_PER_SEC); 
&amp;nbsp;&amp;nbsp;&amp;nbsp; 
int main()
{ 
&amp;nbsp;&amp;nbsp;&amp;nbsp; clock_t start_time, stop_time;
&amp;nbsp;&amp;nbsp;&amp;nbsp; int i=0; 
&amp;nbsp;&amp;nbsp;&amp;nbsp; START
&amp;nbsp;&amp;nbsp;&amp;nbsp; //test program code begins
&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp; //test program code ends 
&amp;nbsp;&amp;nbsp;&amp;nbsp; STOP 
&amp;nbsp;&amp;nbsp;&amp;nbsp; PRINT_TIME_DIFF
&amp;nbsp;&amp;nbsp;&amp;nbsp; return 0; 
} &lt;/pre&gt;&lt;div style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;----------------------------------------------------------&lt;/div&gt;&lt;span style=&quot;font-family: Arial,Helvetica,sans-serif; font-size: medium;&quot;&gt;&lt;u&gt;&lt;b&gt;Approach:&lt;/b&gt;&lt;/u&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Arial,Helvetica,sans-serif;&quot;&gt; &lt;/span&gt;&amp;nbsp;&amp;nbsp; The approach is simple. There is a built-in function clock() in &amp;lt;time.h&amp;gt; header file. &lt;span style=&quot;font-family: Arial,Helvetica,sans-serif;&quot;&gt;In this program I have use macros to differentiate between your test code and the statement which would be used to print the time and other things. I thought this would simplify the look of the program a little bit.&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Arial,Helvetica,sans-serif;&quot;&gt; &lt;/span&gt;&lt;u style=&quot;color: #990000; font-family: Arial,Helvetica,sans-serif;&quot;&gt;&lt;b&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;Remarks:&lt;/span&gt;&lt;/b&gt;&lt;/u&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Arial,Helvetica,sans-serif;&quot;&gt; 1. This program should work on both windows and linux.&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Arial,Helvetica,sans-serif;&quot;&gt; 2. This program will give accuracy of (1/1000)th part of a second.&lt;/span&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programsimply.blogspot.com/feeds/7623382756333677582/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://programsimply.blogspot.com/2011/11/get-execution-time-of-c-program.html#comment-form' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/997202589550114592/posts/default/7623382756333677582'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/997202589550114592/posts/default/7623382756333677582'/><link rel='alternate' type='text/html' href='http://programsimply.blogspot.com/2011/11/get-execution-time-of-c-program.html' title='Get the execution time of a C program'/><author><name>Ranu Pratap Singh</name><uri>http://www.blogger.com/profile/15360265618857473433</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-997202589550114592.post-4775607480416869036</id><published>2011-11-22T16:53:00.006+05:30</published><updated>2011-11-23T12:20:06.051+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="getting execution time of C program"/><category scheme="http://www.blogger.com/atom/ns#" term="getting running time"/><category scheme="http://www.blogger.com/atom/ns#" term="getting running time of C program"/><category scheme="http://www.blogger.com/atom/ns#" term="running time in micro seconds"/><category scheme="http://www.blogger.com/atom/ns#" term="running time in nano seconds"/><category scheme="http://www.blogger.com/atom/ns#" term="time.h in C"/><category scheme="http://www.blogger.com/atom/ns#" term="timespec structure"/><title type='text'>Getting running time of a C program</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;&lt;div style=&quot;font-family: Arial,Helvetica,sans-serif;&quot;&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;u&gt;&lt;b&gt;The need:&lt;/b&gt;&lt;/u&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;font-family: Arial,Helvetica,sans-serif;&quot;&gt;&amp;nbsp; &amp;nbsp;&amp;nbsp; Need is obvious.Whenever you are curious to find running time of a program or you are asked to do so, you can use this program. All you need to do is to place your program code(the code between the brackets of main() function ) in between the comments which indicate the start and end of the program. &lt;/div&gt;&lt;div style=&quot;font-family: Arial,Helvetica,sans-serif;&quot;&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;u&gt;&lt;b&gt;The code:&lt;/b&gt;&lt;/u&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;----------------------------------------------------------&lt;/div&gt;&lt;pre class=&quot;brush: c&quot;&gt;/*This program is intended to work on linux*/
#include &amp;lt;stdio.h&amp;gt; 
#include &amp;lt;time.h&amp;gt; 
void print_time_diff(struct timespec begin,struct timespec end);
int main ( void )
{
&amp;nbsp;&amp;nbsp;&amp;nbsp; struct timespec start, end;
&amp;nbsp;&amp;nbsp;&amp;nbsp; gettimeofday(&amp;amp;start, NULL);
&amp;nbsp;&amp;nbsp;&amp;nbsp; //program code begins
&amp;nbsp;&amp;nbsp;&amp;nbsp; //program code ends
&amp;nbsp;&amp;nbsp;&amp;nbsp; gettimeofday(&amp;amp;end, NULL);
&amp;nbsp;&amp;nbsp;&amp;nbsp; print_time_diff(start,end);
&amp;nbsp;&amp;nbsp;&amp;nbsp; return 0;
}

void print_time_diff(struct timespec begin,struct timespec end)
{
&amp;nbsp;&amp;nbsp;&amp;nbsp; unsigned int sec,nanosec;
&amp;nbsp;&amp;nbsp;&amp;nbsp; if (end.tv_nsec&amp;lt;begin.tv_nsec) {
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; sec = end.tv_sec-begin.tv_sec-1;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; nanosec = 1000000000+end.tv_nsec-begin.tv_nsec;
&amp;nbsp;&amp;nbsp;&amp;nbsp; } else {
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; sec = end.tv_sec-begin.tv_sec;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; nanosec = end.tv_nsec-begin.tv_nsec;
&amp;nbsp;&amp;nbsp;&amp;nbsp; }
&amp;nbsp;&amp;nbsp;&amp;nbsp; printf(&quot;your program took %d seconds and %u nanoseconds to run.\n&quot;,sec,nanosec);
} &lt;/pre&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;----------------------------------------------------------&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: large;&quot;&gt;&lt;span style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;u&gt;&lt;b&gt;Approach:&lt;/b&gt;&lt;/u&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;&lt;span style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&amp;nbsp;&amp;nbsp; The approach is simple. There is a built-in st&lt;/span&gt;&lt;/span&gt;ructure timespec in &amp;lt;time.h&amp;gt; header file of linux.The built-in definition of structure is:&lt;br /&gt;
struct timespec&lt;br /&gt;
&amp;nbsp; {&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; __time_t tv_sec;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; /* Seconds.&amp;nbsp; */&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; long int tv_nsec;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; /* Nanoseconds.&amp;nbsp; */&lt;br /&gt;
&amp;nbsp; };&lt;br /&gt;
The program declares 2 struct variables start and end. built-in function gettimeofday() fills the struct variable passed to it with suitable values at that particular instance. This implies that start gets the value of time when your program (code to be measured) is just about to start and end gets the value of time as soon as your program finishes.&lt;br /&gt;
print_time_diff() is user defined function which prints the difference of the two times passed to it as arguments.&lt;br /&gt;
&lt;u style=&quot;color: #990000;&quot;&gt;&lt;b&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;span style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;Remarks:&lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;/u&gt;&lt;br /&gt;
1. This program should not work on windows and operating systems other than linux.&lt;br /&gt;
2. The program which would work on windows will be posted shortly.&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programsimply.blogspot.com/feeds/4775607480416869036/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://programsimply.blogspot.com/2011/11/getting-running-time-of-c-program.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/997202589550114592/posts/default/4775607480416869036'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/997202589550114592/posts/default/4775607480416869036'/><link rel='alternate' type='text/html' href='http://programsimply.blogspot.com/2011/11/getting-running-time-of-c-program.html' title='Getting running time of a C program'/><author><name>Ranu Pratap Singh</name><uri>http://www.blogger.com/profile/15360265618857473433</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-997202589550114592.post-1603981301654747515</id><published>2011-11-22T15:10:00.004+05:30</published><updated>2011-11-22T15:57:58.146+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="concept of heap"/><category scheme="http://www.blogger.com/atom/ns#" term="heap and array"/><category scheme="http://www.blogger.com/atom/ns#" term="heap sample program"/><category scheme="http://www.blogger.com/atom/ns#" term="heap sort"/><category scheme="http://www.blogger.com/atom/ns#" term="implementation of heap"/><title type='text'>Heap sort in C</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;u style=&quot;font-family: Arial,Helvetica,sans-serif;&quot;&gt;&lt;b&gt;The need:&lt;/b&gt;&lt;/u&gt;&lt;/span&gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp; Heap sort is a better than bubble, selection and insertion algorithm for sorting. To show how heap can be useful in real life implementation.&lt;br /&gt;
&lt;div style=&quot;font-family: Arial,Helvetica,sans-serif;&quot;&gt;&lt;u&gt;&lt;b&gt;&lt;span style=&quot;font-size: large;&quot;&gt;The code:&lt;/span&gt;&lt;/b&gt;&lt;/u&gt;&lt;/div&gt;&lt;div style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;-----------------------------------------------------------&lt;/div&gt;&lt;pre class=&quot;brush: c&quot;&gt;/*this program is an array implementation of heap*/ 
#include&amp;lt;stdio.h&amp;gt; 
#include&amp;lt;string.h&amp;gt; 
#include&amp;lt;stdlib.h&amp;gt; 

void print_array(int a[],int array_size); 
void heap_sort(int a[],int array_size); 
void heapify(int a[],int count); 
void adjust_down(int a[],int start, int end); 

main() 
{ 
    int i,arr[50],size=0; 
    arr[0]=0; 
    printf(&quot;how many elements do you want to enter\n&quot;);
    scanf(&quot;%d&quot;,&amp;amp;size); 
    if(size&amp;lt;1)
    {
        printf(&quot;You did not enter a valid count of elements.\n&quot;);
        printf(&quot;Rerun the program and try again\n&quot;);
        exit(0);
    }
    printf(&quot;enter array\n&quot;); 
    for(i=1;i&amp;lt;=size;i++) 
        scanf(&quot;%d&quot;,&amp;amp;arr[i]);
    printf(&quot;the array before heap sort.\n&quot;); 
    print_array(arr,size); 
    heap_sort(arr,size);
    printf(&quot;the array after heap sort.\n&quot;); 
    print_array(arr,size);    
} 

void heap_sort(int a[],int array_size)
{
    int i,j,heap_end,temp;
    heap_end=array_size;
    heapify(a,array_size);
    while(heap_end&amp;gt;2)
    {
        temp=a[1];
        a[1]=a[heap_end];
        a[heap_end]=temp;
        heap_end--;
        adjust_down(a, 1, heap_end);
    }
}

void heapify(int a[],int count) 
{ 
    int start;
    start = count/2 + 1;
    while(start&amp;gt;0)
    {
        adjust_down(a, start, count);
        start--;
    } 
} 

void adjust_down(int a[],int start, int end)
{
    int root = start,temp,bigger_child;
    while(2*root &amp;lt;= end)
    {
        if(2*root+1 &amp;gt; end)
        {
            if(a[2*root]&amp;lt;a[root])
            {
                temp=a[root];
                a[root]=a[2*root];
                a[2*root]=temp;
            }
            root=2*root;
        }
        else
        {
            bigger_child = (a[2*root]&amp;gt;a[2*root+1])? 2*root : 2*root+1;
            if(a[root]&amp;lt;a[bigger_child])
            {
                temp=a[root];
                a[root]=a[bigger_child];
                a[bigger_child]=temp;
            }
            root=bigger_child;
        }
    }
}

void print_array(int x[],int size) 
{ 
    int i; 
    for(i=0;i&amp;lt;size;i++) 
        printf(&quot;%d\t&quot;,x[i+1]); 
    putchar(&#39;\n&#39;); 
} &lt;/pre&gt;&lt;div style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;-----------------------------------------------------------&lt;/div&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Arial,Helvetica,sans-serif; font-size: large;&quot;&gt;&lt;u&gt;&lt;b&gt;The approach:&lt;/b&gt;&lt;/u&gt;&lt;/span&gt;&lt;br /&gt;
&lt;div style=&quot;font-family: Arial,Helvetica,sans-serif;&quot;&gt;&lt;span style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&amp;nbsp; The approach is simple. This program uses a max heap to sort the numbers in ascending order. You know that in a max heap root is largest element. This program works on the below logic:&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;font-family: Arial,Helvetica,sans-serif;&quot;&gt;&lt;span style=&quot;font-size: small;&quot;&gt;1. first take an array from user. Notice that I have saved the elements from index one leaving index 0 empty. See remark1 below.&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;font-family: Arial,Helvetica,sans-serif;&quot;&gt;&lt;span style=&quot;font-size: small;&quot;&gt;2. make that array to satisfy max heap property(max - heapify array).&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;font-family: Arial,Helvetica,sans-serif;&quot;&gt;&lt;span style=&quot;font-size: small;&quot;&gt;3. Now swap last element of heap with the root. So that the biggest element comes last.&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;font-family: Arial,Helvetica,sans-serif;&quot;&gt;&lt;span style=&quot;font-size: small;&quot;&gt;4. Now leave the last element and assume rest array as heap.&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;font-family: Arial,Helvetica,sans-serif;&quot;&gt;&lt;span style=&quot;font-size: small;&quot;&gt;5. Adjust the new heap so satisfy max heap property.&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;font-family: Arial,Helvetica,sans-serif;&quot;&gt;&lt;span style=&quot;font-size: small;&quot;&gt;6. Repeat step 3-5 till only root is left as rest part of heap.&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;font-family: Arial,Helvetica,sans-serif;&quot;&gt;&lt;span style=&quot;font-size: small;&quot;&gt;7. You will be getting an increasingly sorted array.&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;font-family: Arial,Helvetica,sans-serif;&quot;&gt;&lt;span style=&quot;font-size: small;&quot;&gt;you can refer to &lt;a href=&quot;http://en.wikipedia.org/wiki/Heapsort&quot; target=&quot;_blank&quot;&gt;wikipedia&lt;/a&gt; for more theory and graphics about heap sort.&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;font-family: Arial,Helvetica,sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;font-family: Arial,Helvetica,sans-serif;&quot;&gt;&lt;span style=&quot;font-size: small;&quot;&gt;&lt;span style=&quot;color: #990000; font-size: large;&quot;&gt;&lt;u&gt;&lt;b&gt;Remark:&lt;/b&gt;&lt;/u&gt;&lt;/span&gt; &lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;font-family: Arial,Helvetica,sans-serif;&quot;&gt;&lt;span style=&quot;font-size: small;&quot;&gt;1.  This is the convention I follow when I implement heap on an array  because this gets you left and right children as 2*current_node, and  (2*current_node + 1). You can use other convention of starting with index 0. In that case  left and right children will be evaluated (2*current_node+1), and  (2*current_node + 2) which seems a little bigger expressions to me :P&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programsimply.blogspot.com/feeds/1603981301654747515/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://programsimply.blogspot.com/2011/11/heap-sort-in-c.html#comment-form' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/997202589550114592/posts/default/1603981301654747515'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/997202589550114592/posts/default/1603981301654747515'/><link rel='alternate' type='text/html' href='http://programsimply.blogspot.com/2011/11/heap-sort-in-c.html' title='Heap sort in C'/><author><name>Ranu Pratap Singh</name><uri>http://www.blogger.com/profile/15360265618857473433</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-997202589550114592.post-3849416396421386404</id><published>2011-11-22T14:51:00.001+05:30</published><updated>2011-12-13T11:34:58.139+05:30</updated><title type='text'>Concept of Heap in C</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;Heap is a concept which can be implemented on trees, priority queues and arrays. When we plot a heap on paper, we draw it as binary tree. We also analyse a heap as a binary tree. There are 2 types of heaps:&lt;br /&gt;
1. min heap: parent node is always less than both children.&lt;br /&gt;
2. max heap: parent node is always greater than both children.&lt;br /&gt;
In both the cases equality is permitted. No other property is required for a heap. We do not care about any other thing like which child is greater.&lt;br /&gt;
&lt;br /&gt;
Heap can be utilized in &lt;br /&gt;
1. heapsort&lt;br /&gt;
2. a priority queue can be implemented with a heap&lt;br /&gt;
3. auto balancing binary tree&lt;br /&gt;
&lt;br /&gt;
For general purposes this much on heap is enough. For more you can study in details on &lt;a href=&quot;http://en.wikipedia.org/wiki/Heap_%28data_structure%29&quot; target=&quot;_blank&quot;&gt;wikipedia&lt;/a&gt;.&lt;br /&gt;
&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programsimply.blogspot.com/feeds/3849416396421386404/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://programsimply.blogspot.com/2011/11/concept-of-heap-in-c.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/997202589550114592/posts/default/3849416396421386404'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/997202589550114592/posts/default/3849416396421386404'/><link rel='alternate' type='text/html' href='http://programsimply.blogspot.com/2011/11/concept-of-heap-in-c.html' title='Concept of Heap in C'/><author><name>Ranu Pratap Singh</name><uri>http://www.blogger.com/profile/15360265618857473433</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-997202589550114592.post-5663732001060353349</id><published>2011-10-16T00:30:00.002+05:30</published><updated>2012-01-08T01:24:39.874+05:30</updated><title type='text'>Creating and traversing a binary search tree</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot;&gt;&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot;&gt;&lt;div style=&quot;font-family: Arial,Helvetica,sans-serif;&quot;&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;u&gt;&lt;b&gt;The need:&lt;/b&gt;&lt;/u&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;font-family: Arial,Helvetica,sans-serif;&quot;&gt;&amp;nbsp;     &amp;nbsp;&amp;nbsp; This program is written to show creation and various types of printing of a binary search tree. This program takes values and put them in a binary search tree and then prints the resulting tree in preorder, inorder and post order.&lt;br /&gt;
&amp;nbsp;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;u&gt;&lt;b&gt;The BST(Binary Search tree):&lt;/b&gt;&lt;/u&gt;&lt;span style=&quot;font-size: small;&quot;&gt; a binary search tree is a binary tree in which value of each node is greater or equal than that of its left child and less than that of its right child. Hence all nodes in the left sub-tree of a node are less than or equal to the value of that node and &lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;span style=&quot;font-size: small;&quot;&gt;all nodes in the right sub-tree of a node are greater than the value of that node&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;span style=&quot;font-size: small;&quot;&gt; if it is a binary search tree. We&#39;ll use this property while inserting a node in the BST in insert() function.&lt;/span&gt;&lt;u&gt;&lt;b&gt;&lt;br /&gt;
&lt;/b&gt;&lt;/u&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;font-family: Arial,Helvetica,sans-serif;&quot;&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;u&gt;&lt;b&gt;The code:&lt;/b&gt;&lt;/u&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;----------------------------------------------------------&lt;/div&gt;&lt;pre class=&quot;brush: c&quot;&gt;#define MAX 10
#define DEBUG if(!(root-&amp;gt;left))    printf(&quot;inserted %d to the left of %d\n&quot;,x,root-&amp;gt;data);
#include&amp;lt;stdio.h&amp;gt;
#include&amp;lt;stdlib.h&amp;gt;
struct tree_node
{
    int data;
    struct tree_node *left,*right,*parent;
};
typedef struct tree_node node;
node * new_node(int x);
node * insert_node(node *root, int x);
void print_preorder(node *root);
void print_inorder(node *root);
void print_postorder(node *root);

int main()
{
    node *root=NULL;
    int i;
    int arr[]={5,6,1,9,0,4,2,3,8,7};
    for(i=0;i&amp;lt;MAX;i++)
    root = insert_node(root, arr[i]);
    printf(&quot;\ntree pre order is\n&quot;);
    print_preorder(root);
    printf(&quot;\n\ntree in order is\n&quot;);
    print_inorder(root);
    printf(&quot;\n\ntree post order is\n&quot;);
    print_postorder(root);
    return 0;
}

node * insert_node(node *root, int x)
{
    if(!root)
    {
        root = new_node(x);
        return root;
    }
    if(root-&amp;gt;data &amp;gt; x)
    {
//        DEBUG
        root-&amp;gt;left = insert_node(root-&amp;gt;left,x);
    }
    else
    {
//        DEBUG
        root-&amp;gt;right = insert_node(root-&amp;gt;right,x);
    }
    return root;
}

void print_preorder(node *root)
{
    if(!root)
    return;
    printf(&quot;%d &quot;,root-&amp;gt;data);
    print_preorder(root-&amp;gt;left);
    print_preorder(root-&amp;gt;right);
    return;
}

void print_inorder(node *root)
{
    if(!root)
    return;
    print_inorder(root-&amp;gt;left);
    printf(&quot;%d &quot;,root-&amp;gt;data);
    print_inorder(root-&amp;gt;right);
    return;
}

void print_postorder(node *root)
{
    if(!root)
    return;
    print_postorder(root-&amp;gt;left);
    print_postorder(root-&amp;gt;right);
    printf(&quot;%d &quot;,root-&amp;gt;data);
    return;
}

node * new_node(int x)
{
    node *furnished;
    furnished = (node*)malloc(sizeof(node));
    furnished-&amp;gt;data=x;
    furnished-&amp;gt;left=NULL;
    furnished-&amp;gt;right=NULL;
    furnished-&amp;gt;parent=NULL;
    return furnished;
}&lt;/pre&gt;&lt;div style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;----------------------------------------------------------&lt;/div&gt;&lt;span style=&quot;font-family: Arial,Helvetica,sans-serif; font-size: large;&quot;&gt;&lt;u&gt;&lt;b&gt;Approach:&lt;/b&gt;&lt;/u&gt;&lt;/span&gt;&lt;br /&gt;
&lt;div style=&quot;font-family: Arial,Helvetica,sans-serif;&quot;&gt;&lt;span style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;  This program is just to show the printing of a tree in pre-order,  inorder and post order. For that first we have to make the tree. Here we dont have tree with 3  nodes necessarily. So we need to make a function to construct the tree. Here insert() function does the job.  create_node() function creates a node with given data/value ans returns  it address. we create first node, make it root. After that we create 2  more nodes and make them left and right children of root. Then there are  3 functions in_print(), pre_print() and post_print() which print the  expression in infix, prefix and postfix form respectively.&lt;/span&gt;&lt;/div&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;u style=&quot;color: #990000;&quot;&gt;&lt;b&gt;&lt;span style=&quot;font-family: Arial,Helvetica,sans-serif;&quot;&gt;Remarks:&lt;/span&gt;&lt;/b&gt;&lt;/u&gt;&lt;/span&gt; &lt;br /&gt;
&lt;div style=&quot;font-family: Arial,Helvetica,sans-serif;&quot;&gt;1. We noticed that here in each function I have used recursion like I have already said in previous posts that recursion makes our life easier in trees.&lt;br /&gt;
2. Uncomment DEBUG to see where the nodes are being inserted.&lt;br /&gt;
3. Here I am taking node data from an array we can take data from user also.&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programsimply.blogspot.com/feeds/5663732001060353349/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://programsimply.blogspot.com/2011/10/creating-and-traversing-tree.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/997202589550114592/posts/default/5663732001060353349'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/997202589550114592/posts/default/5663732001060353349'/><link rel='alternate' type='text/html' href='http://programsimply.blogspot.com/2011/10/creating-and-traversing-tree.html' title='Creating and traversing a binary search tree'/><author><name>Ranu Pratap Singh</name><uri>http://www.blogger.com/profile/15360265618857473433</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-997202589550114592.post-6481382247490472893</id><published>2011-10-14T23:09:00.002+05:30</published><updated>2012-01-08T00:12:37.033+05:30</updated><title type='text'>Inorder, Preorder and Post-Order traversal of tree</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot;&gt;&lt;div style=&quot;font-family: Arial,Helvetica,sans-serif;&quot;&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;u&gt;&lt;b&gt;The need:&lt;/b&gt;&lt;/u&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;font-family: Arial,Helvetica,sans-serif;&quot;&gt;&amp;nbsp;     &amp;nbsp;&amp;nbsp; This program is written to show the program implementation of the concept explained in the &lt;a href=&quot;http://programsimply.blogspot.com/2011/09/inorder-preorder-and-post-order.html&quot; target=&quot;_blank&quot;&gt;previous post&lt;/a&gt;. This program is the implementation of 2 + 3 example given there.&lt;/div&gt;&lt;div style=&quot;font-family: Arial,Helvetica,sans-serif;&quot;&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;u&gt;&lt;b&gt;The code:&lt;/b&gt;&lt;/u&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;----------------------------------------------------------&lt;/div&gt;&lt;pre class=&quot;brush: c&quot;&gt;#include&amp;lt;stdio.h&amp;gt;
#include&amp;lt;stdlib.h&amp;gt;
struct tree
{
    char num;
    struct tree *left, *right, *parent;
};
typedef struct tree node;

void in_print(node *p);
void pre_print(node *p);
void post_print(node *p);
node * create_node(char c);
int main()
{
    int i;
    node *root=NULL;
    root = create_node(&#39;+&#39;);
    root-&amp;gt;left = create_node(&#39;a&#39;);
    root-&amp;gt;right = create_node(&#39;b&#39;);
    printf(&quot;Pre order print\t\t&quot;);
    pre_print(root);
    printf(&quot;\nIn order print\t\t&quot;);
    in_print(root);
    printf(&quot;\nPost order print\t&quot;);
    post_print(root);
    putchar(&#39;\n&#39;);
    return 0;
}

node * create_node(char c)
{
    node *new_node;
    new_node = (node *)malloc(sizeof(node));
    new_node-&amp;gt;num = c;
    new_node-&amp;gt;parent = NULL;
    new_node-&amp;gt;left = NULL;
    new_node-&amp;gt;right = NULL;
    return new_node;
}

void pre_print(node *p)
{
    if(p==NULL)
    return;
    else
    printf(&quot;%c &quot;,p-&amp;gt;num);
    pre_print(p-&amp;gt;left);
    pre_print(p-&amp;gt;right);
}

void in_print(node *p)
{
    if(p==NULL)
    return;
    else
    in_print(p-&amp;gt;left);
    printf(&quot;%c &quot;,p-&amp;gt;num);
    in_print(p-&amp;gt;right);
}

void post_print(node *p)
{
    if(p==NULL)
    return;
    else
    post_print(p-&amp;gt;left);
    post_print(p-&amp;gt;right);
    printf(&quot;%c &quot;,p-&amp;gt;num);
}&lt;/pre&gt;&lt;div style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;----------------------------------------------------------&lt;/div&gt;&lt;span style=&quot;font-family: Arial,Helvetica,sans-serif; font-size: large;&quot;&gt;&lt;u&gt;&lt;b&gt;Approach:&lt;/b&gt;&lt;/u&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;&lt;span style=&quot;font-family: Arial,Helvetica,sans-serif;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; This program is just to show the printing of a tree in pre-order, inorder and post order. For that first we have to make the tree. Here I have not created a bigger tree. Here we have the simplest tree with 3 nodes only. So we need not make a function to construct the tree. Here simply I have created 3 nodes and put them to make a small tree. create_node() function creates a node with given data/value ans returns it address. we create first node, make it root. After that we create 2 more nodes and make them left and right children of root. Then there are 3 functions in_print(), pre_print() and post_print() which print the expression in infix, prefix and postfix form respectively.&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: large;&quot;&gt;&lt;u style=&quot;color: #990000;&quot;&gt;&lt;b&gt;&lt;span style=&quot;font-family: Arial,Helvetica,sans-serif;&quot;&gt;Remarks:&lt;/span&gt;&lt;/b&gt;&lt;/u&gt;&lt;/span&gt; &lt;br /&gt;
&lt;div style=&quot;font-family: Arial,Helvetica,sans-serif;&quot;&gt;1. Here this program is shown to print the simplest expression in prefix, infix and post-fix form but this program can be used to print any binary tree in pre-order, inorder and post order.&lt;/div&gt;&lt;div style=&quot;font-family: Arial,Helvetica,sans-serif;&quot;&gt;2. This program is the base of all the tree programs related to trees.&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programsimply.blogspot.com/feeds/6481382247490472893/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://programsimply.blogspot.com/2011/10/inorder-preorder-and-post-order.html#comment-form' title='4 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/997202589550114592/posts/default/6481382247490472893'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/997202589550114592/posts/default/6481382247490472893'/><link rel='alternate' type='text/html' href='http://programsimply.blogspot.com/2011/10/inorder-preorder-and-post-order.html' title='Inorder, Preorder and Post-Order traversal of tree'/><author><name>Ranu Pratap Singh</name><uri>http://www.blogger.com/profile/15360265618857473433</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>4</thr:total></entry><entry><id>tag:blogger.com,1999:blog-997202589550114592.post-7266125037005228769</id><published>2011-10-12T14:23:00.000+05:30</published><updated>2011-12-13T11:38:38.959+05:30</updated><title type='text'>Inorder, Preorder and Post-Order traversal : An overview</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;&lt;u&gt;&lt;b&gt;&lt;span style=&quot;font-family: Arial,Helvetica,sans-serif; font-size: large;&quot;&gt;The Idea:&lt;/span&gt;&lt;/b&gt;&lt;/u&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Arial,Helvetica,sans-serif;&quot;&gt;Lets first have a look at some mathematical side. The Infix, Prefix and Post-fix notations are exactly analogous to the In-order, Preorder and Post-order traversal of trees.&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Arial,Helvetica,sans-serif;&quot;&gt;Have a look at the expresstion:&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Arial,Helvetica,sans-serif;&quot;&gt;a + b&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Arial,Helvetica,sans-serif;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; This is a familiar expression to all of us.&lt;/span&gt;&lt;span style=&quot;font-family: Arial,Helvetica,sans-serif;&quot;&gt; Here + is an operator with a and b as operands.&lt;/span&gt;&lt;span style=&quot;font-family: Arial,Helvetica,sans-serif;&quot;&gt; This is one way to write sum of a and b. This way is called &lt;b&gt;in-fix notation&lt;/b&gt;. In this way we write operator in between the operands. This is the only used method for writing expressions for human understanding. Thats why everybody is familiar to this method. But this is not the only way to be used for every purpose. there are 2 more ways.&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Arial,Helvetica,sans-serif;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;b&gt;Prefix notation:&lt;/b&gt; In this way of writing expressions the operator comes first which is followed by the operators. The same expression can be written in Prefix notation as:&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Arial,Helvetica,sans-serif;&quot;&gt;+ a b&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Arial,Helvetica,sans-serif;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;b&gt;Postfix notation:&lt;/b&gt;  In this way of writing expressions the operator comes last after both the operands have been written. The same expression can be written in Prefix  notation as:&lt;/span&gt;&lt;span style=&quot;font-family: Arial,Helvetica,sans-serif;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Arial,Helvetica,sans-serif;&quot;&gt; a b &lt;/span&gt;+ &lt;br /&gt;
&lt;span style=&quot;font-family: Arial,Helvetica,sans-serif;&quot;&gt;&lt;/span&gt;&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh-AStKLZ5I95Bs7LQjkPJiMtxIp_194v5WU4S_aNnZoWTffTge2YXw7YzZNBr2WWvT8ld1SEDHOy6NjiGwGzd99fuPgAf8oRc5hcUhiIwVmdaZ9yEfpcHK5e642W2wMhujrWoJ_o6NJK8/s1600/images.jpg&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh-AStKLZ5I95Bs7LQjkPJiMtxIp_194v5WU4S_aNnZoWTffTge2YXw7YzZNBr2WWvT8ld1SEDHOy6NjiGwGzd99fuPgAf8oRc5hcUhiIwVmdaZ9yEfpcHK5e642W2wMhujrWoJ_o6NJK8/s1600/images.jpg&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;span style=&quot;font-family: Arial,Helvetica,sans-serif;&quot;&gt;Analogy comes from here. Have a look at the above image.&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Arial,Helvetica,sans-serif;&quot;&gt;Its a tree with + at parent node and 2 and 3 as child nodes. Now we have&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Arial,Helvetica,sans-serif;&quot;&gt;Infix: 2 + 3&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Arial,Helvetica,sans-serif;&quot;&gt;Prefix: + 2 3&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Arial,Helvetica,sans-serif;&quot;&gt;postfix: 2 3 + &lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Arial,Helvetica,sans-serif;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; We see that it is the position of operator which changes and decides which fix notation is this. The relative positions of operands are always same.&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Arial,Helvetica,sans-serif;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; This is about expressions writing. Now lets move to traversal of trees in data-structures topic. In the same manner as in the case of expressions in trees also &lt;/span&gt;&lt;span style=&quot;font-family: Arial,Helvetica,sans-serif;&quot;&gt;&lt;i style=&quot;font-family: Verdana,sans-serif;&quot;&gt;&lt;u&gt;left child will always be accessed before the right. It is the root which decides which type of traversal it is&lt;/u&gt;&lt;/i&gt;.&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Arial,Helvetica,sans-serif;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;b&gt;InOrder Traversal:&lt;/b&gt;  In this way of traversing tree the root is accessed first after that the children are accessed.&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Arial,Helvetica,sans-serif;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;b&gt;PreOrder Traversal:&lt;/b&gt; &lt;/span&gt;&lt;span style=&quot;font-family: Arial,Helvetica,sans-serif;&quot;&gt;In this way of traversing tree one child is accessed first and then the root is accessed after that the other child is accessed.&lt;/span&gt; &lt;br /&gt;
&lt;span style=&quot;font-family: Arial,Helvetica,sans-serif;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;b&gt;PostOrder Traversal:&lt;/b&gt;  In this way of traversing tree the root is accessed after both of the children are accessed.&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Arial,Helvetica,sans-serif;&quot;&gt;&amp;nbsp;&amp;nbsp; here this concept is explained with the basic tree consisting of only 3 nodes but this holds true for any tree(binary tree). Recursion will play very important role in tree traversal. We can store a mathematical expression in tree and then that can be printed in any order using inorder, preorder and postorder traversal giving infix, prefix and post-fix form of that expression respectively. Obviously this is not the only use of trees.&lt;/span&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programsimply.blogspot.com/feeds/7266125037005228769/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://programsimply.blogspot.com/2011/09/inorder-preorder-and-post-order.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/997202589550114592/posts/default/7266125037005228769'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/997202589550114592/posts/default/7266125037005228769'/><link rel='alternate' type='text/html' href='http://programsimply.blogspot.com/2011/09/inorder-preorder-and-post-order.html' title='Inorder, Preorder and Post-Order traversal : An overview'/><author><name>Ranu Pratap Singh</name><uri>http://www.blogger.com/profile/15360265618857473433</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh-AStKLZ5I95Bs7LQjkPJiMtxIp_194v5WU4S_aNnZoWTffTge2YXw7YzZNBr2WWvT8ld1SEDHOy6NjiGwGzd99fuPgAf8oRc5hcUhiIwVmdaZ9yEfpcHK5e642W2wMhujrWoJ_o6NJK8/s72-c/images.jpg" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-997202589550114592.post-7290358712727643242</id><published>2011-10-11T12:39:00.000+05:30</published><updated>2011-12-13T11:39:51.588+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="C concepts"/><category scheme="http://www.blogger.com/atom/ns#" term="C program"/><category scheme="http://www.blogger.com/atom/ns#" term="C tutorial"/><category scheme="http://www.blogger.com/atom/ns#" term="data structure in C"/><category scheme="http://www.blogger.com/atom/ns#" term="Linked list"/><category scheme="http://www.blogger.com/atom/ns#" term="nodes"/><category scheme="http://www.blogger.com/atom/ns#" term="Programming in C"/><category scheme="http://www.blogger.com/atom/ns#" term="Simple C programs"/><category scheme="http://www.blogger.com/atom/ns#" term="trees in C"/><title type='text'></title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;&lt;u&gt;&lt;b&gt;Concept of trees in C:&lt;a class=&quot;jewelButton&quot; data-target=&quot;fbMessagesFlyout&quot; href=&quot;http://www.blogger.com/post-edit.g?blogID=997202589550114592&amp;amp;postID=7290358712727643242&quot; name=&quot;messages&quot; rel=&quot;toggle&quot;&gt;&lt;span class=&quot;jewelCount&quot; id=&quot;messagesCountWrapper&quot;&gt;&lt;span id=&quot;messagesCountValue&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;/a&gt;&lt;/b&gt;&lt;/u&gt;&lt;br /&gt;
&lt;br /&gt;
The concept of trees is same to real world tree. In real word every tree has roots, and can have many branches. Each branch can have sub-branches. If you look at branches and sub-branches as a whole, the combination looks like a tree (lets call it a sub-tree). Same is the case of a tree in programming jargon. There is only one difference in plotting the trees. A real world tree has roots on the downside while in C programming, a tree is upside down which has roots on the top and sub-trees below that. Tree is a variation of linked lists.&lt;br /&gt;
&lt;br /&gt;
Here in programming the most commonly used tree is a binary tree. Now lets come to data-structural jargon. A binary tree is a tree in a tree (data structure) in which each node (say current node) has addresses of 2 next possible nodes. The current node is called parent node and these next 2 nodes are called children of that node. We&#39;ll call them left child and right child. From the current node, if you want to come down, you have 2 options (either you can come on left child or the right child).&lt;br /&gt;
&lt;br /&gt;
You people have seen that we can traverse a singly linked list  in one direction and doubly linked list in two directions in the  similar way in a tree you can reach max three nodes from a current node.    So in a binary tree each node has only 2 address pointers, you can traverse only in the down direction. If you want to traverse the tree in both the directions (downward as well as upward), each node must be having address of its parent node also.&lt;br /&gt;
&lt;br /&gt;
Here the node  structure is a bit different. You can think of a binary tree as a continuation  of linked lists from singly to doubly to binary tree. Let me explain  further. In node of a singly connected linked list we have one single  pointer pointing to next node and in node of a doubly connected linked  list we have 2 pointers which can point to other 2 nodes (next and  previous nodes). Similarly in a node of a binary tree we have 3 pointers  which can point to 3 other nodes one to parent, one to left child, one to right child so that we can traverse easily in both the directions.&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programsimply.blogspot.com/feeds/7290358712727643242/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://programsimply.blogspot.com/2011/09/concept-of-trees-in-c-tree-is-variation.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/997202589550114592/posts/default/7290358712727643242'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/997202589550114592/posts/default/7290358712727643242'/><link rel='alternate' type='text/html' href='http://programsimply.blogspot.com/2011/09/concept-of-trees-in-c-tree-is-variation.html' title=''/><author><name>Ranu Pratap Singh</name><uri>http://www.blogger.com/profile/15360265618857473433</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-997202589550114592.post-7441812193719595434</id><published>2011-09-23T15:36:00.000+05:30</published><updated>2011-12-13T11:37:21.312+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="C program to print linked list"/><category scheme="http://www.blogger.com/atom/ns#" term="print linked list in reverse"/><category scheme="http://www.blogger.com/atom/ns#" term="recursion in linked list"/><title type='text'>program to print a linked list in reverse order</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot;&gt;&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot;&gt;&lt;div style=&quot;font-family: Arial,Helvetica,sans-serif;&quot;&gt;&lt;u&gt;&lt;b&gt;The need:&amp;nbsp;&lt;/b&gt;&lt;/u&gt;&lt;/div&gt;&lt;div style=&quot;font-family: Arial,Helvetica,sans-serif;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;    This program is a program to prints a linked list in reverse order without actually reversing it (For reversing a given linked list have a look on &lt;a href=&quot;http://programsimply.blogspot.com/2011/07/program-78-do-everything-with-linked.html&quot; target=&quot;_blank&quot;&gt;program78&lt;/a&gt; of this blog). This program is written to show how recursion becomes more useful. First it asks user to enter numbers to be entered into linked list then creates a linked list, prints it in same as given and reversed order.&lt;/div&gt;&lt;u style=&quot;font-family: Arial,Helvetica,sans-serif;&quot;&gt;&lt;b&gt;The code:&lt;/b&gt;&lt;/u&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;,Courier,monospace;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;div style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;---------------------------------------------------------&lt;/div&gt;&lt;div style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;&lt;pre class=&quot;brush: c&quot;&gt;#include&amp;lt;stdio.h&amp;gt;
#include&amp;lt;stdlib.h&amp;gt;
typedef struct linked_list
{
&amp;nbsp;&amp;nbsp;&amp;nbsp; int item;
&amp;nbsp;&amp;nbsp;&amp;nbsp; struct linked_list *next;
}node;

node *create_list();
void print_list(node *);
void print_list_in_reverse(node *);

int main()
{
&amp;nbsp;&amp;nbsp;&amp;nbsp; node *head=NULL;
&amp;nbsp;&amp;nbsp;&amp;nbsp; printf(&quot;This program creates a linked list and then prints it first in given order and then in reverse\n&quot;);
&amp;nbsp;&amp;nbsp;&amp;nbsp; printf(&quot;create a list by entering elements press -999 to end\n&quot;);
&amp;nbsp;&amp;nbsp;&amp;nbsp; head=create_list();
&amp;nbsp;&amp;nbsp;&amp;nbsp; printf(&quot;\nThe list is\n&quot;);
&amp;nbsp;&amp;nbsp;&amp;nbsp; print_list(head);
&amp;nbsp;&amp;nbsp;&amp;nbsp; printf(&quot;\nThe list in reverse order is&quot;);
&amp;nbsp;&amp;nbsp;&amp;nbsp; print_list_in_reverse(head);
&amp;nbsp;&amp;nbsp;&amp;nbsp; printf(&quot;\n&quot;);
&amp;nbsp;&amp;nbsp;&amp;nbsp; return 0;
}

node *create_list()
{
&amp;nbsp;&amp;nbsp;&amp;nbsp; int x;
&amp;nbsp;&amp;nbsp;&amp;nbsp; node *temp=NULL;
&amp;nbsp;&amp;nbsp;&amp;nbsp; scanf(&quot;%d&quot;,&amp;amp;x);
&amp;nbsp;&amp;nbsp;&amp;nbsp; if(x!=-999)
&amp;nbsp;&amp;nbsp;&amp;nbsp; {
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; temp=(node*)malloc(sizeof(node));
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; temp-&amp;gt;item=x;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; temp-&amp;gt;next=NULL;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; temp-&amp;gt;next=create_list();
&amp;nbsp;&amp;nbsp;&amp;nbsp; }
&amp;nbsp;&amp;nbsp;&amp;nbsp; return temp;
}
void print_list(node *p)
{
&amp;nbsp;&amp;nbsp;&amp;nbsp; if(p)
&amp;nbsp;&amp;nbsp;&amp;nbsp; {
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; printf(&quot;%d &quot;,p-&amp;gt;item);
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; print_list(p-&amp;gt;next);
&amp;nbsp;&amp;nbsp;&amp;nbsp; }
&amp;nbsp;&amp;nbsp;&amp;nbsp; else
&amp;nbsp;&amp;nbsp;&amp;nbsp; printf(&quot;\n&quot;);
}

void print_list_in_reverse(node *p)
{
&amp;nbsp;&amp;nbsp;&amp;nbsp; if(p)
&amp;nbsp;&amp;nbsp;&amp;nbsp; {
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; print_list_in_reverse(p-&amp;gt;next);
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; printf(&quot;%d &quot;,p-&amp;gt;item);
&amp;nbsp;&amp;nbsp;&amp;nbsp; }
&amp;nbsp;&amp;nbsp;&amp;nbsp; else
&amp;nbsp;&amp;nbsp;&amp;nbsp; printf(&quot;\n&quot;);
}&lt;/pre&gt;&lt;/div&gt;&lt;div style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;---------------------------------------------------------&lt;/div&gt;&lt;/div&gt;&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot;&gt;&lt;u&gt;&lt;b&gt;The need:&lt;/b&gt;&lt;/u&gt;&lt;/div&gt;&lt;div dir=&quot;ltr&quot; style=&quot;font-family: Arial,Helvetica,sans-serif; text-align: left;&quot;&gt;&amp;nbsp;&amp;nbsp; The approach is nothing but basics of recursion.&lt;br /&gt;
If we say the recursion in create_list function in normal English, we&#39;ll  say &quot;It will take a number form user if it is not -999, a new node is created, data is filled in it and address of this node is returned else NULL is returned.&quot; because temp is initialised to NULL each time in the starting of function.&lt;/div&gt;&lt;div dir=&quot;ltr&quot; style=&quot;font-family: Arial,Helvetica,sans-serif; text-align: left;&quot;&gt;If we say the recursion in print_list function in normal English, we&#39;ll say &quot;It will print the data of a node and then go for doing the same to the next node after that if present&quot;.&lt;/div&gt;&lt;div dir=&quot;ltr&quot; style=&quot;font-family: Arial,Helvetica,sans-serif; text-align: left;&quot;&gt;If we say the recursion in print_list_in_reverse function in normal English, we&#39;ll say &quot;It will print the data of a node when all the node after that are printed or if there is no node after that node&quot;.&lt;/div&gt;&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot;&gt;&amp;nbsp; &lt;/div&gt;&lt;div dir=&quot;ltr&quot; style=&quot;color: #990000; font-family: Arial,Helvetica,sans-serif; text-align: left;&quot;&gt;&lt;u&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;Remarks:&lt;/span&gt;&lt;/u&gt;&lt;/div&gt;&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot;&gt;&lt;span style=&quot;font-family: &#39;Courier New&#39;,Courier,monospace;&quot;&gt;  &lt;span style=&quot;font-family: Arial,Helvetica,sans-serif;&quot;&gt;1. We can print linked list as it is (in the given order)&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-family: Arial,Helvetica,sans-serif;&quot;&gt; without using extra storage or extra thinking. We can just use loops but if we are asked to write a program to print the linked list without actually reversing it, we can not do it by just using loops. What we can do is next point.&lt;/span&gt;&lt;br /&gt;
&lt;div style=&quot;font-family: Arial,Helvetica,sans-serif;&quot;&gt;2. For that we have to create a stack and push addresses of all the elements of linked list on it starting from head to end. Then pop addresses one by one and print the corresponding data. This requires extra storage and extra thinking.&lt;/div&gt;&lt;/div&gt;&lt;div dir=&quot;ltr&quot; style=&quot;font-family: Arial,Helvetica,sans-serif; text-align: left;&quot;&gt;3. So in this program recursion is the way to do the same without extra thinking and without &lt;span style=&quot;color: #351c75;&quot;&gt;explicitly&lt;/span&gt; using extra memory and functions.&lt;/div&gt;&lt;div dir=&quot;ltr&quot; style=&quot;font-family: Arial,Helvetica,sans-serif; text-align: left;&quot;&gt;4. I wrote explicitly with stress because recursion actually uses its own stack (System stack). Now I am not going into these details. &lt;/div&gt;&lt;/div&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://programsimply.blogspot.com/feeds/7441812193719595434/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://programsimply.blogspot.com/2011/10/program-to-print-linked-list-in-reverse.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/997202589550114592/posts/default/7441812193719595434'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/997202589550114592/posts/default/7441812193719595434'/><link rel='alternate' type='text/html' href='http://programsimply.blogspot.com/2011/10/program-to-print-linked-list-in-reverse.html' title='program to print a linked list in reverse order'/><author><name>Ranu Pratap Singh</name><uri>http://www.blogger.com/profile/15360265618857473433</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry></feed>