Monday, 6 February 2012

Subtraction of two Matrices

/* Subtraction of two Matrices */
/* TWO_SUB.C */

# include<stdio.h>
# include<stdlib.h>

# define row 10
# define col 10

int i, j;
int row1, col1;
int row2, col2;
float mat1[row][col];
float mat2[row][col];
float mat_res[row][col];

void mat_sub(  float mat1[row][col], int, int,
float mat2[row][col], int, int,
float mat_res[row][col]);

void display(float mat[row][col], int, int);
void input(float mat[row][col], int , int);

/*  Function mat_sub */

void mat_sub(float mat1[row][col], int row1, int col1,
float mat2[row][col], int row2, int col2,
float mat_res[row][col])
{
    if(( row1 == row2) && (col1 == col2))
    {
        printf("\n Subtraction is possible and Result is as follows\n");

        for(i = 0; i < row1; i++)
            for(j = 0; j < col1; j++)
                mat_res[i][j] = mat1[i][j] - mat2[i][j];
        display(mat_res,row1,col1);
    }
    else
        printf("\n Subtraction is not possible");
    exit(0);
}

/* Output Function */

void display(float mat[row][col], int r, int c )
{
    for(i = 0; i < r; i++)
    {
        for(j = 0; j < c; j++)
        {
            printf("  %f", mat[i][j]);
        }
        printf("\n");
    }
}

/* Input function */

void input(float mat[row][col], int r, int c)
{
    for(i = 0 ; i< r; i++)
    {
        for(j = 0 ;  j<c; j++)
        {
            printf("Input Value for : %d: %d: ", i+1, j+1);
            scanf("%f", &mat[i][j]);
        }
    }
}

/* main function */

void main()
{
    int row1, col1;
    int row2, col2;
    float mat1[row][col];
    float mat2[row][col];
    float mat_res[row][col];

    printf("\n Input the row  of the matrix->1:");
    scanf("%d", &row1);
    printf("\n Input the col  of the matrix->1:");
    scanf("%d", &col1);
    printf(" Input data for matrix-> 1\n");

    input(mat1, row1, col1);
    printf("Input the row  of the matrix ->2:");
    scanf("%d", &row2);
    printf(" Input the col  of the matrix->2:");
    scanf("%d", &col2);
    printf(" Input data for matrix-> 2\n");

    input(mat2, row2, col2);
    printf("\n Entered Matrix First is as follows:\n");
    display(mat1,row1,col1);
    printf("\n Entered Matrix Two is as follows:\n");
    display(mat2,row2,col2);
    mat_sub(mat1,row1,col1,mat2,row1,col2,mat_res);
    display(mat_res,row2,col2);
}

Sorting two dimensional arrays row wise

/* Sorting two dimensional arrays row wise */
/* TWO_SORT.C */

# include<stdio.h>

int i, j;
float mat[10][10];

void display( int, int);
void input( int, int);
void Two_Sort_Matrix(int, int);

/* Display function */

void display(int row, int col)
{
    for(i = 0; i < row; i++)
    {
        for(j = 0; j < col; j++)
        {
            printf("  %f", mat[i][j]);
        }
        printf("\n");
    }
}

/* Input function */

void input(int row, int col)
{
    for(i = 0 ; i< row; i++)
    {
        for(j = 0 ;  j<col; j++)
        {
            printf("\nInput Value for : %d: %d: ", i+1, j+1);
            scanf("%f", &mat[i][j]);
        }
    }
}

/* Sorting of a matrix */

void Two_Sort_Matrix(int row, int col)
{
    int flag ;
    do
    {
        flag = 1;
        for(i = 0; i < row; i++)
        {
            for(j = 0; j< col ; j++)
            {
                if(mat[i][j] < mat[i][j+1])
                {
                    float temp = mat[i][j];
                    mat[i][j] = mat[i][j+1];
                    mat[i][j+1] = temp;
                    flag = 0;
                }
            }
        }
        col -= 1;
    } while(flag == 0);
}

/* main function */

void main()
{
    float trace;
    int r,c;
    printf("\n Input number of rows:");
    scanf("%d", &r);
    printf("\n Input number of cols:");
    scanf("%d", &c);

    input(r, c);

    printf("\n Entered Matrix is as follows:\n");
    display(r, c);

    printf("\n Sorted Matrix is as follows:\n");
    Two_Sort_Matrix(r, c);

    display(r, c);
}

Find norm of matrix

/* Find norm of matrix: Norm a matrix is defined as squareroot of the sum
   of the squares of the elements of a matrix */

/* TWO_NOR.C */

# include<stdio.h>
# include<math.h>

# define row 10
# define col 10

int i, j;
float summation;

void display( float mat[row][col], int, int);
void input(float mat[row][col] ,int, int);
float norm_mat(float mat[row][col], int, int);

/* Output function */

void display(float mat[row][col] ,int row1, int col1)
{
    for(i = 0; i < row1; i++)
    {
        for(j = 0; j < col1; j++)
        {
            printf("  %f", mat[i][j]);
        }
        printf("\n");
    }
}

/* Input function */

void input(float mat[row][col] ,int row1, int col1)
{
    for(i = 0 ; i< row1; i++)
    {
        for(j = 0 ;  j<col1; j++)
        {
            printf("\nInput Value for : %d: %d: ", i+1, j+1);
            scanf("%f", &mat[i][j]);
        }
    }
}

/* Calculating norm of a matrix */

