Write code to merge two sorted arrays?

Data Structure >   Array >   Array Introduction in Data Structure  

Long Question

795


Answer:



/* Program to merge two sorted arrays */
#include<stdio.h>
int merge(int[],int[],int[],int,int);
void main()
{
int a[20],b[20],c[40],n,m,i,p;
printf("\nEnter the no.of element present in the first array: ");
scanf("%d",&n);
printf("\nEnter the first array </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/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.