Tuesday, September 11, 2012

Matrix


Q -1 Write a program to transpose the given 4 x 4 matrix  and count the number of swapping operation will be done.

program -

#include stdio.h
#include conio.h
int a[4][4];
inline int Transpose()
{
       int temp,l,m,c=0;
       for( l=0; l < 4 ; l ++) {
           for( m=0; m < 4 ; m++) {
               if(l  <  m) {
                 temp = a[l][m];
                 a[l][m] = a[m][l];
                 a[m][l] = temp;
                 c++;
                 }
                      
              }
              
       }
       printf("number of swaping : %d\n\n",c);
}

int main()
{
    int i,j;
    printf("Enter the matrix :");
    for (i =0;i < 4;i++)
    {
        for(j=0; j < 4; j++)
         {
                scanf("%d",&a[i][j]);
         }
    }
    printf ("Matrix 4x4:");
    for (i =0 ; i < 4; i++)
    {  
        printf("\n");
        for(j=0; j < 4; j++)
         {
                printf("%d",a[i][j]);
                printf("\t");
         }
         printf("\n");
    }
   Transpose();
   printf ("T_Matrix 4x4:");
    for (i =0 ; i < 4; i++)
    {  
        printf("\n");
        for(j=0; j < 4; j++)
         {
                printf("%d",a[i][j]);
                printf("\t");
         }
         printf("\n");
    }
    
getch();
return 0;
}


Q.2  c++ program to multiply two 4x4 matrix

for multiplication number of columns in A must be equal to number of rows in B  here m=n=4

Program : 

No comments:

Post a Comment