float norm_mat( float mat[row][col], int row1, int col1)
{
    summation = 0;
    for(i = 0; i < row1; i++)
    {
        for(j = 0; j < col1; j++)
        {
            summation += mat[i][j] * mat[i][j];
        }
    }
    return(summation);
}

/* main function */

void main()
{
    float mat[row][col];
    int r,c;
    printf("\n Input number of rows:");
    scanf("%d", &r);
    printf("\n Input number of cols:");
    scanf("%d", &c);
    input(mat, r, c);
    printf("\n Entered Two Dimensional array is as follows:\n");
    display(mat, r, c);
    printf("\n Norm of above matix is : %f", sqrt(norm_mat(mat, r, c)));
}

TRAVERSING A TWO DIMENSIONAL ARRAY

/* TRAVERSING A TWO DIMENSIONAL ARRAY */

/* TWO_D_A_.C */

# include<stdio.h>

int i, j;
float mat[10][10];

void Traverse ( int, int);
void input( int, int);

/* Display function */

void Traverse (int row, int col)
{
    printf("\n Traversing in row major order\n");

    for( i = 0; i < row; i++)
    {
        for( j = 0; j < col; j++)
        {
            printf("\n 0x%x", &mat[i][j]);
            printf("  %f", mat[i][j]);
        }
        printf("\n");
    }

    printf("\n Traversing in column major order\n");

    for(j = 0; j < col; j++)
    {
        for(i = 0; i < row; i++)
        {
            printf("\n  0x%x", &mat[i][j]);
            printf("  %f", mat[i][j]);
        }
        printf("\n");
    }
    printf("\n Traversing in row major order\n");

    for(i = 0; i < row; i++)
    {
        for(j = 0; j < col; j++)
        {
            printf("  mat[%d][%d] = ", i, j);
            printf("%f", mat[i][j]);
        }
        printf("\n");
    }

    printf("\n Traversing in column major order\n");

    for(j = 0; j < col; j++)
    {
        for(i = 0; i < row; i++)
        {
            printf("  mat[%d][%d] = ", i, j);
            printf("%f", mat[i][j]);
        }
        printf("\n");
    }
}

/* Input function */

void input(int row, int col)
{
    for(i = 0 ; i< row; i++)
    {
        for(j = 0 ;  j<col; j++)
        {
            printf("\nInput Value for : %d: %d: ",i+1, j+1);
            scanf("%f", &mat[i][j]);
        }
    }

}

/* main function */

void main()
{
    int r,c;
    printf("\n Input the number of rows:");
    scanf("%d", &r);
    printf(" Input the number of cols:");
    scanf("%d", &c);

    input(r, c);
    Traverse(r, c);
}

Add Tow Matrices

/* Add Tow Matrices */
/* TWO_ADD.C */

# include<stdio.h>
# include<stdlib.h>

# define row 10
# define col 10

int i, j;
int row1, col1;
int row2, col2;
float mat1[row][col];
float mat2[row][col];
float mat_res[row][col];

void mat_add(  float mat1[row][col], int, int,
float mat2[row][col], int, int,
float mat_res[row][col]);

void display(float mat[row][col], int, int);
void input(float mat[row][col], int , int);

/* Function mat_add */

void mat_add(float mat1[row][col], int row1, int col1,
float mat2[row][col], int row2, int col2,
float mat_res[row][col])
{
    int i, j;
    if((row1 == row2) && (col1 == col2))
    {
        printf("\n Addition is possible and Result is as follows\n");

        for(i = 0; i<row1; i++)
            for(j = 0; j<col1; j++)
                mat_res[i][j] = mat1[i][j]+mat2[i][j];
        display(mat_res,row1,col1);
    }
    else
        printf("\n Addition is not possible");
    exit(0);
}

/* Output function */

void display(float mat[row][col], int r, int c )
{
    for(i = 0; i < r; i++)
    {
        for(j = 0; j < c; j++)
        {
            printf("  %f", mat[i][j]);
        }
        printf("\n");
    }
}

/* Input function */

void input(float mat[row][col], int r, int c)
{
    for( i = 0 ; i < r; i++)
    {
        for(j = 0 ; j < c; j++)
        {
            printf("Input Value for : %d: %d: ", i+1, j+1);
            scanf("%f", &mat[i][j]);
        }
    }
}

/* main function */

void main()
{
    int row1, col1;
    int row2, col2;
    float mat1[row][col];
    float mat2[row][col];
    float mat_res[row][col];

    printf("\n Input the row  of the matrix->1:");
    scanf("%d", &row1);
    printf(" Input the col  of the matrix->1:");
    scanf("%d", &col1);
    printf("\n Input data for matrix-> 1\n");

    input(mat1, row1, col1);
    printf("\n Input the row  of the matrix ->2:");
    scanf("%d", &row2);
    printf("\n Input the col  of the matrix->2:");
    scanf("%d", &col2);

    printf("\n Input data for matrix-> 2\n");
    input(mat2, row2, col2);

    printf("\n Entered Matrix First is as follows:\n");
    display(mat1,row1,col1);

    printf("\n Entered Matrix Two is as follows:\n");
    display(mat2,row2,col2);
    mat_add(mat1, row1, col1, mat2, row2, col2, mat_res);
}

Find upper and lower half triangle of a matrix

/* Find upper and lower half triangle of a matrix */
/* TRIANGLE.C */

# include<stdio.h>

int i, j;
float mat[10][10];

void display( int, int);
void input( int, int);
void Triangle_Matrix(int, int);

