Showing posts with label DS data structure. Show all posts
Showing posts with label DS data structure. Show all posts

Monday, January 10, 2011

power using recursion / recursive function

/*****************************
* Umang B Bhatt *
* bhatt.umang7@gmail.com *
*****************************/

/**
* program for finding power using recursion
*/


#include<stdio.h>

int
fun(int a,int b)
{


if
(b>1)
{

return
a*fun(a,b-1);
}


else

{

return
a;
}
}


void
main()
{


int
a =3 ;
int
b = 2;

printf("\nEnter a: ");
scanf("%d",&a);
printf("Enter b: ");

scanf("%d",&b);
printf("%d^%d is: %d\n",a,b,fun(a,b));
}

factorial using recursion

/*****************************
* Umang B Bhatt *
* bhatt.umang7@gmail.com *
*****************************/

/**
* program for finding factorial using recursion
*/


#include<stdio.h>

int
fun(int n)
{


if
(n>1)
{

return
n*fun(n-1);
}


else

{

return
1;
}
}


void
main()
{


int
n =3 ;
printf("\nEnter a number to find factorial: ");
scanf("%d",&n);

printf("Factorial is: %d\n",fun(n));
}

infix to prefix with bracket

/****************************
* Umang B Bhatt *
* bhatt.umang7@gmail.com *
*****************************/

/**
* program for infix to prefix with bracket
*/


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

#define MAX 100

/*
* this will not catch errors
* eg if you enter (a*b]+c then it will output
*/


/* OP:
* Enter expression: (a*(c+d)+(e+f)*g)/(j+i)
* prefix of entered (infix) expression is:
* /+*a+cd*+efg+ji
*
*/




// personally, i believe that declaring TOP and S here is bad practice

void
main()
{

int
TOP = -1, PTOP = 0;

char
s[MAX];
char
pref[MAX] = {'\0'};

int
pre(char);
int
push(char[], int, char);

int
pop(char *, int *);

char
c[MAX] = {'\0'}, symb, x;

int
i, j;
char
temp;
printf("Enter expression: ");

gets(c);

/*
* gets will give error when compiling on modern compiler
*/


/*
* here i don't have strrev function on my compiler (gcc) so i am doing this manually
* but you can write strrev(c); if your compiler supports it
*/

i = 0;

j = strlen(c) - 1;
while
(i < j)
{


temp = c[i];
c[i++] = c[j];

c[j--] = temp;
}


// x = pop(s,&TOP);
// TOP = push(s,TOP,symb);

for
(i = 0; c[i] != '\0'; i++)
{


symb = c[i];
if
(isalpha(symb) > 0)
{


pref[PTOP] = symb;
PTOP++;
}

else if
(symb == '+' || symb == '-' || symb == '*' || symb == '/' || symb == '\\' || symb == '^' || symb == '$')
{


while
((pre(symb)) < (pre(s[TOP])))
{


x = pop(s, &TOP);
pref[PTOP] = x;

PTOP++;
}

TOP = push(s, TOP, symb);
}


else if
(symb == '}' || symb == ']' || symb == ')')
{


TOP = push(s, TOP, symb);
}

else


{

x = '\0';
while
(x != '}' && x != ']' && x != ')')
{


x = pop(s, &TOP);
if
(x != '}' && x != ']' && x != ')')
{


pref[PTOP] = x;
PTOP++;
}
}
}
}

while
(TOP >= 0)
{


x = pop(s, &TOP);
pref[PTOP] = x;

PTOP++;
}

pref[PTOP] = '\0';




i = 0;
j = strlen(pref) - 1;


while
(i < j)
{

temp = pref[i];

pref[i++] = pref[j];
pref[j--] = temp;
}




printf("\nprefix of entered (infix) expression is:\n%s\n", pref);


// getch(); // win not work on linux
}

int
push(char *s, int top, char ele)
{


int
i;
if
(top >= MAX)
{


printf("\nStack Overflow");
}

else

{

s[++top] = ele;
}


return
top;
}


int
pop(char *a, int *top)
{


if
((*top) >= 0)
{
(*
top) = (*top) - 1;

return
a[(*top) + 1];
}

else

{


printf("Stack underflow\n");
return
0;
}
}


int
pre(char x)
{


int
a;
switch
(x)
{

case
'(':

case
'{':
case
'[':
a = -5;

break
;
case
'+':
case
'-':
a = 2;

break
;
case
'/':
case
'*':
case
'\\':

case
'%':
a = 5;
break
;

case
'$':
case
'^':
a = 10;

break
;
}

return
a;
}

infix to prefix without bracket

/****************************
* Umang B Bhatt *
* bhatt.umang7@gmail.com *
*****************************/

/**
* program for infix to prefix without bracket
*/


// the actual algorithm is quiet difficult so doing this simple way

/* OP:
* Enter expression: a*b+d/e
* prefix of entered (infix) expression is:
* +*ab/de
*/


// I will be following the BSD style indentation in all the programs because it feels more readable to me


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

#define MAX 100

void
main()
{


int
TOP = -1, PTOP = 0;
char
s[MAX];

char
pref[MAX] = {'\0'};

int
pre(char);

int
push(char[], int, char);
int
pop(char *, int *);

char
c[MAX] = {'\0'}, symb, x;

char
temp;

int
i, j;

printf("Enter expression: ");
gets(c);


/*
* here i don't have strrev function on my compiler (gcc) so i am doing this manually
* but you can write strrev(c); if your compiler supports it
*/


i = 0;
j = strlen(c) - 1;

while
(i < j)
{

temp = c[i];

c[i++] = c[j];
c[j--] = temp;
}


/*
* gets will give error when compiling on modern compiler
*/


for
(i = 0; c[i] != '\0'; i++) // NULL will give error on iso compiler

{
symb = c[i];
if
(isalpha(symb) > 0)
{


pref[PTOP] = symb;
PTOP++;
}

else

{


while
((pre(symb))<(pre(s[TOP])) && TOP >= 0)
{


x = pop(s, &TOP);
pref[PTOP] = x;

PTOP++;
}

TOP = push(s, TOP, symb);
}
}


while
(TOP >= 0)
{

x = pop(s, &TOP);

pref[PTOP] = x;
PTOP++;
}

pref[PTOP] = '\0';


//strrev(pref);
// use strrev if it is there in your compiler
// if not available then use following code
i = 0;
j = strlen(pref) - 1;



while
(i < j)
{

temp = pref[i];

pref[i++] = pref[j];
pref[j--] = temp;
}





// now print the reverse of the string
printf("\nprefix of entered (infix) expression is:\n %s\n", pref);
// getch(); // win not work on linux

}

int
push(char *s, int top, char ele)
{


int
i;
if
(top >= MAX)
{


printf("\nStack Overflow");
}

else

{

s[++top] = ele;
}


return
top;
}


int
pop(char *a, int *top)
{


if
((*top) >= 0)
{
(*
top) = (*top) - 1;

return
a[(*top) + 1];
}

else

{


printf("Stack underflow\n");
return
0;
}
}


int
pre(char x)
{


int
a;
switch
(x)
{

case
'+':

case
'-':
a = 2;
break
;

case
'/':
case
'*':
case
'\\':

case
'%':
a = 5;
break
;

case
'$':
case
'^':
a = 10;

break
;
}

return
a;
}

infix to postfix with bracket

/****************************
* Umang B Bhatt *
* bhatt.umang7@gmail.com *
*****************************/

/**
* program for infix to postfix with bracket
*/


#include<stdio.h>
#define MAX 100


/*
* this will not catch errors
* eg if you enter (a*b]+c then it will output ab*c+
*/

/* OP:
* Enter expression: (a*(c+d)+(e+f)*g)/(j+i)
* postfix of entered (infix) expression is:
* acd+*ef+g*+ji+/
*/



// personally, i believe that declaring TOP and S here is bad practice

void
main()
{

int
TOP = -1, PTOP = 0;

char
s[MAX];
char
post[MAX] = {'\0'};

int
pre(char);
int
push(char[], int, char);

int
pop(char *, int *);

char
c[MAX] = {'\0'}, symb, x;

int
i;
printf("Enter expression: ");
gets(c);

/*
* gets will give error when compiling on modern compiler
*/


// x = pop(s,&TOP);
// TOP = push(s,TOP,symb);

for
(i = 0; c[i] != '\0'; i++)
{


symb = c[i];
if
(isalpha(symb) > 0)
{


post[PTOP] = symb;
PTOP++;
}

else if
(symb == '+' || symb == '-' || symb == '*' || symb == '/' || symb == '\\' || symb == '^' || symb == '$')
{


while
((pre(symb)) <= (pre(s[TOP])) && TOP >= 0)
{


x = pop(s, &TOP);
post[PTOP] = x;

PTOP++;
}

TOP = push(s, TOP, symb);
}


else if
(symb == '(' || symb == '[' || symb == '{')
{


TOP = push(s, TOP, symb);
}

else


{

x = '\0';
while
(x != '(' && x != '{' && x != '[')
{


x = pop(s, &TOP);
if
(x != '(' && x != '{' && x != '[')
{


post[PTOP] = x;
PTOP++;
}
}
}
}

while
(TOP >= 0)
{


x = pop(s, &TOP);
post[PTOP] = x;

PTOP++;
}

post[PTOP] = '\0';
printf("\npostfix of entered (infix) expression is:\n%s\n", post);


// getch(); // win not work on linux
}

