<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[2 lines or 20 lines of code?]]></title><description><![CDATA[2 lines or 20 lines of code?]]></description><link>https://2-lines-or-20-lines-of-code.hashnode.dev</link><generator>RSS for Node</generator><lastBuildDate>Sun, 21 Jun 2026 16:03:03 GMT</lastBuildDate><atom:link href="https://2-lines-or-20-lines-of-code.hashnode.dev/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[2D Array]]></title><description><![CDATA[2D array is a two-dimensional array in C++ , which we can imagine / commonly called as a matrix or grid having some fixed number of rows and columns that has to be defined at time of declaration , just like we define fixed size for a 1D array.
Syntax...]]></description><link>https://2-lines-or-20-lines-of-code.hashnode.dev/2d-array</link><guid isPermaLink="true">https://2-lines-or-20-lines-of-code.hashnode.dev/2d-array</guid><category><![CDATA[ChaiCode]]></category><dc:creator><![CDATA[Vibhu Khati]]></dc:creator><pubDate>Sat, 24 May 2025 11:43:57 GMT</pubDate><content:encoded><![CDATA[<p>2D array is a two-dimensional array in C++ , which we can imagine / commonly called as a matrix or grid having some fixed number of rows and columns that has to be defined at time of declaration , just like we define fixed size for a 1D array.</p>
<p>Syntax to declare an array:-</p>
<p><mark>dataType arrayName[rows][columns];</mark></p>
<p>where, rows and columns are fixed.</p>
<h2 id="heading-defining-a-2-d-array"><strong>Defining a 2-D array</strong></h2>
<p>An array can be created using two ways:</p>
<ol>
<li><p>By declaration</p>
</li>
<li><p>By initialization</p>
</li>
<li><h2 id="heading-by-declaration">By Declaration</h2>
</li>
</ol>
<pre><code class="lang-cpp"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;iostream&gt;</span></span>

<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>{
    <span class="hljs-comment">// Declare a 3x4 2D array</span>
    <span class="hljs-keyword">int</span> <span class="hljs-built_in">array</span>[<span class="hljs-number">3</span>][<span class="hljs-number">4</span>];

    <span class="hljs-built_in">array</span>[<span class="hljs-number">0</span>][<span class="hljs-number">0</span>]=<span class="hljs-number">1</span>;   <span class="hljs-comment">//array[0][0] =&gt; 1st row 1st column</span>
    <span class="hljs-built_in">array</span>[<span class="hljs-number">0</span>][<span class="hljs-number">1</span>]=<span class="hljs-number">1</span>;   <span class="hljs-comment">//array[0][1] =&gt; 1st row 2nd column</span>
    <span class="hljs-built_in">array</span>[<span class="hljs-number">0</span>][<span class="hljs-number">2</span>]=<span class="hljs-number">1</span>;   <span class="hljs-comment">//array[0][2] =&gt; 1st row 3rd column </span>
    <span class="hljs-built_in">array</span>[<span class="hljs-number">0</span>][<span class="hljs-number">3</span>]=<span class="hljs-number">1</span>;

    <span class="hljs-built_in">array</span>[<span class="hljs-number">1</span>][<span class="hljs-number">0</span>]=<span class="hljs-number">1</span>;    <span class="hljs-comment">//array[1][0] =&gt; 2nd row 1st column</span>
    <span class="hljs-built_in">array</span>[<span class="hljs-number">1</span>][<span class="hljs-number">1</span>]=<span class="hljs-number">1</span>;    <span class="hljs-comment">//array[1][1] =&gt; 2nd row 2nd column </span>
    <span class="hljs-built_in">array</span>[<span class="hljs-number">1</span>][<span class="hljs-number">2</span>]=<span class="hljs-number">1</span>;

    <span class="hljs-built_in">array</span>[<span class="hljs-number">2</span>][<span class="hljs-number">0</span>]=<span class="hljs-number">1</span>;     <span class="hljs-comment">//array[2][0] =&gt; 3rd row 1st column</span>


    <span class="hljs-comment">// Print the array</span>
    <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>; i &lt; <span class="hljs-number">3</span>; ++i) {
        <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> j = <span class="hljs-number">0</span>; j &lt; <span class="hljs-number">4</span>; ++j) {
            <span class="hljs-built_in">std</span>::<span class="hljs-built_in">cout</span> &lt;&lt; <span class="hljs-built_in">array</span>[i][j] &lt;&lt; <span class="hljs-string">" "</span>;
        }
        <span class="hljs-built_in">std</span>::<span class="hljs-built_in">cout</span> &lt;&lt; <span class="hljs-built_in">std</span>::<span class="hljs-built_in">endl</span>;
    }

    <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;
}