/* Display function */

void display(int row, int col)
{
    for( i = 0; i < row; i++)
    {
        for( j = 0; j < col; j++)
        {
            printf("  %f", mat[i][j]);
        }
        printf("\n");
    }
}

/* Input function */

void input(int row, int col)
{
    for(i = 0 ; i< row; i++)
    {
        for(j = 0 ;  j<col; j++)
        {
            printf("Input Value for : %d: %d: ",i+1, j+1);
            scanf("%f", &mat[i][j]);
        }
    }
}

/* Finding Triangle of a matrix */

void Triangle_Matrix(int row, int col)
{
    printf("\n Lower Half is as follows:\n");

    for(i = 0; i < row; i++)
    {
        for(j = 0; j< col ; j++)
        {
            if( i >= j )
            {
                printf(" %f", mat[i][j]);
            }
        }
        printf("\n");
    }
    printf("\n Upper Half is as follows:\n");
    for(i = 0; i < row; i++)
    {
        for(j = 0; j < col ; j++)
        {
            if( i <= j )
                printf("  %f", mat[i][j]);
            if(i>j)
                printf("        ");
        }
        printf("\n");
    }
}

/* main function */

void main()
{
    int r,c;
    printf("\n Input number of rows:");
    scanf("%d", &r);
    printf(" Input number of cols:");
    scanf("%d", &c);
    input(r, c);
    printf("\n Entered Matrix is as follows:\n");
    display(r, c);
    printf("\n Triangle Matrix is as follows:\n");
    Triangle_Matrix(r,c);
}

Transpose of a matrix

/* Transpose of a matrix */

/* TRANS_M.C */

# include<stdio.h>

int i, j;
int value;
int mat[10][10];
void display(int, int);
void display_o( int transp[10][10],int, int);
void input( int transp[10][10],int, int);
void transpose( int transp[10][10],int, int);

/* Transpose function */

void transpose(int transp[10][10], int row, int col)
{
    for(i = 0; i< row; i++)
    {
        for(j = 0; j < col; j++)
        {
            mat[i][j] = transp[j][i] ;
        }
    }
}

/* Output function */

void display(int row, int col)
{
    for(i = 0; i < row; i++)
    {
        for(j = 0; j < col; j++)
        {
            printf("  %d", mat[i][j]);
        }
        printf("\n");
    }
}

/* Output function */

void display_o(int transp[10][10], int row, int col)
{
    for(i = 0; i < row; i++)
    {
        for(j = 0; j < col; j++)
        {
            printf("  %d", transp[i][j]);
        }
        printf("\n");
    }
}

/* Input function */

void input(int transp[10][10], int row, int col)
{
    for(i = 0 ; i< row; i++)
    {
        for(j = 0 ;  j<col; j++)
        {
            printf("Input Value for : %d: %d: ", i+1,j+1);
            scanf("%d", &value);
            transp[i][j] = value;
        }
    }
}

/* main function */

void main()
{
    int row,col;
    int transp[10][10];
    printf("\n Input the number of rows:");
    scanf("%d", &row);
    printf(" Input number of cols:");
    scanf("%d", &col);
    input(transp, row, col);
    printf("\n Entered Matrix is as follows:\n");
    display_o(transp, row, col);
    transpose(transp,col,row);
    printf("\n Transpose of above matrix is as follows:\n");
    display(col, row);
}

Find trace of a matrix

/* Find trace of a matrix */
/* TRAC_M.C */

# include<stdio.h>

int i, j;
float mat[10][10];

void display( int, int);
void input( int, int);
float trace_mat(int, int);

/* Display function */

void display(int row, int col)
{
    for(i = 0; i < row; i++)
    {
        for(j = 0; j < col; j++)
        {
            printf("  %f", mat[i][j]);
        }
        printf("\n");
    }
}

/* Input function */

void input(int row, int col)
{
    for(i = 0 ; i < row; i++)
    {
        for(j = 0 ;  j < col; j++)
        {
            printf("\nInput Value for : %d: %d: ", i+1, j+1);
            scanf("%f", &mat[i][j]);
        }
    }
}

/* Finding trace of a matrix */

float trace_mat(int row, int col)
{
    float trace = 0;
    for(i = 0; i < row; i++)
        for(j = 0; j< col; j++)
            if(i == j)
                trace += mat[i][j];
    return(trace);
}

/* main function */

void main()
{
    float trace;
    int r,c;
    printf("\n Input the number of rows:");
    scanf("%d", &r);
    printf(" Input number of cols:");
    scanf("%d", &c);
    input(r,c);
    printf("\n Entered Matrix is as follows:\n");
    display(r,c);
    printf("\n Trace of above matrix is :");
    trace = trace_mat(r,c);
    printf("%f", trace);
}

Test for symmetry of a matrix

/* Test for symmetry of a matrix */

/* SYMMET.C */

# include<stdio.h>

# define r 10
# define c 10

int i, j;
float value;
float mat[r][c];
void display(int, int);
void display_o( float transp[r][c],int, int);
void input( float transp[r][c],int, int);
void transpose( float transp[r][c],int, int);
int Symmetry_Matrix(float transp[r][c], int , int );

/* Transpose function */

void transpose( float transp[r][c], int row, int col)
{
    for(i = 0; i < row; i++)
    {
        for(j = 0; j < col; j++)
        {
            mat[i][j] = transp[j][i] ;
        }
    }
}

/* Output function */

