Header Ads

How to Merge Two matrix in C


By merging property in C we are able to merge(mix) two are more matrix in a single matrix one after one in a known sequence, for this we have to declare a array variable of size equal to the size of arrays variable that are to be merged
example: –int a[5]={1,2,3,4,5},b[5]={6,7,8,9,1};
      int c[10];
that means if we want to merge matrix of array a[5] and matrix of array b[5] then we have to declare a matrix i.e. of size equal to 5+5 means a array of size 10, so here c[10]. Now come to the example:-


******************************Program*********************************

#include<stdio.h>
#include<conio.h>
main()
{
    int arr1[5]={1,2,3,4,5},arr2[5]={6,7,8,9,10};
    int arr3[10],i,k=0;

    printf("1st matrix:\n");
    for(i=0;i<5;i++)
    {
        printf("%d ",arr1[i]);
    }
    printf("\n2nd matrix:\n");
    for(i=0;i<5;i++)
    {
        printf("%d ",arr2[i]);
    }

    for(i=0;i<10;i++)
    {
        if(i>4)
        {
            arr3[i]=arr2[k];
            k=k+1;
        }
        else
        {
            arr3[i]=arr1[i];
        }

    }
    i=0;
    printf("\nMatrix after merging:\n");
    while(i<10)
    {
        printf("%d ",arr3[i]);
        i++;
    }
    getch();
}

*************************************Output*****************************

matrixmerginginc
Output of matrix merging

Technorati Tags: ,

No comments