Tuesday, August 31, 2010

C Program to find determinant of a given matrix

#include<stdio.h>
#include<conio.h>
#define LIMIT 10
void main()
{
  int chdg(float [10][10],int);
  float deter(float [10][10],int);
  float a[10][10],value;
  int i,j,order;
  clrscr();
  printf("Enter order of determent :");
  scanf("%d",&order);
  for(i=0;i<order;i++)
  {
    for(j=0;j<order;j++)
    {
      printf("Enter (%d,%d) element of the determent :",i+1,j+1);
      scanf("%f",&a[i][j]);
    }
  }

  if(chdg(a,order)==0)
     value=0;
  else
     value=deter(a,order);
  printf("Determinent Value :%f",value);
  getch();
}

float deter(float a[10][10],int forder)
{
  int i,j,k;
  float mult;
  float deter=1;
  for(i=0;i<forder;i++)
  {
    for(j=0;j<forder;j++)
    {
      mult=a[j][i]/a[i][i];
      for(k=0;k<forder;k++)
      {
        if(i==j) break;
        a[j][k]=a[j][k]-a[i][k]*mult;
      }
    }
  }
  for(i=0;i<forder;i++)
  {
    deter=deter*a[i][i];
  }
  return(deter);
}


int chdg(float array[10][10],int ord)
{
  int i,j,k;
  for(i=0;i<ord;i++)
  {
     if(array[i][i]==0)
     {
        for(j=0;j<ord;j++)
        {
          if(array[i][j]!=0)
          {
             k=j;
             break;
          }
          if(j==(ord)) //forder-1
             return(0);
        }
        for(j=0;j<ord;j++)
        {
          array[j][i]=array[j][i]-array[j][k];
        }
     }
  }
  return(1);
}

Monday, August 30, 2010

C Program to get the largest of three numbers

#include<stdio.h>
#include<conio.h>
void main()
{
     int a,b,c;
     clrscr();
     printf("\n\n Enter the values of three numbers \n\n");
     scanf("%d%d%d",&a,&b,&c);
     printf("\n The values of three numbers are \n");
     printf(" a = %d\n",a);
     printf(" b = %d\n",b);
     printf(" c = %d\n",c);
     if(a>b)
     {
        if(a>c)
          printf("\ta = %d is larger ",a);
        else
          printf("\tc = %d is larger ",c);
     }
     else
     {
         if(c>b)
            printf("\tc = %d is larger",c);
         else
            printf("\tb = %d is larger",b);
     }
     getch();
}

Sunday, August 29, 2010

C Program to implement Queue as Linked List

#include<stdio.h>
#include<conio.h>
#include<malloc.h>

struct node
{
  int data;
  struct node *next;
};

struct queue
{
    struct node *front;
    struct node *rear;
};

void initqueue(struct queue*);
void addqueue(struct queue*,int);
int delqueue(struct queue*);
void deallqueue(struct queue*);
void main()
{
     struct queue a;
     int i;
     clrscr();
     initqueue(&a);
     addqueue(&a,15);
     addqueue(&a,5);
     addqueue(&a,16);
     addqueue(&a,20);
     addqueue(&a,12);
     addqueue(&a,18);
     addqueue(&a,25);
     i=delqueue(&a);
     printf("\n Item removed :%d",i);
     i=delqueue(&a);
     printf("\n Item removed :%d",i);
     i=delqueue(&a);
     printf("\n Item removed :%d",i);
     getch();
}

/*Initializes data member */
void initqueue(struct queue *q)
{
    q->front=q->rear=NULL;
}

/* Adds an element to the queue */
void addqueue(struct queue *q,int item)
{
    struct node *temp;
    temp=(struct node*)malloc(sizeof(struct node));
    if(temp==NULL)
      printf("\n Queue is NULL ");
    temp->data=item;
    temp->next=NULL;
    if(q->front==NULL)
    {
        q->rear=q->front=temp;
        return;
    }
    q->rear->next=temp;
    q->rear=q->rear->next;

}

/* Removes an element from the queue */
int delqueue(struct queue *q)
{
     struct node *temp;
     int item;
     if(q->front==NULL)
     {
         printf("\n Queue is empty ");
         return NULL;
     }
     item=q->front->data;
     temp=q->front;
     q->front=q->front->next;
     free(temp);
     return item;
}