void display(int row, int col)
{
    for(i = 0; i < row; i++)
    {
        for(j = 0; j < col; j++)
        {
            printf("  %f", mat[i][j]);
        }
        printf("\n");
    }
}

/* Output function */

void display_o(float transp[r][c], int row, int col)
{
    for(i = 0; i < row; i++)
    {
        for(j = 0; j < col; j++)
        {
            printf("  %f", transp[i][j]);
        }
        printf("\n");
    }
}

/* Input function */

void input( float transp[r][c], int row, int col)
{
    for(i = 0 ; i< row; i++)
    {
        for(j = 0 ;  j<col; j++)
        {
            printf("Input Value for : %d: %d: ", i+1,j+1);
            scanf(" %f", &value);
            transp[i][j] = value;
        }
    }
}

/* Test  for Symmetry */

int  Symmetry_Matrix(float transp[r][c], int row, int col)
{
    int status = 0;
    for(i = 0; i < row; i++)
    {
        for(j = 0; j<col; j++)
            if(mat[i][j] != transp[i][j])
                status = 1;
    }
    return(status);
}

/* main function */

void main()
{
    int status;
    int row, col;
    float transp[10][10];
    printf("\n Input number of rows:");
    scanf("%d", &row);
    printf("\n Input number of cols:");
    scanf("%d", &col);
    input(transp, row, col);
    printf("\n Entered Matrix is as follows:\n");
    display_o(transp, row, col);
    transpose(transp, col, row);
    printf(" \n Transpose of above matrix is as follows:\n");
    display(col,row);
    status = Symmetry_Matrix(transp, row, col);
    if(status)
        printf("\n Matrix is not symmetric ");
    else
        printf("\n Matrix is Symmetric ");
}

SEARCHING FOR LARGEST AND SMALLEST ELEMENTS IN A LINEAR ARRAY

/* SEARCHING FOR LARGEST AND SMALLEST ELEMENTS IN A LINEAR ARRAY */
/* S_L_S_A.C */

# include<stdio.h>

int s;
int small, big, temp;

int search_array(int *, int);
void input(int *, int );
void display(int *, int );

/* Definition of the function */

int search_array(int array[], int number)
{
    big = small = array[0];
    temp = 0;

    while(temp < number)
    {
        if(array[temp] > big)
        {
            big = array[temp] ;
        }
        else if(array[temp] < small)
        {
            small = array[temp];
        }

        temp ++;
    }
    s = small;
    return(big);
}

/* Input function */

void input(int array[], int number)
{
    int i;
    for(i = 0; i< number ; i++)
    {
        printf("Input value for : %d: ",i+1);
        scanf("%d", &array[i]);
    }
}

/* Output function */

void display(int array[], int number)
{
    int i;
    for(i = 0; i < number; i++)
    {
        printf("\n Value at the position: %d: %d", i+1, array[i]);
    }
}

/* main function */

void main()
{
    int number,big;
    int array[100];

    printf("\n Input the number of elements in the list:");
    scanf("%d", &number);
    input(array, number);
    printf("\n Entered list is as follows:\n");

    display(array,number);

    big = search_array(array,number);
    printf("\nLargest number in the array is : %d", big);
    printf("\nSmallest number in the array is : %d", s);
}

TEST SINGULARITY OF A GIVEN MATRIX

/* TEST SINGULARITY OF A GIVEN MATRIX */
/* USING CRAMER'S RULES TO FIND DETERMINANT */

/* SINGULAR.C */

# include<stdio.h>

int i, j;
float mat[10][10];
float mat1[10][10];

void display( int, int);
void input( int, int);
float Singular_Matrix(int, int);

/* Output function */

void display( int row, int col)
{
    for(i = 0; i < row; i++)
    {
        for(j = 0; j < col; j++)
        {
            printf("  %f", mat[i][j]);
        }
        printf("\n");
    }
}

/* Input function */
void input( int row, int col)
{
    for(i = 0 ; i< row; i++)
    {
        for(j = 0 ;  j<col; j++)
        {
            printf("Input Value for : %d: %d: ", i+1, j+1);
            scanf("%f", &mat[i][j]);
        }
    }
}

/* Find Determinant using Cramer's rule */

float Singular_Matrix( int row1, int col1)
{
    int i, j, k, l;
    float sum=0, psum=0, partial=0, nsum=0;
    if(row1 == col1)
    {
        printf("\n Number rows  = Number of cols");
        printf("\n Singular Test is possible\n");

        if(row1 < 3)
        {
            sum = mat[0][0]*mat[1][1] - mat[0][1]*mat[1][0];
            return(sum);
        }
        else
        {
            for(k = 0; k <row1; k++)
                for(j = 0; j < row1; j++)
                    mat1[k][j] = mat[k][j];

            for(k = 0; k <row1; k++)
                for(j = row1; j < (2*row1-1); j++)
                    mat1[k][j] = mat1[k][j-row1];

            for(l = 0; l <row1; l++)
            {
                partial = 1;
                for(i = 0; i <row1; i++)
                {
                    partial *= mat1[i][i+l];
                }
                psum += partial;
            }

            for(l = row1-1; l < ( 2*row1 -1); l++)
            {
                partial = 1;
                for(i =0; i < row1; i++)
                {
                    partial *=mat1[i][l-i];
                }
                nsum += partial;
            }
            sum = psum - nsum ;
            return(sum);
        }
    }

    else
        printf("\n Check about singularity is not possible");

    return 0;
}

/* main function */

