Ques:-3) Suppose A,B,C are arrays of interger of size M,N,and M+N respectively.The numbers in array A appear in ascending order while the numbers in array B appear in descending order.write a user defined function in C to producethird array C by merging arrays A and B in ascending.Use A,B,C as arguments in the function.

				
					#include<stdio.h>
#include<conio.h>
void merge(int A[],int B[],int M,int N);
 void main()
{
int A[50],B[50],M,N,i,j;
clrscr();
printf("how many elements do u want in array 1:\n");
scanf("%d",&M);
printf("enter the elements of array 1 in ascending order:\n");
for(i=0;i<M;i++)
scanf("%d",&A[i]);
printf("how many elements do u want in array 2\n");
scanf("%d",&N);
printf("enter the elements of array 2 in decending order:\n");
for(i=0;i<N;i++)
scanf("%d",&B[i]);
merge(A,B,M,N);
getch();
}
void merge(int A[],int B[],int M,int N)
{
  int i,j,k,temp;
  int C[100];
for(i=0,k=0;i<M;i++,k++)
{
C[k]=A[i];
}
for(j=0;j<N;j++,k++)
{
C[k]=B[j];
}
   for(k=0;k<M+N;k++)
   {
     for(j=0;j<M+N;j++)
     {
	if(C[j]>C[j+1])
	{
	   temp=C[j];
	   C[j]=C[j+1];
	   C[j+1]=temp;
	}
     }
   }
      for(k=0;k<M+N;k++)
     printf("%d ",C[k]);
}

				
			

Out Put

 

To merge two arrays and into a third array in ascending order where is in ascending order and is in descending order, you can follow these steps:

  1. Define a function that takes arrays , , and along with their respective sizes and .
  2. Use pointers or indices to iterate through the arrays.
  3. Merge the arrays by comparing elements and placing the smallest element into the third array .

Here is the C code implementing the function:

				
					#include <stdio.h>

// Function prototype
void mergeArrays(int A[], int M, int B[], int N, int C[]);

int main() {
    // Example arrays
    int A[] = {1, 3, 5, 7};
    int B[] = {10, 8, 6, 4, 2};
    int M = sizeof(A) / sizeof(A[0]);
    int N = sizeof(B) / sizeof(B[0]);
    int C[M + N];

    // Merging arrays
    mergeArrays(A, M, B, N, C);

    // Printing the result
    printf("Merged array: ");
    for (int i = 0; i < M + N; i++) {
        printf("%d ", C[i]);
    }
    printf("\n");

    return 0;
}

void mergeArrays(int A[], int M, int B[], int N, int C[]) {
    int i = 0, j = N - 1, k = 0;

    // Merge arrays A and B into C
    while (i < M && j >= 0) {
        if (A[i] <= B[j]) {
            C[k++] = A[i++];
        } else {
            C[k++] = B[j--];
        }
    }

    // Copy remaining elements of A, if any
    while (i < M) {
        C[k++] = A[i++];
    }

    // Copy remaining elements of B, if any
    while (j >= 0) {
        C[k++] = B[j--];
    }
}

				
			

Explanation:

  1. Function Prototype:

				
					void mergeArrays(int A[], int M, int B[], int N, int C[]);

				
			

This defines the function that merges arrays A and B into array C.

  1. Main Function:

    • Initializes example arrays and .
    • Computes their sizes and .
    • Declares array of size +.
    • Calls the mergeArrays function.
    • Prints the merged array .
  2. mergeArrays Function:

    • Uses three indices i, j, and k for iterating through arrays , , and respectively.
    • Merges arrays and by comparing their current elements and placing the smaller one into .
    • After the main loop, any remaining elements in or are copied to .

This function ensures that the merged array is in ascending order as required.

Scroll to Top