Ques:-4)Suppose X,Y,Z are arrays of interger of size M,N,and M+N respectively.The numbers in array X and Y appear in desccending order.write a user defined function in C to produce third array Z by merging arrays X and Y in descending

				
					#include"stdio.h"
#include"conio.h"
void merge(int X[],int Y[],int M,int N);
void main()
{
   int X[50],Y[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 decending order:\n");
   for(i=0;i<M;i++)
      scanf("%d",&X[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",&Y[i]);
      merge(X,Y,M,N);
     getch();
}
void merge(int X[],int Y[],int M,int N)
{
  int i,j,k,temp;
  int Z[100];
   for(i=0,k=0;i<M;i++,k++)
   {
      Z[k]=X[i];
   }
   for(j=0;j<N;j++,k++)
   {
     Z[k]=Y[j];
   }
   for(k=0;k<=M+N;k++)
   {
     for(j=0;j<=M+N;j++)
     {
	if(Z[j]<Z[j+1])
	{
	   temp=Z[j];
	   Z[j]=Z[j+1];
	   Z[j+1]=temp;
	}
     }
   }
     printf("\n");
      for(k=0;k<=M+N;k++)
     printf("%d ",Z[k]);
}

				
			

Out Put

Here’s a C function to merge two arrays, ‘X‘ and ‘Y‘, which are sorted in descending order, into a third array ‘Z‘ that is also sorted in descending orde

				
					#include <stdio.h>

// Function to merge two descending order arrays into a single descending order array
void mergeDescending(int X[], int M, int Y[], int N, int Z[]) {
    int i = 0, j = 0, k = 0;

    // Merge arrays while both have elements left
    while (i < M && j < N) {
        if (X[i] > Y[j]) {
            Z[k++] = X[i++];
        } else {
            Z[k++] = Y[j++];
        }
    }

    // Copy any remaining elements from X
    while (i < M) {
        Z[k++] = X[i++];
    }

    // Copy any remaining elements from Y
    while (j < N) {
        Z[k++] = Y[j++];
    }
}

// Helper function to print an array
void printArray(int arr[], int size) {
    for (int i = 0; i < size; i++) {
        printf("%d ", arr[i]);
    }
    printf("\n");
}

int main() {
    int X[] = {9, 7, 5, 3, 1}; // Example array X
    int Y[] = {10, 8, 6, 4, 2}; // Example array Y

    int M = sizeof(X) / sizeof(X[0]);
    int N = sizeof(Y) / sizeof(Y[0]);
    int Z[M + N]; // Array to hold the merged result

    mergeDescending(X, M, Y, N, Z);

    printf("Merged array Z in descending order:\n");
    printArray(Z, M + N);

    return 0;
}

				
			

Explanation:

  1. Function mergeDescending:

    • Inputs: Arrays X and Y with sizes M and N, respectively. An output array Z of size M + N.
    • Logic:
      • The function uses three indices i, j, and k to traverse arrays X, Y, and Z, respectively.
      • It compares elements of X and Y and inserts the larger one into Z, incrementing the corresponding indices.
      • After one of the arrays is exhausted, any remaining elements from the other array are copied to Z.
  2. Function printArray:

    • A helper function to print the contents of an array.
  3. main Function:

    • Demonstrates the usage of mergeDescending with example arrays X and Y.
    • Prints the merged array Z.

This approach ensures that the merged array Z is also in descending order, leveraging the fact that the input arrays X and Y are already sorted in descending order.

Scroll to Top