void main()
{
    float Determinant;
    int r,c;
    printf("\n Input the number of rows:");
    scanf("%d", &r);
    printf(" Input the number of cols:");
    scanf("%d", &c);
    input(r, c);
    printf("\n Entered Two Dimensional array is as follows:\n");
    display(r, c);
    Determinant = Singular_Matrix(r, c);
    printf("\n Determinant is : %d", Determinant);

    if(Determinant == 0)
        printf("\n The Above matrix is Singular");
    else
        printf("\n Above matrix is not singular");
}

SEARCHING FOR LARGEST AND SMALLEST ELEMENTS IN A LINEAR ARRAY

/* SEARCHING FOR LARGEST AND SMALLEST ELEMENTS IN A LINEAR ARRAY */
/* S_L_S_A.C */

# include<stdio.h>

int s;
int small, big, temp;

int search_array(int *, int);
void input(int *, int );
void display(int *, int );

/* Definition of the function */

int search_array(int array[], int number)
{
    big = small = array[0];
    temp = 0;

    while(temp < number)
    {
        if(array[temp] > big)
        {
            big = array[temp] ;
        }
        else if(array[temp] < small)
        {
            small = array[temp];
        }

        temp ++;
    }
    s = small;
    return(big);
}

/* Input function */

void input(int array[], int number)
{
    int i;
    for(i = 0; i< number ; i++)
    {
        printf("Input value for : %d: ",i+1);
        scanf("%d", &array[i]);
    }
}

/* Output function */

void display(int array[], int number)
{
    int i;
    for(i = 0; i < number; i++)
    {
        printf("\n Value at the position: %d: %d", i+1, array[i]);
    }
}

/* main function */

void main()
{
    int number,big;
    int array[100];

    printf("\n Input the number of elements in the list:");
    scanf("%d", &number);
    input(array, number);
    printf("\n Entered list is as follows:\n");

    display(array,number);

    big = search_array(array,number);
    printf("\nLargest number in the array is : %d", big);
    printf("\nSmallest number in the array is : %d", s);
}

Find Rank of a Matrix

/* Find Rank of a Matrix */
/* RANK.C */

# include<stdio.h>

int R,C;
int i, j;
int mat[10][10];

void display( int, int);
void input( int, int);
int Rank_Mat(int , int);
void swap(int, int, int);

/* This function exchange two rows of a matrix */

void swap( int row1,int row2, int col)
{
    for( i = 0; i < col; i++)
    {
        int temp = mat[row1][i];
        mat[row1][i] = mat[row2][i];
        mat[row2][i] = temp;
    }
}

/* This function find rank of matrix */

int Rank_Mat(int row1, int col1)
{
    int r, c;
    for(r = 0; r< col1; r++)
    {
        display(R,C);
        if( mat[r][r] )  // Diagonal element is not zero
        for(c = 0; c < row1; c++)
            if(c != r)
            {
                /* Make all the elements above and below the current principal
                 diagonal element zero */

                float ratio = mat[c][r]/ mat[r][r];
                for( i = 0; i < col1; i++)
                    mat[c][i] -= ratio * mat[r][i];
            }

            else
                printf("\n");

                /* Principal Diagonal elment is zero */

        else
        {
            for(c =  r+1 ; c < row1;  c++)
                if (mat[c][r])
                {
                    /*  Find non zero elements in the same column */
                    swap(r,c,col1);
                    break ;
                }

            if(c == row1)
            {
                -- col1;

                for(c = 0; c < row1; c ++)
                    mat[c][r] = mat[c][col1];
            }
            --r;
        }
    }
    return col1;
}

/* Output function */

void display( int row, int col)
{
    for(i = 0; i < row; i++)
    {
        for(j = 0; j < col; j++)
        {
            printf("  %d", mat[i][j]);
        }
        printf("\n");
    }
}

/* Input function */

void input( int row, int col)
{
    int value;
    for(i = 0 ; i< row; i++)
    {
        for(j = 0 ;  j<col; j++)
        {
            printf("Input Value for: %d: %d: ", i+1, j+1);
            scanf("%d",  &value);
            mat[i][j] = value;
        }
    }
}

/* main function */

void main()
{
    int rank;
    printf("\n Input number of rows:");
    scanf("%d", &R);
    printf("\n Input number of cols:");
    scanf("%d", &C);
    input(R, C);
    printf("\n Row is : %d", R);
    printf("\n Column is : %d \n", C);

    printf("\n Entered Two Dimensional array is as follows:\n");
    display(R,C);
    printf("\n Row is : %d", R);
    printf("\n Column is : %d\n", C);

    rank = Rank_Mat(R, C);
    printf("\n Rank of above matrix is : %d", rank);
}

Find Orthogonality of a matrix

/* Find Orthogonality of a matrix */

/* ORTH_MAT.C */

# include<stdio.h>
# include<stdlib.h>

# define row 10
# define col 10

int i, j, k;
int row1, col1;
float mat1[row][col];
float mat2[row][col];
float mat_res[row][col];

void mat_mult( float mat1[row][col], int, int,
float mat_res[row][col]);

void transpose( float transp[row][col], int, int);
void display(float mat[row][col], int, int);
void input(float mat[row][col], int , int);

/* function mat_mult */

