Skip to content

Instantly share code, notes, and snippets.

@anisur3036
Last active October 31, 2022 09:03
Show Gist options
  • Save anisur3036/30b205673ca84da4582d07cb23ac8e74 to your computer and use it in GitHub Desktop.
Save anisur3036/30b205673ca84da4582d07cb23ac8e74 to your computer and use it in GitHub Desktop.
Write a C program to merge two array and sort element in desending order.
// Write a c program to sort a array element in Desending order
#include <stdio.h>
int main() {
int i, j, temp, n1, n2, n3;
int arr1[100000], arr2[100000], arr3[200000];
printf("Please enter the size of first array: ");
scanf("%d", &n1);
printf("Input %d elements in the array :\n", n1);
for(i =0; i < n1; i++) {
printf("Element %d - ", i);
scanf("%d", &arr1[i]);
}
printf("Please enter the size of second array: ");
scanf("%d", &n2);
printf("Input %d elements in the array :\n", n2);
for(int i =0; i < n2; i++) {
printf("Element %d - ", i);
scanf("%d", &arr2[i]);
}
n3 = n1 + n2;
for(i=0; i<n1; i++) {
arr3[i] = arr1[i];
}
for(i=0; i<n2; i++) {
arr3[i + n1] = arr2[i];
}
printf("The merged array: ");
for(i =0; i<n3; i++) {
printf("%d ", arr3[i]);
}
// sorting an array...
for(i=0; i<(n3 -1); i++) {
for(int j=i+1; j<n3; j++) {
if(arr3[i] < arr3[j]) {
temp = arr3[i];
arr3[i] = arr3[j];
arr3[j] = temp;
}
}
}
printf("\nAfter sorting: ");
for(i =0; i<n3; i++) {
printf("%d ", arr3[i]);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment