<?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-7996672995157809874</id><updated>2025-03-10T09:24:59.002-07:00</updated><category term="c"/><category term="non recursive"/><category term="c++"/><category term="operator overloading"/><category term="recursive"/><category term="constructors"/><category term="prime"/><category term="Armstrong Number"/><category term="bubble sort"/><category term="c++ development"/><category term="friend function"/><category term="function overriding"/><category term="leap year"/><category term="linked list"/><category term="matrix"/><category term="overloading"/><category term="pass by reference"/><category term="pass by value"/><category term="stack"/><category term="string"/><category term="unary"/><category term="words"/><title type='text'>Programs in C &amp;amp; C++ with Example</title><subtitle type='html'>C / C++ programs, source codes for students and new programmers.</subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://programs-in-c-plus-plus.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7996672995157809874/posts/default?redirect=false'/><link rel='alternate' type='text/html' href='http://programs-in-c-plus-plus.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><link rel='next' type='application/atom+xml' href='http://www.blogger.com/feeds/7996672995157809874/posts/default?start-index=26&amp;max-results=25&amp;redirect=false'/><author><name>HawkEye</name><uri>http://www.blogger.com/profile/15300086365850802171</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>32</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-7996672995157809874.post-2406857138474427030</id><published>2015-04-16T04:54:00.002-07:00</published><updated>2017-09-18T07:08:42.143-07:00</updated><title type='text'>Promoting our new page</title><content type='html'>updated by api call</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7996672995157809874/posts/default/2406857138474427030'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7996672995157809874/posts/default/2406857138474427030'/><link rel='alternate' type='text/html' href='http://programs-in-c-plus-plus.blogspot.com/2015/04/promoting-our-new-page.html' title='Promoting our new page'/><author><name>HawkEye</name><uri>http://www.blogger.com/profile/15300086365850802171</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></entry><entry><id>tag:blogger.com,1999:blog-7996672995157809874.post-1962565578158001183</id><published>2014-10-31T10:13:00.000-07:00</published><updated>2014-10-31T10:15:55.650-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="c"/><category scheme="http://www.blogger.com/atom/ns#" term="pass by reference"/><category scheme="http://www.blogger.com/atom/ns#" term="pass by value"/><title type='text'>Ways to pass variables to functions in C</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
There seems to be a confusion among people (even authors of Many books) about ways of passing values to a function.&lt;br /&gt;
&lt;b&gt;Question: How many ways are there to pass a variable to a function in C ?&lt;/b&gt;&lt;br /&gt;
Ans: &lt;i&gt;proposed by most people (authors)&lt;/i&gt; :&lt;br /&gt;
&lt;ul style=&quot;text-align: left;&quot;&gt;
&lt;li&gt;&lt;b&gt;Passing variables by values&lt;/b&gt;&lt;/li&gt;
&lt;li&gt;&lt;b&gt;Passing variables by references&lt;/b&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;b&gt;My Reaction : Wrong !!!!!!!&lt;/b&gt;&lt;br /&gt;
To make thing even worse, the&lt;b&gt; example&lt;/b&gt; most writer put up for passing values by references is this :&lt;br /&gt;
&amp;nbsp;
&lt;br /&gt;
&lt;pre style=&quot;background: #000; color: #f8f8f8;&quot;&gt;#include&amp;lt;stdio.&amp;gt;

&lt;span style=&quot;color: #99cf50;&quot;&gt;void&lt;/span&gt; &lt;span style=&quot;color: #89bdff;&quot;&gt;swap&lt;/span&gt;(int *x, int *y)
{
     int* t;
     *t=*x;
     *x=*y;
     *y=*t;
}