/* Deallocates memory */
void deallqueue(struct queue *q)
{
             struct node *temp;
             if(q->front == NULL)
             return ;
             while(q->front !=NULL)
             {
                temp=q->front;
                q->front=q->front->next;
                free(temp);
             }
}


C Program to implement queue as an array

#include<stdio.h>
#include<conio.h>
#define MAX 10
void insertque(int *,int,int *,int*);
int deleteque(int *,int*,int*);
void main()
{
  int a[MAX];
  int front= -1,rear= -1,i;
  clrscr();
  insertque(a,20,&front,&rear);
  insertque(a,5,&front,&rear);
  insertque(a,9,&front,&rear);
  insertque(a,7,&front,&rear);
  insertque(a,22,&front,&rear);
  insertque(a,12,&front,&rear);
  insertque(a,15,&front,&rear);
  insertque(a,18,&front,&rear);
  insertque(a,10,&front,&rear);
  insertque(a,30,&front,&rear);
  insertque(a,35,&front,&rear);
  i=deleteque(a,&front,&rear);
  printf("\n Item deleted : %d",i);
  i=deleteque(a,&front,&rear);
  printf("\n Item deleted : %d",i);
  i=deleteque(a,&front,&rear);
  printf("\n Item deleted : %d",i);
  getch();
}

/* Add an element to the queue */
void insertque(int *a,int item,int *pfront,int *prear)
{
        if(*prear == MAX-1)
        {
              printf("\n Queue is full ");
              return ;
        }
        (*prear)++;
        a[*prear]=item;
        if(*pfront==-1)
        *pfront=0;
}

/* Removes an element form the queue */
int deleteque(int *a,int *pfront,int *prear)
{
    int data;
    if(*pfront==-1)
    {
        printf("\n Queue is empty ");
        return NULL;
    }
    data=a[*pfront];
    a[*pfront]=0;
    if(*pfront==*prear)
        *pfront=*prear-1;
    else
        (*pfront)++;
    return data;
}

C Program to evaluate Prefix Expression

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

#define MAX 30
#define OPERAND 10
#define OPERATOR 20

typedef struct prexp
{
  int top;
  int stack[MAX];
}stck;

void init(stck*);
void push(stck*,int);
int pop(stck*);
void eval(stck*,char,int,int);
int gettype(char);
void main()
{
     char pre[MAX];
     int num1,num2,item,l,i,pr;
     stck stk;

     fflush(stdin);
     clrscr();

     init(&stk);
     printf(" ENTER THE PREFIX EXPRESSION ");
     gets(pre);
     l=strlen(pre);

     for(i=l-1;i>=0;i--)
     {
        if(pre[i]==' ' || pre[i]=='\0')
        continue;
        switch(gettype(pre[i]))
        {
          case OPERAND : item=pre[i]-'0';
          push(&stk,item);
          break;
          case OPERATOR : num1=pop(&stk);
          num2=pop(&stk);
          eval(&stk,pre[i],num1,num2);
        }
     }
 printf("%d",stk.stack[0]);
 getch();
}

void init(stck *st )
{
  st->top=-1;
}

void push(stck *st,int num)
{
    st->top++;
    st->stack[st->top]=num;
}

int pop(stck *st)
{
    int num;
    num=st->stack[st->top];
    st->top--;
    return num;
}

void eval(stck *st,char op,int num1,int num2)
{
  int res;
     switch(op)
     {
      case '+': res=num1+num2;
      break;
      case '-': res=num1-num2;
      break;
      case '*': res=num1*num2;
      break;
      case '/': res=num1/num2;
      break;
      case '%': res=num1%num2;
      break;
      case '$': res=pow(num1,num2);
      break;
     }
  push(st,res);
}

int gettype(char c)
{
      switch(c)
      {
         case '+':
         case '-':
         case '*':
         case '/':
         case '$':
         case '%': return OPERATOR;
         default : return OPERAND;
        }
}

C Program to convert an infix expression to prefix expression

#include<stdio.h>
#include<conio.h>
#include<string.h>
#define MAX 30
#define OPERAND 10
#define OPERATOR 20
#define LEFTPARA 30
#define RIGHTPARA 40

typedef struct prestk
{
    int top;
    char stack[MAX];
}stack;