<span class="hljs-comment">//Output</span>
<span class="hljs-number">1</span> <span class="hljs-number">1</span> <span class="hljs-number">1</span> <span class="hljs-number">1</span> 
<span class="hljs-number">1</span> <span class="hljs-number">1</span> <span class="hljs-number">1</span> <span class="hljs-number">31319</span> 
<span class="hljs-number">1</span> <span class="hljs-number">0</span> <span class="hljs-number">1651076199</span> <span class="hljs-number">779647075</span>
</code></pre>
<p>In Declaration, the array is declared first and then after declaration values are assigned later.</p>
<p>The <strong>output of the program</strong>, without initializing all elements of the 2D array, is <strong>undefined behavior</strong> because it includes <strong>uninitialized variables.</strong></p>
<p><strong>C++ does not automatically initialize local variables (including arrays).</strong> The uninitialized elements will contain <strong>garbage values</strong>, which can be anything.</p>
<ol start="2">
<li><h2 id="heading-by-initialization">By Initialization</h2>
</li>
<li><pre><code class="lang-cpp">    <span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;iostream&gt;</span></span>

    <span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>{
        <span class="hljs-comment">// Declare a 3x4 2D array</span>
        <span class="hljs-keyword">int</span> <span class="hljs-built_in">array</span>[<span class="hljs-number">3</span>][<span class="hljs-number">4</span>] = { {<span class="hljs-number">10</span>, <span class="hljs-number">20</span>, <span class="hljs-number">30</span>, <span class="hljs-number">40</span>}, <span class="hljs-comment">// Row 1 initialized </span>
                            {<span class="hljs-number">50</span>,<span class="hljs-number">60</span>}, <span class="hljs-comment">// Row 2: Assign values to 2 elements</span>
                            {<span class="hljs-number">3</span>, <span class="hljs-number">8</span>, <span class="hljs-number">12</span>}  <span class="hljs-comment">// Row 3: Assign values to 3 elements</span>
                          };

        <span class="hljs-comment">// Print the array</span>
        <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>; i &lt; <span class="hljs-number">3</span>; ++i) {
            <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> j = <span class="hljs-number">0</span>; j &lt; <span class="hljs-number">4</span>; ++j) {
                <span class="hljs-built_in">std</span>::<span class="hljs-built_in">cout</span> &lt;&lt; <span class="hljs-built_in">array</span>[i][j] &lt;&lt; <span class="hljs-string">" "</span>;
            }
            <span class="hljs-built_in">std</span>::<span class="hljs-built_in">cout</span> &lt;&lt; <span class="hljs-built_in">std</span>::<span class="hljs-built_in">endl</span>;
        }

        <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;
    }

    <span class="hljs-comment">//OUTPUT</span>
    <span class="hljs-number">10</span> <span class="hljs-number">20</span> <span class="hljs-number">30</span> <span class="hljs-number">40</span>
    <span class="hljs-number">50</span> <span class="hljs-number">60</span> <span class="hljs-number">0</span> <span class="hljs-number">0</span>
    <span class="hljs-number">3</span> <span class="hljs-number">8</span> <span class="hljs-number">12</span> <span class="hljs-number">0</span>
</code></pre>
<p> In Initialization, values are given at time of array declaration. So, the values which are not initialized like in row 2 and row 3 , they are by default initialized with 0.</p>
</li>
</ol>
<pre><code class="lang-cpp"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;iostream&gt;</span></span>

