Wednesday, 4 September 2013

Infix to Prefix Conversion using Stacks in C

Infix to Prefix Conversion  using Stacks in C

Algorithm to Convert Infix to Prefix Form

Suppose A is an arithmetic expression written in infix form. The algorithm finds equivalent prefix expression B. Step 1. Push “)” onto STACK, and add “(“ to end of the A
Step 2. Scan A from right to left and repeat step 3 to 6 for each element of A until the STACK is empty
Step 3. If an operand is encountered add it to B
Step 4. If a right parenthesis is encountered push it onto STACK
Step 5. If an operator is encountered then:
a. Repeatedly pop from STACK and add to B each operator (on the top of STACK) which has same or higher precedence than the operator.
b. Add operator to STACK
Step 6. If left parenthesis is encontered then
a. Repeatedly pop from the STACK and add to B (each operator on top of stack until a left parenthesis is encounterd)
b. Remove the left parenthesis
Step 7. Exit

Program 

#define SIZE 50 /* Size of Stack */
#include<string.h>
#include <ctype.h>
#include<stdio.h>
char s[SIZE]; int top=-1; /* Global declarations */
push(char elem)
{ /* Function for PUSH operation */
s[++top]=elem;
}
char pop()
{ /* Function for POP operation */
return(s[top--]);
}
int pr(char elem)
{ /* Function for precedence */
switch(elem)
{
case '#': return 0;
case ')': return 1;
case '+':
case '-': return 2;
case '*':
case '/':return 3;
}
}
main()
{ /* Main Program */
char infx[50],prfx[50],ch,elem;
int i=0,k=0;
printf("\n\nRead the Infix Expression ? ");
scanf("%s",infx);
push('#');
strrev(infx);
while( (ch=infx[i++]) != '\0')
{
if( ch == ')')
push(ch);
else if(isalnum(ch))
prfx[k++]=ch;
else if( ch == '(')
{
while( s[top] != ')')
prfx[k++]=pop();
elem=pop(); /* Remove ) */
}
else
{ /* Operator */
while( pr(s[top]) >= pr(ch) )
prfx[k++]=pop(); push(ch);
}
}
while( s[top] != '#') /* Pop from stack till empty */
prfx[k++]=pop();
prfx[k]='\0'; /* Make prfx as valid string */
strrev(prfx);
strrev(infx);
printf("\n\nGiven Infix Expn: %s \nPrefix Expn: %s\n",infx,prfx);
}

Out Put:



Tuesday, 28 May 2013

C Program for Linear search

C Program for Linear search

#include<conio.h>
#include<stdio.h>
main()
{
int a[20], n,i,k, flag=0,loc;
printf("Enter the size of Array: ");
scanf("%d",&n);

printf("Enter the Numbers: ");
for(i=0;i<n;i++)
scanf("%d",&a[i]);

printf("Enter Searching Element: ");
scanf("%d",&k);

for(i=0;i<n;i++)
{
if(a[i]==k)
{
loc=i+1;
flag=1;
break;
}
}

if(flag==1)
printf("The element is found at location %d",loc);
else
printf("Element is not found");
}

Tuesday, 5 February 2013

Matrix Multiplication in c

    #include<conio.h>
    #include<stdio.h>
    main()
    {
    int a[5][5],b[5][5],c[5][5],m,n,o,p,i,j,sum,k;

    clrscr();
    printf("Enter the A Matrix size:\n");
    scanf("%d%d",&m,&n);

    printf("Enter the A Matrix Elements\n");
    for(i=0;i<m;i++)
    for(j=0;j<n;j++)
    scanf("%d",&a[i][j]);

    printf("Enter the B Matrix size:\n");
    scanf("%d%d",&o,&p);

    printf("Enter the B Matrix Elements\n");
    for(i=0;i<o;i++)
    for(j=0;j<p;j++)
    scanf("%d",&b[i][j]);

    if(n!=o)
    printf("The 1st Matrix Column are shoul equal to 2nd Matrix Rows");
    else
    {
        for(i=0;i<m;i++)
        {
            for(j=0;j<p;j++)
            {
                sum=0;
                for(k=0;k<n;k++)
                {
                    sum=sum+a[i][k]*b[k][j];
                }
                c[i][j]=sum;
            }
        }


    printf("Enter the AxB Matrix Elements\n");
    for(i=0;i<o;i++)
    {
        printf("\n");
        for(j=0;j<p;j++)
        printf("%d\t",c[i][j]);
    }

    }
    getch();
    }


Out Put:

Saturday, 26 January 2013

Relational Operators in C Programing

#include<conio.h>
    #include<stdio.h>
    main()
    {
    int a,b,c;
    clrscr();
    printf("Enter 2 No's: ");
    scanf("%d%d",&a,&b);
    printf("\na = %d",a);
    printf("\nb = %d",b);
    printf("\na>b = %d",a>b);
    printf("\na<b = %d",a<b);
    printf("\na>=b = %d",a>=b);
    printf("\na<=b = %d",a<=b);
    printf("\na==b = %d",a==b);
    printf("\na!=b = %d",a!=b);
    getch();
    }

Swapping of Given Two Numbers

#include<conio.h>
    #include<stdio.h>
    main()
    {
    int a,b,c;
    clrscr();
    printf("Enter 2 No's: ");
    scanf("%d%d",&a,&b);
    printf("\nBefour Swaping");
    printf("\na = %d",a);
    printf("\nb = %d",b);
    c=a;
    a=b;
    b=c;
    printf("\nAfter Swaping");
    printf("\na = %d",a);
    printf("\nb = %d",b);
    getch();
    }