Write code to insert data in an array at a particular position

Data Structure >   Array >   Array Introduction in Data Structure  

Short Question

603


Answer:



#include<stdio.h>
Void main ()
{
int array [10], n elem, pos;
// Reading an array
printf ( </p>
                                          
                                        <hr>
                                        
                              <div style="text-align:center">
                              <a href="#list" class="btn btn-info btn-sm main-gradient-tutorials">Go to Question List</a>
                              </div>
                              
                              <div class = "shadow p-3 mt-2" style = "border-radius: 10px;">
                               <div class='col m-3'><p> <i class='fa fa-info-circle'></i> <small>This Particular section is dedicated to <b>Question & Answer</b> only. If you want learn more about <b>Data Structure</b>. Then you can visit below links to get more depth on this subject.  </small></p></div><div class='col m-3'><div class="row">
     <div class="col bg-light m-2 shadow btn btn-success"><a href="https://www.atnyla.com/syllabus/data-structure/3">Data Structure Syllabus </a> </div>
     <div class="col bg-light m-2 shadow btn btn-success"><a href="https://www.atnyla.com/tutorial/about-dsa/3/295">Data Structure Tutorial</a> </div>  
     <div class="col bg-light m-2 shadow btn btn-success"><a href="https://www.atnyla.com/array/3/54">Data Structure Programs </a> </div> 
     <div class="col bg-light m-2 shadow btn btn-success"><a href="https://www.atnyla.com/qa/data-structure-introduction/3/56">Data Structure Question Answers </a> </div>
     <div class="col bg-light m-2 shadow btn btn-success"><a href="https://www.atnyla.com/general-knowledge/95/254">Data Structure MCQ </a> </div>  
</div> 
<hr></div><hr style='height:3px;border-width:0;color:gray;background-color:gray'>                              </div>
                                 
                              <br>
                             <div class="shadow p-3 mb-5 bg-body rounded">
                               <a href="/tutorial/about-c-tutorial/1/152" class="btn btn-sm btn-default"># C Tutorials</a> 
                               <a href="/tutorial/java/0/0" class="btn btn-sm btn-default"># JAVA Tutorials</a>
                               <a href="/tutorial/about-html5-tutorial/2/233" class="btn btn-sm btn-default"># HTML Tutorials</a>
                           
                              <a href="https://www.atnyla.com/tutorial/about-tutorial/6/395" class="btn btn-sm btn-default"># Computer Fundamental</a> 
                               <a href="https://www.atnyla.com/tutorial/about-dsa/3/295" class="btn btn-sm btn-default"># Data Structure</a>
                               <a href="https://www.atnyla.com/tutorial/about-dbms-tutorial/13/713" class="btn btn-sm btn-default"># DBMS Tutorials</a>
                           
                              <a href="https://www.atnyla.com/tutorial/about-sql-tutorial/5/355" class="btn btn-sm btn-default">SQL</a> 
                               <a href="https://www.atnyla.com/tutorial/about-c-tutorial/8/468" class="btn btn-sm btn-default"># C# Language</a>
                               <a href="https://www.atnyla.com/tutorial/about-r-tutorial/7/409" class="btn btn-sm btn-default"># R Language</a>
                            
                              <a href="https://www.atnyla.com/tutorial/about-php-tutorial/12/604" class="btn btn-sm btn-default"># PHP</a> 
                               <a href="https://www.atnyla.com/tutorial/about-python-tutorial/11/597" class="btn btn-sm btn-default"># Python</a>
                               <a href="https://www.atnyla.com/tutorial/about-vue-js-tutorial/14/765" class="btn btn-sm btn-default"># Vue JS</a>
                             </div>
                             
                             <hr>
                             <h3>Join Our telegram group to ask Questions </h3>
                             <p> Click below button to join our groups. </p>
                             <div class="row">
                                 <div class="col m-2 bg-light shadow btn btn-success">
                                   <a href="https://t.me/atnyla"  rel="nofollow" target="_blank"> Ask Question - CSE </a>
                                 </div>  
                                  <div class="col m-2 bg-light shadow btn btn-success">
                                    <a href="https://t.me/ComputerLanguageWithRumman"  rel="nofollow" target="_blank"> Programming Language - PDF</a>
                                </div> 
                            </div>
                                        
                              <br>
                               
                            </div> 
                        </div>
                    </div>
                   <div class="col-lg-5">
                
                            <ul class="list-group" >
                                <li id="list" class="list-group-item d-flex justify-content-between align-items-center main-gradient-tutorials mb-2"><strong>Related Program List</strong></li>
                                         <div class="list-group divStyleQuestion" style="border:1px solid #e0e0e0;padding:8px">
            <div class="d-flex w-100 justify-content-between">
              <a href="/qanswer/how-to-delete-an-element-from-a-specified-position-from-an-array-question/54/561">
                  <h5 class="mb-1" style="color:#194757;">561. &nbsp;How to delete an element from a specified position from an array?</h5>
              </a>
            </div>
                            <div class="card">
                  <div class="card-body">
                     Answer: <p> To delete a value from an array, you need to shift every value after the
location you want to delete. The logic is very simple: start from the position
you want to delete and copy the previous item to the current element until
end of the array.</p>                  </div>
                </div>
                        <div class="d-flex w-100 justify-content-between">
                <a style="color:#9e9d9d;" href="/tutorial/array-introduction-in-data-structure/3/300">
                    <small># Tutorial: Array Introduction in Data Structure</small> 
                </a>
            </div>
           
                        <a style="color:#9e9d9d;" href="/qa/-/3/54">
                <small><i class="fa fa-code" aria-hidden="true"></i> Question: Array</small>
            </a>
                                   <span class="float-end" style="color:#9e9d9d"> <small><i class="fa fa-check" aria-hidden="true"></i> Short </small></span>  
                            </div>
      
            <div class="list-group divStyleQuestion" style="border:1px solid #e0e0e0;padding:8px">
            <div class="d-flex w-100 justify-content-between">
              <a href="/qanswer/write-code-to-delete-data-from-an-array-from-a-particular-position-question/54/562">
                  <h5 class="mb-1" style="color:#194757;">562. &nbsp;Write code to delete data from an array from a particular position?</h5>
              </a>
            </div>
                            <div class="card">
                  <div class="card-body">
                     Answer: <pre class="prettyprint">
<xmp>
#include<stdio.h>
void main ()
{
int array[10], n elem ,pos;
// Reading an array
printf(                  </div>
                </div>
                        <div class="d-flex w-100 justify-content-between">
                <a style="color:#9e9d9d;" href="/tutorial/array-introduction-in-data-structure/3/300">
                    <small># Tutorial: Array Introduction in Data Structure</small> 
                </a>
            </div>
           
                        <a style="color:#9e9d9d;" href="/qa/-/3/54">
                <small><i class="fa fa-code" aria-hidden="true"></i> Question: Array</small>
            </a>
                                   <span class="float-end" style="color:#9e9d9d"> <small><i class="fa fa-check" aria-hidden="true"></i> Short </small></span>  
                            </div>
      
            <div class="list-group divStyleQuestion" style="border:1px solid #e0e0e0;padding:8px">
            <div class="d-flex w-100 justify-content-between">
              <a href="/qanswer/how-to-merge-two-sorted-arrays-question/54/563">
                  <h5 class="mb-1" style="color:#194757;">563. &nbsp;How to merge two sorted arrays?</h5>
              </a>
            </div>
                            <div class="card">
                  <div class="card-body">
                     Answer: <p>Assume, that both arrays are sorted in ascending order and we want resulting array to maintain the same order. Algorithm to merge two arrays A[0..m-1] and B[0..n-1] into an array C[0..m+n-1] is as following:<br />1. Introduce read-indices i, j to traverse arrays A and B, accordingly. Introduce write-index k to store position of the first free cell in the resulting array. By default i = j = k = 0.<br />2. At each step: if both indices are in range (i &lt; m and j &lt; n), choose minimum of (A[i], B[j]) and write it to C[k]. Otherwise go to step 4.<br />3. Increase k and index of the array, algorithm located minimal value at, by one. Repeat step 2.<br />4. Copy the rest values from the array, which index is still in range, to the resulting array.</p>                  </div>
                </div>
                        <div class="d-flex w-100 justify-content-between">
                <a style="color:#9e9d9d;" href="/tutorial/array-introduction-in-data-structure/3/300">
                    <small># Tutorial: Array Introduction in Data Structure</small> 
                </a>
            </div>
           
                        <a style="color:#9e9d9d;" href="/qa/-/3/54">
                <small><i class="fa fa-code" aria-hidden="true"></i> Question: Array</small>
            </a>
                                   <span class="float-end" style="color:#9e9d9d"> <small><i class="fa fa-check" aria-hidden="true"></i> Short </small></span>  
                            </div>
      
            <div class="list-group divStyleQuestion" style="border:1px solid #e0e0e0;padding:8px">
            <div class="d-flex w-100 justify-content-between">
              <a href="/qanswer/write-code-to-merge-two-sorted-arrays-question/54/564">
                  <h5 class="mb-1" style="color:#194757;">564. &nbsp;Write code to merge two sorted arrays?</h5>
              </a>
            </div>
                        <div class="d-flex w-100 justify-content-between">
                <a style="color:#9e9d9d;" href="/tutorial/array-introduction-in-data-structure/3/300">
                    <small># Tutorial: Array Introduction in Data Structure</small> 
                </a>
            </div>
           
                        <a style="color:#9e9d9d;" href="/qa/-/3/54">
                <small><i class="fa fa-code" aria-hidden="true"></i> Question: Array</small>
            </a>
                                  <span class="float-end" style="color:#9e9d9d"> <small><i class="fa fa-check" aria-hidden="true"></i> Long </small></span>
                            </div>
      
            <div class="list-group divStyleQuestion" style="border:1px solid #e0e0e0;padding:8px">
            <div class="d-flex w-100 justify-content-between">
              <a href="/qanswer/what-is-two-dimensional-array-question/54/565">
                  <h5 class="mb-1" style="color:#194757;">565. &nbsp;What is two dimensional array?</h5>
              </a>
            </div>
                        <div class="d-flex w-100 justify-content-between">
                <a style="color:#9e9d9d;" href="/tutorial/array-introduction-in-data-structure/3/300">
                    <small># Tutorial: Array Introduction in Data Structure</small> 
                </a>
            </div>
           
                        <a style="color:#9e9d9d;" href="/qa/-/3/54">
                <small><i class="fa fa-code" aria-hidden="true"></i> Question: Array</small>
            </a>
                                  <span class="float-end" style="color:#9e9d9d"> <small><i class="fa fa-check" aria-hidden="true"></i> Long </small></span>
                            </div>
      
            <div class="list-group divStyleQuestion" style="border:1px solid #e0e0e0;padding:8px">
            <div class="d-flex w-100 justify-content-between">
              <a href="/qanswer/how-to-read-and-write-data-in-2-d-array-question/54/566">
                  <h5 class="mb-1" style="color:#194757;">566. &nbsp;How to read and write data in 2-D array?</h5>
              </a>
            </div>
                        <div class="d-flex w-100 justify-content-between">
                <a style="color:#9e9d9d;" href="/tutorial/array-introduction-in-data-structure/3/300">
                    <small># Tutorial: Array Introduction in Data Structure</small> 
                </a>
            </div>
           
                        <a style="color:#9e9d9d;" href="/qa/-/3/54">
                <small><i class="fa fa-code" aria-hidden="true"></i> Question: Array</small>
            </a>
                                  <span class="float-end" style="color:#9e9d9d"> <small><i class="fa fa-check" aria-hidden="true"></i> Long </small></span>
                            </div>
      
            <div class="list-group divStyleQuestion" style="border:1px solid #e0e0e0;padding:8px">
            <div class="d-flex w-100 justify-content-between">
              <a href="/qanswer/write-an-algorithm-to-multiply-two-2-dimensional-array-question/54/567">
                  <h5 class="mb-1" style="color:#194757;">567. &nbsp;Write an algorithm to multiply two 2-Dimensional array?</h5>
              </a>
            </div>
                            <div class="card">
                  <div class="card-body">
                     Answer: <p>Multiplication of two matrixes:<br />Rule: Multiplication of two matrixes is only possible if first matrix has size<strong> m X n</strong> and other matrix has size <strong>n x r</strong>. Where m, n and r are any positive integer.</p>
<p><span style="color: #3366ff;"><em>Multiplication of two matrixes is defined as:</em></span></p>
<p><span style="color: #3366ff;"><em><img class="img-responsive" src="/blog/images/114688820-ds-image-1.png" alt="" width="320" height="69" /></em></span></p>
<p><span style="color: #000000;">Where 1 ? i ? m and 1 ? j ? n</span></p>
<p><span style="color: #000000;"><br /><strong>For example:</strong><br />Suppose two matrixes A and B of size of 2 x 2 and 2 x 3 respectively:</span></p>
<p><span style="color: #000000;"><img class="img-responsive" src="/blog/images/715013965-ds-image-2.png" alt="" width="771" height="596" /></span></p>
<p><span style="color: #000000;">Here A is a two                   </div>
                </div>
                        <div class="d-flex w-100 justify-content-between">
                <a style="color:#9e9d9d;" href="/tutorial/array-introduction-in-data-structure/3/300">
                    <small># Tutorial: Array Introduction in Data Structure</small> 
                </a>
            </div>
           
                        <a style="color:#9e9d9d;" href="/qa/-/3/54">
                <small><i class="fa fa-code" aria-hidden="true"></i> Question: Array</small>
            </a>
                                   <span class="float-end" style="color:#9e9d9d"> <small><i class="fa fa-check" aria-hidden="true"></i> Short </small></span>  
                            </div>
      
            <div class="list-group divStyleQuestion" style="border:1px solid #e0e0e0;padding:8px">
            <div class="d-flex w-100 justify-content-between">
              <a href="/qanswer/wap-to-sum-of-diagonal-elements-of-a-matrix-in-c-question/54/568">
                  <h5 class="mb-1" style="color:#194757;">568. &nbsp;Wap to Sum of diagonal elements of a matrix in c?</h5>
              </a>
            </div>
                        <div class="d-flex w-100 justify-content-between">
                <a style="color:#9e9d9d;" href="/tutorial/array-introduction-in-data-structure/3/300">
                    <small># Tutorial: Array Introduction in Data Structure</small> 
                </a>
            </div>
           
                        <a style="color:#9e9d9d;" href="/qa/-/3/54">
                <small><i class="fa fa-code" aria-hidden="true"></i> Question: Array</small>
            </a>
                                  <span class="float-end" style="color:#9e9d9d"> <small><i class="fa fa-check" aria-hidden="true"></i> Long </small></span>
                            </div>
      
            <div class="list-group divStyleQuestion" style="border:1px solid #e0e0e0;padding:8px">
            <div class="d-flex w-100 justify-content-between">
              <a href="/qanswer/how-to-do-linear-search-in-an-array-question/60/569">
                  <h5 class="mb-1" style="color:#194757;">569. &nbsp;How to do Linear Search in an Array?</h5>
              </a>
            </div>
                            <div class="card">
                  <div class="card-body">
                     Answer: <p>Linear search or Sequential is the simplest search algorithm for finding a particular value in an array, that consists of checking every one of its elements, one at a time and in sequence until the desired one is found.</p>

<p>Here's an example code for performing a linear search in an array using C language: </p>
<pre class = "prettyprint"><xmp>
#include <stdio.h>

// Function to perform linear search in an array
int linearSearch(int arr[], int n, int key) {
    int i;
    for (i = 0; i < n; i++) {
        if (arr[i] == key)
            return i;
    }
    return -1;
}

int main() {
    int arr[] = { 2, 5, 7, 12, 16, 20, 23 };
    int n = sizeof(arr) / sizeof(arr[0]);
    int key = 16;

    int index = linearSearch(arr, n, key);

    if (index == -1) {
        printf("Element not found in array");
    } else {
        printf("Element found at index %d", index);
    }

    return 0;
}

In this code, we have defined a function linearSearch which takes an integer array, its size and the search key as arguments. The function then iterates through the array elements one by one and compares them with the search key. If the key is found, the function returns the index of the key in the array, otherwise it returns -1 indicating that the key was not found.

In the main function, we have declared an integer array, its size and the search key. We then call the linearSearch function passing these arguments, and store the result in the index variable. We then check if the value of index is -1 or not, and print the appropriate message indicating whether the key was found or not.