Wednesday, October 29, 2014

C Identity Matrix



Check if a given Matrix is an Identity Matrix

#include 
#include 
void main()
{
    int a[10][10];
    int i, j, row, column, flag = 1;
    printf("Enter the order of the matrix A \n");
    scanf("%d %d", &row, &column);
    printf("Enter the elements of matrix A \n");
    for (i = 0; i < row; i++)
    {
        for (j = 0; j < column; j++)
        {
            scanf("%d", &a[i][j]);
        }
    }
    printf("matrix a is \n");
    for (i = 0; i < row; i++)
    {
        for (j = 0; j < column; j++)
        {
            printf("%3d", a[i][j]);
        }
        printf("\n");
    }
    
    for (i = 0; i < row; i++)
    {
        for (j = 0; j < column; j++)
        {
            if (a[i][j] != 1 && a[j][i] != 0)
            {
                flag = 0;
                break;
            }
        }
    }
    if (flag == 1 )
        printf("it is identity matrix \n");
    else
        printf("it is not a identity matrix \n");
     getch();
}
 
Output
 
Enter the order of the matrix a
3 3
Enter the elements of matrix a
1 2 3
5 1 8
6 4 1
Matrix a is
  1  2  3
  5  1  8
  6  4  1
it is not a identity matrix
Enter the order of the matrix a
3 3
Enter the elements of matrix a
 1 0 0
 0 1 0
 0 0 1
Matrix a is
  1  0  0
  0  1  0
  0  0  1
it is identity matrix

Find the Frequency of Odd & Even Numbers in the given Matrix

#include 
#include 
void main()
{
   static int array[10][10];
   int i, j, m, n, even = 0, odd = 0;
   clrscr();
   printf("Enter the order ofthe matrix \n");
   scanf("%d %d", &m, &n);
   printf("Enter the coefficients of matrix \n");
   for (i = 0; i < m; ++i)
   {
            for (j = 0; j < n; ++j)
            {
                 scanf("%d", &array[i][j]);
                 if ((array[i][j] % 2) == 0)
                 {
                     ++even;
                 }
                 else
                     ++odd;
             }
   }
   printf("The given matrix is \n");
   for (i = 0; i < m; ++i)
   {
        for (j = 0; j < n; ++j)
        {
            printf(" %d", array[i][j]);
        }
        printf("\n");
    }
printf("\n The frequency of occurance of odd number  = %d \n", odd);
printf("The frequency of occurance of even number = %d\n", even);
getch();
}
Output
Enter the order ofthe matrix
3 3
Enter the coefficients of matrix
34 36 39
23 57 98
12 39 49
The given matrix is
 34 36 39
 23 57 98
 12 39 49
 
The frequency of occurance of odd number  = 5
The frequency of occurance of even number = 4
 

Interchange any two Rows & Columns in the given Matrix

 
      #include 
 #include 
 void main()
 {
    static int array1[10][10], array2[10][10];
    int i, j, m, n, a, b, c, p, q, r;
    clrscr();
    printf("Enter the order of the matrix \n");
    scanf("%d %d", &m, &n);
    printf("Enter the co-efficents of the matrix \n");
    for (i = 0; i < m; ++i)
    {
        for (j = 0; j < n; ++j)
        {
            scanf("%d,", &array1[i][j]);
            array2[i][j] = array1[i][j];
        }
    }
    printf("Enter the numbers of two rows to be exchanged \n");
    scanf("%d %d", &a, &b);
    for (i = 0; i < m; ++i)
    {
        c = array1[a - 1][i];
        array1[a - 1][i] = array1[b - 1][i];
        array1[b - 1][i] = c;
    }
    printf("Enter the numbers of two columns to be exchanged \n");
    scanf("%d %d", &p, &q);
    printf("The given matrix is \n");
    for (i = 0; i < m; ++i)
    {
        for (j = 0; j < n; ++j)
            printf(" %d", array2[i][j]);
        printf("\n");
    }
    for (i = 0; i < n; ++i)
    {
        r = array2[i][p - 1];
        array2[i][p - 1] = array2[i][q - 1];
        array2[i][q - 1] = r;
     }
    printf("The matix after interchanging the two rows(in the original matrix) \n");
    for (i = 0; i < m; ++i)
    {
        for (j = 0; j < n; ++j)
        {
            printf(" %d", array1[i][j]);
        }
        printf("\n");
     }
printf("The matix after interchanging the two columns(in the original matrix) \n");
    for (i = 0; i < m; ++i)
    {
        for (j = 0; j < n; ++j)
            printf(" %d", array2[i][j]);
        printf("\n");
    }
    getch();
}
 
Output
 
Enter the order of the matrix
2 2
Enter the co-efficents of the matrix
34 70
45 90
Enter the numbers of two rows to be exchanged
1 2
Enter the numbers of two columns to be exchanged
1 2
The given matrix is
 34 70
 45 90
           The matix after interchanging the two rows(in the original matrix)
 45 90
 34 70
        The matix after interchanging the two columns(in the original matrix)
 70 34
 90 45

No comments:

Post a Comment