&lt;span style=&quot;color: #99cf50;&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: #89bdff;&quot;&gt;main&lt;/span&gt;()
{
    int x,y;
    x=100;
    y=200;
    swap(&amp;amp;x,&amp;amp;y);
    printf(&quot;%d,%d&quot;,x,y);
}
&lt;/pre&gt;
I don&#39;t know if you get this example or not, But this example is wrong.&lt;br /&gt;
&lt;br /&gt;
This example is using &lt;b&gt;pointers&lt;/b&gt;, and pointers are not references.&lt;br /&gt;
So again:&lt;br /&gt;
&lt;b&gt;Question: How many ways are actually there to pass variables to functions in C ?&lt;/b&gt;&lt;br /&gt;
Well there is only one way:&lt;b&gt; Passing variables by value.&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Question: But What about books claiming that variables can be passed to functions by references? &lt;/b&gt;&lt;br /&gt;
Ans: Well I don&#39;t know what other authors are writing or why they are writing such information and feeding it to students. I think many writer confuse pointers and references to be same, they are not.&lt;br /&gt;
There is a good article on Wikipedia about this :&amp;nbsp; &lt;a href=&quot;http://en.wikipedia.org/wiki/Reference_%28C%2B%2B%29&quot; rel=&quot;nofollow&quot; target=&quot;_blank&quot;&gt;c++ references&lt;/a&gt;&lt;br /&gt;
To simplify pointers and references : Always remember&lt;br /&gt;
Pointers can be created without initializing them (meaning they can have garbage) while references can&#39;t. References are to be initialized as soon as you create them. But funny thing is C doesn&#39;t have references !!. This can be easily proven by using gcc compiler, declare a reference variable and watch gcc complaint during compilation(easy).&lt;br /&gt;
&lt;b&gt;Question: So what about the example above?&lt;/b&gt;&lt;br /&gt;
Well technically the example is not using references (cause C don&#39;t have them). But passing values by reference is a good feature to have (unfortunately C doesn&#39;t have this feature). So how will you simulate this behavior in C? Well by using pointers.&lt;br /&gt;
By explicitly passing the address of variable, our function can do anything with that variable. But it should be noted that these addresses are again passed by value ( since addresses are nothing but integers and C can only pass variables by value only). This is the reason why we can&#39;t modify the address we have passed in C to a function (cause it was passed by value).&lt;br /&gt;
Comment if you didn&#39;t or did get what I was trying to convey.&lt;/div&gt;
</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7996672995157809874/posts/default/1962565578158001183'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7996672995157809874/posts/default/1962565578158001183'/><link rel='alternate' type='text/html' href='http://programs-in-c-plus-plus.blogspot.com/2014/10/ways-to-pass-variables-to-functions-in-c.html' title='Ways to pass variables to functions in C'/><author><name>HawkEye</name><uri>http://www.blogger.com/profile/15300086365850802171</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></entry><entry><id>tag:blogger.com,1999:blog-7996672995157809874.post-8680826199640567882</id><published>2012-08-21T07:48:00.000-07:00</published><updated>2012-08-21T07:48:36.673-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="c"/><category scheme="http://www.blogger.com/atom/ns#" term="linked list"/><title type='text'>Linked list using C</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
This is a Program for Linked List using C. The Code contains enough comments to make your life easy.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;pre style=&quot;background: #ffffff; color: black;&quot;&gt;&lt;span style=&quot;color: #004a43;&quot;&gt;#&lt;/span&gt;&lt;span style=&quot;color: #004a43;&quot;&gt;include &lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #40015a;&quot;&gt;stdio.h&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&amp;gt;&lt;/span&gt;


&lt;span style=&quot;color: dimgrey;&quot;&gt;// structure to hold our data&lt;/span&gt;
&lt;span style=&quot;color: dimgrey;&quot;&gt;// we call it node&lt;/span&gt;
&lt;span style=&quot;color: dimgrey;&quot;&gt;// has a int &#39;value&#39;&lt;/span&gt;
&lt;span style=&quot;color: dimgrey;&quot;&gt;// also has a pointer that points to next node&lt;/span&gt;
&lt;span style=&quot;color: maroon; font-weight: bold;&quot;&gt;struct&lt;/span&gt; node
&lt;span style=&quot;color: purple;&quot;&gt;{&lt;/span&gt;
    &lt;span style=&quot;color: maroon; font-weight: bold;&quot;&gt;int&lt;/span&gt; value&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
    &lt;span style=&quot;color: maroon; font-weight: bold;&quot;&gt;struct&lt;/span&gt; node &lt;span style=&quot;color: #808030;&quot;&gt;*&lt;/span&gt; next&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: purple;&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: dimgrey;&quot;&gt;//typdef will save our time&lt;/span&gt;
&lt;span style=&quot;color: dimgrey;&quot;&gt;//from now onwards &#39;struct node&#39; will be replaced by NODE&lt;/span&gt;
&lt;span style=&quot;color: maroon; font-weight: bold;&quot;&gt;typedef&lt;/span&gt; &lt;span style=&quot;color: maroon; font-weight: bold;&quot;&gt;struct&lt;/span&gt; node NODE&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;


&lt;span style=&quot;color: dimgrey;&quot;&gt;// you can notice that we are passing NODE** to insert&lt;/span&gt;
&lt;span style=&quot;color: dimgrey;&quot;&gt;// &lt;/span&gt;&lt;span style=&quot;background: #808000; color: white;&quot;&gt;Why????&lt;/span&gt;
&lt;span style=&quot;color: dimgrey;&quot;&gt;// The answer is simple&lt;/span&gt;
&lt;span style=&quot;color: dimgrey;&quot;&gt;// since we will be passing the head pointer to the function&lt;/span&gt;
&lt;span style=&quot;color: dimgrey;&quot;&gt;// and head pointer can be modified in insertion operation&lt;/span&gt;
&lt;span style=&quot;color: dimgrey;&quot;&gt;// so we must pass a NODE**&lt;/span&gt;
&lt;span style=&quot;color: dimgrey;&quot;&gt;// reason being if we pass a NODE* to function&lt;/span&gt;
&lt;span style=&quot;color: dimgrey;&quot;&gt;// its value will be restored after function has returned&lt;/span&gt;
&lt;span style=&quot;color: dimgrey;&quot;&gt;// hence even if have modified head pointer, it will be restored&lt;/span&gt;
&lt;span style=&quot;color: dimgrey;&quot;&gt;// so we pass NODE**&lt;/span&gt;
&lt;span style=&quot;color: dimgrey;&quot;&gt;//------------------&lt;/span&gt;
&lt;span style=&quot;color: dimgrey;&quot;&gt;// v is the value to be inserted&lt;/span&gt;

&lt;span style=&quot;color: maroon; font-weight: bold;&quot;&gt;void&lt;/span&gt; insert&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;NODE&lt;span style=&quot;color: #808030;&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;*&lt;/span&gt; base&lt;span style=&quot;color: #808030;&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: maroon; font-weight: bold;&quot;&gt;int&lt;/span&gt; v&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;
&lt;span style=&quot;color: purple;&quot;&gt;{&lt;/span&gt;
    NODE&lt;span style=&quot;color: #808030;&quot;&gt;*&lt;/span&gt; BASE&lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;*&lt;/span&gt;base&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;    &lt;span style=&quot;color: dimgrey;&quot;&gt;// get pointer of head&lt;/span&gt;
    NODE&lt;span style=&quot;color: #808030;&quot;&gt;*&lt;/span&gt; k&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;    &lt;span style=&quot;color: dimgrey;&quot;&gt;// k will be used for allocation&lt;/span&gt;
    NODE&lt;span style=&quot;color: #808030;&quot;&gt;*&lt;/span&gt; p&lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt;BASE&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt; &lt;span style=&quot;color: dimgrey;&quot;&gt;// p will be used like a temp pointer&lt;/span&gt;
    
    
    &lt;span style=&quot;color: dimgrey;&quot;&gt;// if head pointer is NULL&lt;/span&gt;
    &lt;span style=&quot;color: dimgrey;&quot;&gt;// means list is empty&lt;/span&gt;
    &lt;span style=&quot;color: dimgrey;&quot;&gt;// so we will make a new node&lt;/span&gt;
    &lt;span style=&quot;color: dimgrey;&quot;&gt;// attach head pointer to the new node&lt;/span&gt;
    &lt;span style=&quot;color: maroon; font-weight: bold;&quot;&gt;if&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;*&lt;/span&gt;base&lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #7d0045;&quot;&gt;NULL&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;
    &lt;span style=&quot;color: purple;&quot;&gt;{&lt;/span&gt;
        &lt;span style=&quot;color: #603000;&quot;&gt;printf&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color: #0f69ff;&quot;&gt;\n&lt;/span&gt;&lt;span style=&quot;color: #0000e6;&quot;&gt;No nodes in list, Adding first node with value=&lt;/span&gt;&lt;span style=&quot;color: #0f69ff;&quot;&gt;%d&lt;/span&gt;&lt;span style=&quot;color: #0000e6;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;,&lt;/span&gt;v&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
        
        &lt;span style=&quot;color: dimgrey;&quot;&gt;// alloc memory to k&lt;/span&gt;
        k&lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;NODE&lt;span style=&quot;color: #808030;&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #603000;&quot;&gt;malloc&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: maroon; font-weight: bold;&quot;&gt;sizeof&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;NODE&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
        
        &lt;span style=&quot;color: dimgrey;&quot;&gt;// set values in node k&lt;/span&gt;
        k&lt;span style=&quot;color: #808030;&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;&amp;gt;&lt;/span&gt;value&lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt;v&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
        &lt;span style=&quot;color: dimgrey;&quot;&gt;// make its next pointer null&lt;/span&gt;
        k&lt;span style=&quot;color: #808030;&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;&amp;gt;&lt;/span&gt;next&lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #7d0045;&quot;&gt;NULL&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
        &lt;span style=&quot;color: dimgrey;&quot;&gt;//attach k to head node&lt;/span&gt;
        &lt;span style=&quot;color: dimgrey;&quot;&gt;// notice since we have passed NODE**&lt;/span&gt;
        &lt;span style=&quot;color: dimgrey;&quot;&gt;// we will use *base to get the value of the head node&lt;/span&gt;
        &lt;span style=&quot;color: #808030;&quot;&gt;*&lt;/span&gt;base&lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt;k&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
        &lt;span style=&quot;color: dimgrey;&quot;&gt;// this is the only portion where head gets modified&lt;/span&gt;
        
    &lt;span style=&quot;color: purple;&quot;&gt;}&lt;/span&gt;
    &lt;span style=&quot;color: maroon; font-weight: bold;&quot;&gt;else&lt;/span&gt;
    &lt;span style=&quot;color: purple;&quot;&gt;{&lt;/span&gt;
        &lt;span style=&quot;color: dimgrey;&quot;&gt;// if head is not null&lt;/span&gt;
        &lt;span style=&quot;color: dimgrey;&quot;&gt;// loop through list till we reach last node&lt;/span&gt;
        &lt;span style=&quot;color: dimgrey;&quot;&gt;// last node can be checked by checking p-&amp;gt;next value&lt;/span&gt;
        &lt;span style=&quot;color: dimgrey;&quot;&gt;// if its not null we continue moving&lt;/span&gt;
        &lt;span style=&quot;color: maroon; font-weight: bold;&quot;&gt;while&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;p&lt;span style=&quot;color: #808030;&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;&amp;gt;&lt;/span&gt;next&lt;span style=&quot;color: #808030;&quot;&gt;!&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #7d0045;&quot;&gt;NULL&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;
        &lt;span style=&quot;color: purple;&quot;&gt;{&lt;/span&gt;
            p&lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt;p&lt;span style=&quot;color: #808030;&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;&amp;gt;&lt;/span&gt;next&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;    
        &lt;span style=&quot;color: purple;&quot;&gt;}&lt;/span&gt;
        &lt;span style=&quot;color: dimgrey;&quot;&gt;// ok now we are at last node&lt;/span&gt;
        &lt;span style=&quot;color: #603000;&quot;&gt;printf&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color: #0f69ff;&quot;&gt;\n&lt;/span&gt;&lt;span style=&quot;color: #0000e6;&quot;&gt; Adding another node with value=&lt;/span&gt;&lt;span style=&quot;color: #0f69ff;&quot;&gt;%d&lt;/span&gt;&lt;span style=&quot;color: #0000e6;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;,&lt;/span&gt;v&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
        &lt;span style=&quot;color: dimgrey;&quot;&gt;// create a new node on k&lt;/span&gt;
        k&lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;NODE&lt;span style=&quot;color: #808030;&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #603000;&quot;&gt;malloc&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: maroon; font-weight: bold;&quot;&gt;sizeof&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;NODE&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
        &lt;span style=&quot;color: dimgrey;&quot;&gt;// do the usual stuff&lt;/span&gt;
        
        k&lt;span style=&quot;color: #808030;&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;&amp;gt;&lt;/span&gt;value&lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt;v&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
        k&lt;span style=&quot;color: #808030;&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;&amp;gt;&lt;/span&gt;next&lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #7d0045;&quot;&gt;NULL&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;    
        &lt;span style=&quot;color: dimgrey;&quot;&gt;// attach k to last node i.e. p&lt;/span&gt;
        
        p&lt;span style=&quot;color: #808030;&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;&amp;gt;&lt;/span&gt;next&lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt;k&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
    &lt;span style=&quot;color: purple;&quot;&gt;}&lt;/span&gt;
    
    
    
&lt;span style=&quot;color: purple;&quot;&gt;}&lt;/span&gt;

&lt;span style=&quot;color: dimgrey;&quot;&gt;//--- this function shows us the list&lt;/span&gt;
&lt;span style=&quot;color: dimgrey;&quot;&gt;// we pass the head pointer to it and it shows us the list&lt;/span&gt;
&lt;span style=&quot;color: maroon; font-weight: bold;&quot;&gt;void&lt;/span&gt; show&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;NODE&lt;span style=&quot;color: #808030;&quot;&gt;*&lt;/span&gt; base&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;
&lt;span style=&quot;color: purple;&quot;&gt;{&lt;/span&gt;
    &lt;span style=&quot;color: #603000;&quot;&gt;printf&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color: #0f69ff;&quot;&gt;\n&lt;/span&gt;&lt;span style=&quot;color: #0000e6;&quot;&gt; List is= &lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
    
    &lt;span style=&quot;color: dimgrey;&quot;&gt;// if head node is NULL&lt;/span&gt;
    &lt;span style=&quot;color: dimgrey;&quot;&gt;// means list is empty&lt;/span&gt;
    &lt;span style=&quot;color: maroon; font-weight: bold;&quot;&gt;if&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;base&lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #7d0045;&quot;&gt;NULL&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;
    &lt;span style=&quot;color: purple;&quot;&gt;{&lt;/span&gt;
        &lt;span style=&quot;color: #603000;&quot;&gt;printf&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color: #0f69ff;&quot;&gt;\n&lt;/span&gt;&lt;span style=&quot;color: #0000e6;&quot;&gt;Empty List&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
    &lt;span style=&quot;color: purple;&quot;&gt;}&lt;/span&gt;
    &lt;span style=&quot;color: maroon; font-weight: bold;&quot;&gt;else&lt;/span&gt;
    &lt;span style=&quot;color: purple;&quot;&gt;{&lt;/span&gt;
        &lt;span style=&quot;color: dimgrey;&quot;&gt;// else loop through and print values along the way&lt;/span&gt;
        &lt;span style=&quot;color: maroon; font-weight: bold;&quot;&gt;while&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;base&lt;span style=&quot;color: #808030;&quot;&gt;!&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #7d0045;&quot;&gt;NULL&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;
        &lt;span style=&quot;color: purple;&quot;&gt;{&lt;/span&gt;
            &lt;span style=&quot;color: #603000;&quot;&gt;printf&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color: #0f69ff;&quot;&gt;%d&lt;/span&gt;&lt;span style=&quot;color: #0000e6;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;,&lt;/span&gt;base&lt;span style=&quot;color: #808030;&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;&amp;gt;&lt;/span&gt;value&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
            base&lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt;base&lt;span style=&quot;color: #808030;&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;&amp;gt;&lt;/span&gt;next&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
        &lt;span style=&quot;color: purple;&quot;&gt;}&lt;/span&gt;
    &lt;span style=&quot;color: purple;&quot;&gt;}&lt;/span&gt;
&lt;span style=&quot;color: purple;&quot;&gt;}&lt;/span&gt;

&lt;span style=&quot;color: dimgrey;&quot;&gt;// del node can delete any value that is found first&lt;/span&gt;
&lt;span style=&quot;color: dimgrey;&quot;&gt;// you can also see , we pass the NODE** to it &lt;/span&gt;
&lt;span style=&quot;color: dimgrey;&quot;&gt;// thats because deletion can even cause the head to be removed/ modified&lt;/span&gt;
&lt;span style=&quot;color: dimgrey;&quot;&gt;// so address of the head is passsed to it&lt;/span&gt;
&lt;span style=&quot;color: maroon; font-weight: bold;&quot;&gt;void&lt;/span&gt; del&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;NODE&lt;span style=&quot;color: #808030;&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;*&lt;/span&gt; base&lt;span style=&quot;color: #808030;&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: maroon; font-weight: bold;&quot;&gt;int&lt;/span&gt; v&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;
&lt;span style=&quot;color: purple;&quot;&gt;{&lt;/span&gt;
    NODE&lt;span style=&quot;color: #808030;&quot;&gt;*&lt;/span&gt; BASE&lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;*&lt;/span&gt;base&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;&lt;span style=&quot;color: dimgrey;&quot;&gt;// for our head pointer copy&lt;/span&gt;
    NODE&lt;span style=&quot;color: #808030;&quot;&gt;*&lt;/span&gt; PRE&lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt;BASE&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;&lt;span style=&quot;color: dimgrey;&quot;&gt;// another tmp pointer&lt;/span&gt;
    NODE&lt;span style=&quot;color: #808030;&quot;&gt;*&lt;/span&gt; tmp&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt; &lt;span style=&quot;color: dimgrey;&quot;&gt;// one more tmp pointer&lt;/span&gt;
    
    &lt;span style=&quot;color: dimgrey;&quot;&gt;// if head is empty &lt;/span&gt;
    &lt;span style=&quot;color: dimgrey;&quot;&gt;// we cant delete anything&lt;/span&gt;
    &lt;span style=&quot;color: maroon; font-weight: bold;&quot;&gt;if&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;*&lt;/span&gt;base&lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #7d0045;&quot;&gt;NULL&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;
    &lt;span style=&quot;color: purple;&quot;&gt;{&lt;/span&gt;
        &lt;span style=&quot;color: #603000;&quot;&gt;printf&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color: #0f69ff;&quot;&gt;\n&lt;/span&gt;&lt;span style=&quot;color: #0000e6;&quot;&gt; Cant delete anything, List is empty&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
    &lt;span style=&quot;color: purple;&quot;&gt;}&lt;/span&gt;
    &lt;span style=&quot;color: dimgrey;&quot;&gt;// just in case the value to be deleted is on &lt;/span&gt;&lt;span style=&quot;background: #808000; color: white;&quot;&gt;head !!&lt;/span&gt;
    &lt;span style=&quot;color: dimgrey;&quot;&gt;// we move the head on to next node and free head&lt;/span&gt;
    &lt;span style=&quot;color: maroon; font-weight: bold;&quot;&gt;else&lt;/span&gt; &lt;span style=&quot;color: maroon; font-weight: bold;&quot;&gt;if&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;*&lt;/span&gt;base&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;&amp;gt;&lt;/span&gt;value&lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt;v&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;
    &lt;span style=&quot;color: purple;&quot;&gt;{&lt;/span&gt;
        &lt;span style=&quot;color: #603000;&quot;&gt;printf&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color: #0000e6;&quot;&gt;head node found, deleting&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
        &lt;span style=&quot;color: dimgrey;&quot;&gt;// copy head pointer to tmp&lt;/span&gt;
        tmp&lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;*&lt;/span&gt;base&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
        &lt;span style=&quot;color: dimgrey;&quot;&gt;// move head to next node&lt;/span&gt;
        &lt;span style=&quot;color: dimgrey;&quot;&gt;// notice the use of *base&lt;/span&gt;
        &lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;*&lt;/span&gt;base&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;*&lt;/span&gt;base&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;&amp;gt;&lt;/span&gt;next&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
        &lt;span style=&quot;color: dimgrey;&quot;&gt;// free the previous head node&lt;/span&gt;
        &lt;span style=&quot;color: #603000;&quot;&gt;free&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;tmp&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
        
            
    &lt;span style=&quot;color: purple;&quot;&gt;}&lt;/span&gt;
    &lt;span style=&quot;color: maroon; font-weight: bold;&quot;&gt;else&lt;/span&gt;
    &lt;span style=&quot;color: purple;&quot;&gt;{&lt;/span&gt;
        &lt;span style=&quot;color: dimgrey;&quot;&gt;// node is somewhere along the way&lt;/span&gt;
        &lt;span style=&quot;color: dimgrey;&quot;&gt;// lets find it&lt;/span&gt;
        &lt;span style=&quot;color: dimgrey;&quot;&gt;// loop till you find the value&lt;/span&gt;
        &lt;span style=&quot;color: dimgrey;&quot;&gt;// or we reach the last node&lt;/span&gt;
        &lt;span style=&quot;color: maroon; font-weight: bold;&quot;&gt;while&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;BASE&lt;span style=&quot;color: #808030;&quot;&gt;!&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #7d0045;&quot;&gt;NULL&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt; &lt;span style=&quot;color: #808030;&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;BASE&lt;span style=&quot;color: #808030;&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;&amp;gt;&lt;/span&gt;value&lt;span style=&quot;color: #808030;&quot;&gt;!&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt;v&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;
        &lt;span style=&quot;color: purple;&quot;&gt;{&lt;/span&gt;
            &lt;span style=&quot;color: dimgrey;&quot;&gt;// copy BASE ptr value to pre&lt;/span&gt;
            &lt;span style=&quot;color: dimgrey;&quot;&gt;// this is done so that we have a pointer to the last node&lt;/span&gt;
            &lt;span style=&quot;color: dimgrey;&quot;&gt;// why we need previous &lt;/span&gt;&lt;span style=&quot;background: #808000; color: white;&quot;&gt;node???&lt;/span&gt;
            &lt;span style=&quot;color: dimgrey;&quot;&gt;// so that previous node can be linked to next node of the &#39;victim&#39; node&lt;/span&gt;
            PRE&lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt;BASE&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
            BASE&lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt;BASE&lt;span style=&quot;color: #808030;&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;&amp;gt;&lt;/span&gt;next&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
        &lt;span style=&quot;color: purple;&quot;&gt;}&lt;/span&gt;
        &lt;span style=&quot;color: dimgrey;&quot;&gt;// did we find the &lt;/span&gt;&lt;span style=&quot;background: #808000; color: white;&quot;&gt;node??&lt;/span&gt;
        &lt;span style=&quot;color: dimgrey;&quot;&gt;// if BASe is null , that means &lt;/span&gt;
        &lt;span style=&quot;color: dimgrey;&quot;&gt;// we failed to find the node&lt;/span&gt;
        &lt;span style=&quot;color: maroon; font-weight: bold;&quot;&gt;if&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;BASE&lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #7d0045;&quot;&gt;NULL&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;
        &lt;span style=&quot;color: purple;&quot;&gt;{&lt;/span&gt;
            &lt;span style=&quot;color: #603000;&quot;&gt;printf&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color: #0f69ff;&quot;&gt;\n&lt;/span&gt;&lt;span style=&quot;color: #0000e6;&quot;&gt;Node Not Found !!&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
        &lt;span style=&quot;color: purple;&quot;&gt;}&lt;/span&gt;
        &lt;span style=&quot;color: maroon; font-weight: bold;&quot;&gt;else&lt;/span&gt;
        &lt;span style=&quot;color: purple;&quot;&gt;{&lt;/span&gt;
            &lt;span style=&quot;color: dimgrey;&quot;&gt;// if base was not NULL that means we found the node&lt;/span&gt;
            &lt;span style=&quot;color: #603000;&quot;&gt;printf&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color: #0000e6;&quot;&gt;Deleting node......&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
            &lt;span style=&quot;color: dimgrey;&quot;&gt;// attach PRE&#39;s next pointer to BASE&#39;s next pointer&lt;/span&gt;
            &lt;span style=&quot;color: dimgrey;&quot;&gt;// this will eliminate the link    &lt;/span&gt;
            PRE&lt;span style=&quot;color: #808030;&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;&amp;gt;&lt;/span&gt;next&lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt;BASE&lt;span style=&quot;color: #808030;&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;&amp;gt;&lt;/span&gt;next&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
            &lt;span style=&quot;color: dimgrey;&quot;&gt;// free the unlinked node&lt;/span&gt;
            &lt;span style=&quot;color: #603000;&quot;&gt;free&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;BASE&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
        &lt;span style=&quot;color: purple;&quot;&gt;}&lt;/span&gt;
        
    &lt;span style=&quot;color: purple;&quot;&gt;}&lt;/span&gt;
    
&lt;span style=&quot;color: purple;&quot;&gt;}&lt;/span&gt;

&lt;span style=&quot;color: dimgrey;&quot;&gt;// our main&lt;/span&gt;
&lt;span style=&quot;color: maroon; font-weight: bold;&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: #400000;&quot;&gt;main&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;
&lt;span style=&quot;color: purple;&quot;&gt;{&lt;/span&gt;
    &lt;span style=&quot;color: dimgrey;&quot;&gt;// declare a head pointer&lt;/span&gt;
    NODE&lt;span style=&quot;color: #808030;&quot;&gt;*&lt;/span&gt; base&lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #7d0045;&quot;&gt;NULL&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;&lt;span style=&quot;color: dimgrey;&quot;&gt;// its NULL initially&lt;/span&gt;
    
    &lt;span style=&quot;color: dimgrey;&quot;&gt;// insert some values&lt;/span&gt;
    &lt;span style=&quot;color: dimgrey;&quot;&gt;// notice we pass the address of the head node&lt;/span&gt;
    insert&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;&amp;amp;&lt;/span&gt;base&lt;span style=&quot;color: #808030;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #008c00;&quot;&gt;5&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
    insert&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;&amp;amp;&lt;/span&gt;base&lt;span style=&quot;color: #808030;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #008c00;&quot;&gt;6&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
    insert&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;&amp;amp;&lt;/span&gt;base&lt;span style=&quot;color: #808030;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #008c00;&quot;&gt;7&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
    insert&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;&amp;amp;&lt;/span&gt;base&lt;span style=&quot;color: #808030;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #008c00;&quot;&gt;8&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
    &lt;span style=&quot;color: dimgrey;&quot;&gt;// lets see what we have :)&lt;/span&gt;
    show&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;base&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
    &lt;span style=&quot;color: dimgrey;&quot;&gt;// now lets delete a value&lt;/span&gt;
    del&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;&amp;amp;&lt;/span&gt;base&lt;span style=&quot;color: #808030;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #008c00;&quot;&gt;9&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
    &lt;span style=&quot;color: dimgrey;&quot;&gt;// now lets see the list again&lt;/span&gt;
    show&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;base&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: purple;&quot;&gt;}&lt;/span&gt;
&lt;/pre&gt;
&lt;br /&gt;&lt;/div&gt;
</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7996672995157809874/posts/default/8680826199640567882'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7996672995157809874/posts/default/8680826199640567882'/><link rel='alternate' type='text/html' href='http://programs-in-c-plus-plus.blogspot.com/2012/08/linked-list-using-c.html' title='Linked list using C'/><author><name>HawkEye</name><uri>http://www.blogger.com/profile/15300086365850802171</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></entry><entry><id>tag:blogger.com,1999:blog-7996672995157809874.post-5908681175640177111</id><published>2012-02-15T19:18:00.000-08:00</published><updated>2012-02-20T08:08:55.079-08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="constructors"/><category scheme="http://www.blogger.com/atom/ns#" term="overloading"/><title type='text'>constructor overloading in c++</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;b&gt;Constructor&lt;/b&gt; play an important part in c++. They can initialize the values of a given object to some known values. This is important as constructor will be able to set some values in objects which otherwise may have some garbage values. Most uninitialized value in objects are main cause of the bugs. &lt;br /&gt;
But the concept of constructor can allow us to do just anything beside initializing values. Constructor are always called when an object is created. This property of constructor is very useful in situation when you need to perform some task as soon as an object is created.&lt;br /&gt;
So constructor is a special function that gets called automatically when an object is created. This function can like any other function do anything (like initializing values).&lt;br /&gt;
&lt;b&gt;Overloading Constructor :&lt;/b&gt;&lt;br /&gt;
This process is very similar to the overloading of simple functions. Since constructor are also function , they can be overloaded like any other functions. This allows user to create many constructor, which can help user to create (and set) objects differently. In most cases objects have data member set to private (so user can&#39;t directly change the values of the objects). Different constructor can allow creation of objects with desired type of values.&lt;br /&gt;
&lt;b&gt;Method :&lt;/b&gt;&lt;br /&gt;
Overloading a constructor is very similar to function overloading. Just change the signature (no. of args and type of args) of the constructor.&lt;br /&gt;
&lt;b&gt;Overloaded Constructor Usage::&lt;/b&gt;&lt;br /&gt;
&amp;lt;class name&amp;gt; &amp;lt;objectname&amp;gt;(args);&lt;br /&gt;
like &lt;b&gt;Box&lt;/b&gt; b; or &lt;b&gt;Box&lt;/b&gt; b2(9);&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Code:&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;pre style=&quot;background: #ffffff; color: black;&quot;&gt;&lt;span style=&quot;color: #004a43;&quot;&gt;#&lt;/span&gt;&lt;span style=&quot;color: #004a43;&quot;&gt;include&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #40015a;&quot;&gt;iostream&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&amp;gt;&lt;/span&gt;

&lt;span style=&quot;color: maroon; font-weight: bold;&quot;&gt;using&lt;/span&gt; &lt;span style=&quot;color: maroon; font-weight: bold;&quot;&gt;namespace&lt;/span&gt; &lt;span style=&quot;color: #666616;&quot;&gt;std&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;

&lt;span style=&quot;color: maroon; font-weight: bold;&quot;&gt;class&lt;/span&gt; box
&lt;span style=&quot;color: purple;&quot;&gt;{&lt;/span&gt;
    &lt;span style=&quot;color: dimgrey;&quot;&gt;//data member  are private&lt;/span&gt;
    &lt;span style=&quot;color: maroon; font-weight: bold;&quot;&gt;int&lt;/span&gt; width&lt;span style=&quot;color: #808030;&quot;&gt;,&lt;/span&gt;height&lt;span style=&quot;color: #808030;&quot;&gt;,&lt;/span&gt;length&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
    
&lt;span style=&quot;color: #e34adc;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;color: maroon; font-weight: bold;&quot;&gt;public&lt;/span&gt;&lt;span style=&quot;color: #e34adc;&quot;&gt;:&lt;/span&gt;
    
    box&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;
    &lt;span style=&quot;color: purple;&quot;&gt;{&lt;/span&gt;
        &lt;span style=&quot;color: dimgrey;&quot;&gt;//default constructor &lt;/span&gt;
        &lt;span style=&quot;color: dimgrey;&quot;&gt;//initializes values to 0;&lt;/span&gt;
        width&lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt;height&lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt;length&lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #008c00;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
    &lt;span style=&quot;color: purple;&quot;&gt;}&lt;/span&gt;
    box&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: maroon; font-weight: bold;&quot;&gt;int&lt;/span&gt; k&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;
    &lt;span style=&quot;color: purple;&quot;&gt;{&lt;/span&gt;
        &lt;span style=&quot;color: dimgrey;&quot;&gt;//overloaded constructor with one argument&lt;/span&gt;
        &lt;span style=&quot;color: dimgrey;&quot;&gt;//will initialize all 3 data member to value &#39;k&#39;&lt;/span&gt;
        width&lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt;height&lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt;length&lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt;k&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
    &lt;span style=&quot;color: purple;&quot;&gt;}&lt;/span&gt;
    box&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: maroon; font-weight: bold;&quot;&gt;int&lt;/span&gt; x&lt;span style=&quot;color: #808030;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: maroon; font-weight: bold;&quot;&gt;int&lt;/span&gt; y&lt;span style=&quot;color: #808030;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: maroon; font-weight: bold;&quot;&gt;int&lt;/span&gt; z&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;
    &lt;span style=&quot;color: purple;&quot;&gt;{&lt;/span&gt;
        &lt;span style=&quot;color: dimgrey;&quot;&gt;//overloaded constructor with 3 argument&lt;/span&gt;
        &lt;span style=&quot;color: dimgrey;&quot;&gt;//will initialize data member differently&lt;/span&gt;
        width&lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt;x&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
        height&lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt;y&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
        length&lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt;z&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
    &lt;span style=&quot;color: purple;&quot;&gt;}&lt;/span&gt;
    &lt;span style=&quot;color: maroon; font-weight: bold;&quot;&gt;void&lt;/span&gt; show&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;
    &lt;span style=&quot;color: purple;&quot;&gt;{&lt;/span&gt;
        &lt;span style=&quot;color: dimgrey;&quot;&gt;//function to show these values&lt;/span&gt;
        &lt;span style=&quot;color: #603000;&quot;&gt;cout&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color: #0f69ff;&quot;&gt;\n&lt;/span&gt;&lt;span style=&quot;color: #0000e6;&quot;&gt; Width=&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt;width&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
        &lt;span style=&quot;color: #603000;&quot;&gt;cout&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color: #0f69ff;&quot;&gt;\n&lt;/span&gt;&lt;span style=&quot;color: #0000e6;&quot;&gt; Height=&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt;height&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
        &lt;span style=&quot;color: #603000;&quot;&gt;cout&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color: #0f69ff;&quot;&gt;\n&lt;/span&gt;&lt;span style=&quot;color: #0000e6;&quot;&gt; Length=&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt;length&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
    &lt;span style=&quot;color: purple;&quot;&gt;}&lt;/span&gt;
    
&lt;span style=&quot;color: purple;&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;

&lt;span style=&quot;color: maroon; font-weight: bold;&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: #400000;&quot;&gt;main&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;
&lt;span style=&quot;color: purple;&quot;&gt;{&lt;/span&gt;
    box b&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;&lt;span style=&quot;color: dimgrey;&quot;&gt;//using default constructor&lt;/span&gt;
    box b2&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #008c00;&quot;&gt;9&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;&lt;span style=&quot;color: dimgrey;&quot;&gt;//using 1st overloaded constructor&lt;/span&gt;
    box b3&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #008c00;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #008c00;&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #008c00;&quot;&gt;3&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;&lt;span style=&quot;color: dimgrey;&quot;&gt;//using 2nd overloaded constructor&lt;/span&gt;
    
    b&lt;span style=&quot;color: #808030;&quot;&gt;.&lt;/span&gt;show&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
    b2&lt;span style=&quot;color: #808030;&quot;&gt;.&lt;/span&gt;show&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
    b3&lt;span style=&quot;color: #808030;&quot;&gt;.&lt;/span&gt;show&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: purple;&quot;&gt;}&lt;/span&gt;
&lt;/pre&gt;
&lt;/div&gt;</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7996672995157809874/posts/default/5908681175640177111'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7996672995157809874/posts/default/5908681175640177111'/><link rel='alternate' type='text/html' href='http://programs-in-c-plus-plus.blogspot.com/2012/02/constructor-overloading-in-c.html' title='constructor overloading in c++'/><author><name>HawkEye</name><uri>http://www.blogger.com/profile/15300086365850802171</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></entry><entry><id>tag:blogger.com,1999:blog-7996672995157809874.post-2027377566131532173</id><published>2012-01-11T01:39:00.000-08:00</published><updated>2012-01-21T03:28:41.994-08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="bubble sort"/><category scheme="http://www.blogger.com/atom/ns#" term="c++"/><title type='text'>Bubble Sort in C++ with improvements</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
Here is an example of Bubble sort in C++.&amp;nbsp; The example will perform bubble sort on a set of 10 integers. The example is a OO example . So everything is in a class called bubble. The class it self has following methods in it.&lt;br /&gt;
&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;&lt;br /&gt;
&lt;ul style=&quot;text-align: left;&quot;&gt;
&lt;li&gt;&lt;b&gt;set()&lt;/b&gt; This will allow to set the values in the array.&lt;/li&gt;
&lt;li&gt;&lt;b&gt;show()&lt;/b&gt; This will display the given array.&lt;/li&gt;
&lt;li&gt;&lt;b&gt;sort1()&lt;/b&gt; normal bubble sorting.&lt;/li&gt;
&lt;li&gt;&lt;b&gt;sort2()&lt;/b&gt; improved bubble sorting.&lt;/li&gt;
&lt;/ul&gt;
&lt;b&gt;Bubble sort&lt;/b&gt; : The sort is a very easy to implement sort method that compares the element to its next element. If the element is bigger then the first element then it is swapped with next element. The name bubble sort comes from the fact, the method is similar to bubble rising in water. As lighter bubble come up and heavy bubble fall down. So if you have numbers like&lt;br /&gt;
41,13,53,6&lt;br /&gt;
first 41 will be checked against 13. Since its bigger then 13 so it will be replaced with 13.&lt;br /&gt;
&lt;b&gt;13&lt;/b&gt;,&lt;b&gt;41&lt;/b&gt;,53,6&lt;br /&gt;
Now 2nd element which happens to be 41 will be compared to 53. since its smaller then 53 so we do nothing.&lt;br /&gt;
13,&lt;b&gt;41&lt;/b&gt;,&lt;b&gt;53&lt;/b&gt;,6 &lt;br /&gt;
Next compare the next element 53 with 6. Since it is bigger then 6, it will be replaced by 6..&lt;br /&gt;
13,41,&lt;b&gt;6&lt;/b&gt;,&lt;b&gt;53&lt;/b&gt;&lt;br /&gt;
This is over result after &lt;b&gt;First pass&lt;/b&gt;. Notice how the biggest element is at last position already. So we won&#39;t bother about it . Next we will repeat the process for the other elements.&lt;br /&gt;
&lt;b&gt;After second pass&lt;/b&gt;&lt;br /&gt;
13,&lt;b&gt;6&lt;/b&gt;,41,53&lt;br /&gt;
&lt;b&gt;after 3rd pass&amp;nbsp;&lt;/b&gt;&lt;br /&gt;
6,13,41,53&lt;br /&gt;
Hence the array gets sorted.&lt;br /&gt;
&lt;b&gt;Improvement&lt;/b&gt; : So how can bubble sorting be improved. Well the improvement can only happen if in between the passes the array gets sorted out. So when this happens we don&#39;t need to proceed checking the whole passes. How to detect if the array was sorted ?? well if there are no exchange of variables in any pass that means the array elements are already sorted out. We can use a variable &quot;swap&quot;&amp;nbsp; , we will set it to 0 just before any pass. and we will set it to 1 if any exchange happens. after the pass we will check its value, if its 0 that means no exchange happened so the array is already sorted by bubble sort. and we can break from there.&lt;br /&gt;
&lt;b&gt;code:&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;pre style=&quot;background: #ffffff; color: black;&quot;&gt;&lt;span style=&quot;color: #004a43;&quot;&gt;#&lt;/span&gt;&lt;span style=&quot;color: #004a43;&quot;&gt;include&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #40015a;&quot;&gt;iostream&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&amp;gt;&lt;/span&gt;

&lt;span style=&quot;color: maroon; font-weight: bold;&quot;&gt;using&lt;/span&gt; &lt;span style=&quot;color: maroon; font-weight: bold;&quot;&gt;namespace&lt;/span&gt; &lt;span style=&quot;color: #666616;&quot;&gt;std&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;

&lt;span style=&quot;color: maroon; font-weight: bold;&quot;&gt;class&lt;/span&gt; bubble
&lt;span style=&quot;color: purple;&quot;&gt;{&lt;/span&gt;
    &lt;span style=&quot;color: dimgrey;&quot;&gt;// 10 element of type int&lt;/span&gt;
    &lt;span style=&quot;color: maroon; font-weight: bold;&quot;&gt;int&lt;/span&gt; a&lt;span style=&quot;color: #808030;&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color: #008c00;&quot;&gt;10&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
    
&lt;span style=&quot;color: #e34adc;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;color: maroon; font-weight: bold;&quot;&gt;public&lt;/span&gt;&lt;span style=&quot;color: #e34adc;&quot;&gt;:&lt;/span&gt;
    bubble&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;
    &lt;span style=&quot;color: purple;&quot;&gt;{&lt;/span&gt;
        &lt;span style=&quot;color: dimgrey;&quot;&gt;//default constructor sets all elements to 0&lt;/span&gt;
        &lt;span style=&quot;color: maroon; font-weight: bold;&quot;&gt;for&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: maroon; font-weight: bold;&quot;&gt;int&lt;/span&gt; x&lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #008c00;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;x&lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #008c00;&quot;&gt;10&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;x&lt;span style=&quot;color: #808030;&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;
        &lt;span style=&quot;color: purple;&quot;&gt;{&lt;/span&gt;
            a&lt;span style=&quot;color: #808030;&quot;&gt;[&lt;/span&gt;x&lt;span style=&quot;color: #808030;&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #008c00;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
        &lt;span style=&quot;color: purple;&quot;&gt;}&lt;/span&gt;
    &lt;span style=&quot;color: purple;&quot;&gt;}&lt;/span&gt;
    &lt;span style=&quot;color: maroon; font-weight: bold;&quot;&gt;void&lt;/span&gt; &lt;span style=&quot;color: #603000;&quot;&gt;set&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;
    &lt;span style=&quot;color: purple;&quot;&gt;{&lt;/span&gt;
        &lt;span style=&quot;color: #603000;&quot;&gt;cout&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color: #0f69ff;&quot;&gt;\n&lt;/span&gt;&lt;span style=&quot;color: #0000e6;&quot;&gt; Enter Elements of Array=&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
        &lt;span style=&quot;color: maroon; font-weight: bold;&quot;&gt;for&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: maroon; font-weight: bold;&quot;&gt;int&lt;/span&gt; x&lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #008c00;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;x&lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #008c00;&quot;&gt;10&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;x&lt;span style=&quot;color: #808030;&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;
        &lt;span style=&quot;color: purple;&quot;&gt;{&lt;/span&gt;
            &lt;span style=&quot;color: #603000;&quot;&gt;cout&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color: #0f69ff;&quot;&gt;\n&lt;/span&gt;&lt;span style=&quot;color: #0000e6;&quot;&gt;Enter&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt;x&lt;span style=&quot;color: #808030;&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: #008c00;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color: #0000e6;&quot;&gt; Item=&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
            &lt;span style=&quot;color: #603000;&quot;&gt;cin&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;&amp;gt;&lt;/span&gt;a&lt;span style=&quot;color: #808030;&quot;&gt;[&lt;/span&gt;x&lt;span style=&quot;color: #808030;&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
        &lt;span style=&quot;color: purple;&quot;&gt;}&lt;/span&gt;
    &lt;span style=&quot;color: purple;&quot;&gt;}&lt;/span&gt;
    &lt;span style=&quot;color: maroon; font-weight: bold;&quot;&gt;void&lt;/span&gt; show&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;
    &lt;span style=&quot;color: purple;&quot;&gt;{&lt;/span&gt;
        &lt;span style=&quot;color: #603000;&quot;&gt;cout&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color: #0f69ff;&quot;&gt;\n&lt;/span&gt;&lt;span style=&quot;color: #0000e6;&quot;&gt; Printing Array=&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
        &lt;span style=&quot;color: maroon; font-weight: bold;&quot;&gt;for&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: maroon; font-weight: bold;&quot;&gt;int&lt;/span&gt; x&lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #008c00;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;x&lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #008c00;&quot;&gt;10&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;x&lt;span style=&quot;color: #808030;&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;
        &lt;span style=&quot;color: purple;&quot;&gt;{&lt;/span&gt;
            &lt;span style=&quot;color: #603000;&quot;&gt;cout&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color: #0f69ff;&quot;&gt;\n&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt;a&lt;span style=&quot;color: #808030;&quot;&gt;[&lt;/span&gt;x&lt;span style=&quot;color: #808030;&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
        &lt;span style=&quot;color: purple;&quot;&gt;}&lt;/span&gt;
    &lt;span style=&quot;color: purple;&quot;&gt;}&lt;/span&gt;
    &lt;span style=&quot;color: maroon; font-weight: bold;&quot;&gt;void&lt;/span&gt; sort1&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;
    &lt;span style=&quot;color: purple;&quot;&gt;{&lt;/span&gt;
        &lt;span style=&quot;color: maroon; font-weight: bold;&quot;&gt;int&lt;/span&gt; moves&lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #008c00;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
        
        &lt;span style=&quot;color: maroon; font-weight: bold;&quot;&gt;for&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: maroon; font-weight: bold;&quot;&gt;int&lt;/span&gt; x&lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #008c00;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;x&lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #008c00;&quot;&gt;10&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;x&lt;span style=&quot;color: #808030;&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;
        &lt;span style=&quot;color: purple;&quot;&gt;{&lt;/span&gt;
            &lt;span style=&quot;color: maroon; font-weight: bold;&quot;&gt;for&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: maroon; font-weight: bold;&quot;&gt;int&lt;/span&gt; y&lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #008c00;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;y&lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #008c00;&quot;&gt;10&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;-&lt;/span&gt;x&lt;span style=&quot;color: #808030;&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color: #008c00;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;y&lt;span style=&quot;color: #808030;&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;
            &lt;span style=&quot;color: purple;&quot;&gt;{&lt;/span&gt;
                &lt;span style=&quot;color: dimgrey;&quot;&gt;//compare the 2 near values&lt;/span&gt;
                &lt;span style=&quot;color: dimgrey;&quot;&gt;// move the bigger one down by swapping&lt;/span&gt;
                &lt;span style=&quot;color: maroon; font-weight: bold;&quot;&gt;if&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;a&lt;span style=&quot;color: #808030;&quot;&gt;[&lt;/span&gt;y&lt;span style=&quot;color: #808030;&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;&amp;gt;&lt;/span&gt;a&lt;span style=&quot;color: #808030;&quot;&gt;[&lt;/span&gt;y&lt;span style=&quot;color: #808030;&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: #008c00;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;
                &lt;span style=&quot;color: purple;&quot;&gt;{&lt;/span&gt;
                    &lt;span style=&quot;color: dimgrey;&quot;&gt;//swapping by a third variable&lt;/span&gt;
                    &lt;span style=&quot;color: maroon; font-weight: bold;&quot;&gt;int&lt;/span&gt; t&lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt;a&lt;span style=&quot;color: #808030;&quot;&gt;[&lt;/span&gt;y&lt;span style=&quot;color: #808030;&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
                    a&lt;span style=&quot;color: #808030;&quot;&gt;[&lt;/span&gt;y&lt;span style=&quot;color: #808030;&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt;a&lt;span style=&quot;color: #808030;&quot;&gt;[&lt;/span&gt;y&lt;span style=&quot;color: #808030;&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: #008c00;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
                    a&lt;span style=&quot;color: #808030;&quot;&gt;[&lt;/span&gt;y&lt;span style=&quot;color: #808030;&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: #008c00;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt;t&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;    
                    
                &lt;span style=&quot;color: purple;&quot;&gt;}&lt;/span&gt;
                &lt;span style=&quot;color: dimgrey;&quot;&gt;//count moves&lt;/span&gt;
                    moves&lt;span style=&quot;color: #808030;&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
                
            &lt;span style=&quot;color: purple;&quot;&gt;}&lt;/span&gt;
        &lt;span style=&quot;color: purple;&quot;&gt;}&lt;/span&gt;
        &lt;span style=&quot;color: #603000;&quot;&gt;cout&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color: #0f69ff;&quot;&gt;\n&lt;/span&gt;&lt;span style=&quot;color: #0000e6;&quot;&gt; array sorted with &lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt;moves&lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color: #0000e6;&quot;&gt; moves&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
    &lt;span style=&quot;color: purple;&quot;&gt;}&lt;/span&gt;
    &lt;span style=&quot;color: maroon; font-weight: bold;&quot;&gt;void&lt;/span&gt; sort2&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;
    &lt;span style=&quot;color: purple;&quot;&gt;{&lt;/span&gt;
        &lt;span style=&quot;color: maroon; font-weight: bold;&quot;&gt;int&lt;/span&gt; moves&lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #008c00;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
        &lt;span style=&quot;color: maroon; font-weight: bold;&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: #603000;&quot;&gt;swap&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #008c00;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
        &lt;span style=&quot;color: maroon; font-weight: bold;&quot;&gt;for&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: maroon; font-weight: bold;&quot;&gt;int&lt;/span&gt; x&lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #008c00;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;x&lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #008c00;&quot;&gt;10&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;x&lt;span style=&quot;color: #808030;&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;
        &lt;span style=&quot;color: purple;&quot;&gt;{&lt;/span&gt;
            &lt;span style=&quot;color: dimgrey;&quot;&gt;//set variable swap to 0&lt;/span&gt;
            &lt;span style=&quot;color: #603000;&quot;&gt;swap&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #008c00;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
            &lt;span style=&quot;color: maroon; font-weight: bold;&quot;&gt;for&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: maroon; font-weight: bold;&quot;&gt;int&lt;/span&gt; y&lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #008c00;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;y&lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #008c00;&quot;&gt;10&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;-&lt;/span&gt;x&lt;span style=&quot;color: #808030;&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color: #008c00;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;y&lt;span style=&quot;color: #808030;&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;
            &lt;span style=&quot;color: purple;&quot;&gt;{&lt;/span&gt;
                &lt;span style=&quot;color: dimgrey;&quot;&gt;//compare the 2 near values&lt;/span&gt;
                &lt;span style=&quot;color: dimgrey;&quot;&gt;// move the bigger one down by swapping&lt;/span&gt;
                &lt;span style=&quot;color: maroon; font-weight: bold;&quot;&gt;if&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;a&lt;span style=&quot;color: #808030;&quot;&gt;[&lt;/span&gt;y&lt;span style=&quot;color: #808030;&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;&amp;gt;&lt;/span&gt;a&lt;span style=&quot;color: #808030;&quot;&gt;[&lt;/span&gt;y&lt;span style=&quot;color: #808030;&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: #008c00;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;
                &lt;span style=&quot;color: purple;&quot;&gt;{&lt;/span&gt;
                    &lt;span style=&quot;color: dimgrey;&quot;&gt;//swapping by a third variable&lt;/span&gt;
                    &lt;span style=&quot;color: maroon; font-weight: bold;&quot;&gt;int&lt;/span&gt; t&lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt;a&lt;span style=&quot;color: #808030;&quot;&gt;[&lt;/span&gt;y&lt;span style=&quot;color: #808030;&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
                    a&lt;span style=&quot;color: #808030;&quot;&gt;[&lt;/span&gt;y&lt;span style=&quot;color: #808030;&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt;a&lt;span style=&quot;color: #808030;&quot;&gt;[&lt;/span&gt;y&lt;span style=&quot;color: #808030;&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: #008c00;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
                    a&lt;span style=&quot;color: #808030;&quot;&gt;[&lt;/span&gt;y&lt;span style=&quot;color: #808030;&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: #008c00;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt;t&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;    
                    &lt;span style=&quot;color: dimgrey;&quot;&gt;//if swapping is done set swap to 1&lt;/span&gt;
                    &lt;span style=&quot;color: #603000;&quot;&gt;swap&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #008c00;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
                    
                &lt;span style=&quot;color: purple;&quot;&gt;}&lt;/span&gt;
                &lt;span style=&quot;color: dimgrey;&quot;&gt;//count moves&lt;/span&gt;
                    moves&lt;span style=&quot;color: #808030;&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
                    
                
            &lt;span style=&quot;color: purple;&quot;&gt;}&lt;/span&gt;
            &lt;span style=&quot;color: maroon; font-weight: bold;&quot;&gt;if&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #603000;&quot;&gt;swap&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #008c00;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;
            &lt;span style=&quot;color: purple;&quot;&gt;{&lt;/span&gt;
                &lt;span style=&quot;color: dimgrey;&quot;&gt;// break the loop if no swapping were done &lt;/span&gt;
                &lt;span style=&quot;color: dimgrey;&quot;&gt;// this will only happen if the array was sorted already&lt;/span&gt;
                &lt;span style=&quot;color: maroon; font-weight: bold;&quot;&gt;break&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
            &lt;span style=&quot;color: purple;&quot;&gt;}&lt;/span&gt;
        &lt;span style=&quot;color: purple;&quot;&gt;}&lt;/span&gt;
        &lt;span style=&quot;color: #603000;&quot;&gt;cout&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color: #0f69ff;&quot;&gt;\n&lt;/span&gt;&lt;span style=&quot;color: #0000e6;&quot;&gt; array sorted with &lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt;moves&lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color: #0000e6;&quot;&gt; moves&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
    &lt;span style=&quot;color: purple;&quot;&gt;}&lt;/span&gt;
&lt;span style=&quot;color: purple;&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;

&lt;span style=&quot;color: maroon; font-weight: bold;&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: #400000;&quot;&gt;main&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;
&lt;span style=&quot;color: purple;&quot;&gt;{&lt;/span&gt;
    bubble b&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
    b&lt;span style=&quot;color: #808030;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #603000;&quot;&gt;set&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
    b&lt;span style=&quot;color: #808030;&quot;&gt;.&lt;/span&gt;show&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
    b&lt;span style=&quot;color: #808030;&quot;&gt;.&lt;/span&gt;sort2&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
    b&lt;span style=&quot;color: #808030;&quot;&gt;.&lt;/span&gt;show&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: purple;&quot;&gt;}&lt;/span&gt;
&lt;/pre&gt;
&lt;/div&gt;</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7996672995157809874/posts/default/2027377566131532173'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7996672995157809874/posts/default/2027377566131532173'/><link rel='alternate' type='text/html' href='http://programs-in-c-plus-plus.blogspot.com/2012/01/bubble-sort-in-c-with-improvements.html' title='Bubble Sort in C++ with improvements'/><author><name>HawkEye</name><uri>http://www.blogger.com/profile/15300086365850802171</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></entry><entry><id>tag:blogger.com,1999:blog-7996672995157809874.post-4766575625534988049</id><published>2012-01-08T01:09:00.000-08:00</published><updated>2012-01-13T00:08:10.623-08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Armstrong Number"/><category scheme="http://www.blogger.com/atom/ns#" term="c"/><title type='text'>Find Armstrong Number in C</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
Finding Armstrong number is again a great exercise for students The exercise involves clever use of modulus operator to extract the digits and then using those digits to perform calculations.&lt;br /&gt;
&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;&lt;br /&gt;
&lt;b&gt;Armstrong Number&lt;/b&gt; : These are the special numbers which is equal to the sum of the cube of their own digits.&lt;br /&gt;
like 153. since 153= 1&lt;sup&gt;3&lt;/sup&gt;+5&lt;sup&gt;3&lt;/sup&gt;+3&lt;sup&gt;3&lt;/sup&gt;=1+125+27=153 cool isn&#39;t it. So how can you find them? Here is the logic.&lt;br /&gt;
&lt;b&gt;Logic&lt;/b&gt;: use modulus operator to get the last digit of the number. so 153%10=3 (last digit). cube this number, 3*3*3=27, then save it to a variable. again now reduce the number 153. 153/10=15 (see the last digit will be removed :) ). repeat the process till this number becomes 0.&lt;br /&gt;
&lt;b&gt;Code:&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;pre style=&quot;background: #ffffff; color: black;&quot;&gt;&lt;span style=&quot;color: #004a43;&quot;&gt;#&lt;/span&gt;&lt;span style=&quot;color: #004a43;&quot;&gt;include&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #40015a;&quot;&gt;stdio.h&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&amp;gt;&lt;/span&gt;

&lt;span style=&quot;color: maroon; font-weight: bold;&quot;&gt;int&lt;/span&gt; armstrong&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: maroon; font-weight: bold;&quot;&gt;int&lt;/span&gt; x&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;
&lt;span style=&quot;color: purple;&quot;&gt;{&lt;/span&gt;
    &lt;span style=&quot;color: dimgrey;&quot;&gt;//copy x to k&lt;/span&gt;
    &lt;span style=&quot;color: maroon; font-weight: bold;&quot;&gt;int&lt;/span&gt; k&lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt;x&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
    
    &lt;span style=&quot;color: dimgrey;&quot;&gt;//start extracting its digit, &lt;/span&gt;
    &lt;span style=&quot;color: dimgrey;&quot;&gt;// cube the digit and start saving it&lt;/span&gt;
    &lt;span style=&quot;color: dimgrey;&quot;&gt;// we will use sum to save the total.&lt;/span&gt;
    &lt;span style=&quot;color: maroon; font-weight: bold;&quot;&gt;int&lt;/span&gt; sum&lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #008c00;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
    &lt;span style=&quot;color: maroon; font-weight: bold;&quot;&gt;int&lt;/span&gt; r&lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #008c00;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
    &lt;span style=&quot;color: dimgrey;&quot;&gt;//r will be used for remainder&lt;/span&gt;
    &lt;span style=&quot;color: maroon; font-weight: bold;&quot;&gt;while&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;k&lt;span style=&quot;color: #808030;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: #008c00;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;
    &lt;span style=&quot;color: purple;&quot;&gt;{&lt;/span&gt;
        r&lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt;k&lt;span style=&quot;color: #808030;&quot;&gt;%&lt;/span&gt;&lt;span style=&quot;color: #008c00;&quot;&gt;10&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
        &lt;span style=&quot;color: dimgrey;&quot;&gt;//now cude the remainder and add it to sum&lt;/span&gt;
        sum&lt;span style=&quot;color: #808030;&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt;r&lt;span style=&quot;color: #808030;&quot;&gt;*&lt;/span&gt;r&lt;span style=&quot;color: #808030;&quot;&gt;*&lt;/span&gt;r&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
        &lt;span style=&quot;color: dimgrey;&quot;&gt;//reduce k by 10&lt;/span&gt;
        k&lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt;k&lt;span style=&quot;color: #808030;&quot;&gt;/&lt;/span&gt;&lt;span style=&quot;color: #008c00;&quot;&gt;10&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;    
    &lt;span style=&quot;color: purple;&quot;&gt;}&lt;/span&gt;
    &lt;span style=&quot;color: maroon; font-weight: bold;&quot;&gt;if&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;x&lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt;sum&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;
    &lt;span style=&quot;color: purple;&quot;&gt;{&lt;/span&gt;
        &lt;span style=&quot;color: maroon; font-weight: bold;&quot;&gt;return&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #008c00;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
    &lt;span style=&quot;color: purple;&quot;&gt;}&lt;/span&gt;
    &lt;span style=&quot;color: maroon; font-weight: bold;&quot;&gt;else&lt;/span&gt;
    &lt;span style=&quot;color: purple;&quot;&gt;{&lt;/span&gt;
        &lt;span style=&quot;color: maroon; font-weight: bold;&quot;&gt;return&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #008c00;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
    &lt;span style=&quot;color: purple;&quot;&gt;}&lt;/span&gt;
&lt;span style=&quot;color: purple;&quot;&gt;}&lt;/span&gt;

&lt;span style=&quot;color: maroon; font-weight: bold;&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: #400000;&quot;&gt;main&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;
&lt;span style=&quot;color: purple;&quot;&gt;{&lt;/span&gt;
    &lt;span style=&quot;color: maroon; font-weight: bold;&quot;&gt;int&lt;/span&gt; k&lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #008c00;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
    &lt;span style=&quot;color: dimgrey;&quot;&gt;//n will be used how far we want to calculate&lt;/span&gt;
    &lt;span style=&quot;color: maroon; font-weight: bold;&quot;&gt;int&lt;/span&gt; n&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
    &lt;span style=&quot;color: #603000;&quot;&gt;printf&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color: #0f69ff;&quot;&gt;\n&lt;/span&gt;&lt;span style=&quot;color: #0000e6;&quot;&gt; Enter a number=&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
    &lt;span style=&quot;color: #603000;&quot;&gt;scanf&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color: #0f69ff;&quot;&gt;%d&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;&amp;amp;&lt;/span&gt;n&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
    &lt;span style=&quot;color: maroon; font-weight: bold;&quot;&gt;for&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;k&lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #008c00;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;k&lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt;n&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;k&lt;span style=&quot;color: #808030;&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;
    &lt;span style=&quot;color: purple;&quot;&gt;{&lt;/span&gt;
        &lt;span style=&quot;color: maroon; font-weight: bold;&quot;&gt;if&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;armstrong&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;k&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;
        &lt;span style=&quot;color: purple;&quot;&gt;{&lt;/span&gt;
            &lt;span style=&quot;color: #603000;&quot;&gt;printf&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color: #0f69ff;&quot;&gt;\n&lt;/span&gt;&lt;span style=&quot;color: #0000e6;&quot;&gt; Armstring number=&lt;/span&gt;&lt;span style=&quot;color: #0f69ff;&quot;&gt;%d&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;,&lt;/span&gt;k&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
        &lt;span style=&quot;color: purple;&quot;&gt;}&lt;/span&gt;
    &lt;span style=&quot;color: purple;&quot;&gt;}&lt;/span&gt;
    
&lt;span style=&quot;color: purple;&quot;&gt;}&lt;/span&gt;
&lt;/pre&gt;
&lt;/div&gt;</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7996672995157809874/posts/default/4766575625534988049'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7996672995157809874/posts/default/4766575625534988049'/><link rel='alternate' type='text/html' href='http://programs-in-c-plus-plus.blogspot.com/2012/01/find-armstrong-number-in-c.html' title='Find Armstrong Number in C'/><author><name>HawkEye</name><uri>http://www.blogger.com/profile/15300086365850802171</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></entry><entry><id>tag:blogger.com,1999:blog-7996672995157809874.post-5919646508386756247</id><published>2012-01-03T19:50:00.000-08:00</published><updated>2012-01-05T00:46:13.688-08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="c++"/><category scheme="http://www.blogger.com/atom/ns#" term="operator overloading"/><category scheme="http://www.blogger.com/atom/ns#" term="unary"/><title type='text'>Overloading Unary Operator in C++</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
In this Example I will demonstrate overloading a unary operator. The overloading is done in similar way as we did for binary operator. In this example I will be using a Vector Class. The operator we are going to over load is the unary &quot;~&quot; (tilde) operator. although you can use any unary overloadable operator.&lt;br /&gt;
&lt;b&gt;Method: &lt;/b&gt;In this example the overloaded operator will be the part of the class i.e. the over loaded definition will be a member function. (As we know overloading can be done using non member functions too). So the unary operator will not have any arguments in it.&lt;br /&gt;
We will overload ~ operator so that it will reflect the vector so that its z component becomes x component and vice versa. So input vector will be 2i+3j+5k and its output vector will be 5i+3j+2k. although you can do anything you like.&lt;br /&gt;
&lt;br /&gt;
Code:&lt;br /&gt;
&lt;pre style=&quot;background: #ffffff; color: black;&quot;&gt;&lt;span style=&quot;color: #004a43;&quot;&gt;#&lt;/span&gt;&lt;span style=&quot;color: #004a43;&quot;&gt;include&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #40015a;&quot;&gt;iostream&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&amp;gt;&lt;/span&gt;

&lt;span style=&quot;color: maroon; font-weight: bold;&quot;&gt;using&lt;/span&gt; &lt;span style=&quot;color: maroon; font-weight: bold;&quot;&gt;namespace&lt;/span&gt; &lt;span style=&quot;color: #666616;&quot;&gt;std&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;


&lt;span style=&quot;color: maroon; font-weight: bold;&quot;&gt;class&lt;/span&gt; &lt;span style=&quot;color: #603000;&quot;&gt;vector&lt;/span&gt;
&lt;span style=&quot;color: purple;&quot;&gt;{&lt;/span&gt;
    &lt;span style=&quot;color: maroon; font-weight: bold;&quot;&gt;int&lt;/span&gt; x&lt;span style=&quot;color: #808030;&quot;&gt;,&lt;/span&gt;y&lt;span style=&quot;color: #808030;&quot;&gt;,&lt;/span&gt;z&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #e34adc;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;color: maroon; font-weight: bold;&quot;&gt;public&lt;/span&gt;&lt;span style=&quot;color: #e34adc;&quot;&gt;:&lt;/span&gt;
    &lt;span style=&quot;color: #603000;&quot;&gt;vector&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;
    &lt;span style=&quot;color: purple;&quot;&gt;{&lt;/span&gt;
        x&lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt;y&lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt;z&lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #008c00;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
        &lt;span style=&quot;color: dimgrey;&quot;&gt;//default constructor&lt;/span&gt;
    &lt;span style=&quot;color: purple;&quot;&gt;}&lt;/span&gt;
    &lt;span style=&quot;color: #603000;&quot;&gt;vector&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: maroon; font-weight: bold;&quot;&gt;int&lt;/span&gt; a&lt;span style=&quot;color: #808030;&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: maroon; font-weight: bold;&quot;&gt;int&lt;/span&gt; b&lt;span style=&quot;color: #808030;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: maroon; font-weight: bold;&quot;&gt;int&lt;/span&gt; c&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;
    &lt;span style=&quot;color: purple;&quot;&gt;{&lt;/span&gt;
        x&lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt;a&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;y&lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt;b&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;z&lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt;c&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
    &lt;span style=&quot;color: purple;&quot;&gt;}&lt;/span&gt;
    &lt;span style=&quot;color: maroon; font-weight: bold;&quot;&gt;void&lt;/span&gt; show&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: dimgrey;&quot;&gt;// to show vector&lt;/span&gt;
    &lt;span style=&quot;color: purple;&quot;&gt;{&lt;/span&gt;
        &lt;span style=&quot;color: #603000;&quot;&gt;cout&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color: #0f69ff;&quot;&gt;\n&lt;/span&gt;&lt;span style=&quot;color: #0000e6;&quot;&gt; Vector=&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt;x&lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color: #0000e6;&quot;&gt;i+&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt;y&lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color: #0000e6;&quot;&gt;j+&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt;z&lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color: #0000e6;&quot;&gt;k&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
    &lt;span style=&quot;color: purple;&quot;&gt;}&lt;/span&gt;
    &lt;span style=&quot;color: dimgrey;&quot;&gt;//the operator overloading returns a vector&lt;/span&gt;
    &lt;span style=&quot;color: dimgrey;&quot;&gt;//although return type can be anything&lt;/span&gt;
    &lt;span style=&quot;color: #603000;&quot;&gt;vector&lt;/span&gt; &lt;span style=&quot;color: maroon; font-weight: bold;&quot;&gt;operator&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;~&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;
    &lt;span style=&quot;color: purple;&quot;&gt;{&lt;/span&gt;
        &lt;span style=&quot;color: dimgrey;&quot;&gt;//notice how the ans object is being created&lt;/span&gt;
        &lt;span style=&quot;color: dimgrey;&quot;&gt;// we are passing it the value by our constructor&lt;/span&gt;
        &lt;span style=&quot;color: #603000;&quot;&gt;vector&lt;/span&gt; ans&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;z&lt;span style=&quot;color: #808030;&quot;&gt;,&lt;/span&gt;y&lt;span style=&quot;color: #808030;&quot;&gt;,&lt;/span&gt;x&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
        &lt;span style=&quot;color: maroon; font-weight: bold;&quot;&gt;return&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;ans&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
    &lt;span style=&quot;color: purple;&quot;&gt;}&lt;/span&gt;
&lt;span style=&quot;color: purple;&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;

&lt;span style=&quot;color: maroon; font-weight: bold;&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: #400000;&quot;&gt;main&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;
&lt;span style=&quot;color: purple;&quot;&gt;{&lt;/span&gt;
    &lt;span style=&quot;color: dimgrey;&quot;&gt;//create 2 vector&lt;/span&gt;
    &lt;span style=&quot;color: dimgrey;&quot;&gt;// one has value i+2j+3k other is 0&lt;/span&gt;
    &lt;span style=&quot;color: #603000;&quot;&gt;vector&lt;/span&gt; v&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #008c00;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #008c00;&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #008c00;&quot;&gt;3&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;,&lt;/span&gt;c&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
    &lt;span style=&quot;color: dimgrey;&quot;&gt;//use ~ operator to reflect the vector and pas it to c&lt;/span&gt;
    c&lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;~&lt;/span&gt;v&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
    v&lt;span style=&quot;color: #808030;&quot;&gt;.&lt;/span&gt;show&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
    c&lt;span style=&quot;color: #808030;&quot;&gt;.&lt;/span&gt;show&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
    
&lt;span style=&quot;color: purple;&quot;&gt;}&lt;/span&gt;
&lt;/pre&gt;
&lt;/div&gt;</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7996672995157809874/posts/default/5919646508386756247'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7996672995157809874/posts/default/5919646508386756247'/><link rel='alternate' type='text/html' href='http://programs-in-c-plus-plus.blogspot.com/2012/01/overloading-unary-operator.html' title='Overloading Unary Operator in C++'/><author><name>HawkEye</name><uri>http://www.blogger.com/profile/15300086365850802171</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></entry><entry><id>tag:blogger.com,1999:blog-7996672995157809874.post-4367119279665468777</id><published>2011-12-08T21:01:00.001-08:00</published><updated>2012-01-05T00:46:25.218-08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="c"/><category scheme="http://www.blogger.com/atom/ns#" term="string"/><category scheme="http://www.blogger.com/atom/ns#" term="words"/><title type='text'>Counting Words in a String using C</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;b&gt;Counting words in a String using C:&lt;/b&gt;&lt;br /&gt;
This is very good exercise to test new programmers pointer skills. The task is to count the words in a given string using C. Well there might be many ways you can do it, I will demonstrate mine way.&lt;br /&gt;
Note that mine solution might not be the optimal one, but it gets the job done.&lt;br /&gt;
&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;&lt;br /&gt;
&lt;b&gt;Logic:&lt;/b&gt;&lt;br /&gt;
How to calculate words in a given string using C. Well I generally use two pointers, one pointer pointers to first char of the string, second point to next char. Then we compare the content on the two pointers. There will be 3 cases:&lt;br /&gt;
&lt;ol style=&quot;text-align: left;&quot;&gt;
&lt;li&gt;Both pointers are blank chars (space), so that means we are in between words&lt;/li&gt;
&lt;li&gt;First pointer is blank, other is not. This is denote beginning of the Word.&lt;/li&gt;
&lt;li&gt;Rest cases will be when either in the middle of the words or at the ending of the words.&lt;/li&gt;
&lt;/ol&gt;
Check this image for clarity.&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/AVvXsEiCrC_C7XyVmgg0V5wBtSGr1wZmtbpbdJovvmjCPstfM8C5vArLCAFyF7K3KGQAOVJfP4lv3VGRELWCAot0asDZOoiUM3IWSCd2gXev_lwz6MPiIlw2dxTw9TWaqjNbtL6IjAHptBUIL-c/s1600/counting+words+in+String+using+C+program.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/AVvXsEiCrC_C7XyVmgg0V5wBtSGr1wZmtbpbdJovvmjCPstfM8C5vArLCAFyF7K3KGQAOVJfP4lv3VGRELWCAot0asDZOoiUM3IWSCd2gXev_lwz6MPiIlw2dxTw9TWaqjNbtL6IjAHptBUIL-c/s1600/counting+words+in+String+using+C+program.jpg&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
The two Orange dots represent the two pointers in the middle of words.&lt;br /&gt;
The two Green dots represent when first char is blank and other is not. this marks the beginning of the word and here we count the word.&lt;br /&gt;
The two red dots represent the case when pointers are in the middle of the words, we don&#39;t do anything.&lt;br /&gt;
&lt;b&gt;Why are we using 2 pointers?&lt;/b&gt;&lt;br /&gt;
This will allow our function to count the words that are separated by more then one space (which is very handy, when user puts too many spaces in the string)&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Code:&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;pre style=&quot;background: #ffffff; color: black;&quot;&gt;&lt;span style=&quot;color: #004a43;&quot;&gt;#&lt;/span&gt;&lt;span style=&quot;color: #004a43;&quot;&gt;include&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #40015a;&quot;&gt;stdio.h&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&amp;gt;&lt;/span&gt;
&lt;span style=&quot;color: #004a43;&quot;&gt;#&lt;/span&gt;&lt;span style=&quot;color: #004a43;&quot;&gt;include&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #40015a;&quot;&gt;conio.h&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&amp;gt;&lt;/span&gt;
&lt;span style=&quot;color: #004a43;&quot;&gt;#&lt;/span&gt;&lt;span style=&quot;color: #004a43;&quot;&gt;include&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #40015a;&quot;&gt;string.h&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: dimgrey;&quot;&gt;// for strlen function&lt;/span&gt;

&lt;span style=&quot;color: maroon; font-weight: 700;&quot;&gt;int&lt;/span&gt; countWord&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: maroon; font-weight: 700;&quot;&gt;const&lt;/span&gt; &lt;span style=&quot;color: maroon; font-weight: 700;&quot;&gt;char&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;*&lt;/span&gt; str&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;
&lt;span style=&quot;color: purple;&quot;&gt;{&lt;/span&gt;
    &lt;span style=&quot;color: maroon; font-weight: 700;&quot;&gt;int&lt;/span&gt; c&lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #008c00;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;,&lt;/span&gt;l&lt;span style=&quot;color: #808030;&quot;&gt;,&lt;/span&gt;i&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
    &lt;span style=&quot;color: maroon; font-weight: 700;&quot;&gt;char&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;*&lt;/span&gt; p1&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;&lt;span style=&quot;color: dimgrey;&quot;&gt;//first pointer&lt;/span&gt;
    &lt;span style=&quot;color: maroon; font-weight: 700;&quot;&gt;char&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;*&lt;/span&gt; p2&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;&lt;span style=&quot;color: dimgrey;&quot;&gt;//second pointer&lt;/span&gt;
    p1&lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt;str&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;&lt;span style=&quot;color: dimgrey;&quot;&gt;//assing first pointer to first char of string&lt;/span&gt;
    p2&lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt;p1&lt;span style=&quot;color: #808030;&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: #008c00;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;&lt;span style=&quot;color: dimgrey;&quot;&gt;//assign 2nd pointer to next of first&lt;/span&gt;
    l&lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #603000;&quot;&gt;strlen&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;str&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
    &lt;span style=&quot;color: #603000;&quot;&gt;printf&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color: #0f69ff;&quot;&gt;\n&lt;/span&gt;&lt;span style=&quot;color: #0000e6;&quot;&gt; String has  &lt;/span&gt;&lt;span style=&quot;color: #0f69ff;&quot;&gt;%d&lt;/span&gt;&lt;span style=&quot;color: #0000e6;&quot;&gt; length&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;,&lt;/span&gt;l&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
    
    
    &lt;span style=&quot;color: maroon; font-weight: 700;&quot;&gt;if&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;p1&lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #7d0045;&quot;&gt;NULL&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;
    &lt;span style=&quot;color: purple;&quot;&gt;{&lt;/span&gt;
        &lt;span style=&quot;color: maroon; font-weight: 700;&quot;&gt;return&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #008c00;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
    &lt;span style=&quot;color: purple;&quot;&gt;}&lt;/span&gt;
    &lt;span style=&quot;color: maroon; font-weight: 700;&quot;&gt;else&lt;/span&gt;
    &lt;span style=&quot;color: purple;&quot;&gt;{&lt;/span&gt;
        &lt;span style=&quot;color: dimgrey;&quot;&gt;//check if first char is space or not, if not then its the first word&lt;/span&gt;
        &lt;span style=&quot;color: maroon; font-weight: 700;&quot;&gt;if&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;*&lt;/span&gt;p1&lt;span style=&quot;color: #808030;&quot;&gt;!&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #0000e6;&quot;&gt;&#39; &#39;&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;
        &lt;span style=&quot;color: purple;&quot;&gt;{&lt;/span&gt;
            c&lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #008c00;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
        &lt;span style=&quot;color: purple;&quot;&gt;}&lt;/span&gt;
    &lt;span style=&quot;color: purple;&quot;&gt;}&lt;/span&gt;
    
    &lt;span style=&quot;color: maroon; font-weight: 700;&quot;&gt;for&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;i&lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #008c00;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;i&lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt;l&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;i&lt;span style=&quot;color: #808030;&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;
    &lt;span style=&quot;color: purple;&quot;&gt;{&lt;/span&gt;
        
        &lt;span style=&quot;color: maroon; font-weight: 700;&quot;&gt;if&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;*&lt;/span&gt;p1&lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #0000e6;&quot;&gt;&#39; &#39;&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;*&lt;/span&gt;p2&lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #0000e6;&quot;&gt;&#39; &#39;&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;
        &lt;span style=&quot;color: purple;&quot;&gt;{&lt;/span&gt;
            &lt;span style=&quot;color: dimgrey;&quot;&gt;//both p1 and p2 are blank , so do nothing&lt;/span&gt;
        &lt;span style=&quot;color: purple;&quot;&gt;}&lt;/span&gt;
        &lt;span style=&quot;color: maroon; font-weight: 700;&quot;&gt;else&lt;/span&gt; &lt;span style=&quot;color: maroon; font-weight: 700;&quot;&gt;if&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;*&lt;/span&gt;p1&lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #0000e6;&quot;&gt;&#39; &#39;&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt; &lt;span style=&quot;color: #808030;&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;*&lt;/span&gt;p2&lt;span style=&quot;color: #808030;&quot;&gt;!&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #0000e6;&quot;&gt;&#39; &#39;&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;
        &lt;span style=&quot;color: purple;&quot;&gt;{&lt;/span&gt;
            &lt;span style=&quot;color: dimgrey;&quot;&gt;//first pointer is blank but second is not, means a word is there&lt;/span&gt;
            c&lt;span style=&quot;color: #808030;&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;    
        &lt;span style=&quot;color: purple;&quot;&gt;}&lt;/span&gt;
        &lt;span style=&quot;color: maroon; font-weight: 700;&quot;&gt;else&lt;/span&gt;
        &lt;span style=&quot;color: purple;&quot;&gt;{&lt;/span&gt;
            &lt;span style=&quot;color: dimgrey;&quot;&gt;// rest dont need to worry&lt;/span&gt;
            
        &lt;span style=&quot;color: purple;&quot;&gt;}&lt;/span&gt;
        &lt;span style=&quot;color: dimgrey;&quot;&gt;//increment both pointers&lt;/span&gt;
        p1&lt;span style=&quot;color: #808030;&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
        p2&lt;span style=&quot;color: #808030;&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
    &lt;span style=&quot;color: purple;&quot;&gt;}&lt;/span&gt;
    
    
    &lt;span style=&quot;color: maroon; font-weight: 700;&quot;&gt;return&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;c&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: purple;&quot;&gt;}&lt;/span&gt;

&lt;span style=&quot;color: maroon; font-weight: 700;&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: #400000;&quot;&gt;main&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;
&lt;span style=&quot;color: purple;&quot;&gt;{&lt;/span&gt;
    &lt;span style=&quot;color: maroon; font-weight: 700;&quot;&gt;char&lt;/span&gt; &lt;span style=&quot;color: #808030;&quot;&gt;*&lt;/span&gt;name&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
    &lt;span style=&quot;color: #603000;&quot;&gt;printf&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color: #0f69ff;&quot;&gt;\n&lt;/span&gt;&lt;span style=&quot;color: #0000e6;&quot;&gt; Enter a string= &lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
    &lt;span style=&quot;color: #603000;&quot;&gt;gets&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;name&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
    &lt;span style=&quot;color: #603000;&quot;&gt;printf&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color: #0f69ff;&quot;&gt;%s&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;,&lt;/span&gt;name&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
    &lt;span style=&quot;color: #603000;&quot;&gt;printf&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color: #0000e6;&quot;&gt;number of words=&lt;/span&gt;&lt;span style=&quot;color: #0f69ff;&quot;&gt;%d&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;,&lt;/span&gt;countWord&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;name&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: purple;&quot;&gt;}&lt;/span&gt;
&lt;/pre&gt;
&lt;/div&gt;</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7996672995157809874/posts/default/4367119279665468777'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7996672995157809874/posts/default/4367119279665468777'/><link rel='alternate' type='text/html' href='http://programs-in-c-plus-plus.blogspot.com/2011/12/counting-words-in-string-using-c.html' title='Counting Words in a String using C'/><author><name>HawkEye</name><uri>http://www.blogger.com/profile/15300086365850802171</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/AVvXsEiCrC_C7XyVmgg0V5wBtSGr1wZmtbpbdJovvmjCPstfM8C5vArLCAFyF7K3KGQAOVJfP4lv3VGRELWCAot0asDZOoiUM3IWSCd2gXev_lwz6MPiIlw2dxTw9TWaqjNbtL6IjAHptBUIL-c/s72-c/counting+words+in+String+using+C+program.jpg" height="72" width="72"/></entry><entry><id>tag:blogger.com,1999:blog-7996672995157809874.post-7746744510825723550</id><published>2011-12-08T00:38:00.001-08:00</published><updated>2012-01-05T00:46:52.317-08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="friend function"/><category scheme="http://www.blogger.com/atom/ns#" term="operator overloading"/><title type='text'>Operator Overloading using Friend Functions with Complex Numbers</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;b&gt;Operator overloading using friend functions:&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;br /&gt;
Operators can be overloaded in c++ with friend functions also. The procedure is same as we discussed earlier. Here I will demonstrate operator overloading with &quot;+&quot; operator which is a binary operator. I will be using a complex number class (called complex). The class will have 2 data types, real and imaginary.&lt;br /&gt;
Operator overloading will not be a part of class complex, but we will declare that this function is a friend of class complex.&lt;br /&gt;
&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;&lt;br /&gt;
Note that since overloaded function is not part of class , so will require 2 args (overloading with member function require 1 arg for binary operator, as the first arg is the object itself.)&lt;br /&gt;
&lt;b&gt;Use:&lt;/b&gt;&lt;br /&gt;
Most student wonder why we need to use operator overloading in such manner? The answer is simple, this will allow you to manipulate operator for predefined objects that you mat not have access. (Like cout or cin)&lt;br /&gt;
&lt;br /&gt;
Code:&lt;br /&gt;
&lt;br /&gt;
&lt;pre style=&quot;background: #ffffff; color: black;&quot;&gt;&lt;span style=&quot;color: #004a43;&quot;&gt;#&lt;/span&gt;&lt;span style=&quot;color: #004a43;&quot;&gt;include&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #40015a;&quot;&gt;iostream&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&amp;gt;&lt;/span&gt;


&lt;span style=&quot;color: maroon; font-weight: 700;&quot;&gt;using&lt;/span&gt; &lt;span style=&quot;color: maroon; font-weight: 700;&quot;&gt;namespace&lt;/span&gt; &lt;span style=&quot;color: #666616;&quot;&gt;std&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: dimgrey;&quot;&gt;// complex class with real and imaginary part&lt;/span&gt;
&lt;span style=&quot;color: maroon; font-weight: 700;&quot;&gt;class&lt;/span&gt; complex
&lt;span style=&quot;color: purple;&quot;&gt;{&lt;/span&gt;
&lt;span style=&quot;color: #e34adc;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;color: maroon; font-weight: 700;&quot;&gt;public&lt;/span&gt;&lt;span style=&quot;color: #e34adc;&quot;&gt;:&lt;/span&gt;
    &lt;span style=&quot;color: maroon; font-weight: 700;&quot;&gt;int&lt;/span&gt; real&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
    &lt;span style=&quot;color: maroon; font-weight: 700;&quot;&gt;int&lt;/span&gt; img&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
    &lt;span style=&quot;color: dimgrey;&quot;&gt;//default constructor&lt;/span&gt;
    complex&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;
    &lt;span style=&quot;color: purple;&quot;&gt;{&lt;/span&gt;
        real&lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt;img&lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #008c00;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;    
    &lt;span style=&quot;color: purple;&quot;&gt;}&lt;/span&gt;
    &lt;span style=&quot;color: dimgrey;&quot;&gt;//overloaded constructor with 2 args&lt;/span&gt;
    complex&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: maroon; font-weight: 700;&quot;&gt;int&lt;/span&gt; x&lt;span style=&quot;color: #808030;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: maroon; font-weight: 700;&quot;&gt;int&lt;/span&gt; y&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;
    &lt;span style=&quot;color: purple;&quot;&gt;{&lt;/span&gt;
        real&lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt;x&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
        img&lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt;y&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;    
    &lt;span style=&quot;color: purple;&quot;&gt;}&lt;/span&gt;
    &lt;span style=&quot;color: dimgrey;&quot;&gt;//show function to display complex number&lt;/span&gt;
    &lt;span style=&quot;color: maroon; font-weight: 700;&quot;&gt;void&lt;/span&gt; show&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;
    &lt;span style=&quot;color: purple;&quot;&gt;{&lt;/span&gt;
        &lt;span style=&quot;color: #603000;&quot;&gt;cout&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color: #0f69ff;&quot;&gt;\n&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt;real&lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color: #0000e6;&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt;img&lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color: #0000e6;&quot;&gt;i&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
    &lt;span style=&quot;color: purple;&quot;&gt;}&lt;/span&gt;
    &lt;span style=&quot;color: dimgrey;&quot;&gt;//declaring that &quot;opeartor+&quot; is friend of class complex&lt;/span&gt;
    &lt;span style=&quot;color: maroon; font-weight: 700;&quot;&gt;friend&lt;/span&gt; complex &lt;span style=&quot;color: maroon; font-weight: 700;&quot;&gt;operator&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;complex c&lt;span style=&quot;color: #808030;&quot;&gt;,&lt;/span&gt;complex d&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: purple;&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;

&lt;span style=&quot;color: dimgrey;&quot;&gt;//operator+ is not part of complex class so have 2 args for + operator overload.&lt;/span&gt;
complex &lt;span style=&quot;color: maroon; font-weight: 700;&quot;&gt;operator&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;complex c&lt;span style=&quot;color: #808030;&quot;&gt;,&lt;/span&gt; complex f&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;
&lt;span style=&quot;color: purple;&quot;&gt;{&lt;/span&gt;
    complex ans&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
    ans&lt;span style=&quot;color: #808030;&quot;&gt;.&lt;/span&gt;real&lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt;c&lt;span style=&quot;color: #808030;&quot;&gt;.&lt;/span&gt;real&lt;span style=&quot;color: #808030;&quot;&gt;+&lt;/span&gt;f&lt;span style=&quot;color: #808030;&quot;&gt;.&lt;/span&gt;real&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
    ans&lt;span style=&quot;color: #808030;&quot;&gt;.&lt;/span&gt;img&lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt;c&lt;span style=&quot;color: #808030;&quot;&gt;.&lt;/span&gt;img&lt;span style=&quot;color: #808030;&quot;&gt;+&lt;/span&gt;f&lt;span style=&quot;color: #808030;&quot;&gt;.&lt;/span&gt;img&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
    &lt;span style=&quot;color: maroon; font-weight: 700;&quot;&gt;return&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;ans&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: purple;&quot;&gt;}&lt;/span&gt;
&lt;span style=&quot;color: maroon; font-weight: 700;&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: #400000;&quot;&gt;main&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;
&lt;span style=&quot;color: purple;&quot;&gt;{&lt;/span&gt;
    complex x&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #008c00;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #008c00;&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;,&lt;/span&gt; y&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #008c00;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #008c00;&quot;&gt;7&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
    complex c&lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt;x&lt;span style=&quot;color: #808030;&quot;&gt;+&lt;/span&gt;y&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;&lt;span style=&quot;color: dimgrey;&quot;&gt;//overloaded + is called here&lt;/span&gt;
    &lt;span style=&quot;color: dimgrey;&quot;&gt;//note that compiler will convert this to&lt;/span&gt;
    &lt;span style=&quot;color: dimgrey;&quot;&gt;// c=operator+(x,y)&lt;/span&gt;
    c&lt;span style=&quot;color: #808030;&quot;&gt;.&lt;/span&gt;show&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: purple;&quot;&gt;}&lt;/span&gt;
&lt;/pre&gt;
&lt;/div&gt;</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7996672995157809874/posts/default/7746744510825723550'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7996672995157809874/posts/default/7746744510825723550'/><link rel='alternate' type='text/html' href='http://programs-in-c-plus-plus.blogspot.com/2011/12/operator-overloading-using-friend.html' title='Operator Overloading using Friend Functions with Complex Numbers'/><author><name>HawkEye</name><uri>http://www.blogger.com/profile/15300086365850802171</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></entry><entry><id>tag:blogger.com,1999:blog-7996672995157809874.post-873140424288940918</id><published>2011-11-29T01:58:00.001-08:00</published><updated>2014-10-28T04:52:14.004-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="c++"/><category scheme="http://www.blogger.com/atom/ns#" term="non recursive"/><category scheme="http://www.blogger.com/atom/ns#" term="prime"/><title type='text'>Find nth Prime Number in C++</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;b&gt;c++ program to find prime numbers:&lt;/b&gt;&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
The  problem of finding prime number can be solved by checking all numbers, testing them for prime and then moving ahead. If you want to calculate nth prime. Then this can be done in a brutal way by checking the number one by one. This may sound odd, by there is no easy way then this &amp;nbsp;for prime numbers (Well Actually there are like Pollard&#39;s Rho Algorithm, Number Sieves or Shor&#39;s Quantum Algorithm, but we are talking about the one that most people may understand easily). There may be way to pre-calculate the prime numbers but that again is not sufficient. So how can we use c++ to create a program to find prime numbers.&lt;br /&gt;
&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;&lt;br /&gt;
&lt;b&gt;Method:&lt;/b&gt;&lt;br /&gt;
The method is pretty similar to what i have discussed in my other post.&lt;br /&gt;
&lt;a href=&quot;http://programs-in-c-plus-plus.blogspot.com/2011/09/program-to-find-nth-prime-number-in-c.html&quot; target=&quot;_blank&quot;&gt;http://programs-in-c-plus-plus.blogspot.com/2011/09/program-to-find-nth-prime-number-in-c.html&lt;/a&gt;&lt;br /&gt;
To convert it into a c++ program , we will just encapsulate the main functions into one single class. That class will serve us for calculations. The class has a member variable &quot;n&quot; , which can be used to calculate &quot;nth&quot; prime. There will be 2 methods to set this &quot;n&quot; variable. One is an overloaded constructor, other is a member function. Choice is yours.&lt;br /&gt;
&lt;br /&gt;
Code:&lt;br /&gt;
&lt;br /&gt;
&lt;pre style=&quot;background: #ffffff; color: black;&quot;&gt;&lt;span style=&quot;color: #004a43;&quot;&gt;#&lt;/span&gt;&lt;span style=&quot;color: #004a43;&quot;&gt;include &lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #40015a;&quot;&gt;iostream&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&amp;gt;&lt;/span&gt;
&lt;span style=&quot;color: maroon; font-weight: 700;&quot;&gt;using&lt;/span&gt; &lt;span style=&quot;color: maroon; font-weight: 700;&quot;&gt;namespace&lt;/span&gt; &lt;span style=&quot;color: #666616;&quot;&gt;std&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;


&lt;span style=&quot;color: maroon; font-weight: 700;&quot;&gt;class&lt;/span&gt; prime
&lt;span style=&quot;color: purple;&quot;&gt;{&lt;/span&gt;
    &lt;span style=&quot;color: maroon; font-weight: 700;&quot;&gt;int&lt;/span&gt; n&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;&lt;span style=&quot;color: dimgrey;&quot;&gt;// this will be used to find the nth prime&lt;/span&gt;
&lt;span style=&quot;color: #e34adc;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;color: maroon; font-weight: 700;&quot;&gt;public&lt;/span&gt;&lt;span style=&quot;color: #e34adc;&quot;&gt;:&lt;/span&gt;
    prime&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;
    &lt;span style=&quot;color: purple;&quot;&gt;{&lt;/span&gt;
        n&lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #008c00;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;&lt;span style=&quot;color: dimgrey;&quot;&gt;//default value 1&lt;/span&gt;
    &lt;span style=&quot;color: purple;&quot;&gt;}&lt;/span&gt;
    prime&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: maroon; font-weight: 700;&quot;&gt;int&lt;/span&gt; t&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;
    &lt;span style=&quot;color: purple;&quot;&gt;{&lt;/span&gt;
        n&lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt;t&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;&lt;span style=&quot;color: dimgrey;&quot;&gt;//set n by using constructor&lt;/span&gt;
    &lt;span style=&quot;color: purple;&quot;&gt;}&lt;/span&gt;
    &lt;span style=&quot;color: maroon; font-weight: 700;&quot;&gt;void&lt;/span&gt; setGoal&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: maroon; font-weight: 700;&quot;&gt;int&lt;/span&gt; k&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;
    &lt;span style=&quot;color: purple;&quot;&gt;{&lt;/span&gt;
        n&lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt;k&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;&lt;span style=&quot;color: dimgrey;&quot;&gt;//set n by function&lt;/span&gt;
    &lt;span style=&quot;color: purple;&quot;&gt;}&lt;/span&gt;
    &lt;span style=&quot;color: maroon; font-weight: 700;&quot;&gt;void&lt;/span&gt; &lt;span style=&quot;color: #603000;&quot;&gt;find&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: dimgrey;&quot;&gt;// find the prime&lt;/span&gt;
    &lt;span style=&quot;color: purple;&quot;&gt;{&lt;/span&gt;
        &lt;span style=&quot;color: maroon; font-weight: 700;&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: #603000;&quot;&gt;count&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #008c00;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
        &lt;span style=&quot;color: maroon; font-weight: 700;&quot;&gt;long&lt;/span&gt; a &lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #008c00;&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
            
        &lt;span style=&quot;color: maroon; font-weight: 700;&quot;&gt;while&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #603000;&quot;&gt;count&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt;n&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;
        &lt;span style=&quot;color: purple;&quot;&gt;{&lt;/span&gt;
            
            &lt;span style=&quot;color: maroon; font-weight: 700;&quot;&gt;long&lt;/span&gt; b &lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #008c00;&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
            &lt;span style=&quot;color: maroon; font-weight: 700;&quot;&gt;int&lt;/span&gt; prime &lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #008c00;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
            &lt;span style=&quot;color: maroon; font-weight: 700;&quot;&gt;while&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;b &lt;span style=&quot;color: #808030;&quot;&gt;*&lt;/span&gt; b &lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt; a&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;
            &lt;span style=&quot;color: purple;&quot;&gt;{&lt;/span&gt;
                &lt;span style=&quot;color: maroon; font-weight: 700;&quot;&gt;if&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;a &lt;span style=&quot;color: #808030;&quot;&gt;%&lt;/span&gt; b &lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #008c00;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;
                &lt;span style=&quot;color: purple;&quot;&gt;{&lt;/span&gt;
                    prime &lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #008c00;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
                    &lt;span style=&quot;color: maroon; font-weight: 700;&quot;&gt;break&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
                &lt;span style=&quot;color: purple;&quot;&gt;}&lt;/span&gt;
                b&lt;span style=&quot;color: #808030;&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
            &lt;span style=&quot;color: purple;&quot;&gt;}&lt;/span&gt;
            &lt;span style=&quot;color: maroon; font-weight: 700;&quot;&gt;if&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;prime&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;
            &lt;span style=&quot;color: purple;&quot;&gt;{&lt;/span&gt;
                &lt;span style=&quot;color: #603000;&quot;&gt;count&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
            &lt;span style=&quot;color: purple;&quot;&gt;}&lt;/span&gt;
            a&lt;span style=&quot;color: #808030;&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
        &lt;span style=&quot;color: purple;&quot;&gt;}&lt;/span&gt;
        &lt;span style=&quot;color: #603000;&quot;&gt;cout&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color: #0f69ff;&quot;&gt;\n&lt;/span&gt;&lt;span style=&quot;color: #0000e6;&quot;&gt; The &lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt;n&lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color: #0000e6;&quot;&gt; th prime=&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;-&lt;/span&gt;a&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
    &lt;span style=&quot;color: purple;&quot;&gt;}&lt;/span&gt;
&lt;span style=&quot;color: purple;&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: maroon; font-weight: 700;&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: #400000;&quot;&gt;main&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;
&lt;span style=&quot;color: purple;&quot;&gt;{&lt;/span&gt;
    prime p&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
    p&lt;span style=&quot;color: #808030;&quot;&gt;.&lt;/span&gt;setGoal&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #008c00;&quot;&gt;10&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
    p&lt;span style=&quot;color: #808030;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #603000;&quot;&gt;find&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;

&lt;span style=&quot;color: purple;&quot;&gt;}&lt;/span&gt;
&lt;/pre&gt;
&lt;/div&gt;
</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7996672995157809874/posts/default/873140424288940918'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7996672995157809874/posts/default/873140424288940918'/><link rel='alternate' type='text/html' href='http://programs-in-c-plus-plus.blogspot.com/2011/11/c-program-to-find-prime-numbers.html' title='Find nth Prime Number in C++'/><author><name>HawkEye</name><uri>http://www.blogger.com/profile/15300086365850802171</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></entry><entry><id>tag:blogger.com,1999:blog-7996672995157809874.post-7322318854668388567</id><published>2011-11-28T20:44:00.001-08:00</published><updated>2012-02-15T18:56:14.455-08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="c++"/><category scheme="http://www.blogger.com/atom/ns#" term="function overriding"/><title type='text'>Program for Multi Level Inheritance and Function Overriding</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;b&gt;Program for Multi Level Inheritance and Function Overriding:&lt;/b&gt;&lt;br /&gt;
Function overriding is a very key programming feature in OOP system. This allows programmers to create objects which has similar function ( with same name) but having different implementations. Such technique is really helpful in situations where each object has a common function but implementations ( games for example).&lt;br /&gt;
&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;&lt;br /&gt;
&lt;b&gt;Multi Level Inheritance:&lt;/b&gt;&lt;br /&gt;
In this type of inheritance one class inherits B inherits a base class A. After that B acts as a base class for next class C. So B inherits A, C inherits B.&lt;br /&gt;
in this example we have a base class &quot;animal&quot;.&lt;br /&gt;
The next class &quot;Tiger&quot; inherits &quot;animal&quot;.&lt;br /&gt;
Then &quot;SaberTooth&quot; inherits &quot;Tiger&quot;.&lt;br /&gt;
Both Tiger and SaberTooth get the functionality of Base class Animal.&lt;br /&gt;
&lt;b&gt;Function Overriding:&lt;/b&gt;&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;br /&gt;
This is the feature that can change the behaviour of inherited function in derived class. Here we will change the inherited function &quot;attack()&quot;. Note that overridden function may have completely different functioning then original&lt;br /&gt;
&lt;br /&gt;
Code:&lt;br /&gt;
&lt;br /&gt;
&lt;pre style=&quot;background: #ffffff; color: black;&quot;&gt;&lt;span style=&quot;color: #004a43;&quot;&gt;#&lt;/span&gt;&lt;span style=&quot;color: #004a43;&quot;&gt;include&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #40015a;&quot;&gt;iostream&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&amp;gt;&lt;/span&gt;

&lt;span style=&quot;color: maroon; font-weight: 700;&quot;&gt;using&lt;/span&gt; &lt;span style=&quot;color: maroon; font-weight: 700;&quot;&gt;namespace&lt;/span&gt; &lt;span style=&quot;color: #666616;&quot;&gt;std&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;

&lt;span style=&quot;color: dimgrey;&quot;&gt;//--Our base class&lt;/span&gt;
&lt;span style=&quot;color: maroon; font-weight: 700;&quot;&gt;class&lt;/span&gt; animal
&lt;span style=&quot;color: purple;&quot;&gt;{&lt;/span&gt;
&lt;span style=&quot;color: #e34adc;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;color: maroon; font-weight: 700;&quot;&gt;public&lt;/span&gt;&lt;span style=&quot;color: #e34adc;&quot;&gt;:&lt;/span&gt;
    &lt;span style=&quot;color: maroon; font-weight: 700;&quot;&gt;int&lt;/span&gt; life&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;&lt;span style=&quot;color: dimgrey;&quot;&gt;// base class has an attribute that will get passed to derived classes&lt;/span&gt;
    animal&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;
    &lt;span style=&quot;color: purple;&quot;&gt;{&lt;/span&gt;
        life&lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #008c00;&quot;&gt;100&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;        
    &lt;span style=&quot;color: purple;&quot;&gt;}&lt;/span&gt;
    &lt;span style=&quot;color: maroon; font-weight: 700;&quot;&gt;void&lt;/span&gt; say&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;
    &lt;span style=&quot;color: purple;&quot;&gt;{&lt;/span&gt;
        &lt;span style=&quot;color: #603000;&quot;&gt;cout&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color: #0f69ff;&quot;&gt;\n&lt;/span&gt;&lt;span style=&quot;color: #0000e6;&quot;&gt; This is Animal Class with Health=&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt;life&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
    &lt;span style=&quot;color: purple;&quot;&gt;}&lt;/span&gt;
    &lt;span style=&quot;color: maroon; font-weight: 700;&quot;&gt;void&lt;/span&gt; showHealth&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;
    &lt;span style=&quot;color: purple;&quot;&gt;{&lt;/span&gt;
        &lt;span style=&quot;color: #603000;&quot;&gt;cout&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color: #0f69ff;&quot;&gt;\n&lt;/span&gt;&lt;span style=&quot;color: #0000e6;&quot;&gt; Health=&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt;life&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
    &lt;span style=&quot;color: purple;&quot;&gt;}&lt;/span&gt;
&lt;span style=&quot;color: purple;&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;

&lt;span style=&quot;color: dimgrey;&quot;&gt;//---Tiger class inherits animal class so gets all his public members:&lt;/span&gt;
&lt;span style=&quot;color: dimgrey;&quot;&gt;//---life&lt;/span&gt;
&lt;span style=&quot;color: dimgrey;&quot;&gt;//--say()&lt;/span&gt;
&lt;span style=&quot;color: dimgrey;&quot;&gt;//---showHealth()&lt;/span&gt;
&lt;span style=&quot;color: maroon; font-weight: 700;&quot;&gt;class&lt;/span&gt; Tiger &lt;span style=&quot;color: purple;&quot;&gt;:&lt;/span&gt; &lt;span style=&quot;color: maroon; font-weight: 700;&quot;&gt;public&lt;/span&gt; animal
&lt;span style=&quot;color: purple;&quot;&gt;{&lt;/span&gt;
&lt;span style=&quot;color: #e34adc;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;color: maroon; font-weight: 700;&quot;&gt;public&lt;/span&gt;&lt;span style=&quot;color: #e34adc;&quot;&gt;:&lt;/span&gt;
    Tiger&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;
    &lt;span style=&quot;color: purple;&quot;&gt;{&lt;/span&gt;
        
    &lt;span style=&quot;color: purple;&quot;&gt;}&lt;/span&gt;
    &lt;span style=&quot;color: maroon; font-weight: 700;&quot;&gt;void&lt;/span&gt; attack&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;
    &lt;span style=&quot;color: purple;&quot;&gt;{&lt;/span&gt;
        &lt;span style=&quot;color: #603000;&quot;&gt;cout&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color: #0f69ff;&quot;&gt;\n&lt;/span&gt;&lt;span style=&quot;color: #0000e6;&quot;&gt; This class can Attack !!&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
    &lt;span style=&quot;color: purple;&quot;&gt;}&lt;/span&gt;
&lt;span style=&quot;color: purple;&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: dimgrey;&quot;&gt;//--this class inherits tiger class also gets his public members:&lt;/span&gt;
&lt;span style=&quot;color: dimgrey;&quot;&gt;//--attack()&lt;/span&gt;
&lt;span style=&quot;color: dimgrey;&quot;&gt;//--this class has its own member data bigFangs&lt;/span&gt;
&lt;span style=&quot;color: dimgrey;&quot;&gt;//--this also overrrides the inherited attack function&lt;/span&gt;
&lt;span style=&quot;color: maroon; font-weight: 700;&quot;&gt;class&lt;/span&gt; SaberTooth &lt;span style=&quot;color: purple;&quot;&gt;:&lt;/span&gt; &lt;span style=&quot;color: maroon; font-weight: 700;&quot;&gt;public&lt;/span&gt; Tiger
&lt;span style=&quot;color: purple;&quot;&gt;{&lt;/span&gt;
&lt;span style=&quot;color: #e34adc;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;color: maroon; font-weight: 700;&quot;&gt;public&lt;/span&gt;&lt;span style=&quot;color: #e34adc;&quot;&gt;:&lt;/span&gt;
    &lt;span style=&quot;color: maroon; font-weight: 700;&quot;&gt;int&lt;/span&gt; bigFangs&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
    SaberTooth&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;
    &lt;span style=&quot;color: purple;&quot;&gt;{&lt;/span&gt;
        bigFangs&lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #008c00;&quot;&gt;10&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
    &lt;span style=&quot;color: purple;&quot;&gt;}&lt;/span&gt;
    &lt;span style=&quot;color: dimgrey;&quot;&gt;// redefining inherited method will overrride it&lt;/span&gt;
    &lt;span style=&quot;color: maroon; font-weight: 700;&quot;&gt;void&lt;/span&gt; attack&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;
    &lt;span style=&quot;color: purple;&quot;&gt;{&lt;/span&gt;
        &lt;span style=&quot;color: #603000;&quot;&gt;cout&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color: #0f69ff;&quot;&gt;\n&lt;/span&gt;&lt;span style=&quot;color: #0000e6;&quot;&gt; this class can attack with &lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt;bigFangs&lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color: #0000e6;&quot;&gt; inch Jawz&lt;/span&gt;&lt;span style=&quot;color: #0f69ff;&quot;&gt;\n&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
    &lt;span style=&quot;color: purple;&quot;&gt;}&lt;/span&gt;
    
&lt;span style=&quot;color: purple;&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;

&lt;span style=&quot;color: maroon; font-weight: 700;&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: #400000;&quot;&gt;main&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;
&lt;span style=&quot;color: purple;&quot;&gt;{&lt;/span&gt;
    animal a&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
    a&lt;span style=&quot;color: #808030;&quot;&gt;.&lt;/span&gt;say&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
    a&lt;span style=&quot;color: #808030;&quot;&gt;.&lt;/span&gt;showHealth&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
    
    &lt;span style=&quot;color: #603000;&quot;&gt;cout&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color: #0f69ff;&quot;&gt;\n&lt;/span&gt;&lt;span style=&quot;color: #0000e6;&quot;&gt;---- Tiger Class-----&lt;/span&gt;&lt;span style=&quot;color: #0f69ff;&quot;&gt;\n&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
    Tiger t&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
    t&lt;span style=&quot;color: #808030;&quot;&gt;.&lt;/span&gt;showHealth&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;&lt;span style=&quot;color: dimgrey;&quot;&gt;// &amp;lt;--gets it from animal class&lt;/span&gt;
    t&lt;span style=&quot;color: #808030;&quot;&gt;.&lt;/span&gt;attack&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;&lt;span style=&quot;color: dimgrey;&quot;&gt;//&amp;lt;--its own&lt;/span&gt;
    &lt;span style=&quot;color: #603000;&quot;&gt;cout&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color: #0f69ff;&quot;&gt;\n&lt;/span&gt;&lt;span style=&quot;color: #0000e6;&quot;&gt;---- SaberTooth Class-----&lt;/span&gt;&lt;span style=&quot;color: #0f69ff;&quot;&gt;\n&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
    SaberTooth s&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
    s&lt;span style=&quot;color: #808030;&quot;&gt;.&lt;/span&gt;showHealth&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;&lt;span style=&quot;color: dimgrey;&quot;&gt;//&amp;lt;--from base class to tiger then to this&lt;/span&gt;
    s&lt;span style=&quot;color: #808030;&quot;&gt;.&lt;/span&gt;attack&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;&lt;span style=&quot;color: dimgrey;&quot;&gt;//&amp;lt;--Overrrided method&lt;/span&gt;
&lt;span style=&quot;color: purple;&quot;&gt;}&lt;/span&gt;
&lt;/pre&gt;
&lt;br /&gt;
output:&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/AVvXsEiJVI2DL32k5RA0GIlEm10uYH6sz8halcXKG8f8dxxMs49t8ctl4FVkKxP-g2-isWonslXEAmoRtD-uBgxz1dy0EeUnyulfwv70UXpuO6TZ7jkfyqiCab6PLFHtPBp_EYpfsU1XlF_9B0U/s1600/inheritance-function-overriding-in-c%252B%252B-program.jpg&quot; imageanchor=&quot;1&quot; style=&quot;clear: left; float: left; margin-bottom: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;196&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiJVI2DL32k5RA0GIlEm10uYH6sz8halcXKG8f8dxxMs49t8ctl4FVkKxP-g2-isWonslXEAmoRtD-uBgxz1dy0EeUnyulfwv70UXpuO6TZ7jkfyqiCab6PLFHtPBp_EYpfsU1XlF_9B0U/s320/inheritance-function-overriding-in-c%252B%252B-program.jpg&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;/div&gt;</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7996672995157809874/posts/default/7322318854668388567'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7996672995157809874/posts/default/7322318854668388567'/><link rel='alternate' type='text/html' href='http://programs-in-c-plus-plus.blogspot.com/2011/11/program-for-multi-level-inheritance-and.html' title='Program for Multi Level Inheritance and Function Overriding'/><author><name>HawkEye</name><uri>http://www.blogger.com/profile/15300086365850802171</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/AVvXsEiJVI2DL32k5RA0GIlEm10uYH6sz8halcXKG8f8dxxMs49t8ctl4FVkKxP-g2-isWonslXEAmoRtD-uBgxz1dy0EeUnyulfwv70UXpuO6TZ7jkfyqiCab6PLFHtPBp_EYpfsU1XlF_9B0U/s72-c/inheritance-function-overriding-in-c%252B%252B-program.jpg" height="72" width="72"/></entry><entry><id>tag:blogger.com,1999:blog-7996672995157809874.post-4567519208457759192</id><published>2011-11-27T23:58:00.001-08:00</published><updated>2011-11-27T23:58:49.332-08:00</updated><title type='text'>News: All Code now have color highlighting</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
I have updated my codes to now have color highlighting, This will make code look a little better :)&lt;br /&gt;
Cheers&lt;/div&gt;</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7996672995157809874/posts/default/4567519208457759192'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7996672995157809874/posts/default/4567519208457759192'/><link rel='alternate' type='text/html' href='http://programs-in-c-plus-plus.blogspot.com/2011/11/news-all-code-now-have-color.html' title='News: All Code now have color highlighting'/><author><name>HawkEye</name><uri>http://www.blogger.com/profile/15300086365850802171</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></entry><entry><id>tag:blogger.com,1999:blog-7996672995157809874.post-5788857381236823976</id><published>2011-11-27T23:13:00.001-08:00</published><updated>2011-11-29T03:21:13.078-08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="c++"/><category scheme="http://www.blogger.com/atom/ns#" term="matrix"/><category scheme="http://www.blogger.com/atom/ns#" term="non recursive"/><category scheme="http://www.blogger.com/atom/ns#" term="operator overloading"/><title type='text'>Program for Matrix Multiplication in C++ using Operator Overloading</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;b&gt;Program for Matrix Multiplication in C++ using Operator Overloading :&lt;/b&gt;&lt;br /&gt;
We will now try to multiply two matrix by using the concept of operator overloading. The described way is very very easy to understand. We will be overloading &quot;*&quot; operator for this purpose. Although this can be done by any binary operator.&lt;br /&gt;
&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;&lt;br /&gt;
We will start with a basic class Matrix. In this example we will take a 3x3 matrix.&lt;br /&gt;
Description:&lt;br /&gt;
&lt;ul style=&quot;text-align: left;&quot;&gt;
&lt;li&gt;class Matrix has a constructor that will initialize its element to 0.&lt;/li&gt;
&lt;li&gt;also has a set() function that can be used to set values in the Matrix.&lt;/li&gt;
&lt;li&gt;A show() function shows the matrix.&lt;/li&gt;
&lt;li&gt;operator overloading definition for &quot;*&quot; operator.&lt;/li&gt;
&lt;/ul&gt;
&lt;br /&gt;
Code:&lt;br /&gt;
&lt;br /&gt;
&lt;pre style=&quot;background: #ffffff; color: black;&quot;&gt;&lt;span style=&quot;color: #004a43;&quot;&gt;#&lt;/span&gt;&lt;span style=&quot;color: #004a43;&quot;&gt;include&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #40015a;&quot;&gt;iostream&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&amp;gt;&lt;/span&gt;

&lt;span style=&quot;color: maroon; font-weight: 700;&quot;&gt;using&lt;/span&gt; &lt;span style=&quot;color: maroon; font-weight: 700;&quot;&gt;namespace&lt;/span&gt; &lt;span style=&quot;color: #666616;&quot;&gt;std&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;

&lt;span style=&quot;color: maroon; font-weight: 700;&quot;&gt;class&lt;/span&gt; matrix
&lt;span style=&quot;color: purple;&quot;&gt;{&lt;/span&gt;
    
&lt;span style=&quot;color: #e34adc;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;color: maroon; font-weight: 700;&quot;&gt;public&lt;/span&gt;&lt;span style=&quot;color: #e34adc;&quot;&gt;:&lt;/span&gt;
    &lt;span style=&quot;color: maroon; font-weight: 700;&quot;&gt;int&lt;/span&gt; a&lt;span style=&quot;color: #808030;&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color: #008c00;&quot;&gt;3&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color: #008c00;&quot;&gt;3&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
    
    matrix&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: dimgrey;&quot;&gt;//default constructor&lt;/span&gt;
    &lt;span style=&quot;color: purple;&quot;&gt;{&lt;/span&gt;
        &lt;span style=&quot;color: maroon; font-weight: 700;&quot;&gt;for&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: maroon; font-weight: 700;&quot;&gt;int&lt;/span&gt; i&lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #008c00;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;i&lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #008c00;&quot;&gt;3&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;i&lt;span style=&quot;color: #808030;&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;
        &lt;span style=&quot;color: purple;&quot;&gt;{&lt;/span&gt;
            &lt;span style=&quot;color: maroon; font-weight: 700;&quot;&gt;for&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: maroon; font-weight: 700;&quot;&gt;int&lt;/span&gt; j&lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #008c00;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;j&lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #008c00;&quot;&gt;3&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;j&lt;span style=&quot;color: #808030;&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;
            &lt;span style=&quot;color: purple;&quot;&gt;{&lt;/span&gt;
                a&lt;span style=&quot;color: #808030;&quot;&gt;[&lt;/span&gt;i&lt;span style=&quot;color: #808030;&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;[&lt;/span&gt;j&lt;span style=&quot;color: #808030;&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #008c00;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
            &lt;span style=&quot;color: purple;&quot;&gt;}&lt;/span&gt;
        &lt;span style=&quot;color: purple;&quot;&gt;}&lt;/span&gt;
    &lt;span style=&quot;color: purple;&quot;&gt;}&lt;/span&gt;
    &lt;span style=&quot;color: maroon; font-weight: 700;&quot;&gt;void&lt;/span&gt; &lt;span style=&quot;color: #603000;&quot;&gt;set&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: dimgrey;&quot;&gt;// to set matrix elements&lt;/span&gt;
    &lt;span style=&quot;color: purple;&quot;&gt;{&lt;/span&gt;
        &lt;span style=&quot;color: maroon; font-weight: 700;&quot;&gt;for&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: maroon; font-weight: 700;&quot;&gt;int&lt;/span&gt; i&lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #008c00;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;i&lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #008c00;&quot;&gt;3&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;i&lt;span style=&quot;color: #808030;&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;
        &lt;span style=&quot;color: purple;&quot;&gt;{&lt;/span&gt;
            &lt;span style=&quot;color: maroon; font-weight: 700;&quot;&gt;for&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: maroon; font-weight: 700;&quot;&gt;int&lt;/span&gt; j&lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #008c00;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;j&lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #008c00;&quot;&gt;3&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;j&lt;span style=&quot;color: #808030;&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;
            &lt;span style=&quot;color: purple;&quot;&gt;{&lt;/span&gt;
                &lt;span style=&quot;color: #603000;&quot;&gt;cout&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color: #0f69ff;&quot;&gt;\n&lt;/span&gt;&lt;span style=&quot;color: #0000e6;&quot;&gt; Enter &lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt;i&lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color: #0000e6;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt;j&lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color: #0000e6;&quot;&gt; element=&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
                &lt;span style=&quot;color: #603000;&quot;&gt;cin&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;&amp;gt;&lt;/span&gt;a&lt;span style=&quot;color: #808030;&quot;&gt;[&lt;/span&gt;i&lt;span style=&quot;color: #808030;&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;[&lt;/span&gt;j&lt;span style=&quot;color: #808030;&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
            &lt;span style=&quot;color: purple;&quot;&gt;}&lt;/span&gt;
        &lt;span style=&quot;color: purple;&quot;&gt;}&lt;/span&gt;
    &lt;span style=&quot;color: purple;&quot;&gt;}&lt;/span&gt;
    &lt;span style=&quot;color: maroon; font-weight: 700;&quot;&gt;void&lt;/span&gt; show&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: dimgrey;&quot;&gt;// to show matrix elements&lt;/span&gt;
    &lt;span style=&quot;color: purple;&quot;&gt;{&lt;/span&gt;
        &lt;span style=&quot;color: #603000;&quot;&gt;cout&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color: #0f69ff;&quot;&gt;\n&lt;/span&gt;&lt;span style=&quot;color: #0000e6;&quot;&gt; Matrix is=&lt;/span&gt;&lt;span style=&quot;color: #0f69ff;&quot;&gt;\n&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
        &lt;span style=&quot;color: maroon; font-weight: 700;&quot;&gt;for&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: maroon; font-weight: 700;&quot;&gt;int&lt;/span&gt; i&lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #008c00;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;i&lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #008c00;&quot;&gt;3&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;i&lt;span style=&quot;color: #808030;&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;
        &lt;span style=&quot;color: purple;&quot;&gt;{&lt;/span&gt;
            &lt;span style=&quot;color: maroon; font-weight: 700;&quot;&gt;for&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: maroon; font-weight: 700;&quot;&gt;int&lt;/span&gt; j&lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #008c00;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;j&lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #008c00;&quot;&gt;3&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;j&lt;span style=&quot;color: #808030;&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;
            &lt;span style=&quot;color: purple;&quot;&gt;{&lt;/span&gt;
                
                &lt;span style=&quot;color: #603000;&quot;&gt;cout&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt;a&lt;span style=&quot;color: #808030;&quot;&gt;[&lt;/span&gt;i&lt;span style=&quot;color: #808030;&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;[&lt;/span&gt;j&lt;span style=&quot;color: #808030;&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color: #0000e6;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
            &lt;span style=&quot;color: purple;&quot;&gt;}&lt;/span&gt;
            &lt;span style=&quot;color: #603000;&quot;&gt;cout&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color: #0f69ff;&quot;&gt;\n&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
        &lt;span style=&quot;color: purple;&quot;&gt;}&lt;/span&gt;
    &lt;span style=&quot;color: purple;&quot;&gt;}&lt;/span&gt;
    &lt;span style=&quot;color: dimgrey;&quot;&gt;/*&lt;/span&gt;
&lt;span style=&quot;color: dimgrey;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;binary * operator will require one more arg, since it is a binary operator&lt;/span&gt;
&lt;span style=&quot;color: dimgrey;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;one arg is the object itself that will call it, other will be passed as arg(in this case x). also this will return a matrix object&lt;/span&gt;
&lt;span style=&quot;color: dimgrey;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;*/&lt;/span&gt;
    matrix &lt;span style=&quot;color: maroon; font-weight: 700;&quot;&gt;operator&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;matrix x&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: dimgrey;&quot;&gt;// overloading * for multiplication&lt;/span&gt;
    &lt;span style=&quot;color: purple;&quot;&gt;{&lt;/span&gt;
        matrix c&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;&lt;span style=&quot;color: dimgrey;&quot;&gt;// this will hold our result&lt;/span&gt;
        &lt;span style=&quot;color: maroon; font-weight: 700;&quot;&gt;for&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: maroon; font-weight: 700;&quot;&gt;int&lt;/span&gt; i&lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #008c00;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;i&lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #008c00;&quot;&gt;3&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;i&lt;span style=&quot;color: #808030;&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;
        &lt;span style=&quot;color: purple;&quot;&gt;{&lt;/span&gt;
            &lt;span style=&quot;color: maroon; font-weight: 700;&quot;&gt;for&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: maroon; font-weight: 700;&quot;&gt;int&lt;/span&gt; j&lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #008c00;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;j&lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #008c00;&quot;&gt;3&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;j&lt;span style=&quot;color: #808030;&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;
            &lt;span style=&quot;color: purple;&quot;&gt;{&lt;/span&gt;
                c&lt;span style=&quot;color: #808030;&quot;&gt;.&lt;/span&gt;a&lt;span style=&quot;color: #808030;&quot;&gt;[&lt;/span&gt;i&lt;span style=&quot;color: #808030;&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;[&lt;/span&gt;j&lt;span style=&quot;color: #808030;&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #008c00;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
                &lt;span style=&quot;color: maroon; font-weight: 700;&quot;&gt;for&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: maroon; font-weight: 700;&quot;&gt;int&lt;/span&gt; k&lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #008c00;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;k&lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #008c00;&quot;&gt;3&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;k&lt;span style=&quot;color: #808030;&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;
                &lt;span style=&quot;color: purple;&quot;&gt;{&lt;/span&gt;
                    c&lt;span style=&quot;color: #808030;&quot;&gt;.&lt;/span&gt;a&lt;span style=&quot;color: #808030;&quot;&gt;[&lt;/span&gt;i&lt;span style=&quot;color: #808030;&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;[&lt;/span&gt;j&lt;span style=&quot;color: #808030;&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt;c&lt;span style=&quot;color: #808030;&quot;&gt;.&lt;/span&gt;a&lt;span style=&quot;color: #808030;&quot;&gt;[&lt;/span&gt;i&lt;span style=&quot;color: #808030;&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;[&lt;/span&gt;j&lt;span style=&quot;color: #808030;&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;+&lt;/span&gt;a&lt;span style=&quot;color: #808030;&quot;&gt;[&lt;/span&gt;i&lt;span style=&quot;color: #808030;&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;[&lt;/span&gt;k&lt;span style=&quot;color: #808030;&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;*&lt;/span&gt;x&lt;span style=&quot;color: #808030;&quot;&gt;.&lt;/span&gt;a&lt;span style=&quot;color: #808030;&quot;&gt;[&lt;/span&gt;k&lt;span style=&quot;color: #808030;&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;[&lt;/span&gt;j&lt;span style=&quot;color: #808030;&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;        
                &lt;span style=&quot;color: purple;&quot;&gt;}&lt;/span&gt;
            &lt;span style=&quot;color: purple;&quot;&gt;}&lt;/span&gt;
        &lt;span style=&quot;color: purple;&quot;&gt;}&lt;/span&gt;
        &lt;span style=&quot;color: maroon; font-weight: 700;&quot;&gt;return&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;c&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
        
    &lt;span style=&quot;color: purple;&quot;&gt;}&lt;/span&gt;
&lt;span style=&quot;color: purple;&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;

&lt;span style=&quot;color: maroon; font-weight: 700;&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: #400000;&quot;&gt;main&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;
&lt;span style=&quot;color: purple;&quot;&gt;{&lt;/span&gt;
    matrix a&lt;span style=&quot;color: #808030;&quot;&gt;,&lt;/span&gt;b&lt;span style=&quot;color: #808030;&quot;&gt;,&lt;/span&gt;c&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
    a&lt;span style=&quot;color: #808030;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #603000;&quot;&gt;set&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
    b&lt;span style=&quot;color: #808030;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #603000;&quot;&gt;set&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
    c&lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt;a&lt;span style=&quot;color: #808030;&quot;&gt;*&lt;/span&gt;b&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
    &lt;span style=&quot;color: dimgrey;&quot;&gt;/*&lt;/span&gt;
&lt;span style=&quot;color: dimgrey;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;note that compiler will break this statement as &lt;/span&gt;
&lt;span style=&quot;color: dimgrey;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;c=a.operator*(b);&lt;/span&gt;
&lt;span style=&quot;color: dimgrey;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;this is how 2nd arg is passed.&lt;/span&gt;
&lt;span style=&quot;color: dimgrey;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;and this is how object &quot;a&quot; acts as the calling object&lt;/span&gt;
&lt;span style=&quot;color: dimgrey;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;*/&lt;/span&gt;
    
    a&lt;span style=&quot;color: #808030;&quot;&gt;.&lt;/span&gt;show&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
    b&lt;span style=&quot;color: #808030;&quot;&gt;.&lt;/span&gt;show&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
    c&lt;span style=&quot;color: #808030;&quot;&gt;.&lt;/span&gt;show&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: purple;&quot;&gt;}&lt;/span&gt;
&lt;/pre&gt;
&lt;/div&gt;</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7996672995157809874/posts/default/5788857381236823976'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7996672995157809874/posts/default/5788857381236823976'/><link rel='alternate' type='text/html' href='http://programs-in-c-plus-plus.blogspot.com/2011/11/program-for-matrix-multiplication-in-c.html' title='Program for Matrix Multiplication in C++ using Operator Overloading'/><author><name>HawkEye</name><uri>http://www.blogger.com/profile/15300086365850802171</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></entry><entry><id>tag:blogger.com,1999:blog-7996672995157809874.post-3210253069283371558</id><published>2011-11-21T02:01:00.001-08:00</published><updated>2011-11-29T03:25:21.169-08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="c"/><category scheme="http://www.blogger.com/atom/ns#" term="stack"/><title type='text'>Stack in C using functions</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;b&gt;Stack in C&lt;/b&gt;: Stack is a very useful data structure in C, It is used in many situations. Implementing stack can be done using a global array and a global pointer of the stack. although this implementation reveals how good OOP is for such situations. There will be three basic functions of the stack. One will be push() operation, which will push the given element to the stack. Pop() function &amp;nbsp;will pop out the element from the top. A show function to show the content of the stack.&lt;br /&gt;
&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;&lt;br /&gt;
The size of the stack will be controlled by a preprocessor called SIZE. we will always check for boundaries of the stack. Modular approach for such situation is not safe. A better OO way is there, that I will discuss later.&lt;br /&gt;
The code to implement Stack in C using functions is as follows :-&lt;br /&gt;
&lt;br /&gt;
Code :&lt;br /&gt;
&lt;pre style=&quot;background: #ffffff; color: black;&quot;&gt;&lt;span style=&quot;color: #004a43;&quot;&gt;#&lt;/span&gt;&lt;span style=&quot;color: #004a43;&quot;&gt;include&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #40015a;&quot;&gt;stdio.h&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&amp;gt;&lt;/span&gt;
&lt;span style=&quot;color: #004a43;&quot;&gt;#&lt;/span&gt;&lt;span style=&quot;color: #004a43;&quot;&gt;include&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #40015a;&quot;&gt;dos.h&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&amp;gt;&lt;/span&gt;

&lt;span style=&quot;color: #004a43;&quot;&gt;#&lt;/span&gt;&lt;span style=&quot;color: #004a43;&quot;&gt;define&lt;/span&gt;&lt;span style=&quot;color: #004a43;&quot;&gt; SIZE 5&lt;/span&gt;
&lt;span style=&quot;color: maroon; font-weight: 700;&quot;&gt;int&lt;/span&gt; cursor&lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #008c00;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;&lt;span style=&quot;color: dimgrey;&quot;&gt;//global&lt;/span&gt;
&lt;span style=&quot;color: maroon; font-weight: 700;&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: #603000;&quot;&gt;stack&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color: #603000;&quot;&gt;SIZE&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: maroon; font-weight: 700;&quot;&gt;void&lt;/span&gt; initstack&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;
&lt;span style=&quot;color: purple;&quot;&gt;{&lt;/span&gt;
        &lt;span style=&quot;color: maroon; font-weight: 700;&quot;&gt;int&lt;/span&gt; i&lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #008c00;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
        &lt;span style=&quot;color: maroon; font-weight: 700;&quot;&gt;for&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;i&lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #008c00;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;i&lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #603000;&quot;&gt;SIZE&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;i&lt;span style=&quot;color: #808030;&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;
       &lt;span style=&quot;color: purple;&quot;&gt;{&lt;/span&gt;
            &lt;span style=&quot;color: #603000;&quot;&gt;stack&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;[&lt;/span&gt;i&lt;span style=&quot;color: #808030;&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #008c00;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;&lt;span style=&quot;color: dimgrey;&quot;&gt;//set all element of the stack to 0&lt;/span&gt;
         &lt;span style=&quot;color: purple;&quot;&gt;}&lt;/span&gt;
&lt;span style=&quot;color: purple;&quot;&gt;}&lt;/span&gt;
&lt;span style=&quot;color: maroon; font-weight: 700;&quot;&gt;void&lt;/span&gt; show&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;
&lt;span style=&quot;color: purple;&quot;&gt;{&lt;/span&gt;
       &lt;span style=&quot;color: maroon; font-weight: 700;&quot;&gt;int&lt;/span&gt; i&lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #008c00;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
       &lt;span style=&quot;color: maroon; font-weight: 700;&quot;&gt;for&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;i&lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #008c00;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;i&lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #603000;&quot;&gt;SIZE&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;i&lt;span style=&quot;color: #808030;&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;
       &lt;span style=&quot;color: purple;&quot;&gt;{&lt;/span&gt;
           &lt;span style=&quot;color: #603000;&quot;&gt;printf&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color: #0f69ff;&quot;&gt;\n&lt;/span&gt;&lt;span style=&quot;color: #0000e6;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #0f69ff;&quot;&gt;%d&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #603000;&quot;&gt;stack&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;[&lt;/span&gt;i&lt;span style=&quot;color: #808030;&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
        &lt;span style=&quot;color: purple;&quot;&gt;}&lt;/span&gt;
&lt;span style=&quot;color: purple;&quot;&gt;}&lt;/span&gt;
&lt;span style=&quot;color: maroon; font-weight: 700;&quot;&gt;void&lt;/span&gt; push&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: maroon; font-weight: 700;&quot;&gt;int&lt;/span&gt; v&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;
&lt;span style=&quot;color: purple;&quot;&gt;{&lt;/span&gt;
       &lt;span style=&quot;color: maroon; font-weight: 700;&quot;&gt;if&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;cursor&lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #603000;&quot;&gt;SIZE&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;
        &lt;span style=&quot;color: purple;&quot;&gt;{&lt;/span&gt;
             &lt;span style=&quot;color: #603000;&quot;&gt;printf&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color: #0f69ff;&quot;&gt;\n&lt;/span&gt;&lt;span style=&quot;color: #0000e6;&quot;&gt; -----Stack is full------&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
         &lt;span style=&quot;color: purple;&quot;&gt;}&lt;/span&gt;
         &lt;span style=&quot;color: maroon; font-weight: 700;&quot;&gt;else&lt;/span&gt;
         &lt;span style=&quot;color: purple;&quot;&gt;{&lt;/span&gt;

           &lt;span style=&quot;color: #603000;&quot;&gt;stack&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;[&lt;/span&gt;cursor&lt;span style=&quot;color: #808030;&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt;v&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
           cursor&lt;span style=&quot;color: #808030;&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
          &lt;span style=&quot;color: purple;&quot;&gt;}&lt;/span&gt;

&lt;span style=&quot;color: purple;&quot;&gt;}&lt;/span&gt;
&lt;span style=&quot;color: maroon; font-weight: 700;&quot;&gt;int&lt;/span&gt; pop&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;
&lt;span style=&quot;color: purple;&quot;&gt;{&lt;/span&gt;
        &lt;span style=&quot;color: maroon; font-weight: 700;&quot;&gt;int&lt;/span&gt; t&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
         &lt;span style=&quot;color: maroon; font-weight: 700;&quot;&gt;if&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;cursor&lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #008c00;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;
         &lt;span style=&quot;color: purple;&quot;&gt;{&lt;/span&gt;
            &lt;span style=&quot;color: #603000;&quot;&gt;printf&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color: #0f69ff;&quot;&gt;\n&lt;/span&gt;&lt;span style=&quot;color: #0000e6;&quot;&gt; Stack is empty&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
         &lt;span style=&quot;color: purple;&quot;&gt;}&lt;/span&gt;
        &lt;span style=&quot;color: maroon; font-weight: 700;&quot;&gt;else&lt;/span&gt;
        &lt;span style=&quot;color: purple;&quot;&gt;{&lt;/span&gt;
               cursor&lt;span style=&quot;color: #808030;&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
                t&lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #603000;&quot;&gt;stack&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;[&lt;/span&gt;cursor&lt;span style=&quot;color: #808030;&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
              &lt;span style=&quot;color: #603000;&quot;&gt;stack&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;[&lt;/span&gt;cursor&lt;span style=&quot;color: #808030;&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #008c00;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
          &lt;span style=&quot;color: maroon; font-weight: 700;&quot;&gt;return&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;t&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
        &lt;span style=&quot;color: purple;&quot;&gt;}&lt;/span&gt;
&lt;span style=&quot;color: purple;&quot;&gt;}&lt;/span&gt;


&lt;span style=&quot;color: maroon; font-weight: 700;&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: #400000;&quot;&gt;main&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;
&lt;span style=&quot;color: purple;&quot;&gt;{&lt;/span&gt;
           &lt;span style=&quot;color: maroon; font-weight: 700;&quot;&gt;char&lt;/span&gt; c&lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #0000e6;&quot;&gt;&#39;a&#39;&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
           &lt;span style=&quot;color: maroon; font-weight: 700;&quot;&gt;int&lt;/span&gt; v&lt;span style=&quot;color: #808030;&quot;&gt;,&lt;/span&gt;k&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
             k&lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #008c00;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;

&lt;span style=&quot;color: maroon; font-weight: 700;&quot;&gt;while&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;k&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;
&lt;span style=&quot;color: purple;&quot;&gt;{&lt;/span&gt;
               &lt;span style=&quot;color: #603000;&quot;&gt;printf&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color: #0f69ff;&quot;&gt;\n&lt;/span&gt;&lt;span style=&quot;color: #0000e6;&quot;&gt; Enter your choice: 1:Push&lt;/span&gt;&lt;span style=&quot;color: #0f69ff;&quot;&gt;\n&lt;/span&gt;&lt;span style=&quot;color: #0000e6;&quot;&gt; 2:Pop &lt;/span&gt;&lt;span style=&quot;color: #0f69ff;&quot;&gt;\n&lt;/span&gt;&lt;span style=&quot;color: #0000e6;&quot;&gt;3:Show &lt;/span&gt;&lt;span style=&quot;color: #0f69ff;&quot;&gt;\n&lt;/span&gt;&lt;span style=&quot;color: #0000e6;&quot;&gt;4:Exit&lt;/span&gt;&lt;span style=&quot;color: #0f69ff;&quot;&gt;\n&lt;/span&gt;&lt;span style=&quot;color: #0000e6;&quot;&gt;your choice::&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
               &lt;span style=&quot;color: #603000;&quot;&gt;scanf&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color: #0f69ff;&quot;&gt;%c&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;&amp;amp;&lt;/span&gt;c&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
               &lt;span style=&quot;color: maroon; font-weight: 700;&quot;&gt;switch&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;c&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;
               &lt;span style=&quot;color: purple;&quot;&gt;{&lt;/span&gt;
                   &lt;span style=&quot;color: maroon; font-weight: 700;&quot;&gt;case &lt;/span&gt;&lt;span style=&quot;color: #0000e6;&quot;&gt;&#39;1&#39;&lt;/span&gt;&lt;span style=&quot;color: #e34adc;&quot;&gt;:&lt;/span&gt;
                   &lt;span style=&quot;color: #603000;&quot;&gt;printf&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color: #0f69ff;&quot;&gt;\n&lt;/span&gt;&lt;span style=&quot;color: #0000e6;&quot;&gt; enter a value:&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
                  &lt;span style=&quot;color: #603000;&quot;&gt;scanf&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color: #0f69ff;&quot;&gt;%d&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;&amp;amp;&lt;/span&gt;v&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
                      push&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;v&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
                      &lt;span style=&quot;color: maroon; font-weight: 700;&quot;&gt;break&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
                  &lt;span style=&quot;color: maroon; font-weight: 700;&quot;&gt;case &lt;/span&gt;&lt;span style=&quot;color: #0000e6;&quot;&gt;&#39;2&#39;&lt;/span&gt;&lt;span style=&quot;color: #e34adc;&quot;&gt;:&lt;/span&gt;
                  pop&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
                  &lt;span style=&quot;color: maroon; font-weight: 700;&quot;&gt;break&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
                  &lt;span style=&quot;color: maroon; font-weight: 700;&quot;&gt;case &lt;/span&gt;&lt;span style=&quot;color: #0000e6;&quot;&gt;&#39;3&#39;&lt;/span&gt;&lt;span style=&quot;color: #e34adc;&quot;&gt;:&lt;/span&gt;
                  show&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
                  &lt;span style=&quot;color: maroon; font-weight: 700;&quot;&gt;break&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
                  &lt;span style=&quot;color: maroon; font-weight: 700;&quot;&gt;case &lt;/span&gt;&lt;span style=&quot;color: #0000e6;&quot;&gt;&#39;4&#39;&lt;/span&gt;&lt;span style=&quot;color: #e34adc;&quot;&gt;:&lt;/span&gt;
                     k&lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #008c00;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;&lt;span style=&quot;color: dimgrey;&quot;&gt;//will break the while loop&lt;/span&gt;
                     &lt;span style=&quot;color: maroon; font-weight: 700;&quot;&gt;break&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
                 &lt;span style=&quot;color: purple;&quot;&gt;}&lt;/span&gt;



   &lt;span style=&quot;color: purple;&quot;&gt;}&lt;/span&gt;
&lt;span style=&quot;color: purple;&quot;&gt;}&lt;/span&gt;
&lt;/pre&gt;
&lt;/div&gt;</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7996672995157809874/posts/default/3210253069283371558'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7996672995157809874/posts/default/3210253069283371558'/><link rel='alternate' type='text/html' href='http://programs-in-c-plus-plus.blogspot.com/2011/11/stack-in-c-using-functions.html' title='Stack in C using functions'/><author><name>HawkEye</name><uri>http://www.blogger.com/profile/15300086365850802171</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></entry><entry><id>tag:blogger.com,1999:blog-7996672995157809874.post-1357216476018262630</id><published>2011-11-05T23:05:00.000-07:00</published><updated>2011-11-05T23:08:02.845-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="c"/><title type='text'>Pointers in C for starters</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;color: black;&quot;&gt;
Ask any new computer science student ,&quot;&lt;b&gt;What is the most dreaded topic in C language&lt;/b&gt;?&quot;. They will reply ,&quot;&lt;i&gt;Still having nightmares about pointers&lt;/i&gt; !!!&quot;. And I don&#39;t blame them. In fact pointers become even more dreaded as the learning curve is really steep for beginner.&lt;/div&gt;
&lt;div style=&quot;color: black;&quot;&gt;
So what makes pointers so &quot;evil&quot;
 ? I personally think its not the pointers but its lack of knowledge 
about memory that actually kills a new student. Hence I claim &quot;&lt;i&gt;&lt;b&gt;Pointers are innocent &lt;/b&gt;&lt;/i&gt;&quot;.&lt;/div&gt;
&lt;br /&gt;
&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;div style=&quot;color: black;&quot;&gt;
Pointers can be easily mastered by any new student, they just need to work with them. It is said that a good programmer will always avoid any type of programming error except that of pointers.
 Even the mightiest fail with pointers. So don&#39;t take it hard on your 
self , we all make mistakes (specially with pointers and then laugh 
about it later ).&lt;/div&gt;
&lt;div style=&quot;color: black;&quot;&gt;
So whats the deal 
about these strange looking creatures (pointers that is :P ). To 
understand it, lets look at the memory in detail.&lt;/div&gt;
If you want to visualize memory, its more like a huge set of&lt;b&gt; boxes &lt;/b&gt;with a numbers on it. These &lt;b&gt;boxes&lt;/b&gt; contain your data and the number on the box helps us getting to these &lt;b&gt;boxes&lt;/b&gt;. These &quot;numbers&quot; are called &quot;&lt;b style=&quot;color: #351c75;&quot;&gt;memory addresses&lt;/b&gt;&quot;.
 So everything you create in a C program will be stored in these boxes. 
every data type require a particular amount of boxes for storage. Like &lt;b&gt;int&lt;/b&gt; requires 2 boxes each of 1 Byte in size (assuming int is 2 Byte long)&lt;br /&gt;
&lt;br /&gt;
&lt;table style=&quot;border: medium solid;&quot;&gt;&lt;tbody&gt;
&lt;tr&gt; &lt;td style=&quot;color: #351c75;&quot;&gt;&lt;b&gt;X&lt;/b&gt;&lt;/td&gt;&lt;td style=&quot;color: #351c75;&quot;&gt;&lt;b&gt;Y&lt;/b&gt;&lt;/td&gt;&lt;td style=&quot;color: #351c75;&quot;&gt;&lt;b&gt;Z&lt;/b&gt;&lt;/td&gt; &lt;/tr&gt;
&lt;tr&gt; &lt;td&gt;1&lt;/td&gt;&lt;td&gt;2&lt;/td&gt;&lt;td&gt;3&lt;/td&gt; &lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;100&lt;/td&gt;&lt;td&gt;102&lt;/td&gt;&lt;td&gt;104&lt;/td&gt; &lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
here we see 3 int variables X,Y,Z. with values 1,2,3 
and they are stored at memory locations 100,102,104 respectively. As we 
can see these int require 2 Byte of memory. Easy right?&lt;br /&gt;
&lt;br /&gt;
&lt;div style=&quot;color: black;&quot;&gt;
&lt;b&gt;Q: So where are the pointers?&lt;/b&gt;&lt;/div&gt;
&lt;div style=&quot;color: black;&quot;&gt;
Ans : In
 Simple words the pointers will store these Addresses. That&#39;s the whole 
purpose of a pointer,to store the address of other variable (int in this
 case).&lt;/div&gt;
&lt;div style=&quot;color: black;&quot;&gt;
&lt;b&gt;Q: Can Pointers store values?&lt;/b&gt;&lt;/div&gt;
&lt;div style=&quot;color: black;&quot;&gt;
Ans : Well if the value is an address, YES.&lt;/div&gt;
&lt;div style=&quot;color: black;&quot;&gt;
&lt;b&gt;Q : How do I create a pointer?&lt;/b&gt;&lt;/div&gt;
&lt;div style=&quot;color: black;&quot;&gt;
Ans :Well its very easy, you have to just mention that what data type the pointer will be &quot;pointing&quot;.&lt;/div&gt;
&lt;br /&gt;
&lt;div style=&quot;color: #38761d; font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;
&lt;b&gt;int* ptr;&lt;/b&gt;&lt;/div&gt;
&lt;div style=&quot;color: #38761d;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;color: black;&quot;&gt;
Read this statement from right to left. &quot;&lt;i&gt;ptr is a pointer to data type int&lt;/i&gt;&quot;.&lt;/div&gt;
&lt;div style=&quot;color: black;&quot;&gt;
so my &lt;i&gt;ptr&lt;/i&gt; pointer will store the address of a int.&lt;/div&gt;
&lt;div style=&quot;color: black;&quot;&gt;
But i don&#39;t know where my variables are stored. :(&lt;/div&gt;
&lt;div style=&quot;color: black;&quot;&gt;
Yeah
 we know that that&#39;s why we use a special operator to get the address of
 your variable. this operator is &quot;&amp;amp;amp;&quot; operator.&lt;/div&gt;
&lt;div style=&quot;color: black;&quot;&gt;
so i can write :-&lt;/div&gt;
&lt;div style=&quot;color: #38761d;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;color: #38761d; font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;
&lt;b&gt;ptr=&amp;amp;X;&lt;/b&gt;&lt;/div&gt;
&lt;div style=&quot;color: #38761d;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;color: black;&quot;&gt;
read this statement from right to left. &quot;&lt;i&gt;Address of X gets assigned to pointer ptr&lt;/i&gt;&quot;. easy right?&lt;/div&gt;
&lt;div style=&quot;color: black;&quot;&gt;
&lt;b&gt;Q: So &amp;amp;X gives me the address of X. Will &amp;amp;Y give me address of Y? &lt;/b&gt;&lt;/div&gt;
&lt;div style=&quot;color: black;&quot;&gt;
Ans : of course it will.&lt;/div&gt;
&lt;div style=&quot;color: black;&quot;&gt;
&lt;b&gt;Q : But what is pointer itself?&lt;/b&gt;&lt;/div&gt;
&lt;div style=&quot;color: black;&quot;&gt;
Ans: Technically speaking pointer itself acts as an special integer that stores addresses.&lt;/div&gt;
&lt;div style=&quot;color: black;&quot;&gt;
&lt;b&gt;Q: Pointer is Integer?&lt;/b&gt;&lt;/div&gt;
&lt;div style=&quot;color: black;&quot;&gt;
Ans : Yes it is.&lt;/div&gt;
&lt;div style=&quot;color: black;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;color: black;&quot;&gt;
&lt;b&gt;Q: You mean the pointer itself is stored in memory too? &lt;/b&gt;&lt;/div&gt;
&lt;div style=&quot;color: black;&quot;&gt;
Ans: Of course like any other variable.&lt;/div&gt;
&lt;div style=&quot;color: #38761d;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;table style=&quot;border: medium solid;&quot;&gt;&lt;tbody&gt;
&lt;tr&gt; &lt;td&gt;&lt;b&gt;X&lt;/b&gt;&lt;/td&gt;&lt;td&gt;&lt;b&gt;Y&lt;/b&gt;&lt;/td&gt;&lt;td&gt;&lt;b&gt;Z&lt;/b&gt;&lt;/td&gt;&lt;td&gt;&lt;b&gt;PTR&lt;/b&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt; &lt;td&gt;1&lt;/td&gt;&lt;td&gt;2&lt;/td&gt;&lt;td&gt;3&lt;/td&gt;&lt;td&gt;100&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;100&lt;/td&gt;&lt;td&gt;102&lt;/td&gt;&lt;td&gt;104&lt;/td&gt;&lt;td&gt;200&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;div style=&quot;color: black;&quot;&gt;
You can see that the 
pointer itself is stored at address 200. and we can see that pointer 
contains the value 100 which is the address of variable X. and we say 
that ptr points to X.&lt;/div&gt;
&lt;div style=&quot;color: black;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;color: black;&quot;&gt;
&lt;b&gt;Q: Fine. What can do with pointers now?&lt;/b&gt;&lt;/div&gt;
&lt;div style=&quot;color: black;&quot;&gt;
&amp;nbsp;Ans : actually u can access the values &quot;pointed&quot; by the pointers.&lt;/div&gt;
&lt;div style=&quot;color: black;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;color: black;&quot;&gt;
&lt;b&gt;Q: I can? how?&lt;/b&gt;&lt;/div&gt;
&lt;div style=&quot;color: black;&quot;&gt;
Ans: its very easy , we use a special operator &quot;*&quot; called &quot;value at pointer&quot;.&lt;/div&gt;
&lt;div style=&quot;color: black;&quot;&gt;
So if I say&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;b&gt;&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;*ptr&lt;/span&gt;&amp;nbsp; &lt;/b&gt;, I mean&amp;nbsp; &quot;&lt;i&gt;value at ptr&lt;/i&gt;&quot;.&lt;/div&gt;
&lt;div style=&quot;color: black;&quot;&gt;
pointer
 contains address 100, and the value AT 100 is =1 which is the value of 
X. (So basically we get the value of the data type we were pointing ).&lt;/div&gt;
&lt;div style=&quot;color: #38761d;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;color: black;&quot;&gt;
&lt;b&gt;Summary:-&lt;/b&gt;&lt;/div&gt;
&lt;ul style=&quot;color: black;&quot;&gt;
&lt;li&gt;Pointers points to data types.&lt;/li&gt;
&lt;li&gt;Pointers only contain addresses.&lt;/li&gt;
&lt;li&gt;&quot;&amp;amp;;&quot; operators is used to get the memory address of a data type.&lt;/li&gt;
&lt;li&gt;&quot;*&quot; is used by pointers to read value it is pointing.&lt;/li&gt;
&lt;/ul&gt;
I hope it helps new C programmers.&lt;/div&gt;</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7996672995157809874/posts/default/1357216476018262630'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7996672995157809874/posts/default/1357216476018262630'/><link rel='alternate' type='text/html' href='http://programs-in-c-plus-plus.blogspot.com/2011/11/pointers-in-c-for-starters.html' title='Pointers in C for starters'/><author><name>HawkEye</name><uri>http://www.blogger.com/profile/15300086365850802171</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></entry><entry><id>tag:blogger.com,1999:blog-7996672995157809874.post-7850155044780874310</id><published>2011-11-05T21:48:00.000-07:00</published><updated>2011-11-05T21:49:25.784-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="c++"/><category scheme="http://www.blogger.com/atom/ns#" term="constructors"/><title type='text'>Understanding Constructors in C++</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;&lt;b&gt;What are Contructors in C++?&lt;/b&gt;&lt;br /&gt;
Ans: Constructor are special function that can help initialize an object.&lt;br /&gt;
&lt;b&gt;Do we really need contructors in C++?&lt;/b&gt;&lt;br /&gt;
Well, Its a good feature and really helps programmer.&lt;br /&gt;
&lt;b&gt;I am a good programmer and I don&#39;t Need contructors.&lt;/b&gt;&lt;br /&gt;
LOL !!!!!&lt;br /&gt;
Ok enough of chit chat, lets get to the point. Why contructors were included in C++. The answer lies in very nature of programmers. Programmer frequently get messy with code and allow mistakes to happen ( LOL :P). How? Lets have a look on following code :&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;div style=&quot;color: #38761d; font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;#include&amp;lt;iostream&amp;gt;&lt;/div&gt;&lt;div style=&quot;color: #38761d; font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;color: #38761d; font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;using namespace std;&lt;/div&gt;&lt;div style=&quot;color: #38761d; font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;color: #38761d; font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;int main()&lt;/div&gt;&lt;div style=&quot;color: #38761d; font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;{&lt;/div&gt;&lt;div style=&quot;color: #38761d; font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;int k;&lt;/div&gt;&lt;div style=&quot;color: #38761d; font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;cout&amp;lt;&amp;lt;k;&lt;/div&gt;&lt;div style=&quot;color: #38761d; font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;}&lt;/div&gt;This is a classical example of programmer forgetting to initialize the variable. Hence causing unexpected errors. This problem gets more complicated as we start creating objects. Since objects may have many data, its a very common mistake to forget to initialize the data portions of objects. This is where construcors come handy. The contructors have a property that they get called automatically when objects gets created.&lt;br /&gt;
&lt;div style=&quot;color: #38761d; font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;#include&amp;lt;iostream&amp;gt;&lt;/div&gt;&lt;div style=&quot;color: #38761d; font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;color: #38761d; font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;using namespace std;&lt;/div&gt;&lt;div style=&quot;color: #38761d; font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;class Me&lt;/div&gt;&lt;div style=&quot;color: #38761d; font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;{&lt;/div&gt;&lt;div style=&quot;color: #38761d; font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; int x,y;&lt;/div&gt;&lt;div style=&quot;color: #38761d; font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; public:&lt;/div&gt;&lt;div style=&quot;color: #38761d; font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;&amp;nbsp;&amp;nbsp; Me()&lt;/div&gt;&lt;div style=&quot;color: #38761d; font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;&amp;nbsp;&amp;nbsp; {&lt;/div&gt;&lt;div style=&quot;color: #38761d; font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; x=y=0; &lt;/div&gt;&lt;div style=&quot;color: #38761d; font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;&amp;nbsp;&amp;nbsp; }&lt;/div&gt;&lt;div style=&quot;color: #38761d; font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;&amp;nbsp;&amp;nbsp; void show()&lt;/div&gt;&lt;div style=&quot;color: #38761d; font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;&amp;nbsp;&amp;nbsp; {&lt;/div&gt;&lt;div style=&quot;color: #38761d; font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; cout&amp;lt;&amp;lt;x&amp;lt;&amp;lt;&quot;,&quot;y;&lt;/div&gt;&lt;div style=&quot;color: #38761d; font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;&amp;nbsp;&amp;nbsp; }&lt;/div&gt;&lt;div style=&quot;color: #38761d; font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;};&lt;/div&gt;&lt;div style=&quot;color: #38761d; font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;int main()&lt;/div&gt;&lt;div style=&quot;color: #38761d; font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;{&lt;/div&gt;&lt;div style=&quot;color: #38761d; font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;&amp;nbsp; Me x;&lt;/div&gt;&lt;div style=&quot;color: #38761d; font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;x.show();&lt;/div&gt;&lt;div style=&quot;color: #38761d; font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;}&lt;/div&gt;Declaring a Constructors will ensure that whatever initialization are put into the contructors to execute always whenever object gets created. Hence objects data will always will be in initialize state. &lt;br /&gt;
&lt;b&gt;How to declare Contructors in C++?&lt;/b&gt;&lt;br /&gt;
Its really easy, just follow the following rules :&lt;br /&gt;
&lt;ul style=&quot;text-align: left;&quot;&gt;&lt;li&gt;&lt;b&gt;Contructors are public.&lt;/b&gt;&lt;/li&gt;
&lt;li&gt;&lt;b&gt;Constructors don&#39;t have any return type.&lt;/b&gt;&lt;/li&gt;
&lt;li&gt;&lt;b&gt;Constructors name is always as that of the Class.&lt;/b&gt;&lt;/li&gt;
&lt;li&gt;&lt;b&gt;Constructors with no arguments are called default constructors.&lt;/b&gt;&lt;/li&gt;
&lt;li&gt;&lt;b&gt;Constructors can be overloaded.&lt;/b&gt;&lt;/li&gt;
&lt;/ul&gt;So next time you put some initialization code in a contructors, your object won&#39;t have garbage values in them.&lt;br /&gt;
Happy coding.&lt;/div&gt;</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7996672995157809874/posts/default/7850155044780874310'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7996672995157809874/posts/default/7850155044780874310'/><link rel='alternate' type='text/html' href='http://programs-in-c-plus-plus.blogspot.com/2011/11/understanding-constructors-in-c.html' title='Understanding Constructors in C++'/><author><name>HawkEye</name><uri>http://www.blogger.com/profile/15300086365850802171</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></entry><entry><id>tag:blogger.com,1999:blog-7996672995157809874.post-5675648515625264441</id><published>2011-10-16T06:05:00.000-07:00</published><updated>2012-01-05T00:48:04.642-08:00</updated><title type='text'>Calculate Factorial in python</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;color: blue;&quot;&gt;
&lt;span style=&quot;font-size: large;&quot;&gt;Objective : Using python calculate factorial of a number.&lt;/span&gt;&lt;/div&gt;
Logic : the logic is exactly the same as we do it in C language. so we use loops.&lt;br /&gt;
&lt;br /&gt;
&lt;div style=&quot;color: #38761d; font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;
Code:&lt;/div&gt;
&lt;pre style=&quot;background: #ffffff; color: black;&quot;&gt;&lt;span style=&quot;color: maroon; font-weight: 700;&quot;&gt;def&lt;/span&gt; factorial&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;number&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;:&lt;/span&gt;

    ans&lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #008c00;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;;&lt;/span&gt;

    &lt;span style=&quot;color: maroon; font-weight: 700;&quot;&gt;for&lt;/span&gt; i &lt;span style=&quot;color: maroon; font-weight: 700;&quot;&gt;in&lt;/span&gt; &lt;span style=&quot;color: #e34adc;&quot;&gt;range&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #008c00;&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;,&lt;/span&gt;number&lt;span style=&quot;color: #808030;&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: #008c00;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;:&lt;/span&gt;

        ans&lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt;ans&lt;span style=&quot;color: #808030;&quot;&gt;*&lt;/span&gt;i&lt;span style=&quot;color: #808030;&quot;&gt;;&lt;/span&gt;

    &lt;span style=&quot;color: maroon; font-weight: 700;&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;ans&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;

    &lt;span style=&quot;color: maroon; font-weight: 700;&quot;&gt;return&lt;/span&gt;
&lt;/pre&gt;
&lt;br /&gt;
What are the benefits of this code in python over the same in C? well on the first you won&#39;t see much advantages, but that&#39;s where you overlook Python !!! Python utilizes a heavy bit integers (128 bit integers), so 200! can be easily calculated without using decimal representation. Isn&#39;t that cool !!!&lt;/div&gt;</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7996672995157809874/posts/default/5675648515625264441'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7996672995157809874/posts/default/5675648515625264441'/><link rel='alternate' type='text/html' href='http://programs-in-c-plus-plus.blogspot.com/2011/10/program-to-calculate-factorial-in.html' title='Calculate Factorial in python'/><author><name>HawkEye</name><uri>http://www.blogger.com/profile/15300086365850802171</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></entry><entry><id>tag:blogger.com,1999:blog-7996672995157809874.post-2790566327021404840</id><published>2011-10-14T11:13:00.000-07:00</published><updated>2011-11-27T23:31:57.074-08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="c"/><category scheme="http://www.blogger.com/atom/ns#" term="non recursive"/><title type='text'>Program to check if a number is palindrome or not 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 style=&quot;color: blue;&quot;&gt;
&lt;strong&gt;&lt;span style=&quot;font-size: small;&quot;&gt;&lt;span style=&quot;color: black;&quot;&gt;Objective : To check if given number is palindrome or not in C.&lt;/span&gt;&lt;/span&gt;&lt;/strong&gt;&lt;/div&gt;
&lt;b&gt;Programming logic&lt;/b&gt; : This is a very easy program if you know how to reverse a number. Once you have found the reverse of number just compare it with original, if both are same then the number is palindrome.&lt;br /&gt;
&lt;br /&gt;
&lt;div style=&quot;color: #38761d; font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;
Code:
&lt;br /&gt;
&lt;pre style=&quot;background: #ffffff; color: black;&quot;&gt;&lt;span style=&quot;color: #004a43;&quot;&gt;#&lt;/span&gt;&lt;span style=&quot;color: #004a43;&quot;&gt;include&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #40015a;&quot;&gt;stdio.h&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&amp;gt;&lt;/span&gt;
&lt;span style=&quot;color: maroon; font-weight: 700;&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: #400000;&quot;&gt;main&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;
&lt;span style=&quot;color: purple;&quot;&gt;{&lt;/span&gt;
    &lt;span style=&quot;color: maroon; font-weight: 700;&quot;&gt;int&lt;/span&gt; k&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
    &lt;span style=&quot;color: #603000;&quot;&gt;printf&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color: #0f69ff;&quot;&gt;\n&lt;/span&gt;&lt;span style=&quot;color: #0000e6;&quot;&gt; enter number =&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
    &lt;span style=&quot;color: #603000;&quot;&gt;scanf&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color: #0f69ff;&quot;&gt;%d&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;&amp;amp;&lt;/span&gt;k&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
    &lt;span style=&quot;color: maroon; font-weight: 700;&quot;&gt;if&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;palindrome&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;k&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;
    &lt;span style=&quot;color: purple;&quot;&gt;{&lt;/span&gt;
        &lt;span style=&quot;color: #603000;&quot;&gt;printf&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color: #0f69ff;&quot;&gt;\n&lt;/span&gt;&lt;span style=&quot;color: #0000e6;&quot;&gt; The number is palindrome&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
    &lt;span style=&quot;color: purple;&quot;&gt;}&lt;/span&gt;
    &lt;span style=&quot;color: maroon; font-weight: 700;&quot;&gt;else&lt;/span&gt;
    &lt;span style=&quot;color: purple;&quot;&gt;{&lt;/span&gt;
        &lt;span style=&quot;color: #603000;&quot;&gt;printf&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color: #0f69ff;&quot;&gt;\n&lt;/span&gt;&lt;span style=&quot;color: #0000e6;&quot;&gt; The number is not palindrome&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
    &lt;span style=&quot;color: purple;&quot;&gt;}&lt;/span&gt;
    &lt;span style=&quot;color: maroon; font-weight: 700;&quot;&gt;return&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #008c00;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: purple;&quot;&gt;}&lt;/span&gt;
&lt;span style=&quot;color: maroon; font-weight: 700;&quot;&gt;int&lt;/span&gt; palindrome&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: maroon; font-weight: 700;&quot;&gt;int&lt;/span&gt; x&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;
&lt;span style=&quot;color: purple;&quot;&gt;{&lt;/span&gt;
    &lt;span style=&quot;color: maroon; font-weight: 700;&quot;&gt;int&lt;/span&gt; rev&lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #008c00;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;&lt;span style=&quot;color: dimgrey;&quot;&gt;//to calculate reverse of number&lt;/span&gt;
    &lt;span style=&quot;color: maroon; font-weight: 700;&quot;&gt;int&lt;/span&gt; k&lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt;x&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
    &lt;span style=&quot;color: maroon; font-weight: 700;&quot;&gt;while&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;k&lt;span style=&quot;color: #808030;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: #008c00;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;
    &lt;span style=&quot;color: purple;&quot;&gt;{&lt;/span&gt;
        rev&lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt;rev&lt;span style=&quot;color: #808030;&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color: #008c00;&quot;&gt;10&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;+&lt;/span&gt;k&lt;span style=&quot;color: #808030;&quot;&gt;%&lt;/span&gt;&lt;span style=&quot;color: #008c00;&quot;&gt;10&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
        k&lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt;k&lt;span style=&quot;color: #808030;&quot;&gt;/&lt;/span&gt;&lt;span style=&quot;color: #008c00;&quot;&gt;10&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
    &lt;span style=&quot;color: purple;&quot;&gt;}&lt;/span&gt;
    &lt;span style=&quot;color: maroon; font-weight: 700;&quot;&gt;if&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;x&lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt;rev&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: dimgrey;&quot;&gt;// check if reverse is equal to given number&lt;/span&gt;
    &lt;span style=&quot;color: purple;&quot;&gt;{&lt;/span&gt;
        &lt;span style=&quot;color: maroon; font-weight: 700;&quot;&gt;return&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #008c00;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;&lt;span style=&quot;color: dimgrey;&quot;&gt;//if yes then return 1&lt;/span&gt;
    &lt;span style=&quot;color: purple;&quot;&gt;}&lt;/span&gt;
    &lt;span style=&quot;color: maroon; font-weight: 700;&quot;&gt;else&lt;/span&gt;
    &lt;span style=&quot;color: purple;&quot;&gt;{&lt;/span&gt;
        &lt;span style=&quot;color: maroon; font-weight: 700;&quot;&gt;return&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #008c00;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;&lt;span style=&quot;color: dimgrey;&quot;&gt;//else return 0&lt;/span&gt;
    &lt;span style=&quot;color: purple;&quot;&gt;}&lt;/span&gt;
&lt;span style=&quot;color: purple;&quot;&gt;}&lt;/span&gt;
&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7996672995157809874/posts/default/2790566327021404840'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7996672995157809874/posts/default/2790566327021404840'/><link rel='alternate' type='text/html' href='http://programs-in-c-plus-plus.blogspot.com/2011/10/program-to-check-if-number-is.html' title='Program to check if a number is palindrome or not in C'/><author><name>HawkEye</name><uri>http://www.blogger.com/profile/15300086365850802171</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></entry><entry><id>tag:blogger.com,1999:blog-7996672995157809874.post-5355615626063870540</id><published>2011-10-14T10:56:00.000-07:00</published><updated>2012-01-05T00:48:23.692-08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="c"/><category scheme="http://www.blogger.com/atom/ns#" term="non recursive"/><title type='text'>Fibonacci series in C non recursive</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: small;&quot;&gt;Objective : To print given numbers of Fibonacci series. (1,1,2,3,5.....) non recursively.&lt;/span&gt;&lt;br /&gt;
&lt;b&gt;Programming Logic&lt;/b&gt;: Its easy to print the above Fibonacci series in C. the number is always sum of previous 2 terms. This series starts with numbers 1,1. so next term is 1+1=2. Then next term is 1+2=3 and so on.&lt;br /&gt;
(It may be noted that series 1,3,4,7,11,18.... is also a Fibonacci series, but it starts with 1,3 )&lt;br /&gt;
&lt;br /&gt;
Code :
&lt;br /&gt;
&lt;pre style=&quot;background: #ffffff; color: black;&quot;&gt;&lt;span style=&quot;color: #004a43;&quot;&gt;#&lt;/span&gt;&lt;span style=&quot;color: #004a43;&quot;&gt;include&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #40015a;&quot;&gt;stdio.h&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&amp;gt;&lt;/span&gt;
&lt;span style=&quot;color: maroon; font-weight: 700;&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: #400000;&quot;&gt;main&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;
&lt;span style=&quot;color: purple;&quot;&gt;{&lt;/span&gt;
    &lt;span style=&quot;color: maroon; font-weight: 700;&quot;&gt;int&lt;/span&gt; k&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
    &lt;span style=&quot;color: #603000;&quot;&gt;printf&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color: #0f69ff;&quot;&gt;\n&lt;/span&gt;&lt;span style=&quot;color: #0000e6;&quot;&gt; enter number of terms to print=&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
    &lt;span style=&quot;color: #603000;&quot;&gt;scanf&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color: #0f69ff;&quot;&gt;%d&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;&amp;amp;&lt;/span&gt;k&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
    fic&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;k&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
    &lt;span style=&quot;color: maroon; font-weight: 700;&quot;&gt;return&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #008c00;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: purple;&quot;&gt;}&lt;/span&gt;
&lt;span style=&quot;color: maroon; font-weight: 700;&quot;&gt;void&lt;/span&gt; fic&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: maroon; font-weight: 700;&quot;&gt;int&lt;/span&gt; x&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: dimgrey;&quot;&gt;// x are number of terms to print&lt;/span&gt;
&lt;span style=&quot;color: purple;&quot;&gt;{&lt;/span&gt;
    &lt;span style=&quot;color: maroon; font-weight: 700;&quot;&gt;int&lt;/span&gt; a&lt;span style=&quot;color: #808030;&quot;&gt;,&lt;/span&gt;b&lt;span style=&quot;color: #808030;&quot;&gt;,&lt;/span&gt;sum&lt;span style=&quot;color: #808030;&quot;&gt;,&lt;/span&gt;i&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
    a&lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt;b&lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #008c00;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
    &lt;span style=&quot;color: #603000;&quot;&gt;printf&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color: #0000e6;&quot;&gt;1,1,&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;,&lt;/span&gt;sum&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;&lt;span style=&quot;color: dimgrey;&quot;&gt;//first 2  terms&lt;/span&gt;
    &lt;span style=&quot;color: maroon; font-weight: 700;&quot;&gt;for&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;i&lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #008c00;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;i&lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt;x&lt;span style=&quot;color: #808030;&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color: #008c00;&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;i&lt;span style=&quot;color: #808030;&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;
    &lt;span style=&quot;color: purple;&quot;&gt;{&lt;/span&gt;
        sum&lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt;a&lt;span style=&quot;color: #808030;&quot;&gt;+&lt;/span&gt;b&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
        &lt;span style=&quot;color: #603000;&quot;&gt;printf&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color: #0f69ff;&quot;&gt;%d&lt;/span&gt;&lt;span style=&quot;color: #0000e6;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;,&lt;/span&gt;sum&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
        a&lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt;b&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
        b&lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt;sum&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;    
    &lt;span style=&quot;color: purple;&quot;&gt;}&lt;/span&gt;
&lt;span style=&quot;color: purple;&quot;&gt;}&lt;/span&gt;
&lt;/pre&gt;
&lt;br /&gt;
It may be noted that using a non recursive way to calculate Fibonacci series in C provides faster calculations as compared with recursive method. As recursive program have a large overhead of function calls. Also the recursive way is very unoptimised since it doesn&#39;t uses the last 2 values that have been calculated already..&lt;/div&gt;</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7996672995157809874/posts/default/5355615626063870540'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7996672995157809874/posts/default/5355615626063870540'/><link rel='alternate' type='text/html' href='http://programs-in-c-plus-plus.blogspot.com/2011/10/program-to-print-fibonacci-series-in-c.html' title='Fibonacci series in C non recursive'/><author><name>HawkEye</name><uri>http://www.blogger.com/profile/15300086365850802171</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></entry><entry><id>tag:blogger.com,1999:blog-7996672995157809874.post-4143430195620086954</id><published>2011-10-13T23:45:00.000-07:00</published><updated>2012-01-05T00:48:36.804-08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="c"/><category scheme="http://www.blogger.com/atom/ns#" term="non recursive"/><title type='text'>Reversing a Number 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 style=&quot;color: blue;&quot;&gt;
&lt;span style=&quot;font-size: large;&quot;&gt;Objective : You are given a&amp;nbsp; number and you have to find the reverse of the given number in C. reverse of 1234 will be 4321.&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: blue;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;b&gt;Programming logic&lt;/b&gt;: The logic lies in constructing the reverse numbers from the digits of the given number. So we extract each digit with % modulus operator and then build the number up.To build the number we simply multiply the reverse number by 10 and then add the next remainder to it.&lt;br /&gt;
Like 1234:&lt;br /&gt;
1234%10=4 (remainder). rev=4.&lt;br /&gt;
123%10= 3. rev=4x10+3=43.&lt;br /&gt;
12%10=2 rev=43x10+2=432.&lt;br /&gt;
1%10=1 rev=432x10+1=4321.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Code:
&lt;br /&gt;
&lt;pre style=&quot;background: #ffffff; color: black;&quot;&gt;&lt;span style=&quot;color: #004a43;&quot;&gt;#&lt;/span&gt;&lt;span style=&quot;color: #004a43;&quot;&gt;include&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #40015a;&quot;&gt;stdio.h&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&amp;gt;&lt;/span&gt;
&lt;span style=&quot;color: maroon; font-weight: 700;&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: #400000;&quot;&gt;main&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;
&lt;span style=&quot;color: purple;&quot;&gt;{&lt;/span&gt;
    &lt;span style=&quot;color: maroon; font-weight: 700;&quot;&gt;int&lt;/span&gt; k&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
   &lt;span style=&quot;color: #603000;&quot;&gt;printf&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color: #0f69ff;&quot;&gt;\n&lt;/span&gt;&lt;span style=&quot;color: #0000e6;&quot;&gt; enter a number=&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
   &lt;span style=&quot;color: #603000;&quot;&gt;scanf&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color: #0f69ff;&quot;&gt;%d&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;&amp;amp;&lt;/span&gt;k&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
   &lt;span style=&quot;color: #603000;&quot;&gt;printf&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color: #0f69ff;&quot;&gt;\n&lt;/span&gt;&lt;span style=&quot;color: #0000e6;&quot;&gt; Reverse of the number is=&lt;/span&gt;&lt;span style=&quot;color: #0f69ff;&quot;&gt;%d&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #603000;&quot;&gt;reverse&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;k&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
   &lt;span style=&quot;color: maroon; font-weight: 700;&quot;&gt;return&lt;/span&gt; &lt;span style=&quot;color: #008c00;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: purple;&quot;&gt;}&lt;/span&gt;
&lt;span style=&quot;color: maroon; font-weight: 700;&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: #603000;&quot;&gt;reverse&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: maroon; font-weight: 700;&quot;&gt;int&lt;/span&gt; x&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;
&lt;span style=&quot;color: purple;&quot;&gt;{&lt;/span&gt;
    &lt;span style=&quot;color: maroon; font-weight: 700;&quot;&gt;int&lt;/span&gt; ans&lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #008c00;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;,&lt;/span&gt;r&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
    &lt;span style=&quot;color: maroon; font-weight: 700;&quot;&gt;while&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt; x &lt;span style=&quot;color: #808030;&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span style=&quot;color: #008c00;&quot;&gt;0&lt;/span&gt; &lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;
    &lt;span style=&quot;color: purple;&quot;&gt;{&lt;/span&gt;
        r&lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt;x&lt;span style=&quot;color: #808030;&quot;&gt;%&lt;/span&gt;&lt;span style=&quot;color: #008c00;&quot;&gt;10&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;&lt;span style=&quot;color: dimgrey;&quot;&gt;//extract last digit&lt;/span&gt;
        ans&lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt;ans&lt;span style=&quot;color: #808030;&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color: #008c00;&quot;&gt;10&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;+&lt;/span&gt;r&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;&lt;span style=&quot;color: dimgrey;&quot;&gt;// multiply by 10 and then add the remainder&lt;/span&gt;
          x&lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt;x&lt;span style=&quot;color: #808030;&quot;&gt;/&lt;/span&gt;&lt;span style=&quot;color: #008c00;&quot;&gt;10&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;&lt;span style=&quot;color: dimgrey;&quot;&gt;//reduce number&lt;/span&gt;
    &lt;span style=&quot;color: purple;&quot;&gt;}&lt;/span&gt;
   &lt;span style=&quot;color: maroon; font-weight: 700;&quot;&gt;return&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;ans&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;

&lt;span style=&quot;color: purple;&quot;&gt;}&lt;/span&gt;
&lt;/pre&gt;
&lt;/div&gt;</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7996672995157809874/posts/default/4143430195620086954'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7996672995157809874/posts/default/4143430195620086954'/><link rel='alternate' type='text/html' href='http://programs-in-c-plus-plus.blogspot.com/2011/10/reversing-number-in-c.html' title='Reversing a Number in C'/><author><name>HawkEye</name><uri>http://www.blogger.com/profile/15300086365850802171</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></entry><entry><id>tag:blogger.com,1999:blog-7996672995157809874.post-716588015533018309</id><published>2011-10-13T23:10:00.000-07:00</published><updated>2012-01-05T00:48:56.008-08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="c"/><category scheme="http://www.blogger.com/atom/ns#" term="non recursive"/><title type='text'>Integer to Binary 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 style=&quot;color: blue;&quot;&gt;
&lt;b&gt;&lt;span style=&quot;font-size: small;&quot;&gt;&lt;span style=&quot;color: black;&quot;&gt;Objective : To convert the given integer to binary in C language.&lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;b&gt;Programming technique&lt;/b&gt; : The conversion of integer to binary can be done in many ways. I will demonstrate a very simple (possibly very unoptimised ) way which it is easy to understand.&lt;br /&gt;
This way involves using an array to store the remainder of the number when we divide it by 2. The remainder will be saved in array. We will start filling the array from the last and at last we will print the array.&lt;br /&gt;
This technique will convert integer number to binary using array, it may not be a true solution but you will get the picture.&lt;br /&gt;
we will use % modules operator to extract remainder. Then will reduce the number till it reaches 0. The array will be initialized to 0.&lt;br /&gt;
&lt;br /&gt;
Code :
&lt;br /&gt;
&lt;pre style=&quot;background: #ffffff; color: black;&quot;&gt;&lt;span style=&quot;color: #004a43;&quot;&gt;#&lt;/span&gt;&lt;span style=&quot;color: #004a43;&quot;&gt;include&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #40015a;&quot;&gt;stdio.h&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&amp;gt;&lt;/span&gt;
&lt;span style=&quot;color: maroon; font-weight: 700;&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: #400000;&quot;&gt;main&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;
&lt;span style=&quot;color: purple;&quot;&gt;{&lt;/span&gt;
    &lt;span style=&quot;color: maroon; font-weight: 700;&quot;&gt;int&lt;/span&gt; p&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
    &lt;span style=&quot;color: #603000;&quot;&gt;printf&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color: #0f69ff;&quot;&gt;\n&lt;/span&gt;&lt;span style=&quot;color: #0000e6;&quot;&gt; Enter a number=&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
    &lt;span style=&quot;color: #603000;&quot;&gt;scanf&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color: #0f69ff;&quot;&gt;%d&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;&amp;amp;&lt;/span&gt;p&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
    bin&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;p&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: purple;&quot;&gt;}&lt;/span&gt;

&lt;span style=&quot;color: maroon; font-weight: 700;&quot;&gt;void&lt;/span&gt; bin&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: maroon; font-weight: 700;&quot;&gt;int&lt;/span&gt; x&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;
&lt;span style=&quot;color: purple;&quot;&gt;{&lt;/span&gt;
    &lt;span style=&quot;color: maroon; font-weight: 700;&quot;&gt;long&lt;/span&gt; b&lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #008c00;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
    &lt;span style=&quot;color: maroon; font-weight: 700;&quot;&gt;int&lt;/span&gt; binary&lt;span style=&quot;color: #808030;&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color: #008c00;&quot;&gt;20&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;,&lt;/span&gt;i&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
    &lt;span style=&quot;color: maroon; font-weight: 700;&quot;&gt;for&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;i&lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #008c00;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;i&lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #008c00;&quot;&gt;20&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;i&lt;span style=&quot;color: #808030;&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;
    &lt;span style=&quot;color: purple;&quot;&gt;{&lt;/span&gt;
        binary&lt;span style=&quot;color: #808030;&quot;&gt;[&lt;/span&gt;i&lt;span style=&quot;color: #808030;&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #008c00;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;&lt;span style=&quot;color: dimgrey;&quot;&gt;//setting array to 0&lt;/span&gt;
    &lt;span style=&quot;color: purple;&quot;&gt;}&lt;/span&gt;
    i&lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #008c00;&quot;&gt;19&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;&lt;span style=&quot;color: dimgrey;&quot;&gt;// starting from last of array&lt;/span&gt;
    &lt;span style=&quot;color: maroon; font-weight: 700;&quot;&gt;while&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;x&lt;span style=&quot;color: #808030;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: #008c00;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;
    &lt;span style=&quot;color: purple;&quot;&gt;{&lt;/span&gt;

        binary&lt;span style=&quot;color: #808030;&quot;&gt;[&lt;/span&gt;i&lt;span style=&quot;color: #808030;&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt;x&lt;span style=&quot;color: #808030;&quot;&gt;%&lt;/span&gt;&lt;span style=&quot;color: #008c00;&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;&lt;span style=&quot;color: dimgrey;&quot;&gt;//save remainder to array&lt;/span&gt;
        x&lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt;x&lt;span style=&quot;color: #808030;&quot;&gt;/&lt;/span&gt;&lt;span style=&quot;color: #008c00;&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt; &lt;span style=&quot;color: dimgrey;&quot;&gt;//reduce number&lt;/span&gt;
        i&lt;span style=&quot;color: #808030;&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;&lt;span style=&quot;color: dimgrey;&quot;&gt;//moving index &lt;/span&gt;
    &lt;span style=&quot;color: purple;&quot;&gt;}&lt;/span&gt;
    &lt;span style=&quot;color: #603000;&quot;&gt;printf&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color: #0f69ff;&quot;&gt;\n&lt;/span&gt;&lt;span style=&quot;color: #0000e6;&quot;&gt; Printing Binary=&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
    &lt;span style=&quot;color: maroon; font-weight: 700;&quot;&gt;for&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;i&lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #008c00;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;i&lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #008c00;&quot;&gt;20&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;i&lt;span style=&quot;color: #808030;&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;
    &lt;span style=&quot;color: purple;&quot;&gt;{&lt;/span&gt;
        &lt;span style=&quot;color: #603000;&quot;&gt;printf&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color: #0f69ff;&quot;&gt;%d&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;,&lt;/span&gt;binary&lt;span style=&quot;color: #808030;&quot;&gt;[&lt;/span&gt;i&lt;span style=&quot;color: #808030;&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
    &lt;span style=&quot;color: purple;&quot;&gt;}&lt;/span&gt;
&lt;span style=&quot;color: purple;&quot;&gt;}&lt;/span&gt;
&lt;/pre&gt;
&lt;br /&gt;
&lt;br /&gt;
one possible drawback of this technique is that you cant use the binary number directly&amp;nbsp; in calculations.&lt;br /&gt;
Note : The above method will store only 20 digits of binary.&lt;/div&gt;</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7996672995157809874/posts/default/716588015533018309'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7996672995157809874/posts/default/716588015533018309'/><link rel='alternate' type='text/html' href='http://programs-in-c-plus-plus.blogspot.com/2011/10/converting-integer-to-binary-in-c.html' title='Integer to Binary in c'/><author><name>HawkEye</name><uri>http://www.blogger.com/profile/15300086365850802171</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></entry><entry><id>tag:blogger.com,1999:blog-7996672995157809874.post-4137207321900156044</id><published>2011-10-12T23:31:00.000-07:00</published><updated>2012-01-05T00:49:18.452-08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="c"/><category scheme="http://www.blogger.com/atom/ns#" term="non recursive"/><title type='text'>Find Sum of Digits of a Number 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;b&gt;Objective&lt;/b&gt;: &lt;/span&gt;&lt;span style=&quot;color: blue; font-size: large;&quot;&gt;Your are given a number and you have to find the sum of its digits. Like 1234 so its digits sum 1+2+3+4=10.&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Programming technique&lt;/b&gt; : This problem&amp;nbsp; can be solved using the fact that modulus operator (%) gives the last digit when divided by 10. So we successively Divide it , till all numbers are exhausted.&lt;br /&gt;
so The program runs like this &amp;gt;&amp;gt;&lt;br /&gt;
1234%10 gives last digit. that is 4. (we save it ) then divide the number by 10 to reduce it.&lt;br /&gt;
1234/10=123. and repeat the process till the number becomes 0.&lt;br /&gt;
First run : 1234%10 gives 4, 1234/10=123.&lt;br /&gt;
Second run: 123%10 gives 3 , 123/10=12.&lt;br /&gt;
Third Run :12%10 gives 2 , 12/10=1&lt;br /&gt;
Fourth run: 1%10 gives 1, 1/10=0 (we stop here).&lt;br /&gt;
pretty easy stuff.&lt;br /&gt;
&lt;br /&gt;
Code :-&lt;br /&gt;
&lt;span style=&quot;color: blue; font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;#include&amp;lt;stdio.h&amp;gt;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;color: blue; font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;int main()&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;{&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; int k;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; printf(&quot;\n enter any number=&quot;);&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; scanf(&quot;%d&quot;,&amp;amp;k);&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; printf(&quot;\n sum of digit=%d&quot;,sumdigit(k));&lt;/span&gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style=&quot;color: blue; font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;return(0);&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;}&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;color: blue; font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;int sumdigit(int x)&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;{&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; int sum=0;// will save sum&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; int r=0; // for remainder &lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; while(x&amp;gt;0)&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; r=x%10;//get remainder&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; sum+=r;//sum it&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; x=x/10;&amp;nbsp;&amp;nbsp;&amp;nbsp; //reduce number&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; return(sum);&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;}&lt;/span&gt;&lt;/div&gt;</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7996672995157809874/posts/default/4137207321900156044'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7996672995157809874/posts/default/4137207321900156044'/><link rel='alternate' type='text/html' href='http://programs-in-c-plus-plus.blogspot.com/2011/10/program-in-c-to-find-sum-of-digits.html' title='Find Sum of Digits of a Number in C'/><author><name>HawkEye</name><uri>http://www.blogger.com/profile/15300086365850802171</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></entry><entry><id>tag:blogger.com,1999:blog-7996672995157809874.post-6404949769818626553</id><published>2011-09-22T19:36:00.000-07:00</published><updated>2014-10-28T04:56:27.736-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="c"/><category scheme="http://www.blogger.com/atom/ns#" term="non recursive"/><category scheme="http://www.blogger.com/atom/ns#" term="prime"/><title type='text'>Program to find nth Prime number 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 style=&quot;color: blue;&quot;&gt;
&lt;span style=&quot;font-size: large;&quot;&gt;&lt;b&gt;Objective: To print any Prime number in C.&amp;nbsp;&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;
This task may sound easy but is a very good exercise to help optimize CPU intensive programs. Finding Prime number is a very CPU hogging task and it becomes even more dreaded as the number starts to grow. Lets say we want to find 30001th prime. how will we find it?? Here is the program.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Programming logic&lt;/b&gt;: The easiest method is to check if the given number N is divisible by any number from 2 to N-1.&lt;br /&gt;
But it may be noted that we don&#39;t need to go beyond the number that is square root of N.&amp;nbsp; why??&lt;br /&gt;
lets have a look.&lt;br /&gt;
Say we are testing number 49 for prime number. Our objective is to find any number that may divide it. so we start from 2,3,4 etc. As soon as we cross 7 there will be no number that will divide 49, if there was we have got it earlier since it will be smaller then 7.&amp;nbsp; Get it?&lt;br /&gt;
another example 64 :&lt;br /&gt;
2x32&lt;br /&gt;
4x16&lt;br /&gt;
8x8&lt;br /&gt;
16x4&lt;br /&gt;
32x2&lt;br /&gt;
as you can see when we pass number 8 we don&#39;t need to test as the number that will come , we have tested them already. (clever right ?)&lt;br /&gt;
&lt;br /&gt;
Code:&lt;br /&gt;
&lt;pre style=&quot;background: #ffffff; color: black;&quot;&gt;&lt;span style=&quot;color: #004a43;&quot;&gt;#&lt;/span&gt;&lt;span style=&quot;color: #004a43;&quot;&gt;include &lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #40015a;&quot;&gt;stdio.h&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&amp;gt;&lt;/span&gt;
&lt;span style=&quot;color: maroon; font-weight: 700;&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: #400000;&quot;&gt;main&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;
&lt;span style=&quot;color: purple;&quot;&gt;{&lt;/span&gt;
&lt;span style=&quot;color: maroon; font-weight: 700;&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: #603000;&quot;&gt;count&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #008c00;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: maroon; font-weight: 700;&quot;&gt;long&lt;/span&gt; a &lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #008c00;&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: maroon; font-weight: 700;&quot;&gt;while&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #603000;&quot;&gt;count&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #008c00;&quot;&gt;30001&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;
&lt;span style=&quot;color: purple;&quot;&gt;{&lt;/span&gt;
    &lt;span style=&quot;color: maroon; font-weight: 700;&quot;&gt;long&lt;/span&gt; b &lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #008c00;&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
    &lt;span style=&quot;color: maroon; font-weight: 700;&quot;&gt;int&lt;/span&gt; prime &lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #008c00;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;&lt;span style=&quot;color: dimgrey;&quot;&gt;// to check if found a prime&lt;/span&gt;
    &lt;span style=&quot;color: maroon; font-weight: 700;&quot;&gt;while&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;b &lt;span style=&quot;color: #808030;&quot;&gt;*&lt;/span&gt; b &lt;span style=&quot;color: #808030;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt; a&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;
    &lt;span style=&quot;color: purple;&quot;&gt;{&lt;/span&gt;
        &lt;span style=&quot;color: maroon; font-weight: 700;&quot;&gt;if&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;a &lt;span style=&quot;color: #808030;&quot;&gt;%&lt;/span&gt; b &lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #008c00;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;
        &lt;span style=&quot;color: purple;&quot;&gt;{&lt;/span&gt;
            prime &lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #008c00;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
            &lt;span style=&quot;color: maroon; font-weight: 700;&quot;&gt;break&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
        &lt;span style=&quot;color: purple;&quot;&gt;}&lt;/span&gt;
        b&lt;span style=&quot;color: #808030;&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
    &lt;span style=&quot;color: purple;&quot;&gt;}&lt;/span&gt;
    &lt;span style=&quot;color: maroon; font-weight: 700;&quot;&gt;if&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;prime&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;
    &lt;span style=&quot;color: #603000;&quot;&gt;count&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
    a&lt;span style=&quot;color: #808030;&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: purple;&quot;&gt;}&lt;/span&gt;
&lt;span style=&quot;color: #603000;&quot;&gt;printf&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color: #0f69ff;&quot;&gt;%d&lt;/span&gt;&lt;span style=&quot;color: #0000e6;&quot;&gt; st prime no. : &lt;/span&gt;&lt;span style=&quot;color: #0f69ff;&quot;&gt;%ld&lt;/span&gt;&lt;span style=&quot;color: #0f69ff;&quot;&gt;\n&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #603000;&quot;&gt;count&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;-&lt;/span&gt;a&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: maroon; font-weight: 700;&quot;&gt;return&lt;/span&gt; &lt;span style=&quot;color: #008c00;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: purple;&quot;&gt;}&lt;/span&gt;
&lt;/pre&gt;
&lt;br /&gt;
&lt;br /&gt;
It may be noted that this program can be further optimized in several ways. One possible way is to consider only odd terms ( since even terms cant be prime, except 2)&lt;/div&gt;
</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7996672995157809874/posts/default/6404949769818626553'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7996672995157809874/posts/default/6404949769818626553'/><link rel='alternate' type='text/html' href='http://programs-in-c-plus-plus.blogspot.com/2011/09/program-to-find-nth-prime-number-in-c.html' title='Program to find nth Prime number in C'/><author><name>HawkEye</name><uri>http://www.blogger.com/profile/15300086365850802171</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></entry><entry><id>tag:blogger.com,1999:blog-7996672995157809874.post-1317488568787187117</id><published>2011-07-13T08:01:00.000-07:00</published><updated>2011-11-29T02:34:31.749-08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="c"/><category scheme="http://www.blogger.com/atom/ns#" term="leap year"/><title type='text'>calculating leap year in c using if else</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
Most new students get really confused when it comes to calculating Leap year in C. It is given as an exercise to enhance their knowledge about using if else usages. This can be calculated using Logical operators too. But using if else provides clearer ways to calculate it. Both of the methods can be implemented in C/C++ program. Here we will discuss the if else method in C/C++&lt;br /&gt;
&lt;br /&gt;
&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-size: small;&quot;&gt;&lt;b&gt;Leap year&lt;/b&gt;&lt;/span&gt; : A year is Leap year if&lt;br /&gt;
&lt;ol&gt;
&lt;li&gt;It is not a century and gets divided by 4.&lt;/li&gt;
&lt;li&gt;It is a century and gets divided by 400. &lt;/li&gt;
&lt;/ol&gt;
it is clear that a year will either be century (like 1600,1700,1800,1900) or it will not be a century (like 1988,1999 etc). You get the picture right.&lt;br /&gt;
So the first step is to check if the given year is a century or not? it can be easily checked using modulus operator % . (Modulus operator calculates remainder) . So if the modulus gives us 0, that means the given number is divided exactly.&lt;br /&gt;
So 1700,1800,1900 are not Leap Years (as they all are centuries but not divided by 400).&lt;br /&gt;
Years like 1600,2000,2400 are Leap Years (they all are divided exactly vy 400).&lt;br /&gt;
On the other hand Years like 1946,1950,1950 are not Leap year (they are not centuries and are not divided exactly by 4).&lt;br /&gt;
While years like 1944,1948,1952 are Leap years (as they are not centuries and are divided by 4 exactly ).&lt;br /&gt;
How can we calculate leap year in c/ c++? well its easy:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Program:&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;pre style=&quot;background: #ffffff; color: black;&quot;&gt;&lt;span style=&quot;color: #004a43;&quot;&gt;#&lt;/span&gt;&lt;span style=&quot;color: #004a43;&quot;&gt;include&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #40015a;&quot;&gt;stdio.h&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&amp;gt;&lt;/span&gt;
&lt;span style=&quot;color: maroon; font-weight: 700;&quot;&gt;int&lt;/span&gt; leapyear&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: maroon; font-weight: 700;&quot;&gt;int&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;&lt;span style=&quot;color: dimgrey;&quot;&gt;// protyping function&lt;/span&gt;

&lt;span style=&quot;color: maroon; font-weight: 700;&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: #400000;&quot;&gt;main&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;
&lt;span style=&quot;color: purple;&quot;&gt;{&lt;/span&gt;
    &lt;span style=&quot;color: maroon; font-weight: 700;&quot;&gt;int&lt;/span&gt; x&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
    &lt;span style=&quot;color: #603000;&quot;&gt;printf&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color: #0000e6;&quot;&gt;Enter a number=&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
    &lt;span style=&quot;color: #603000;&quot;&gt;scanf&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color: #0f69ff;&quot;&gt;%d&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;&amp;amp;&lt;/span&gt;x&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
    &lt;span style=&quot;color: maroon; font-weight: 700;&quot;&gt;if&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;leapyear&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;x&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;
    &lt;span style=&quot;color: purple;&quot;&gt;{&lt;/span&gt;
        &lt;span style=&quot;color: #603000;&quot;&gt;printf&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color: #0000e6;&quot;&gt;The &lt;/span&gt;&lt;span style=&quot;color: #0f69ff;&quot;&gt;%d&lt;/span&gt;&lt;span style=&quot;color: #0000e6;&quot;&gt; year is Leap year&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;,&lt;/span&gt;x&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
    &lt;span style=&quot;color: purple;&quot;&gt;}&lt;/span&gt;
    &lt;span style=&quot;color: maroon; font-weight: 700;&quot;&gt;else&lt;/span&gt;
    &lt;span style=&quot;color: purple;&quot;&gt;{&lt;/span&gt;
        &lt;span style=&quot;color: #603000;&quot;&gt;printf&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color: #0000e6;&quot;&gt;The &lt;/span&gt;&lt;span style=&quot;color: #0f69ff;&quot;&gt;%d&lt;/span&gt;&lt;span style=&quot;color: #0000e6;&quot;&gt; year is not Leap year&lt;/span&gt;&lt;span style=&quot;color: maroon;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;,&lt;/span&gt;x&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;
    &lt;span style=&quot;color: purple;&quot;&gt;}&lt;/span&gt;
&lt;span style=&quot;color: purple;&quot;&gt;}&lt;/span&gt;
&lt;span style=&quot;color: maroon; font-weight: 700;&quot;&gt;int&lt;/span&gt; leapyear&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: maroon; font-weight: 700;&quot;&gt;int&lt;/span&gt; year&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;
&lt;span style=&quot;color: purple;&quot;&gt;{&lt;/span&gt;
    &lt;span style=&quot;color: maroon; font-weight: 700;&quot;&gt;int&lt;/span&gt; iscentury&lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #008c00;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;&lt;span style=&quot;color: dimgrey;&quot;&gt;// to test if year is century or not&lt;/span&gt;
    &lt;span style=&quot;color: maroon; font-weight: 700;&quot;&gt;if&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;year&lt;span style=&quot;color: #808030;&quot;&gt;%&lt;/span&gt;&lt;span style=&quot;color: #008c00;&quot;&gt;100&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #008c00;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;
    &lt;span style=&quot;color: purple;&quot;&gt;{&lt;/span&gt;
        iscentury&lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #008c00;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;&lt;span style=&quot;color: dimgrey;&quot;&gt;//yes its a century&lt;/span&gt;
    &lt;span style=&quot;color: purple;&quot;&gt;}&lt;/span&gt;
    &lt;span style=&quot;color: maroon; font-weight: 700;&quot;&gt;if&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;iscentury&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;
    &lt;span style=&quot;color: purple;&quot;&gt;{&lt;/span&gt;
        &lt;span style=&quot;color: maroon; font-weight: 700;&quot;&gt;if&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;year&lt;span style=&quot;color: #808030;&quot;&gt;%&lt;/span&gt;&lt;span style=&quot;color: #008c00;&quot;&gt;400&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #008c00;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: dimgrey;&quot;&gt;//century must be divided by 400&lt;/span&gt;
        &lt;span style=&quot;color: purple;&quot;&gt;{&lt;/span&gt;
            &lt;span style=&quot;color: dimgrey;&quot;&gt;//yes?&lt;/span&gt;
            &lt;span style=&quot;color: maroon; font-weight: 700;&quot;&gt;return&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #008c00;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;&lt;span style=&quot;color: dimgrey;&quot;&gt;// you have leap year... return 1&lt;/span&gt;
        &lt;span style=&quot;color: purple;&quot;&gt;}&lt;/span&gt;
        &lt;span style=&quot;color: maroon; font-weight: 700;&quot;&gt;else&lt;/span&gt;
        &lt;span style=&quot;color: purple;&quot;&gt;{&lt;/span&gt;
            &lt;span style=&quot;color: maroon; font-weight: 700;&quot;&gt;return&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #008c00;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;&lt;span style=&quot;color: dimgrey;&quot;&gt;// no leap year return 0&lt;/span&gt;
        &lt;span style=&quot;color: purple;&quot;&gt;}&lt;/span&gt;
    &lt;span style=&quot;color: purple;&quot;&gt;}&lt;/span&gt;
    &lt;span style=&quot;color: maroon; font-weight: 700;&quot;&gt;else&lt;/span&gt;
    &lt;span style=&quot;color: purple;&quot;&gt;{&lt;/span&gt;
        &lt;span style=&quot;color: maroon; font-weight: 700;&quot;&gt;if&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;year&lt;span style=&quot;color: #808030;&quot;&gt;%&lt;/span&gt;&lt;span style=&quot;color: #008c00;&quot;&gt;4&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #008c00;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: dimgrey;&quot;&gt;// normal year must be divided by 4&lt;/span&gt;
        &lt;span style=&quot;color: purple;&quot;&gt;{&lt;/span&gt;
            &lt;span style=&quot;color: maroon; font-weight: 700;&quot;&gt;return&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #008c00;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;&lt;span style=&quot;color: dimgrey;&quot;&gt;// you have leap year return 1;    &lt;/span&gt;
        &lt;span style=&quot;color: purple;&quot;&gt;}&lt;/span&gt;
        &lt;span style=&quot;color: maroon; font-weight: 700;&quot;&gt;else&lt;/span&gt;
        &lt;span style=&quot;color: purple;&quot;&gt;{&lt;/span&gt;
            &lt;span style=&quot;color: maroon; font-weight: 700;&quot;&gt;return&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #008c00;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: #808030;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: purple;&quot;&gt;;&lt;/span&gt;&lt;span style=&quot;color: dimgrey;&quot;&gt;// you don&#39;t have leap year return 0&lt;/span&gt;
        &lt;span style=&quot;color: purple;&quot;&gt;}&lt;/span&gt;       
    &lt;span style=&quot;color: purple;&quot;&gt;}&lt;/span&gt;  
&lt;span style=&quot;color: purple;&quot;&gt;}&lt;/span&gt;
&lt;/pre&gt;
The above C program returns 1 if it finds a Leap year otherwise returns 0 if it doesn&#39;t. easy right?&lt;/div&gt;</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7996672995157809874/posts/default/1317488568787187117'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7996672995157809874/posts/default/1317488568787187117'/><link rel='alternate' type='text/html' href='http://programs-in-c-plus-plus.blogspot.com/2011/07/calculating-leap-year-in-c-using-if.html' title='calculating leap year in c using if else'/><author><name>HawkEye</name><uri>http://www.blogger.com/profile/15300086365850802171</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></entry><entry><id>tag:blogger.com,1999:blog-7996672995157809874.post-6575861634275146576</id><published>2011-05-08T10:23:00.000-07:00</published><updated>2011-07-13T08:23:19.170-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="c"/><category scheme="http://www.blogger.com/atom/ns#" term="c++ development"/><title type='text'>Tools for C/C++ development</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;C/C++ development can be done in variety of ways. If you are comfortable using the compiler and command prompt, you can just download the compiler and start working. But most of developer will prefer some sort of Integrated development environment (IDE) for C/C++ development.&lt;br /&gt;
&lt;br /&gt;
&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;&lt;br /&gt;
Most new developer get confused when it comes to choosing a IDE. Actually there are many options available in Internet for development purposes. Some of them may cost a little while others may be free.&lt;br /&gt;
But sticking to one IDE will probably what makes an IDE great. A new developer who is starting his way with c/c++ programming can start with old IDE like turboC. I personally have worked with many IDE myself. Some of them are &quot;codelite&quot;, &quot;code blocks&quot; , DevCPP, Visual studio etc.&lt;br /&gt;
I think codelite and devcpp are really good way to start. If you are comfortable with Eclipse IDE then there is a plugin for c/c++ development. Visual studio express edition is also a great tool available.&lt;br /&gt;
Cfree is has a free version that can be used.&lt;br /&gt;
My personnel choice for bigger projects has been DevCPP and Visual Studio, due to their great project support.&lt;br /&gt;
For starters just get an IDE and work with it for like one month. This will get you going with that IDE and once you get familiar with it , you will start to like it. As far as IDE for c/c++ goes no one is perfect, everyone has their own pros and cons.&lt;br /&gt;
&quot;PEACE TO ALL&quot;&lt;/div&gt;</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7996672995157809874/posts/default/6575861634275146576'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7996672995157809874/posts/default/6575861634275146576'/><link rel='alternate' type='text/html' href='http://programs-in-c-plus-plus.blogspot.com/2011/05/tools-for-cc-development.html' title='Tools for C/C++ development'/><author><name>HawkEye</name><uri>http://www.blogger.com/profile/15300086365850802171</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></entry></feed>