Friday 27 November 2015

Find the Sum of each Row & each Column of a MxN Matrix

#include <stdio.h>

void main ()
{
    static int array[10][10];
    int i, j, m, n, sum = 0;

    printf("Enter the order of the matrix\n");
    scanf("%d %d", &m, &n);
    printf("Enter the co-efficients of the matrix\n");
    for (i = 0; i < m; ++i)
    {
        for (j = 0; j < n; ++j)
        {
            scanf("%d", &array[i][j]);
        }
    }
    for (i = 0; i < m; ++i)
    {
        for (j = 0; j < n; ++j)
        {
            sum = sum + array[i][j] ;
        }
        printf("Sum of the %d row is = %d\n", i, sum);
        sum = 0;
    }
    sum = 0;
    for (j = 0; j < n; ++j)
    {
        for (i = 0; i < m; ++i)
        {
            sum = sum + array[i][j];
        }
        printf("Sum of the %d column is = %d\n", j, sum);
        sum = 0;
    }
}

Tuesday 24 November 2015

Inserting An Element in Array using C

#include <stdio.h>
 
int main()
{
   int array[100], position, c, n, value;
 
   printf("Enter number of elements in array\n");
   scanf("%d", &n);
 
   printf("Enter %d elements\n", n);
 
   for (c = 0; c < n; c++)
      scanf("%d", &array[c]);
 
   printf("Enter the location where you wish to insert an element\n");
   scanf("%d", &position);
 
   printf("Enter the value to insert\n");
   scanf("%d", &value);
 
   for (c = n - 1; c >= position - 1; c--)
      array[c+1] = array[c];
 
   array[position-1] = value;
 
   printf("Resultant array is\n");
 
   for (c = 0; c <= n; c++)
      printf("%d\n", array[c]);
 
   return 0;
}

Saturday 21 November 2015

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

#include <stdio.h>

void main()
{
static int array[10][10];
int i, j, m, n, even = 0, odd = 0;

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);
}

Friday 20 November 2015

Calculate the Sum of the Elements of each Row & Column In Matrix

#include <stdio.h>
int Addrow(int array1[10][10], int k, int c);
int Addcol(int array1[10][10], int k, int r);

void main()
{
    int arr[10][10];
    int i, j, row, col, rowsum, colsum, sumall=0;

    printf("Enter the order of the matrix \n");
    scanf("%d %d", &row, &col);
    printf("Enter the elements of the matrix \n");
    for (i = 0; i < row; i++)
    {
        for (j = 0; j < col; j++)
        {
            scanf("%d", &arr[i][j]);
        }
    }
    printf("Input matrix is \n");
    for (i = 0; i < row; i++)
    {
        for (j = 0; j < col; j++)
        {
            printf("%3d", arr[i][j]);
        }
        printf("\n");
    }
    /*  computing row sum */
    for (i = 0; i < row; i++)
    {
        rowsum = Addrow(arr, i, col);
        printf("Sum of row %d = %d\n", i + 1, rowsum);
    }
    /*  computing col sum */
    for (j = 0; j < col; j++)
    {
        colsum = Addcol(arr, j, row);
        printf("Sum of column  %d = %d\n", j + 1, colsum);
    }
    /*  computation of all elements */
    for (j = 0; j < row; j++)
    {
        sumall = sumall + Addrow(arr, j, col);
    }
    printf("Sum of all elements of matrix = %d\n", sumall);
}
/*  Function to add each row */
int Addrow(int array1[10][10], int k, int c)
{
    int rsum = 0, i;
    for (i = 0; i < c; i++)
    {
        rsum = rsum + array1[k][i];
    }
    return(rsum);
}
/*  Function to add each column */
int Addcol(int array1[10][10], int k, int r)
{
    int csum = 0, j;
    for (j = 0; j < r; j++)
    {
        csum = csum + array1[j][k];
    }
    return(csum);
}

Sunday 15 November 2015

Interchange any two Rows & Columns in the given Matrix

#include <stdio.h>
 
void main()
{
    static int array1[10][10], array2[10][10];
    int i, j, m, n, a, b, c, p, q, r;
 
    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)
    {
        /*  first row has index is 0 */
        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)
    {
        /*  first column index is 0 */
        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");
    }
}

Friday 13 November 2015

Check if a given Matrix is an Identity Matrix

#include <stdio.h>

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");
    }
    /*  Check for unit (or identity) matrix */
    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");
}