void mat_mult( float mat1[row][col], int row1, int col1,
float mat_res[row][col])
{
    int flag ;
    if(col1 == row1)
    {
        printf("\n Multiplication is possible and Result is as follows\n");

        for(i = 0; i<row1; i++)
            for(j = 0; j<col1; j++)
            {
                mat_res[i][j] = 0;
                for(k = 0; k < col1; k ++)
                {
                    mat_res[i][j] += mat1[i][k] * mat2[k][j];
                }
            }
        display(mat_res,row1,col1);
    }

    else
    {
        printf("\n Multiplication is not possible");
        exit(0);
    }

    for(i = 0 ; i< row1; i++)
    {
        for(j = 0; j < col1; j++)
        {
            if(((int)mat_res[i][i] == 1) && ((int ) mat_res[i][j] == 0))
                flag = 1;
        }
    }
    if( flag == 1)
    {
        printf("\n Matrix X * Transpose of X = Identity Matrix");
        printf("\n The given Matrix is Orthogonal");
    }
    else
    {
        printf("\n Matrix X * Transpose of X != Identity Matrix");
        printf("\n The given Matrix is not Orthogonal");
    }
}

/* Output function */

void display(float mat[row][col], int r, int c )
{
    printf("\n");
    for( i = 0; i < r; i++)
    {
        for( j = 0; j < c; j++)
        {
            printf("   %f", mat[i][j]);
        }
        printf("\n");
    }
}

/* Input function */

void input(float mat[row][col], int r, int c)
{
    for( i = 0 ; i< r; i++)
    {
        for( j = 0 ;  j<c; j++)
        {
            printf("Input Value for: %d: %d: ", i+1, j+1);
            scanf("%f", &mat[i][j]);
        }
    }
}

/* Transpose of a matrix */

void transpose( float transp[10][10],int row1, int col1)
{
    for( i = 0; i< row1; i++)
    {
        for( j = 0; j < col1; j++)
        {
            mat2[i][j] = transp[j][i] ;
        }
    }
    display(mat2,row1,col1);
}


/*  main function */

void main()
{
    int row1, col1;
    int row2, col2;
    float mat1[row][col];
    float mat2[row][col];
    float mat_res[row][col];

    printf("\n Input the row  of the matrix:");
    scanf("%d", &row1);

    printf(" Input the col  of the matrix:");
    scanf("%d", &col1);

    printf("\n Input data for matrix\n");
    input(mat1, row1, col1);

    printf("\n Entered Matrix First is as follows:\n");
    display(mat1,row1,col1);

    printf(" \n Transpose of above matrix is as follows:\n");
    transpose(mat1, col1, row1);

    mat_mult(mat1 ,row1 ,col1, mat_res);
}

Multiply Two Matrices

/* Multiply Two Matrices */

/* MULT_MAT.C */

# include<stdio.h>
# include<stdlib.h>

# define row 10
# define col 10

int i, j;
int row1, col1;
int row2, col2;
float mat1[row][col];
float mat2[row][col];
float mat_res[row][col];

void mat_mult(  float mat1[row][col], int, int,
float mat2[row][col], int, int,
float mat_res[row][col]);

void display(float mat[row][col], int, int);
void input(float mat[row][col], int , int);

/* function to multiply two matrices */

void mat_mult( float mat1[row][col], int row1, int col1,
float mat2[row][col], int row2, int col2,
float mat_res[row][col])
{
    int i, j, k;
    if(col1 == row2)
    {
        printf("\n Multiplication is possible and Result is as follows\n");

        for(i =0; i<row1; i++)
            for(j=0; j<col2; j++)
            {
                mat_res[i][j] = 0;
                for(k = 0; k < col1; k ++)
                {
                    mat_res[i][j] += mat1[i][k] * mat2[k][j];

                }
            }
        display(mat_res, row1, col2);
    }

    else
        printf("\n Multiplication is not possible");
    exit(0);
}

/* Output function */

void display(float mat[row][col], int r, int c )
{
    for( i = 0; i < r; i++)
    {
        for( j = 0; j < c; j++)
        {
            printf("  %f", mat[i][j]);
        }
        printf("\n");
    }
}

/* Input function */

void input(float mat[row][col], int r, int c)
{

    for( i = 0 ; i< r; i++)
    {
        for( j = 0 ;  j<c; j++)
        {
            printf("Input Value for : %d: %d: ", i+1, j+1);
            scanf("%f", &mat[i][j]);
        }
    }
}


/* main function */

void main()
{

    int row1, col1;
    int row2, col2;
    float mat1[row][col];
    float mat2[row][col];
    float mat_res[row][col];

    printf("\n Input the row  of the matrix->1:");
    scanf("%d", &row1);

    printf("\n Input the col  of the matrix->1:");
    scanf("%d", &col1);

    printf("\n Input data for matrix-> 1\n");
    input(mat1, row1, col1);

    printf("\n Input the row  of the matrix ->2:");
    scanf("%d", &row2);

    printf("\n Input the col  of the matrix->2:");
    scanf("%d", &col2);

    printf("\n Input data for matrix-> 2\n");
    input(mat2, row2, col2);

    printf("\n Entered Matrix First is as follows:\n");
    display(mat1,row1,col1);

    printf("\n Entered Matrix Two is as follows:\n");
    display(mat2,row2,col2);

    mat_mult(mat1 ,row1 ,col1, mat2, row2, col2, mat_res);
}

Find Inverse of a Matrix

/* Find Inverse of a Matrix */
/* INVERSE.C */

# include<stdio.h>

int i, j;

void display( int, int,  float mat[10][10],float mat1[10][10]);
void input( int, int,  float mat[10][10],float mat1[10][10]);
Inverse_Mat(int , int,  float mat[10][10],float mat1[10][10]);
void swap(int, int, int,  float mat[10][10],float mat1[10][10]);