void init(stack*);
void push(stack*,char);
char pop(stack*);
int getprec(char);
int gettype(char);
void main()
{
    stack stk;
    char inf[MAX],ch,pre[MAX];
    int l,i,k=0,pr;
    fflush(stdin);
    clrscr();

    init(&stk);
    printf(" Enter the infix expression : ");
    gets(inf);
    l=strlen(inf);
    for(i=l-1;i>=0;i--)
    {
        switch(gettype(inf[i]))
        {
             case OPERAND : pre[k++]=inf[i];
             break;
             case OPERATOR: pr=getprec(inf[i]);
             while(pr<getprec(stk.stack[stk.top])&&stk.top!=-1)
             pre[k++]=pop(&stk);
             push(&stk,inf[i]);
             break;
             case RIGHTPARA:push(&stk,inf[i]);
             break;
             case LEFTPARA    : while((ch=pop(&stk)!=')'))
                                 {pre[k++]=ch;}
                                 break;
         }
    }

    while(stk.top!=-1)
    pre[k++]=pop(&stk);
    pre[k]='\0';
    strrev(pre);
    puts(pre);
    getch();
}

void init(stack *st)
{
  st->top=-1;
}

void push(stack *st,char c)
{
  st->top++;
  st->stack[st->top]=c;
}

char pop(stack *st)
{
    char c;
    c=st->stack[st->top];
    st->top--;
    return c;
}

int getprec(char c)
{
    switch(c)
    {
        case ')' : return 0;
        case '+' :
        case '-' : return 1;
        case '*' :
        case '/' :
        case '%' : return 2;
        case '$' : return 3;
    }
}

int gettype(char c)
{
     switch(c)
     {
     case '+':
     case '-':
     case '*':
     case '/':
     case '$':
     case '%': return OPERATOR;
     case '(': return LEFTPARA;
     case ')': return RIGHTPARA;
     default : return OPERAND;
     }
}


C Program to evaluate postfix expression

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

#define MAX 30
#define OPERAND 10
#define OPERATOR 20

typedef struct prexp
{
  int top;
  int stack[MAX];
}stck;

void init(stck*);
void push(stck *,int);
int pop(stck *);
void eval(stck *,char,int,int);
int gettype(char);
void main()
{
    char pos[MAX];
    int num1,num2,item,l,i,pr;
    stck stk;
    fflush(stdin);
    clrscr();
    init(&stk);
    printf(" Enter the postfix Expression : ");
    gets(pos);

        for(i=0;pos[i]!='\0';i++)
        {
            if(pos[i]==' '|| pos[i]=='\t')
            continue;

            switch(gettype(pos[i]))
            {
              case OPERAND: item=pos[i]-'0';
                                 push(&stk,item);
                                 break;

              case OPERATOR: num1=pop(&stk);
                                  num2=pop(&stk);
                                  eval(&stk,pos[i],num2,num1);
            }
      }
    printf("%d",stk.stack[0]);
    getch();
}

void init(stck *st)
{
  st->top=-1;
}

void push(stck *st,int num)
{
  st->top++;
  st->stack[st->top]=num;
}

int pop(stck *st)
{
    int num;
    num=st->stack[st->top];
    st->top--;
    return num;
}

void eval(stck *st,char op,int num1,int num2)
{
    int res;
      switch(op)
      {
          case '+': res=num1+num2;
                        break;
          case '-': res=num1-num2;
                        break;
          case '*': res=num1*num2;
                        break;
          case '/': res=num1/num2;
                        break;
          case '%': res=num1%num2;
                        break;
          case '$': res=pow(num1,num2);
                        break;
      }
    push(st,res);
}

int gettype(char c)
{
     switch(c)
     {
     case '+':
     case '-':
     case '*':
     case '/':
     case '$':
     case '%': return OPERATOR;
     default : return OPERAND;
     }
}

Saturday, August 28, 2010

C Program to calculate Factorial of a number by Recursion


#include<stdio.h>
#include<conio.h>
int f1=1;
int factorial(int);
void main()
{
int num,f;
printf(" Enter the number : ");
scanf("%d",&num);
f=factorial(num);
printf(" Factorial %d ",f);
getch();
}
int factorial(int n)
{
if(n==1)
return 1;
}
else
return (n*factorial(n-1));
}

The Father Of C " Dennis Ritchie"

C

Friday, August 27, 2010

C Program to calculate GCD by Recursion


