top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Write a C program to merge the elements of 2 sorted array?

0 votes
503 views
Write a C program to merge the elements of 2 sorted array?
posted May 16, 2017 by Pankaj Singh

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button
Homework problem, please do it yourself.

2 Answers

0 votes
#include<stdio.h>
#include<stdlib.h>

int main(void)
{
    // array number 1
    int arr1[] = {1,2,3,4,5,6,7,8,9,0};

    // array number 2
    int arr2[] = {1,2,3,4,5,6,7,8,9,0,4,3,4,6,4,5,6};

    int size_of_A1,size_of_A2,sum;
    int loop1=0,loop2=0,loop_main=0;
    int *p;

    //get number of element in arr1 and arr2 and sum 
    size_of_A1 = sizeof(arr1)/sizeof(int);
    size_of_A2 = sizeof(arr2)/sizeof(int);
    sum = size_of_A1 + size_of_A2;

    // alocate memory for array size of arr1+arr2
    p = (int*)calloc(sum,sizeof(int));

    // input elements from string one
    while(loop1 != size_of_A1)
    {
        p[loop_main] = arr1[loop1];
        loop1++;
        loop_main++;
    }

    loop_main++; // increment loop main  for 1 becose you started from inedex zero 

    //input elements of arr2 and set it on top arr1 in new arr
    while(loop2 != size_of_A2) 
    {
        loop_main[p] = arr2[loop2];
        loop2++;
        loop_main++;
    }

    //print elements of new arr and set loop_main to 0
    loop_main=0;
    while(loop_main != sum) 
    {
        printf("%d,",loop_main[p]);
        loop_main++;
    }
    free(p);
    //this is for stoping program at end 
    getchar();
    getchar();
    return 0;
}
answer May 16, 2017 by Leon Martinović
0 votes
#include<stdio.h>
#include<conio.h>
#define size 
main()
{
    int arr1[size],arr2[size],arr3[size+size],i,j,temp=0,no,num,k=0;
    printf("Enter number of element in array first:\t");
    scanf("%d",&no);
    printf("\nEnter element of array first:\n");
    for(i=0;i<no;i++)
    {
        scanf("%d",&arr1[i]);
    }
    printf("\nEnter number of element in array second:\t");
    scanf("%d",&num);
    printf("\nEnter element of array second:\n");
    for(i=0;i<num;i++)
    {
        scanf("%d",&arr2[i]);
    }
    printf("\n element of array first:\n");
    for(i=0;i<no;i++)
    {
        printf("%d  ",arr1[i]);
    }
    printf("\n element of array second:\n");
    for(i=0;i<num;i++)
    {
        printf("%d  ",arr2[i]);
    }
    for(i=0;i<no+num;i++)
    {
        k=0;
        if(i>no-1)
        {
            arr3[i]=arr2[k];
            k++;
        }
        arr3[i]=arr1[i];
    }
    printf("\n mearg element of array first and second are:\n");
    for(i=0;i<no+num;i++)
    {
        printf("%d  ",arr3[i]);
    }
    printf("\nsorted array3 are\n");
    for(i=0;i<no+num;i++)
    {
        for(j=i+1;j<no+num+1;j++)
        {
            if(arr3[j]>arr3[i])
            {
                temp=arr3[i];
                arr3[i]=arr3[j];
                arr3[j]=temp;
            }
        }
    }
    for(i=0;i<no+num;i++)
    {
        printf("%d ",arr3[i]);
    }
    getch();
    return 0;
}
answer Jul 1, 2017 by Ajay Kumar
Similar Questions
0 votes

Given an array of integers, rearrange the array in such a way that the first element is first maximum and second element is first minimum.

+1 vote

Write a program to print the sum of the elements of the array with the given below condition. If the array has 6 and 7 in succeeding orders, ignore 6 and 7 and the numbers between them for the calculation of sum.
Eg1) Array Elements - 10,3,6,1,2,7,9
O/P: 22
[i.e 10+3+9]
Eg2) Array Elements - 7,1,2,3,6
O/P:19
Eg3) Array Elements - 1,6,4,7,9
O/P:10

...