/* This function exchange two rows of a matrix */

void swap( int row1,int row2, int col, float mat[10][10],float mat1[10][10])
{
    for( i = 0; i < col; i++)
    {
        float   temp = mat[row1][i];
        mat[row1][i] = mat[row2][i];
        mat[row2][i] = temp;

        temp = mat1[row1][i];
        mat1[row1][i] = mat1[row2][i];
        mat1[row2][i] = temp;

    }
}

/* This function find inverse of matrix */

int Inverse_Mat(int row1, int col1, float mat[10][10],float mat1[10][10])
{
    int singular = 0;
    int r, c;
    for(r = 0;( r < row1)&& !singular;  r++)
    {

        if( mat[r][r] )  /* Diagonal element is not zero */
            for(c = 0; c < col1; c++)

                if( c == r)
                {

                    /* Make all the elements above and below the current principal
                     diagonal element zero */

                    float ratio =  mat[r][r];
                    for( i = 0; i < col1; i++)
                    {
                        mat[r][i] /= ratio ;
                        mat1[r][i] /= ratio;
                    }
                }
                else
                {
                    float ratio = mat[c][r] / mat[r][r];
                    for( i = 0; i < col1;  i++)
                    {
                        mat[c][i] -= ratio * mat[r][i];
                        mat1[c][i] -= ratio * mat1[r][i];
                    }
                }

        else
        {
            /* If principal diagonal element is zero */
            singular = 1;

            for(c = (r+1); (c < col1) && singular; ++c)

                if(mat[c][r])
                {
                    singular = 0;
                    /* Find non zero elements in the same column */
                    swap(r,c,col1, mat, mat1);
                    --r;
                }

        }
    }
    return(!singular);
}

/* To print output this is used */

void display( int row, int col, float mat[10][10],float mat1[10][10])
{
    printf("\n");
    /* Output of inverse Matrix */
    for( i = 0; i < row; i++)
    {
        for( j = 0; j < col; j++)
        {
            printf("   %f", mat1[i][j]);
        }
        printf("\n");
    }

}

/* input function */

void input( int row, int col, float mat[10][10],float mat1[10][10])
{
    for( i = 0 ; i< row; i++)
    {
        for( j = 0 ;  j<col; j++)
        {
            printf("Input Value for: %d: %d: ", i+1, j+1);
            scanf("%f", &mat[i][j]);
            mat1[i][j] = 0;
        }
        mat1[i][i] = 1;
    }
    printf("\n Entered Matrix is as follows:\n");
    for( i = 0; i < row; i++)
    {
        for( j = 0; j < col; j++)
        {
            printf("   %f", mat[i][j]);
        }
        printf("\n");
    }
}


/* main function */


void main()
{

    int R, C;
    float mat[10][10];
    float mat1[10][10];
    printf("\n Input number of rows:");
    scanf("%d", &R);
    printf(" Input number of cols:");
    scanf("%d", &C);
    input(R,C, mat, mat1);

    Inverse_Mat(R,C, mat,  mat1);
    printf("\n Inverse of above matrix is as follows:\n ");
    display(R,C, mat, mat1);
}

DELETING DUPLICATES FROM A LINEAR ARRAY


/*  DELETING  DUPLICATES FROM A LINEAR ARRAY*/
/* DUPL_A.C */

# include<stdio.h>

int status ;
int dup;
int duplicate_array(int *, int);
void input(int *, int );
void display(int *, int );

/* Definition of the function */

int duplicate_array(int array[], int number)
{
    int i, j, k;
    status = 0;
    dup  = number;

    for(i = 0; i< number-1; i++)
        for(j = i+1; j< number; j++)
        {
            if(array[i] == array[j])
            {
                number = number - 1 ;
                for(k = j; k<number; k++)
                    array[k] = array[k+1];
                status = 1;
                j = j - 1;
            }
        }
    if( status ==0)
        printf("\n No duplicate is found");
    return(dup-number);
}

/* Input function to read data */

void input(int array[], int number)
{
    int i;
    for(i = 0; i< number ; i++)
    {
        printf("Input value for: %d: ", i+1);
        scanf("%d", &array[i]);
    }
}

/* Output function */

void display(int array[], int number)
{
    int i;
    for(i = 0; i< number; i++)
    {
        printf("\n Value at the position: %d: %d", i+1, array[i]);
    }
}

/* main function */

void main()
{
    int number;
    int array[100];
    int n;
    printf("\n Input the number of elements in the list: ");
    scanf("%d", &number);
    input(array, number);
    printf("\n Entered list is as follows:\n");

    display(array,number);

    n = duplicate_array(array,number);
    printf("\nNumber of duplicate elements in the list are: %d", n);
    printf("\nAfter removing duplicates from the list, the list is as follows:");
    display(array,number-n);
}

Traversing a linear array2


/* Traversing a linear array */
/* ARRAY_T.C */

# include<stdio.h>

void traversing_array(int *, int, int);
void input(int *, int, int);

/*  Definition of the function*/

void traversing_array(int linear_array[], int l_b, int u_b)
{
    int counter;
    for(counter = l_b; counter<=u_b; counter++)
    {
        printf("\n Element at position: %d is %d ", counter, linear_array[counter]);
    }
}

/*  Defintion of the function*/

void input(int array[], int l_b, int u_b)
{
    int counter;
    for(counter = l_b; counter<= u_b; counter++)
    {
        printf("\n Input value for the: %d: ", counter);
        scanf("%d", &array[counter]);
    }
}


