Showing posts with label c. Show all posts
Showing posts with label c. Show all posts

Wednesday, June 12, 2013

Determinant Calculation of Nth Order Square Matrix| Cramer's Rule Implementation in C

Determinant calculation of a square matrix is widely used in solving many applied Mathematics problems. Some of them are while solving a set of linear equations using Cramer's rule (iff they have unique solutions), calculation of inverse matrix, Eigen values/ Eigen vectors problem and so on.

The determinant of a square matrix A∈Rn×n is the real number det(A) defined as follows:

det(A) = SUMperm [sign(ν1,ν2,...,νn)a1ν1a2ν2...anνn]
.

Friday, June 7, 2013

Huffman Algorithm Implementation in C| Huffman Encoding| Huffman Algorithm Example

Huffman Algorithm is an encoding technique for symbols where most frequently occuring symbols are represented with short length bit strings and least frequently occuring symbols are represented with long bit strings.

This algorithm is widely used as the fundamental encoding algorithm in compressing audio and image files.

Friday, May 24, 2013

Gauss Jordan Method Implementation with C Source Code

In linear algebra, Gaussian Jordan method is an algorithm for solving systems of linear equations. It is usually understood as a sequence of operations performed on the associated matrix of coefficients. This method can also be used to find the rank of a matrix, to calculate the determinant of a matrix, and to calculate the inverse of an invertible square matrix.

We can implement this method in C using the following source code:
//Implementation of Gauss Jordan Method, @author: +Jivan Nepali, @URL: codeplustech
 #include<stdio.h>  
 #include<conio.h>  
 #include<stdlib.h>  
 #define Max 10  
 int main()  
 {  
   float a[Max][Max+1],t,det=1;  
   int i,j,k,N;  
   printf("Enter the number of unknowwns : ");  
   scanf("%d",&N);  

Saturday, May 18, 2013

Newton-Raphson Method in C with source codes

Newton-Raphson method is a method for finding successively better roots (zeros) of a real valued function
x: f(x) = 0.

The algorithm can be implemented in C as follows:


#include<stdio.h>
#include<math.h>
#include<conio.h>
#define h pow(10,-6)
#define error pow(10,-6)
double f(double x)
{
 return(x*x-4);
}
double df(double x)
{
 return 2*x;
}

C Source Code for Dijkstra's Shortest Path Algorithm

Dijkstra's Shortest Path Algorithm is the popular algorithm for finding shortest/optimal path in compute science due to its widespread application in different branches- computer networks, distributed systems, signal processing etc.

I've implemented the algorithm for finding the minimum distance between any two nodes:



///......................................SHORTEST PATH ALGORITHM << Dijkstra Algorithm >>......................

#include<stdio.h>
#include<conio.h>
#define MAX_NODES 10
#define INFINITY 1000
#define MEMBER 1
#define NON_MEMBER 0

struct arc{
    int adj;
    int weight;
};
int main(){

    int i,j,k,weight[MAX_NODES][MAX_NODES],num_nodes,source,dest,precede[MAX_NODES];
    int distance[MAX_NODES],permanent_nodes_set[MAX_NODES];
    int current,current_distance,n;
    int smalldist,newdist;
    char s,d,node;

    printf("\n\t\t................................................................................\n");
    printf("\t\t\t\tSHORTEST PATH ALGORITHM << Dijkstra Algorithm >>");
    printf("\n\t\t................................................................................\n");
    printf("\n\n\t\t\tAssign 1000 as INFINITY for non-adjacent nodes !!\n\n");
    printf("\nEnter the number of nodes in the GRAPH :");
    scanf("%d",&n);

    for(i=0;i<n;i++)
        for(j=i+1;j<n;j++)
            {
                printf("\nEnter weight for arc[%c][%c] :",65+i,65+j);
                scanf("%d",&weight[i][j]);
                weight[j][i] =weight[i][j];
                weight[i][i] =INFINITY;
            }


    printf("\nEnter the source _node :");scanf(" %c",&s);
    source =(int)(s-97);
    printf("\nEnter the destination_node :");scanf(" %c",&d);
    dest = (int)(d-97);

    printf("\n\t\t\tThe shortest path is :>>\n\n\t\t\t");
    for(i=0;i<n;i++)
    {
        permanent_nodes_set[i] =NON_MEMBER;
        distance[i] =INFINITY;
    }
    permanent_nodes_set[source]=MEMBER;
    distance[source]=0;
    current=source;
    while(current!=dest)
    {
        smalldist =INFINITY;
        current_distance =distance[current];
        for(i=0;i<n;i++)
            if(permanent_nodes_set[i] ==NON_MEMBER)
        {
            newdist =current_distance+weight[current][i];
            if(newdist<distance[i])
            {
                distance[i]=newdist;
                precede[i]=current;
                if(node!=(65+current))
                    printf(" %c ->",65+current);
                node =65+current;

            }
            if(distance[i]<smalldist)
            {
                smalldist =distance[i];
                k =i;
            }

        }

         current =k;
         permanent_nodes_set[current] =MEMBER;

        }

        printf(" %c ->",65+dest);
        printf("\n\n\t\t\tThe shortest distance = %d\n",distance[dest]);

        getch();
        return 0;
}

Priority Queue Implementation: C source codes

A priority queue is data structure in which intrinsic ordering of the elements does determine the results of its basic operations. These are of two types- ascending (e.g. queue) and descending (e.g. stack).
The following source code implements ascending type priority queue in C:


// Ascending Priority Queue Implementation in C
#include<iostream.h>
    #include<conio.h>

    const int MAX=5;

    class pqueue
    {
          int front,rear;
        public:
          struct data
          {
           int val,p,o;
          }d[MAX];

Merge Sort Algorithm with C source Codes

Here, I've presented the merge sort algorithm as the straight merge sort. The basic idea is to break the file into n subfiles of size 1 and merge adjacent disjoint pairs of files and repeating the process until we've single file remaining of size n.
 The C Source codes is as follows:


// Merge sort implementation in C
#include<stdio.h>

void getdata(int arr[],int n)
{
 int i;
  printf("\nEnter the data:");
  for(i=0;i<n;i++)
    {
     scanf("%d",&arr[i]);
    }
}

C Source Codes for Implementing Linked List

The linked list can be implemented using the pointer structure in C so that we can

  • add an element
  • delete an element
  • search for an element
  • concatenate two linked list
  • Invert a linked list
  • Display elements in the list


//Program to demonstrate linked list operations

# include<stdio.h>
# include<conio.h>
# include "malloc.h"
struct node
{
int data;
struct node *link;
};

Implement Infix to postfix conversion using Stack C source codes

We can implement a valid infix expression to a valid postfix expression using stacks in c as shown:





//C PROGRAM TO CONVERT GIVEN VALID INFIX EXPRESSION INTO POSTFIX EXPRESSION USING STACKS.

#include<stdio.h>
#include<math.h>
#include<string.h>
#include<stdlib.h>
#define MAX 20

char stack[MAX];
int istack[MAX];
int top = -1;
char pop();
void push(char item);

Code Generator in C

The following code generates the code itself. The code seems to be simple but it's little bit tricky and is FAQ for programmers in an interview.




#include"stdio.h"
#include"conio.h"
char *s="#include%c#include%cchar *s=%c%s%c;%cvoid main()%c{%cclrscr();-%cprintf(s,10,10,34,s,34,10,10,10,10,10,10);%cgetch();%c}";
void main(){
//clrscr();
printf(s,10,10,34,s,34,10,10,10,10,10,10);
getch();
}