<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span>
</span>{
    <span class="hljs-keyword">int</span> a[][<span class="hljs-number">4</span>]= {<span class="hljs-number">1</span>,<span class="hljs-number">1</span>};
    <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;
}
<span class="hljs-comment">//No error</span>
</code></pre>
<p>In above program, if we declare 2D array like:</p>
<p>int a[][];</p>
<p>Or</p>
<p>int a[4][];</p>
<p>Then, we will get error.</p>
<p><mark>error: declaration of 'a' as multidimensional , array must have bounds for all dimensions except the first</mark></p>
<h2 id="heading-address-calculation-representation-of-2d-array-in-memory-and-base-address-of-2d-array"><strong>Address Calculation , representation of 2D array in memory and Base Address of 2D array</strong></h2>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1747489153963/f7199761-11f2-4f25-9d06-a14b74d7a873.png?auto=compress,format&amp;format=webp" alt /></p>
<p>Actually, <strong>in C/C++, multi-dimensional arrays are stored in “Row Major” order.</strong> This means that in memory, it is stored array-by-array i.e. 1D array after 1D array row-wise.</p>
<p>This means that <strong>Row 0</strong> is stored then <strong>Row 1</strong> is stored and so on. Diagrammatically, its like: -</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1747489572917/e3784f33-26de-4a86-9fff-c102ad007d10.png?auto=compress,format&amp;format=webp" alt /></p>
<p>So, in memory , 2D array is actually stored as a 1D array. Thus, the memory blocks are allocated consecutively in a 2D array. This is why 1016 is the starting memory address of first memory block of row 2 , after the last memory block of row 1 which has starting memory address of 1012.</p>
<p><strong>Base address</strong> of this 2D array is 1000. So, we can refer to whole 2D array by this base address.</p>
<p><mark>a = &amp;a[0] =&amp;a[0][0]</mark></p>
<p>array Name of 2D array is base address of 2D array.</p>
<p>Formula for <strong>address calculation of 2D array</strong> a[r][c] is :-</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1747490006776/af4470fc-9ae8-4f6b-89f3-13945d16ed6a.png?auto=compress,format&amp;format=webp" alt /></p>
<p><strong>a[i][j] is interpreted by C++ compiler</strong> as:-</p>
<p><mark>a[i][j] =<em>( a[i]+j ) = </em></mark> <mark>(</mark>*<mark>(a+i) + j)</mark></p>
<p><strong>Internally, this a[i][j] expression is solved by above formula.</strong></p>
<p><strong>Example:-</strong></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1747493015668/ce40c9ef-914c-4076-9f8e-21cad017fb3a.png?auto=compress,format&amp;format=webp" alt /></p>
<p>So, here also the a[i][j] expression can be wriiten as:-</p>
<p><mark>a[i][j] = j[ a[i] ]</mark></p>
<p>Here, a+1 means array after 1 row skip (i.e. row 1 (2nd row) )</p>
<p><mark><em>( a + i)</em></mark> <em>*\=&gt;</em> the i-th row, which is itself an array (decays to a pointer to the first element in that row i) .</p>
<p>This <strong>array decay in C/C++</strong> is happening because *(a+ i) = a[i] is a 1D array but array a is a 2D array , so, in C++, <strong>a[i] decays to a pointer pointing to row i.</strong></p>
<p>Since <strong>pointer addition is commutative</strong>, we can write</p>
<p><mark>a[i][j] == i[a][j] == j[ i[a] ] == j[ a[i] ]</mark></p>
<p>and</p>
<p><mark>a[i][j] ≠ a[j][i]</mark></p>
]]></content:encoded></item><item><title><![CDATA[Arrays in C++ Explained: Essential Concepts and Usage]]></title><description><![CDATA[What is an Array?
Array is a one-dimensional (can be 2D and 3D also) data structure in C++, that can store similar kind/type of data elements in it. It stores those elements in consecutive memory blocks (even in 2D array).
Advantages
Advantage of usi...]]></description><link>https://2-lines-or-20-lines-of-code.hashnode.dev/arrays-in-c-explained-essential-concepts-and-usage</link><guid isPermaLink="true">https://2-lines-or-20-lines-of-code.hashnode.dev/arrays-in-c-explained-essential-concepts-and-usage</guid><category><![CDATA[ChaiCode]]></category><dc:creator><![CDATA[Vibhu Khati]]></dc:creator><pubDate>Thu, 22 May 2025 17:07:56 GMT</pubDate><content:encoded><![CDATA[<h1 id="heading-what-is-an-array">What is an Array?</h1>
<p>Array is a one-dimensional (can be 2D and 3D also) data structure in C++, that can store similar kind/type of data elements in it. It stores those elements in consecutive memory blocks (even in 2D array).</p>
<h2 id="heading-advantages">Advantages</h2>
<p>Advantage of using an array is that we don’t have to write different names of variables for all the collection of data elements we are storing in the memory. We can have only one variable name (that is <strong>array name</strong>) for that collection of data elements and <strong>to access each data element</strong> we use :-</p>
<p><mark>arrayName[index]</mark></p>
<p>So, the variable name for that collection of data elements(or array) followed by subscript operator [] and inside it index.</p>
<p><strong>Indexing in C++</strong> starts from 0, so , arrayName[0] will give us the first data element of array , in constant time O(1). Even, arrayName[size-1] will give us last element of array in constant time O(1). Thus, accessing any element is easy in array as it takes O(1) time because of indexing.</p>
<h2 id="heading-disadvantages">Disadvantages</h2>
<h2 id="heading-we-cant-store-different-types-of-data-elements-in-an-array">We can’t store different types of data elements in an array.</h2>
<p>We have to specify a <strong>fixed size</strong> for <strong>static array</strong> at time of declaration of array. And so, adding a new data element in array can become not possible if adding it will increase size of array &gt; <strong>fixed size</strong>. And giving too much of <strong>fixed size</strong> creates unnecessary memory blocks and <strong>garbage values</strong>.</p>
<p><strong>Deleting</strong> operation in array only skips that data element which we wants to delete and does not actually deletes that memory location/block. So, unnecessary memory is left behind.</p>
<h2 id="heading-defining-an-array">Defining an array</h2>
<p>An array can be created using two ways:</p>
<ol>
<li><p>By declaration</p>
</li>
<li><p>By initialization</p>
</li>
</ol>
<ol>
<li><h3 id="heading-declaring-an-array">Declaring an array</h3>
<pre><code class="lang-cpp"> <span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;iostream&gt;</span></span>

 <span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>{
     <span class="hljs-comment">// Declare an array of size 10</span>
     <span class="hljs-keyword">int</span> arr[<span class="hljs-number">10</span>];

     <span class="hljs-comment">// Assign values to the first 5 indexes</span>
     arr[<span class="hljs-number">0</span>] = <span class="hljs-number">10</span>;
     arr[<span class="hljs-number">1</span>] = <span class="hljs-number">20</span>;
     arr[<span class="hljs-number">2</span>] = <span class="hljs-number">30</span>;
     arr[<span class="hljs-number">3</span>] = <span class="hljs-number">40</span>;
     arr[<span class="hljs-number">4</span>] = <span class="hljs-number">50</span>;

     <span class="hljs-comment">// Optionally, you can print the array to verify</span>
     <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>; i &lt; <span class="hljs-number">10</span>; ++i) {
         <span class="hljs-built_in">std</span>::<span class="hljs-built_in">cout</span> &lt;&lt; <span class="hljs-string">"arr["</span> &lt;&lt; i &lt;&lt; <span class="hljs-string">"] = "</span> &lt;&lt; arr[i] &lt;&lt; <span class="hljs-built_in">std</span>::<span class="hljs-built_in">endl</span>;
     }

     <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;
 }

 <span class="hljs-comment">//Output</span>
 arr[<span class="hljs-number">0</span>] = <span class="hljs-number">10</span>
 arr[<span class="hljs-number">1</span>] = <span class="hljs-number">20</span>
 arr[<span class="hljs-number">2</span>] = <span class="hljs-number">30</span>
 arr[<span class="hljs-number">3</span>] = <span class="hljs-number">40</span>
 arr[<span class="hljs-number">4</span>] = <span class="hljs-number">50</span>
 arr[<span class="hljs-number">5</span>] = <span class="hljs-number">-858993460</span>
 arr[<span class="hljs-number">6</span>] = <span class="hljs-number">32767</span>
 arr[<span class="hljs-number">7</span>] = <span class="hljs-number">0</span>
 arr[<span class="hljs-number">8</span>] = <span class="hljs-number">4195760</span>
 arr[<span class="hljs-number">9</span>] = <span class="hljs-number">1</span>