#include<stdio.h>
#include<conio.h>
void main()
{
int gcd(int,int);
int a,b,g;
;

scanf("%d %d",&a,&b);
clrscr();
g=gcd(a,b);
printf("\n");
printf(" gcd = %d",g);
getch();
}


int gcd(int a,int b)
{
 int hcf;
if(a%b==0)
return (b);
else
{
hcf=gcd(b,a%b);
return (hcf);
}
}


C Program to convert infix to postfix expression


#include<stdio.h>
#include<conio.h>
char infix[100],post[100];
int top=0,stack[10];
void push(int);
char pop();
void postfix();
main()
{
char ch;
clrscr();
printf("\n Infix Expression ");
gets(infix);
postfix();
getch();
}


void postfix()
{
int i=0,j=0;
for(i=0;infix[i]!='\0';i++)
{
switch(infix[i])
{
case '+': while(stack[top]>=1)
                 post[j++]=pop();
                 push(1);
                 break;
case '-': while(stack[top]>=1)
                post[j++]=pop();
                push(2);
                break;
case '*': while(stack[top]>=3)
                post[j++]=pop();
                push(3);
                break;
case '/': while(stack[top]>=3)
                post[j++]=pop();
                push(4);
                break;
case '^': while(stack[top]>=4)
                post[j++]=pop();
                push(5);
                break;
case '(': push(0);
                break;
                top--;
                break;
default: post[j++]=infix[i];
}
while(top>0)
post[j++]=pop();
printf("\n The postfix expression is %s\n",post);
}


void push(int element )
{
top++;
stack[top]=element;
}
char pop()
{
int ele;
char ex;
ele=stack[top];
top--;
switch(ele)
{
case 1: ex='+';
break
case 2: ex='-';
break;
case 3: ex='*';
break;
case 4: ex='/';
break;
case 5= ex='^';
break;
}
return ex;
}

C Program to implement linked list as a stack


#include<stdio.h>
#include<conio.h>
#include<malloc.h>
sturct node
{
int data;
struct node *link;
};
void push(struct node**,int);
int pop(struct node**);
void delstack(struct node**);


void main()
{
struct node *s=NULL;
int i;
clrscr();
push(&s,10);
push(&s,19);
push(&s,1);
push(&s,20);
push(&s,32);
push(&s,16);


i=pop(&s);
printf(" \n Item popped : %d ",i);


i=pop(&s);
printf(" \n Item popped : %d ",i);


i=pop(&s);
printf(" \n Item popped : %d ",i);
delstack(&s);
getch();
}
/* Adds a new node to the stack as linked list */
void push(struct node **top,int item)
{
struct node*temp;
temp=(struct node*)malloc(sizeof(struct node));
if(temp==NULL)
printf("\n Stack is Full");
temp->data=item;
temp->link=*top;
*top=temp;
}

/*Pops an element from the stack */
int pop(struct node **top)
{
struct node *temp;
int temp;
if(*top==NULL)
{
printf("\n Stack is Empty ");
return NULL;
}
temp=*top;
item=temp->data;
*top=(*top)->link;
free(temp);
return item;
}


/* Deallocates memory */
void delstack(struct node **top)
{
struct node *temp;
if(*top==NULL)
return;
while(*top!==NULL)
{
temp=*top;
*top=(*top)->link;
free(temp);
}
}

C Program to implement stack as an array


#include<stdio.h>
#include<conio.h>
#define ARR 10

struct stack
{
int a[ARR];
int top;
};

void init(struct stack*);
void push(struct stack*,int,int);
int pop(struct stack*);
void main()
{
struct stack s;
int i;
clrscr();
init(&s);

push(&s,8);
push(&s,20);
push(&s,-4);
push(&s,15);
push(&s,18);


i=pop(&s);
printf("\n\n Item popped : %d",i);


i=pop(&s);
printf("\n\n Item popped : %d",i);

i=pop(&s);
printf("\n\n Item popped : %d",i);

getch();
}


/* Initializes the Stack */
void init(struct stack *s)
{
  s->top=-1;
}


/*Add an element to the Stack */
void push(struct stack *s,int item)
{
if(s->top==ARR-1)
{
printf("\n Stack is Full ");
return;
}

s->top++;
s->a[s->top]=item;
}


/* Removes an element from the stack */
int pop(struct stack *s)
{
int data;


if(s->top==-1)
{
printf(" Stack is Empty ");
return NULL;
}


data=s->a[s->top];
s->top--;
return data;
}





C Program to demonstrate stack Push and Pop


#include<stdio.h>
#include<conio.h>
#include<process.h>
#define MAXSIZE 10
void push();
int pop();
void traverse();
int stack[MAXSIZE];
int top=-1;
void main()
{
int choice;
char ch;
do
{
clrscr();
printf("\n 1. PUSH");
printf("\n 2. POP");
printf("\n 3. TRAVERSE");
printf("\n Enter your choice ");
scanf("%d",&choice);


switch(choice)
{
case 1: push();
break;
case 2: printf("\n The popped element is %d",pop());
break;
case 3: traverse();
break;
default : printf("\n You entered the WRONG CHOICE ");
}
printf("\n Do you wish to continue(Y/N)");
fflush(stdin);
scanf("%c",&ch);
}
while(ch=='y' || ch=='Y');
}


void push()
{
int item;
if(top==MAXSIZE-1)
{
printf("\n The stack is full");
getch();
exit(0);
}
else
{
printf(" Enter the element to be inserted ");
scanf("%d",&item);
top=top+1;
stack[top]=item;
}
}
int pop()
{
int item;
if(top==-1)
{
printf(" The stack is Empty ");
getch();
exit(0);
}
else
{
item=stack[top];
top=top-1;
}
return (item);
}


void traverse()
{
int i;
if(top==-1)
{
printf(" The stack is Empty ");
getch();
exit(0);
}
else
{
printf(" The Elements are :\n");
for(i=top;i>=0;i--)
{
printf("\n %d",stack[i]);
}
}
}

C Program to delete an element from array


#include<stdio.h>
#include<conio.h>
int i,n;
main()
{
int a[100],pos;
void del(int a[],int,int);
clrscr();
printf("\n How many elements in the array \n");
scanf("%d",&n);
printf("\n Enter the element of the array \n");
for(i=0;i<=n;i++)
scanf("%d",&a[i]);
printf("\n On which position element do you want to delete \n");
scanf("%d",pos);
del(a,pos,num);
getch();
}

void del(int a[],int pos,int n)
{
int j,item;
item=a[pos];
for(j=pos;j<=n-1;j++)
{
a[j]=a[j+1];
}
n=n-1;
printf(" New Array \n");
for(i=0;i<=n-1;i++)
printf(" %d\n",a[i]);
}

C Program to insert an element in array



#include<stdio.h>
#include<conio.h>
int i,len,pos,num;
main()
{
int a[100];
void insert(int a[],int,int,int);
clrscr();
printf(" Enter the no. of array elements  to be read ");
scanf(" %d",&len);
printf("\n Enter integers ");
for(i=0;i<=len-1;i++)
{
scanf(" %d",&a[i]);
}
printf(" Enter integer to be inserted ");
scanf("%d",&pos);
--pos;
insert(a,len,pos,num);
}
void insert(int a[],int len,int pos,int num)
{
for(i=len;i>=pos;i--)
{
a[i+1]=a[i];
}
a[pos]=num;

if(pos>len)
{
printf(" Insertion Outside the Array ");
}
len++;
printf(" \n New Array");
for(i=0;i<len;i++)
{
printf(" %d\n",a[i]);
}
}


C Program to multiply two matrices


#include<stdio.h>
#include<conio.h>
void main()
{
int a[3][3],b[3][3],c[3][3];
int i,j,k;
clrscr();
printf(" Enter the 1st Matrix ");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&a[i][j] );
}
}
printf("\n Array A is \n");
for(i=0;i<3;i++)
{  printf("\n");
for(j=0;j<3;j++)
{
printf(" %d",a[i][j] );
}
}

printf(" Enter the 2nd Matrix ");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&b[i][j] );
}
}
printf("\n Array B is \n");
for(i=0;i<3;i++)
{  printf("\n");
for(j=0;j<3;j++)
{
printf(" %d",b[i][j] );
}
}

for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
for(k=0;k<3;k++)
{
c[i][j]+=a[i][k]*b[k][j];
}
}
}
printf("\n Multiplied resultant array C is \n");
for(i=0;i<3;i++)
{
printf("\n");
for(j=0;j<3;j++)
{
printf(" %d",c[i][j]);
}
}
getch();
}