<?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" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Nikita Gosai on Medium]]></title>
        <description><![CDATA[Stories by Nikita Gosai on Medium]]></description>
        <link>https://medium.com/@gosainikita67?source=rss-25eaf3d8cec8------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*R8-BsXS0OYB9Wovk</url>
            <title>Stories by Nikita Gosai on Medium</title>
            <link>https://medium.com/@gosainikita67?source=rss-25eaf3d8cec8------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 17 Nov 2026 13:51:02 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@gosainikita67/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Arrays in C++]]></title>
            <link>https://medium.com/@gosainikita67/arrays-in-c-e5098d88e677?source=rss-25eaf3d8cec8------2</link>
            <guid isPermaLink="false">https://medium.com/p/e5098d88e677</guid>
            <category><![CDATA[cpp]]></category>
            <category><![CDATA[datastrucutre]]></category>
            <dc:creator><![CDATA[Nikita Gosai]]></dc:creator>
            <pubDate>Mon, 11 Oct 2021 16:42:37 GMT</pubDate>
            <atom:updated>2021-10-11T16:42:37.420Z</atom:updated>
            <content:encoded><![CDATA[<p>An Array is a data structure consisting of homogenous set of elements stored at contiguous memory locations.</p><p>You can imagine arrays as a container where each bucket inside the container can hold a value of same data type , and each bucket inside an array could be accessed by index numbers which range from 0 to size-1.</p><p>For example, consider an array of size 7 with data type as integer :</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/815/1*YLnb-TdXbor6GA5m54uoAA.png" /><figcaption>integer type array of size 7</figcaption></figure><p>Here, array is a container which contains collections of multiple buckets all of type integers and each bucket consists of a single element. These buckets are stored in a continuous manner and each bucket is referenced by its index number which starts from 0 to 6 (i.e. size-1).</p><h3>Array declaration and initialization in C++ :</h3><h4>1. By specifying size :</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/393/1*fyVeZ0EfYl8N2YwDlm3bMg.png" /></figure><pre>#include &lt;iostream&gt;</pre><pre>using namespace std;</pre><pre>int main()<br>{<br>    int arr[6];       //arr of size 6 , type integer<br>    float arr2[4]      //arr of size 4 , type integer and float<br>    <br>    /* by default all the buckets contain garbage <br>    value right now , unless you update these values */<br>    <br>    return 0;<br>}<br></pre><h4>2. By initializing elements:</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/486/1*JPr33OuGZmBX7VyoEFdzuQ.png" /></figure><pre>#include &lt;iostream&gt;</pre><pre>using namespace std;</pre><pre>int main()<br>{<br>   int arr[] = {45,2,12};            //arr of size 3, type integer <br>   float arr2[] = {1.2,3,4,5.6}   //arr of size 4, type int &amp; float<br>    <br>    return 0;<br>}</pre><h4>3. By specifying size and initializing elements:</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/423/1*1lEzT-ivq1dOiHzyLJdltw.png" /></figure><pre>#include &lt;iostream&gt;</pre><pre>using namespace std;</pre><pre>int main()<br>{<br>   int arr[5] = {7};           <br>   //arr of size 5, first element in the array is 7 rest all are 0<br>  <br>   int arr2[6] = {33,21,3}<br>   //first 3 elements of the array are 33, 21, 3 rest all are 0</pre><pre>return 0;<br>}</pre><h3>Accessing array in C++</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/258/1*2S3y3ZskLCReSC-HwL9MEw.png" /></figure><p>If you want to refer to the bucket at ith position, you can simply write array_name[i] , where i is the index of the bucket.</p><pre>#include &lt;iostream&gt;</pre><pre>using namespace std;</pre><pre>int main()<br>{<br>    int n=6;<br>    int arr1[n] = {7};  <br>    float arr2[n] = {1.2,4,3,2};<br>    <br>    //Note: indexing starts from 0 in arrays<br>    cout&lt;&lt;&quot;Accessing 3rd position , that is the 2nd index of array &quot;&lt;&lt;arr1[2];<br>    cout&lt;&lt;&quot;\nAccessing 4rd position , that is the 3nd index of array &quot;&lt;&lt;arr2[3];<br>    cout&lt;&lt;&quot;\nArray 1: &quot;;<br>    for(int i=0;i&lt;n;i++)  <br>    cout&lt;&lt;&quot; &quot;&lt;&lt;arr1[i];<br>    <br>    cout&lt;&lt;&quot;\nArray 2: &quot;;<br>    for(int i=0;i&lt;n;i++)  <br>    cout&lt;&lt;&quot; &quot;&lt;&lt;arr2[i];</pre><pre>return 0;<br>}<br></pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/738/1*nKJTXTtJ1pYkRthVoK9y6w.png" /><figcaption>Output</figcaption></figure><h3>Update / Input elements in Arrays in C++ :</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/346/1*jCk8Gql49HAauHLFXx5pDg.png" /></figure><pre>#include &lt;iostream&gt;</pre><pre>using namespace std;</pre><pre>int main()<br>{<br>    int n=6;<br>    int arr1[n] = {0};  <br>    <br>    //Input elements in array;<br>    cout&lt;&lt;&quot;Enter Array elements: &quot;;<br>    for(int i=0;i&lt;n;i++)<br>    {<br>        //input one integer and store at i&#39;th bucket of the array<br>        cin&gt;&gt;arr1[i];   <br>    }<br>    <br>    cout&lt;&lt;&quot;\n Array elements: &quot;;<br>    for(int i=0;i&lt;n;i++)<br>    {<br>        cout&lt;&lt;arr1[i]&lt;&lt;&quot;, &quot;;<br>    }<br>    <br>    <br>    //Modify any element<br>    arr1[3]=45;<br>    arr1[0]=99;<br>    <br>    cout&lt;&lt;&quot;\n After Updating Array Elements: &quot;;<br>    for(int i=0;i&lt;n;i++)<br>    {<br>        cout&lt;&lt;arr1[i]&lt;&lt;&quot;, &quot;;<br>    }</pre><pre>return 0;<br>}</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/763/1*wMGlJHCdxTC_XWRZyUgUaw.png" /></figure><h3>Size of the Array in C++</h3><p>let us consider an array of integer type, arr=[2,3,4,5,9] if each bucket is of 4 bytes then 5 buckets would be of size 4*5 bytes , so total size of the array would be 4*5bytes=20 bytes.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/261/1*upZWCDYC_37nLcbGL8U19w.png" /></figure><pre>#include &lt;iostream&gt;</pre><pre>using namespace std;</pre><pre>int main()<br>{<br>    int arr1[10] = {0}; <br>    cout&lt;&lt;sizeof(arr1);  //10*4bytes = 40bytes;<br>    <br>    double arr2[3] ={0};<br>    cout&lt;&lt;sizeof(arr2);  //3*8bytes = 24bytes;<br>    <br>    return 0;<br>}</pre><p>Alternatively you can also find length of an array, by dividing the size of the array by the size of each element(in bytes).</p><pre>#include &lt;iostream&gt;</pre><pre>using namespace std;</pre><pre>int main()<br>{<br>    int arr1[] = {34,5,6,2,3}; <br>    int arr_size = sizeof(arr1)/sizeof(arr1[0]);  // 20/4 bytes = 5<br>    cout&lt;&lt;arr_size;<br>    <br>    return 0;<br>}</pre><h3>Insertion in arrays in C++ :</h3><h4>1. Insert at the beginning</h4><pre>#include &lt;iostream&gt;</pre><pre>using namespace std;</pre><pre>int main()<br>{<br>    int arr[] = {23,56,34,78,99}; <br>    int ele;<br>    int arr_size = sizeof(arr)/sizeof(arr[0]);  <br>    cout&lt;&lt;&quot;Enter element to be inserted at the beginning: &quot;;<br>    cin&gt;&gt;ele;<br>    <br>    for(int i=arr_size-1;i&gt;=0;i--){<br>        arr[i+1]=arr[i];  //shift array elements downwards<br>    }<br>    arr[0]=ele;<br>    arr_size++;  //increment size of array<br>    <br>    //element inserted at the beginning<br>    for(int i=0;i&lt;arr_size;i++){<br>        cout&lt;&lt;arr[i]&lt;&lt;&quot;, &quot;;<br>    }<br>    <br>    <br>    return 0;<br>}</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/708/1*ElfFRq9zcROZqKG3eHrdfA.png" /><figcaption>Output</figcaption></figure><h4>2. Insert at Specific position</h4><pre>#include &lt;iostream&gt;</pre><pre>using namespace std;</pre><pre>int main()<br>{<br>    int arr[] = {23,56,34,78,99}; <br>    int pos;<br>    int ele;<br>    int arr_size = sizeof(arr)/sizeof(arr[0]);  <br>    cout&lt;&lt;&quot;Position at which element is to be inserted: &quot;;<br>    cin&gt;&gt;pos;<br>    cout&lt;&lt;&quot;Enter element to be inserted: &quot;;<br>    cin&gt;&gt;ele;<br>    <br>    //increment the size of the array<br>    arr_size++;<br>  <br>    for (int i = arr_size-1; i &gt;= pos; i--)<br>        arr[i] = arr[i - 1];  //shifting elements forward to make space for pos<br>  <br> <br>    // insert element at pos<br>    arr[pos-1] = ele;<br>    <br>    //element inserted at given position <br>    for(int i=0;i&lt;arr_size;i++){<br>        cout&lt;&lt;arr[i]&lt;&lt;&quot;, &quot;;<br>    }<br>    <br>    <br>    return 0;<br>}</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/810/1*i5Tkn9rWWEMxP_P7sDp4mg.png" /><figcaption>Output</figcaption></figure><h3>Deletion in arrays in C++ :</h3><h4>1. Delete from beginning</h4><pre>#include &lt;iostream&gt;</pre><pre>using namespace std;</pre><pre>int main()<br>{<br>    int arr[] = {23,56,34,78,99}; <br>    <br>    int arr_size = sizeof(arr)/sizeof(arr[0]);<br>    <br>    for (int i=0; i&lt;arr_size; i++)<br>        arr[i] = arr[i+1];  //shifting elements ahead<br>        <br>    //decrease array size<br>    arr_size--;<br>    <br>    <br>    //element deleted from beginning<br>    for(int i=0;i&lt;arr_size;i++){<br>        cout&lt;&lt;arr[i]&lt;&lt;&quot;, &quot;;<br>    }<br>    <br>    <br>    return 0;<br>}</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/797/1*IvEwdUJ1u6FkfcotJEyNjA.png" /><figcaption>Output</figcaption></figure><h4>2. Delete specific element</h4><pre>#include &lt;iostream&gt;</pre><pre>using namespace std;</pre><pre>int main()<br>{<br>    int arr[] = {23,56,34,78,99}; <br>    int ele;<br>    int pos;<br>    cout&lt;&lt;&quot;Enter Element to be deleted: &quot;;<br>    cin&gt;&gt;ele;<br>    <br>    int arr_size = sizeof(arr)/sizeof(arr[0]);<br>    <br>    //find the position of element<br>    for(int i=0;i&lt;arr_size;i++){<br>        if(arr[i]==ele){<br>            pos=i;<br>            break;<br>        }<br>    }</pre><pre>//decrease array size<br>    arr_size--;<br>    <br>    for (int i=pos; i&lt;arr_size; i++)<br>        arr[i] = arr[i+1];  //shifting elements ahead<br>  <br>    <br>    //element deleted at given position <br>    for(int i=0;i&lt;arr_size;i++){<br>        cout&lt;&lt;arr[i]&lt;&lt;&quot;, &quot;;<br>    }<br>    <br>    <br>    return 0;<br>}</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/491/1*GhdATQxme11jDBSea3ryLw.png" /></figure><h3>Advantages and Disadvantages of Arrays</h3><h4>Advantages:</h4><ul><li>Searching and accessing elements becomes easy just by using indexes.</li><li>If a user wants to store many values of the same kind, arrays can be used effectively.</li><li>It allocates memory in contiguous memory locations for its elements. It does not allocate any extra memory for its elements. As a result, there is no memory overflow or shortage in arrays.</li></ul><h4>Disadvantages:</h4><ul><li>Array sized is fixed .We can’t change the size of an array once it’s been declared. At build time, we should know the size of the array.</li><li>Memory waste occurs when more memory is assigned more than is required, while less memory allocation might result in issues.</li><li>Inserting and deleting items from an array is a costly operation as to insert or delete any element in the array, you must traverse the array first and then move the remaining elements according to the action.</li></ul><h3>Applications of Arrays</h3><ul><li>Perform Matrix operations</li><li>Implement other Data structures such as Stack, Queues</li><li>Implement CPU Scheduling Algorithms</li></ul><p>Hope this article helps you. Please share your feedback 🙌🏽🙌🏽</p><p>THANK YOU !!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e5098d88e677" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>