</code></pre>
<p> This is how we declare an array a of fixed size 10 and we assign values to each memory blocks created ,after the array is declared. So, we already have some garbage values initially in each memory blocks of the array. If we assign values to only 5 indexes where each index (i) corresponds to i th memory location/block , the rest indexes(or ith locations) will have garbage values(unpredictable values).</p>
</li>
<li><h3 id="heading-initalize-an-array">Initalize an array</h3>
</li>
</ol>
<pre><code class="lang-cpp"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;iostream&gt;</span></span>

<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>{
    <span class="hljs-keyword">const</span> <span class="hljs-keyword">int</span> SIZE = <span class="hljs-number">10</span>;
    <span class="hljs-keyword">int</span> <span class="hljs-built_in">array</span>[SIZE] = {<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>, <span class="hljs-number">5</span>}; <span class="hljs-comment">// Initializing the first 5 indexes</span>
    <span class="hljs-comment">//or </span>
    <span class="hljs-comment">//int array[SIZE] = {[0]=1, [1]=2, [2]=3, [3]=4, [4]=5};</span>

    <span class="hljs-comment">// Displaying the array</span>
    <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>; i &lt; SIZE; ++i) {
        <span class="hljs-built_in">std</span>::<span class="hljs-built_in">cout</span> &lt;&lt; <span class="hljs-string">"array["</span> &lt;&lt; i &lt;&lt; <span class="hljs-string">"] = "</span> &lt;&lt; <span class="hljs-built_in">array</span>[i] &lt;&lt; <span class="hljs-built_in">std</span>::<span class="hljs-built_in">endl</span>;
    }

    <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;
}

