0% found this document useful (0 votes)
10 views14 pages

Lab 5

Uploaded by

vandana u
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views14 pages

Lab 5

Uploaded by

vandana u
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Analysis and Design of Algorithms

Lab (BCSL404)
4th Sem – AI-ML

By,
Vandana U
Asst. Professor
Dept. of AI-DS
Develop a Program in C for the following Stack
applications
A).Evaluation of Suffix expression with single
digit operands and operators: +, -, *, /, %, ^
B). Solving Tower of Hanoi problem with n
disks.
This can be the part of the
presentation where you introduce
yourself, write your email…
This can be the part of the
presentation where you introduce
yourself, write your email…
#include <stdio.h>
Function to calculate the value of
#include<ctype.h> postfix expression
#include<math.h>
#include<string.h>
float compute(char symbol,float op1,float op2)
{ switch(symbol)
{
case '+': return op1+op2;
break;
case '-': return op1-op2;
break;
case '*': return op1*op2;
break; Power is denoted as ^
case '/': return op1/op2;
break;
case ‘^': return pow(op1,op2);
break;
default: return 0; }}
void main() {
float s[20],res,op1,op2;
int top,i;
char postfix[20],symbol;
printf("Enter the postfix expression:\n");
scanf("%s",postfix);
Take one value from postfix and assign it to symbol
top=-1;
for(i=0;i<strlen(postfix);i++)
Check whether the entered symbol is a digit. If so, take the
{ symbol=postfix[i];
symbol and find its equivalent ASCII value and assign it to
if(isdigit(symbol)) the top of the stack.
s[++top]=symbol-'0'; Ex: ASCII value of 5 is 53 and 0 is 48 ,

else \so symbol – ‘0’ = 53 – 48 = 5


{ Else pop the two values from
the stack into op2 and op1
op2=s[top--];
respectively and compute
op1=s[top--];
the result. Once the result is
res=compute(symbol,op1,op2); obtained, push it back to the
s[++top]=res; stack. Continue this till the all

} the characters are checked.

}
Once, we finish the calculation for
res=s[top--]; all the values, pop out the element
printf("The result is %f",res); left in the stack and display that as
the result.
}
B) Towers of Hanoi :
Solving Tower of Hanoi problem with n disks. The Tower of Hanoi is a
mathematical game or puzzle. It consists of three rods, and a number of disks of
different sizes which can slide onto any rod. The puzzle starts with the disks in a
neat stack in ascending order of size on one rod, the smallest at the top, thus
making a conical shape.
The objective of the puzzle is to move the entire stack to another rod,
obeying the following simple rules:
- Only one disk can be moved at a time.
- Each move consists of taking the upper disk from one of the stacks and
placing it on top of another stack i.e. a disk can only be moved if it is the
uppermost disk on a stack.
- No disk may be placed on top of a smaller disk. With three disks, the
puzzle can be solved in seven moves. The minimum number of moves
required to solve a Tower of Hanoi puzzle is 2 n - 1, where n is the number
of disks
ALGORITHM:
Step 1: Start.
Step 2: Read N number of discs.
Step 3: Move all the discs from source to destination by using temp rod.
Step 4: Stop.
#include <stdio.h> If number of disc

void tower(int n,char beg,char aux,char end) entered is 0, then


moving not required at
{
all.
if(n==0)
If number of disc
printf("Illegal, Try with non-zero positive Integers\n"); entered is 1, then
else if(n==1) directly move the disc
from source to
printf("Move disc from %c to %c\n",beg,end);
destination
else
Move the n-1 disc from source to intermediate pole ie, if 3 discs are
{ tower(n-1,beg,end,aux); present, then move 2 poles from source to aux

tower(1,beg,aux,end); Once the above step is carried out, you will be left with only 1 disc at
source. Now move it from source to destination
tower(n-1,aux,beg,end);
Now move the remaining discs present in intermediate to the
}} destination.
void main()
{
int n; Call the tower of Hanoi
function
char a,b,c;
printf("Enter the number of discs:\n");
scanf("%d",&n);
printf("Tower of Hanoi for %d discs has the following steps:\n",n);
tower(n,'a','b','c');
printf("\n\nTotal Number of moves are: %d", (int)pow(2,n)-1);
}

Total number of moves :


(23) – 1 = 8 – 1 = 7
Output:
Evaluating Postfix Expression:
1st Run: 5 7 + 6 7 + * Expected o/p: 156
2nd Run: 452*+5+ Expected o/p: 19
Tower of Hanoi
1st Run: Number of Disc: 3
2nd Run: Number of Disc: 0
THANK YOU

You might also like