int
push(char *s, int top, char ele)
{


int
i;
if
(top >= MAX)
{


printf("\nStack Overflow");
}

else

{

s[++top] = ele;
}


return
top;
}


int
pop(char *a, int *top)
{


if
((*top) >= 0)
{
(*
top) = (*top) - 1;

return
a[(*top) + 1];
}

else

{


printf("Stack underflow\n");
return
0;
}
}


int
pre(char x)
{


int
a;
switch
(x)
{

case
'(':

case
'{':
case
'[':
a = -5;

break
;
case
'+':
case
'-':
a = 2;

break
;
case
'/':
case
'*':
case
'\\':

case
'%':
a = 5;
break
;

case
'$':
case
'^':
a = 10;

break
;
}

return
a;
}

infix to postfix without bracket

/****************************
* Umang B Bhatt *
* bhatt.umang7@gmail.com *
*****************************/

/**
* program for infix to postfix without bracket
*/


#include<stdio.h>
#define MAX 100


void
main()
{

int
TOP = -1, PTOP = 0;

char
s[MAX];
char
post[MAX] = {'\0'};

int
pre(char);
int
push(char[], int, char);

int
pop(char *, int *);

char
c[MAX] = {'\0'}, symb, x;

int
i;
printf("Enter expression: ");
gets(c);

/*
* gets will give error when compiling on modern compiler
*/


for
(i = 0; c[i] != '\0'; i++) // NULL will give error on iso compiler

{
symb = c[i];
if
(isalpha(symb) > 0)
{


post[PTOP] = symb;
PTOP++;
}

else

{


while
((pre(symb)) <= (pre(s[TOP])) && TOP >= 0)
{


x = pop(s, &TOP);
post[PTOP] = x;

PTOP++;
}

TOP = push(s, TOP, symb);
}
}


while
(TOP >= 0)
{

x = pop(s, &TOP);

post[PTOP] = x;
PTOP++;
}

post[PTOP] = '\0';

printf("\nPostfix of entered (infix) expression is:\n %s\n", post);
// getch(); // win not work on linux
}

int
push(char *s, int top, char ele)
{


int
i;
if
(top >= MAX)
{


printf("\nStack Overflow");
}

else

{

s[++top] = ele;
}


return
top;
}


int
pop(char *a, int *top)
{


if
((*top) >= 0)
{
(*
top) = (*top) - 1;

return
a[(*top) + 1];
}

else

{


printf("Stack underflow\n");
return
0;
}
}


int
pre(char x)
{


int
a;
switch
(x)
{

case
'+':

case
'-':
a = 2;
break
;

case
'/':
case
'*':
case
'\\':

case
'%':
a = 5;
break
;

case
'$':
case
'^':
a = 10;

break
;
}

return
a;
}

infix to postfix without bracket

/****************************
* Umang B Bhatt *
* bhatt.umang7@gmail.com *
*****************************/

/**
* program for infix to postfix without bracket
*/


#include<stdio.h>
#define MAX 100


void
main()
{

int
TOP = -1, PTOP = 0;

char
s[MAX];
char
post[MAX] = {'\0'};

int
pre(char);
int
push(char[], int, char);

int
pop(char *, int *);

char
c[MAX] = {'\0'}, symb, x;

int
i;
printf("Enter expression: ");
gets(c);

/*
* gets will give error when compiling on modern compiler
*/


for
(i = 0; c[i] != '\0'; i++) // NULL will give error on iso compiler

{
symb = c[i];
if
(isalpha(symb) > 0)
{


post[PTOP] = symb;
PTOP++;
}

else

{


while
((pre(symb)) <= (pre(s[TOP])) && TOP >= 0)
{


x = pop(s, &TOP);
post[PTOP] = x;

PTOP++;
}

TOP = push(s, TOP, symb);
}
}


while
(TOP >= 0)
{

x = pop(s, &TOP);

post[PTOP] = x;
PTOP++;
}

post[PTOP] = '\0';

printf("\nPostfix of entered (infix) expression is:\n %s\n", post);
// getch(); // win not work on linux
}

