Skip to main content

Featured

C Pattern Programs: Square Number and Alphabet Patterns Explained

πŸ”· Square Star Pattern πŸ“‹ Copy Code #include <stdio.h> int main() { int num; printf("Enter the number:\n"); scanf("%d", &num); for(int i = 1; i <= num; i++) { for(int j = 1; j <= num; j++) { printf("* ");//keep"* " } printf("\n"); } return 0; } πŸ”· Reverse Square Alphabet Pattern (Column-wise) πŸ“‹ Copy Code #include <stdio.h> int main() { int num; printf("Enter the number:\n"); scanf("%d", &num); for(int i = num; i >= 1; i--) { for(int j = num; j >= 1; j--) { printf("%c ", j + 64);//%c for Character and 64 will be ASIIC VALUE } printf("\n"); } return 0; } πŸ”· Reverse Square Alphabet Pattern (Row-wise) πŸ“‹ Copy Code #include <stdio.h> int main() { int num; ...

C Program to Merge Two Arrays and Sort the Result

πŸ”„ C Program to Merge Two Arrays and Sort the Result

#include <stdio.h>
int main( )
{
    int num1, num2, num3;

    printf("Enter the size of the first array:\n");
    scanf("%d", &num1);
    if(num1 <= 0)
    {
        printf("!invalid size of the first array:\n");
        return 1;
    }

    int a[num1];
    printf("Enter %d elements in first array:\n", num1);
    for(int i = 0; i < num1; i++)
    {
        scanf("%d", &a[i]);
    }

    printf("Enter the size of the second array:\n");
    scanf("%d", &num2);
    if(num2 <= 0)
    {
        printf("!invalid second array size:\n");
        return 1;
    }

    int b[num2];
    printf("Enter %d elements of the second array:\n", num2);
    for(int i = 0; i < num2; i++)
    {
        scanf("%d", &b[i]);
    }

    num3 = num1 + num2;
    int c[num3];

    // Merging first array
    for(int i = 0; i < num1; i++)
    {
        c[i] = a[i];
    }

    // Merging second array
    for(int i = 0; i < num2; i++)
    {
        c[i + num1] = b[i];
    }

    printf("The Merged Array:\n");
    for(int i = 0; i < num3; i++)
    {
        printf("%d ", c[i]);
    }

    // Bubble sort
    printf("\nSorted Array:\n");
    for(int i = 0; i < num3 - 1; i++)
    {
        for(int j = 0; j < num3 - i - 1; j++)
        {
            if(c[j] > c[j + 1])
            {
                int temp = c[j];
                c[j] = c[j + 1];
                c[j + 1] = temp;
            }
        }
    }

    for(int i = 0; i < num3; i++)
    {
        printf("%d ", c[i]);
    }

    return 0;
}
  

πŸ“ Explanation:

This program takes two arrays as input from the user, merges them into a third array, and sorts the result using the bubble sort algorithm. It checks the validity of sizes and uses separate loops for merging and sorting.

πŸ’‘ Sample Output:

Enter the size of the first array:
3
Enter 3 elements in first array:
5 1 7
Enter the size of the second array:
3
Enter 3 elements of the second array:
4 2 9
The Merged Array:
5 1 7 4 2 9
Sorted Array:
1 2 4 5 7 9
  

πŸ” Keywords:

merge two arrays in C, sort merged array C, bubble sort logic, C array programs, data structure basics, C programming for beginners

Comments

Popular Posts

πŸŒ™