/* Definition of the function */

void main()
{
    int a[100];
    int lb, ub;

    printf("\n Lower limit of the array:");
    scanf("%d", &lb);
    printf("\n Upper limit of the array:");
    scanf("%d", &ub);
    input(a, lb, ub);
    traversing_array(a,lb,ub);
}


Traversing a linear array

/* Traversing a linear array */
/* ARRAY_L.C */

# include<stdio.h>


void memory(int *, int, int);
void memory1(char *, int, int);

/* Definition of the function memory */

void memory(int a[], int l_b, int u_b)
{
    int counter;

    for(counter = l_b; counter<=u_b; counter++)
    {
        printf("\n Element at location: 0x%x is %d", &a[counter], a[counter]);
    }
    printf("\n Array size = %d ", &a[counter-1] - &a[0] + 1);
}

void memory1(char b[], int l_b, int u_b)
{
    char *pointer;
    int counter;
    pointer =&b[0];

    for(counter = l_b; counter<=u_b; counter++)
    {
        printf("\n Element at location: 0x%x is %d", &b[counter], b[counter]);
    }
    printf("\n Array size = %d", &b[counter-1] - &b[0] + 1);
}

/*Function main */

void main()
{
    int a[12] = {
        99,88,77,66,55,44,33,22,11,100,200,300    };
    char b[] = {
        'A','B','C','D','E','F','G','H','I','J','K',
                      'L','M','N','O','P','Q','R','S','T','U','V',
                      'W','X','Y','Z'    };
    int lb=0, ub=11;
    memory(a,lb,ub);
    lb = 0;
    ub = 25;
    memory1(b,lb,ub);
}


INSERTING AN ELEMENT INTO LINEAR ARRAY AT SPECIFIED POSITION

/* INSERTING AN ELEMENT INTO LINEAR ARRAY AT SPECIFIED POSITION */
/* ARRAY_I.C */

# include<stdio.h>

int insert_array(char *, int, int, char);
void input(char *, int );
void display(char *, int );

/* Definition of the function */

int insert_array(char array[], int number, int position, char element)
{
    int temp = number;
    while( temp >= position)
    {
        array[temp+1] = array[temp];
        temp --;
    }

    array[position] = element;
    number = number +1 ;
    return(number);
}

/* INPUT FUNCTION TO READ DATA */

void input(char array[], int number)
{
    int i;
    for(i = 1; i<= number ; i++)
    {
        fflush(stdin);
        printf("\n Input value for: %d: ", i);
        scanf("%c", &array[i]);
    }
}

/* OUTPUT FUNCTION TO PRINT ON THE CONSOLE */

void display(char array[], int number)
{
    int i;
    for(i = 1; i<= number; i++)
    {
        printf("\n Value at the position: %d: %c ", i, array[i]);
    }
}

/* main function */

void main()
{
    int number;
    char array[100];
    int position;
    char element;
    fflush(stdin);
    printf("\n Input the number of element into the LIST: ");
    scanf("%d", &number);
    fflush(stdin);
    input(array, number);
    printf("\n Entered list as follows:\n");
    fflush(stdin);
    display(array,number);
    fflush(stdin);
    printf("\n Input the position where you want add a new data:");
    scanf("%d", &position);
    fflush(stdin);
    printf("\n Input the value for the position:");
    scanf("%c", &element);
    number = insert_array(array,number,position,element);
    display(array,number);
}

DELETING FROM A LINEAR ARRAY AT SPECIFIED POSITION


/* DELETING  FROM A LINEAR ARRAY AT SPECIFIED POSITION */
/* ARRAY_D.C */

# include<stdio.h>

# define s 20

char employ[s][s];
int delete_array(char employ[s][s], int, int, char *);
void input(char emply[s][s], int );
void display(char employ[s][s], int );

/* Definition of the function */

int delete_array(char employ[s][s], int number, int position, char element[])
{
    int temp = position;
    element = employ[position];
    printf("\n Information which we have to delete: %s", element);

    while( temp <= number-1)
    {
        *employ [temp] = *employ[temp+1];
        temp ++;
    }
    number = number - 1 ;
    return(number);
}

void input(char employ[s][s], int number)
{
    int i;
    for(i = 1; i<= number ; i++)
    {
        fflush(stdin);
        printf("\n Input value for: %d: ", i);
        gets(employ[i]);
    }
}

void display(char employ[s][s], int number)
{
    int i;
    for(i = 1; i<= number; i++)
    {
        printf("\n Value at the position: %d: %s", i, employ[i]);
    }
}

/* main function */

void main()
{
    int number;
    int position;
    char element[s];

    printf("\n Input the number of elements in the array:");
    scanf("%d", &number);
    fflush(stdin);
    input(employ, number);
    printf("\n Entered list is as follows:\n");

    display(employ, number);
    fflush(stdin);
    printf("\n Input the position from where you want delete an element:");
    scanf("%d", &position);

    number = delete_array(employ, number, position, element);
    display(employ,number);
}

Print all the numbers which multiplied by 3 till last limit

// Print all the numbers which multiplied by 3 till last limit

#include<stdio.h>
#include<conio.h>
void main()
{
    int a,l;
    clrscr();

    printf("\n Enter a last limit : ");
    scanf("%d",&l);

    for(a=0;a<=l;a++)
    {
        if(a%3==0)
            printf("\n%d",a);
    }

    getch();
}