Skip to main content

Featured

Mastering Hollow Square Patterns in C: Stars, Numbers, Alphabets & Binary

πŸ”’ C Program to Print Hollow Continuous Number Square πŸ“„ Source Code: #include <stdio.h> int main() { int num, k = 0; printf("Enter the number:\n"); scanf("%d", &num); for(int i = 1; i <= num; i++) { for(int j = 1; j <= num; j++) { if(i == 1 || i == num || j == 1 || j == num) { // k increments sequentially only along the borders printf("%d ", k++); } else { printf(" "); } } printf("\n"); } return 0; } πŸ“‹ Copy Code πŸ’» Expected Output (Input: 5): Enter the number: 5 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 πŸ”’ C Program to Print Standard Hollow Binary Row Square πŸ“„ Source Code (Fixed Specifier): #include <stdio.h> int main() { ...

Merge Two Sorted Arrays - C Program

Merge Two Sorted Arrays - C Program

πŸ”€ Merge Two Sorted Arrays (C Program)

#include <stdio.h>

int main() {
    int size1, size2;

    // Read size1
    printf("Enter a size1: ");
    scanf("%d", &size1);
    int a1[size1];

    printf("Enter array1 elements: ");
    for (int i = 0; i < size1; i++) {
        scanf("%d", &a1[i]);
    }

    // Read size2
    printf("Enter a size2: ");
    scanf("%d", &size2);
    int a2[size2];

    printf("Enter array2 elements: ");
    for (int i = 0; i < size2; i++) {
        scanf("%d", &a2[i]);
    }

    int merged[size1 + size2];
    int i = 0, j = 0, k = 0;

    // Merge two sorted arrays
    while (i < size1 && j < size2) {
        if (a1[i] < a2[j])
            merged[k++] = a1[i++];
        else
            merged[k++] = a2[j++];
    }

    // Copy remaining elements
    while (i < size1)
        merged[k++] = a1[i++];

    while (j < size2)
        merged[k++] = a2[j++];

    // Print merged array
    printf("Merged array: ");
    for (int m = 0; m < k; m++) {
        printf("%d ", merged[m]);
    }
    printf("\n");

    return 0;
}
  

πŸ“˜ Explanation:

πŸ”Ή This program takes two sorted arrays as input from the user.
πŸ”Ή It merges them into a single sorted array using the two-pointer technique.
πŸ”Ή Remaining elements from either array are added at the end.
πŸ”Ή The final merged array is printed in sorted order.

πŸ§ͺ Sample Output:

Enter a size1: 5
Enter array1 elements: 1 3 5 7 9
Enter a size2: 4
Enter array2 elements: 2 4 6 8
Merged array: 1 2 3 4 5 6 7 8 9
    

🏷️ Keywords:

C program merge arrays, sorted array merging, combine two sorted arrays, two-pointer technique C, array manipulation in C

Comments

Popular Posts

πŸŒ™