<span class="hljs-comment">//Output</span>
<span class="hljs-built_in">array</span>[<span class="hljs-number">0</span>] = <span class="hljs-number">1</span>
<span class="hljs-built_in">array</span>[<span class="hljs-number">1</span>] = <span class="hljs-number">2</span>
<span class="hljs-built_in">array</span>[<span class="hljs-number">2</span>] = <span class="hljs-number">3</span>
<span class="hljs-built_in">array</span>[<span class="hljs-number">3</span>] = <span class="hljs-number">4</span>
<span class="hljs-built_in">array</span>[<span class="hljs-number">4</span>] = <span class="hljs-number">5</span>
<span class="hljs-built_in">array</span>[<span class="hljs-number">5</span>] = <span class="hljs-number">0</span>
<span class="hljs-built_in">array</span>[<span class="hljs-number">6</span>] = <span class="hljs-number">0</span>
<span class="hljs-built_in">array</span>[<span class="hljs-number">7</span>] = <span class="hljs-number">0</span>
<span class="hljs-built_in">array</span>[<span class="hljs-number">8</span>] = <span class="hljs-number">0</span>
<span class="hljs-built_in">array</span>[<span class="hljs-number">9</span>] = <span class="hljs-number">0</span>
</code></pre>
<p>Here, in intialization, the values to the array are assigned at the time of array declaration . If we assign values to only first 5 indices, then the rest of indices (rest of elements) by default initialized to 0 and 0.0 in case of float which will cout as 0).</p>
<h2 id="heading-address-calculation-representation-of-array-in-memory-and-base-address-of-array">Address Calculation , representation of array in memory and Base Address of array</h2>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1747484693383/2b3bf062-2532-4579-b184-d1fe82c8f65d.png?auto=compress,format&amp;format=webp" alt /></p>
<p>When an array is declared then memory blocks each of <strong>size of datatype of array</strong> are allocated in RAM.</p>
<p>Example:-</p>
<p>int a[10]; //means size or no. of elements of array = 10</p>
<p>This statement / declaration will allocate memory blocks each of size 4 bytes(size of int) in RAM. So, the total size or capacity of array in memory is 4bytes * no. of elements in array = 4bytes * 10 = <strong>40 bytes.</strong></p>
<p><strong>Base Address /Starting Address :-</strong> It is the memory address of the first element of an array. In example, 1000 is the base address. 1001,1002,1003 memory addresses are also present in first memory block(as <strong>each byte has a memory address</strong>), but because 1000 is the starting address of this memory block of 4 bytes , we can represent this complete first memory block with <strong>base address 1000</strong>.</p>
<p>Other consecutive memory blocks will be represented by their <strong>Starting addresses</strong> not <strong>base address of array.</strong></p>
<p><strong>Any array name is base address of that array.</strong></p>
<p><mark>a= &amp;a[0];</mark></p>
<p>where '<strong>&amp;’</strong> is called <strong>Address-of Operator.</strong></p>
<p><strong>For Address Calculation</strong> of array element A[i] , use below formula:-</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1747485573982/c0f106ab-9cce-4499-8c7e-37258b8f1eac.png?auto=compress,format&amp;format=webp" alt /></p>
<h2 id="heading-how-ai-is-same-as-ia">How a[i] is same as i[a]</h2>
<p>a[i] is an expression where a and i are operands and [] is an operator (subscript operator). Just like how (a+b) is an expression where a and b are operands and + is operator.</p>
<p>The <strong>c++ compiler translates</strong> a[i] internally as :-</p>
<p><mark>a[i] = *(a+i)</mark></p>
<p>where <em>is</em> <strong><em>de-reference operator,</em></strong> <em>a and i are operands and</em> + is addition operator.</p>
<p>According to commutaive law, we can write</p>
<p>(a+b) = (b+a)</p>
<p>So, using same law , the expression *(a+i) can also be written as :-</p>
<p><em>\</em>(a+i) =<em> \</em>(i+a)</p>
<p>Thus,</p>
<p><mark>a[i]= <em></em></mark><mark>(a+i) = </mark><em> <mark></mark></em>(i+a) = i[a]</p>
<p><mark>a[i] = i[a]</mark></p>
<p>This thing is the answer to <strong>“Why array index starts with 0 in C/C++”.</strong> Because, in order to get a[0] <strong>(first</strong> <strong>element/data of array)</strong>, we must have index 0 so that <em>\</em>(a + 0) =<em> \</em>(a) i.e. Value at address a . (Remember writing <strong>a or &amp;a[0]</strong> is same thing as both denote <strong>Base Address.</strong> Here '<strong>&amp;’</strong> is called <strong>Address-of Operator.)</strong></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1747487151940/095c1e35-c7f9-4108-8dcb-71ed5856e29f.png?auto=compress,format&amp;format=webp" alt /></p>
<ul>
<li><p>Notice how address calculation is being done here. Since 1000 is a memory address and 3 is index and there is a ‘+’ b/w them, that doesn’t mean that it will do binary addition i.e. 1003. Actually, this is called <strong>Pointer Arithmetic.</strong> And here we are adding an integer to the pointer a</p>
</li>
<li><p>C++ compiler internally is calculating the address of the index 3 using the formula that we discussed previously OR we can also say that <strong>3 memory blocks are skipped.</strong></p>
<p>  The <strong>c++ compiler interprets a[i]</strong> internally as</p>
<p>  <mark>a[i] = </mark> <em><mark>(base_address_of_a + i </mark></em> <mark>* sizeof(dataType))</mark></p>
<p>  <strong>Example : -</strong></p>
<p>  int a[i] ;</p>
<p>  interpreted as :-</p>
<p>  <mark>a[i] = </mark> <em><mark>(base_address_of_a + i </mark></em> <mark>* sizeof(int)) ;</mark></p>
</li>
</ul>
]]></content:encoded></item><item><title><![CDATA[2 lines or 20 lines of code?]]></title><description><![CDATA[The question may pop into the minds of many programmers while writing their code that what to choose a 2 lines of code to solve the problem statement or 20 lines of code to solve the problem statement. Which code or program is better one? Well, the a...]]></description><link>https://2-lines-or-20-lines-of-code.hashnode.dev/2-lines-or-20-lines-of-code</link><guid isPermaLink="true">https://2-lines-or-20-lines-of-code.hashnode.dev/2-lines-or-20-lines-of-code</guid><category><![CDATA[ChaiCode]]></category><category><![CDATA[Chaiaurcode]]></category><dc:creator><![CDATA[Vibhu Khati]]></dc:creator><pubDate>Wed, 07 May 2025 15:40:34 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/stock/unsplash/npxXWgQ33ZQ/upload/524da9423b2d17341fc43a694ad2b515.jpeg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>The question may pop into the minds of many programmers while writing their code that what to choose a 2 lines of code to solve the problem statement or 20 lines of code to solve the problem statement. Which code or program is better one? Well, the answer is not that simple that it must be 2 lines of code because it is quick to read and understand or it should be 20 lines of code because it must have handled some extra issues that may occur. The answer is it depends.</p>
<blockquote>
<p>Choosing between writing 2 lines of code or 20 lines to solve a problem isn't straightforward. The best approach depends on factors like readability, simplicity, and handling potential issues.</p>
</blockquote>
<p>Let us give you all programmers a real- world example. Suppose , we have 12 planes in which 1 is lighter plane and others are heavier planes and we have to find the plane with lightest weight assuming all the other heavier planes than this plane have same weight. There can be two approaches to solve this problem as shown below.</p>
<h3 id="heading-brute-force-approach">Brute Force Approach</h3>
<pre><code class="lang-cpp"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;stdio.h&gt;</span></span>

<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">findLighterPlane</span><span class="hljs-params">(<span class="hljs-keyword">float</span> planes[], <span class="hljs-keyword">int</span> size)</span> </span>{
    <span class="hljs-keyword">float</span> referenceWeight = planes[<span class="hljs-number">0</span>];
    <span class="hljs-keyword">int</span> lighterIndex = <span class="hljs-number">0</span>;

    <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">1</span>; i &lt; size; i++) {
        <span class="hljs-keyword">if</span> (planes[i] &lt; referenceWeight) {
            referenceWeight = planes[i];
            lighterIndex = i;
        }
    }

    <span class="hljs-keyword">return</span> lighterIndex;
}

<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>{
    <span class="hljs-keyword">float</span> planes[<span class="hljs-number">12</span>] = {<span class="hljs-number">1000</span>, <span class="hljs-number">1000</span>, <span class="hljs-number">1000</span>, <span class="hljs-number">1000</span>, <span class="hljs-number">1000</span>, <span class="hljs-number">1000</span>, <span class="hljs-number">950</span>, <span class="hljs-number">1000</span>, <span class="hljs-number">1000</span>, <span class="hljs-number">1000</span>, <span class="hljs-number">1000</span>, <span class="hljs-number">1000</span>};
    <span class="hljs-keyword">int</span> lighterIndex = findLighterPlane(planes, <span class="hljs-number">12</span>);

    <span class="hljs-built_in">printf</span>(<span class="hljs-string">"The lighter plane is at index: %d\n"</span>, lighterIndex);
    <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;
}
</code></pre>
<p>In this approach, findLighterPlane(float planes[],int size) function takes 2 or 3 lines of code to give us the result or answer of the problem statement , which is “The lighter plane is at index: 6”.</p>
<h2 id="heading-divide-and-conquer-approach">Divide and Conquer Approach</h2>
<pre><code class="lang-cpp"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;stdio.h&gt;</span></span>

<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">findLighterPlane</span><span class="hljs-params">(<span class="hljs-keyword">float</span> planes[],<span class="hljs-keyword">int</span> size)</span></span>{
 <span class="hljs-keyword">float</span> referenceWeight = planes[<span class="hljs-number">0</span>];
 <span class="hljs-keyword">float</span> A = planes[<span class="hljs-number">0</span>] + planes[<span class="hljs-number">1</span>] + planes[<span class="hljs-number">2</span>] + planes[<span class="hljs-number">3</span>]; 
 <span class="hljs-keyword">float</span> B = planes[<span class="hljs-number">4</span>] + planes[<span class="hljs-number">5</span>] + planes[<span class="hljs-number">6</span>] + planes[<span class="hljs-number">7</span>];
 <span class="hljs-keyword">float</span> C = planes[<span class="hljs-number">8</span>] + planes[<span class="hljs-number">9</span>] + planes[<span class="hljs-number">10</span>] + planes[<span class="hljs-number">11</span>];

 <span class="hljs-keyword">if</span>(A == B) {
  <span class="hljs-keyword">if</span>(planes[<span class="hljs-number">8</span>]==planes[<span class="hljs-number">9</span>]) {
   <span class="hljs-keyword">if</span>(planes[<span class="hljs-number">10</span>] &lt; planes[<span class="hljs-number">11</span>])
    <span class="hljs-keyword">return</span> <span class="hljs-number">10</span>;
   <span class="hljs-keyword">else</span>
    <span class="hljs-keyword">return</span> <span class="hljs-number">11</span>;  
  }
<span class="hljs-comment">// if planes[8] != planes[9]</span>
  <span class="hljs-keyword">else</span>{
   <span class="hljs-keyword">if</span>(planes[<span class="hljs-number">8</span>] &lt; planes[<span class="hljs-number">9</span>]
    <span class="hljs-keyword">return</span> <span class="hljs-number">8</span>;
   <span class="hljs-keyword">else</span>
    <span class="hljs-keyword">return</span> <span class="hljs-number">9</span>;
  }   
 }
<span class="hljs-comment">// if A != B</span>
<span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span>(A &lt; B)
{
  <span class="hljs-keyword">if</span>(planes[<span class="hljs-number">0</span>]==planes[<span class="hljs-number">1</span>]) {
   <span class="hljs-keyword">if</span>(planes[<span class="hljs-number">2</span>] &lt; planes[<span class="hljs-number">3</span>])
    <span class="hljs-keyword">return</span> <span class="hljs-number">2</span>;
   <span class="hljs-keyword">else</span>
    <span class="hljs-keyword">return</span> <span class="hljs-number">3</span>;  
  }
<span class="hljs-comment">// if planes[0] != planes[1]</span>
  <span class="hljs-keyword">else</span>{
   <span class="hljs-keyword">if</span>(planes[<span class="hljs-number">0</span>] &lt; planes[<span class="hljs-number">1</span>]
    <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;
   <span class="hljs-keyword">else</span>
    <span class="hljs-keyword">return</span> <span class="hljs-number">1</span>;
  }
}

<span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span>(A &gt; B)
{
   <span class="hljs-comment">// for B &lt; A </span>
   <span class="hljs-keyword">if</span>(planes[<span class="hljs-number">4</span>]==planes[<span class="hljs-number">5</span>]) {
   <span class="hljs-keyword">if</span>(planes[<span class="hljs-number">6</span>] &lt; planes[<span class="hljs-number">7</span>])
    <span class="hljs-keyword">return</span> <span class="hljs-number">6</span>;
   <span class="hljs-keyword">else</span>
    <span class="hljs-keyword">return</span> <span class="hljs-number">7</span>;  
  }
<span class="hljs-comment">// if planes[4] != planes[5]</span>
  <span class="hljs-keyword">else</span>{
   <span class="hljs-keyword">if</span>(planes[<span class="hljs-number">4</span>] &lt; planes[<span class="hljs-number">5</span>]
    <span class="hljs-keyword">return</span> <span class="hljs-number">4</span>;
   <span class="hljs-keyword">else</span>
    <span class="hljs-keyword">return</span> <span class="hljs-number">5</span>;
  }
}

 <span class="hljs-comment">//function body closes</span>
}

<span class="hljs-keyword">int</span> main() {
    <span class="hljs-keyword">float</span> planes[<span class="hljs-number">12</span>] = {<span class="hljs-number">1000</span>, <span class="hljs-number">1000</span>, <span class="hljs-number">1000</span>, <span class="hljs-number">1000</span>, <span class="hljs-number">1000</span>, <span class="hljs-number">1000</span>, <span class="hljs-number">950</span>, <span class="hljs-number">1000</span>, <span class="hljs-number">1000</span>, <span class="hljs-number">1000</span>, <span class="hljs-number">1000</span>, <span class="hljs-number">1000</span>};
    <span class="hljs-keyword">int</span> lighterIndex = findLighterPlane(planes, <span class="hljs-number">12</span>);

    <span class="hljs-built_in">printf</span>(<span class="hljs-string">"The lighter plane is at index: %d\n"</span>, lighterIndex);
    <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;
}
</code></pre>
<p>In this approach, we get the right answer of the problem statement i.e. lighterIndex is 6 but in a fewer steps of comparisons which are 3 if statement comparisons. While, in brute force approach , steps of comparisons are more as in that approach , we visit all the indexes of planes[] array. Thus, divide and conquer approach which has more lines of code is suprisingly a better approach.</p>
<h2 id="heading-conclusion">Conclusion</h2>
<p>The better approach among the two approaches is the code with more lines of code i.e. the second approach. The second approach i.e. divide and conquer is an efficient approach. So, based on efficiency factor, that is an important factor , we can say that it is the best approach. But now the question is how we know that this has greater efficiency ? The answer to this question is by knowing the time complexity of both codes.</p>
<h3 id="heading-time-complexity"><strong>Time Complexity :-</strong></h3>
<p>It is a measure of running time of a program as the input size of the program grows. It can be used to compare efficiency and performance of programs.</p>
<p>If we generalize , the above two codes then the brute force approach takes O(n) time. And the divide and conquer approach takes O(log n) time. Thus, the second approach is an efficient approach.</p>
<p>Thus, Time Complexity make it easy to choose between 2 lines or 20 lines of code to solve the same problem as we will always choose an efficient approach.</p>
]]></content:encoded></item></channel></rss>