int
push(char *s, int top, char ele)
{


int
i;
if
(top >= MAX)
{


printf("\nStack Overflow");
}

else

{

s[++top] = ele;
}


return
top;
}


int
pop(char *a, int *top)
{


if
((*top) >= 0)
{
(*
top) = (*top) - 1;

return
a[(*top) + 1];
}

else

{


printf("Stack underflow\n");
return
0;
}
}


int
pre(char x)
{


int
a;
switch
(x)
{

case
'+':

case
'-':
a = 2;
break
;

case
'/':
case
'*':
case
'\\':

case
'%':
a = 5;
break
;

case
'$':
case
'^':
a = 10;

break
;
}

return
a;
}

Tuesday, December 28, 2010

stack using array

/****************************
* Umang B Bhatt *
* bhatt.umang7@gmail.com *
*****************************/

/**
* program for stack using array
*/

#include<stdio.h>
#define SIZE 100


int
peep(int a[], int top, int i)
{


if
( i < 0 && i>= top)
{

printf("This is invalid operation. Index out of range.");
}


else

{

return
a[i];
}
}


int
push(int *a , int top, int ele)
{


if
(top >= SIZE)
{

printf("\nStack Overflow");
}

else


{

a[++top] = ele;
}

return
top;
}


int
pop(int *a,int *top)
{

if
((*top) >=0)
{
(*
top) = (*top) -1 ;

return
a[(*top)+1];
}

else

{

printf("Stack underflow\n");

return
0;
}
}

void
change(int *a,int top, int index, int element)
{


if
(index < 0 || index > top)
{

printf("The index is out of range");
}


else

{

a[index] = element;
}
}



void
main()
{

// void push(int [] ,int ,int );
int i= 0 ;

int
top = -1;
int
a[SIZE];

int
d = 1;
int
temp,temp1;

do

{

printf("\n\nSelect any option");
printf("\n1 Display stack");

printf("\n2 push");
printf("\n3 pop");
printf("\n4 peep");

printf("\n5 change");
printf("\n6 exit\n");
scanf("%d",&d);


switch
(d)
{

case
1:
if
(top>=0)
{


for
(i = 0 ; i <= top ; i++ )
{


printf("\nelement at %d is %d" , i , peep(a,top,i));
}
}


else

{

printf("\nThe stack is empty");
}

break
;
case
2:

printf("Enter number you want to insert in the stack: ");
scanf("%d",&temp);
top = push( a , top , temp);

break
;
case
3:
printf("The popped element is: %d" , pop(a,&top));

break
;
case
4:
printf("At which position you want to peep ? ");
scanf("%d",&temp);

printf("Peeped element is: %d" , peep(a,top,temp) );
break
;

case
5:
printf("Enter the index at which you want to change: ");
scanf("%d",&temp);

printf("Enter he actual element that you want to enter: ");
scanf("%d",&temp1);
change(a,top,temp,temp1);

break
;
default
:
printf("Please enter a valid choice.");
break
;

}
}


while
(d != 6);
// and yes, i think you will be able to writ getch here (on TC3)
// i am on linux so that thing is not available here
}

Monday, December 27, 2010

find the smallest from array

/****************************
* Umang B Bhatt *
* bhatt.umang7@gmail.com *
*****************************/

/**
* program for scannign n numbers and finding the smallest from them
*/

#include<stdio.h>
void main()
{


int
n = 0 , i =0 , j = 0;

int
no[100];
int
smallest;
printf("How many numbers you want to scan ? ");

scanf("%d",&n);
if
(n < 0 || n>100)
{


// if the user enters invalid no of elemtns then we come here
printf("Invalid number. It must be brtween 1 to 100");
}

else

{

// logic for scanning the numbers
for(i = 0 ; i < n ;i++)
{


printf("Enter element %d: ", i);
scanf("%d",&no[i]);
}



// assign biggest the first element's value
biggest = no[0];
// the logic for finding the smallest number
for(i = 0 ; i < n ;i++)
{

if
(no[i] < smallest)
{


smallest = no[i];
}
}


printf("The smallest number among those numbers is: %d" , smallest);

}


printf("\n");
}