Friday, 6 November 2015

Recurrsive solution for the towers of hanoi problem.(CODE)


This topic is taught in B.tech 3rd semester in data structures and algorithms. So, here is a program for this problem.

In this program, you simply have to enter the number of discs and the program will generate the moves required for the given number of discs. I will post the algorithm also.

Please give your comments and tell me if there is any problem with the program.
 HAPPY TO HELP YOU..!!

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

void towers(int,char,char,char);

void towers(int n,char frompeg,char topeg,char auxpeg)
{ /* If only 1 disk, make the move and return */
 if(n==1)
   { printf("%c to %c\t",frompeg,topeg);
     return;
   }
 /* Move top n-1 disks from A to B, using C as auxiliary */
 towers(n-1,frompeg,auxpeg,topeg);
 /* Move remaining disks from A to C */
 printf(" %c to %c\t",frompeg,topeg);
 /* Move n-1 disks from B to C using A as auxiliary */
 towers(n-1,auxpeg,topeg,frompeg);
}
void main()
{
 int n;
clrscr();
 printf("Enter the number of disks : ");
 scanf("%d",&n);
// printf("The Tower of Hanoi involves the moves :\n\n");
 towers(n,'A','C','B');
getch();
}




Expanding the blog


Yay.. expanding the blog to PHP, Java, HTML, CSS, Jquery, AJAX, Andriod, JSP.
Feel free to leave comments!

Thursday, 28 November 2013

Banker's Algorithm (C++ Program)

#include<iostream.h>
#include<conio.h>
void main()
{
int alloc[10][10],max[10][10],avail[10],n,m,need[10][10],i,j,k,finish[10],work[10];
int SSeq[10];
clrscr();

cout<<"Enter no. of processes:";
cin>>n;
cout<<"Enter no. of resources:";
cin>>m;
       // cout<<"Enter allocation matrix:";
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
cout<<"\nEnter no. of instances of "<<j+1<<"allocated to "<<i+1<<":" ;
cin>>alloc[i][j];
}
}
       // cout<<"\nEnter max matrix";
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
cout<<"\nEnter maximum instances of"<<j+1<<"allocated to"<<i+1<<":";
cin>>max[i][j];
}
}

for(i=0;i<m;i++)
{
cout<<"\nEnter available instances of"<<i+1;
cin>>avail[i];
}
//calculate need matrix
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
need[i][j]=max[i][j]-alloc[i][j];
}
}
cout<<"\nAllocation matrix is:";
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
cout<<alloc[i][j]<<"\t";
}
cout<<endl;
}
cout<<"\nMax matrix is:";
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
cout<<max[i][j]<<"\t";
}
cout<<endl;
}
cout<<"\nNeed matrix is:";
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
cout<<need[i][j]<<"\t";
}
cout<<endl;
}
//initialize work and finish
for(i=0;i<n;i++)
work[i]=avail[i];
k=0;
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
if(finish[i]==0 && need[i][j]<=work[j])
{
SSeq[k]=i+1;
k++;
work[i]=work[i]+alloc[i][j];
}
}
}
cout<<"safe sequence is:";
for(i=0;i<k;i++)
cout<<SSeq[i]<<",";
if(k==n)
cout<<"\nThe system is safe.\n";
else
cout<<"\nDeadlock occurence!!\n";
getch();
}

First Come First Serve scheduling (C++ Program)

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int n,i,j,k,bt[10],pid[10],temp,temp1,wt[10],tat[10],twt=0,ttat=0;
cout<<"Enter no. of process:";
cin>>n;
for(i=0;i<n;i++)
{
cout<<"Enter burst time for process "<<i+1<<":";
cin>>bt[i];
pid[i]=i;
}
wt[0]=0;
for(i=0;i<n;i++)
{
wt[i+1]=wt[i]+bt[i];
tat[i]=wt[i]+bt[i];
}
for(i=0;i<n;i++)
{
twt=twt+wt[i];
ttat=ttat+tat[i];
}
float awt=(float)twt/n;
float atat=(float)ttat/n;
cout<<"Process ID          :";
for(i=0;i<n;i++)
cout<<pid[i]<<"\t";
cout<<"\nBurst Time        :";
for(i=0;i<n;i++)
cout<<bt[i]<<"\t";
cout<<"\nWaiting time      :";
for(i=0;i<n;i++)
cout<<wt[i]<<"\t";
cout<<"\nTurn around time  :";
for(i=0;i<n;i++)
cout<<tat[i]<<"\t";

cout<<"\n";

cout<<"Avg waiting time:"<<awt<<endl;
cout<<"Avg turn around time:"<<atat<<endl;
getch();
}

Shortest job first scheduling (C++ program)

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int n,i,j,k,bt[10],pid[10],temp,temp1,wt[10],tat[10],twt=0,ttat=0;
//reading process ID and burst time
cout<<"Enter no. of process:";
cin>>n;
for(i=0;i<n;i++)
{
cout<<"Enter burst time for process "<<i+1<<":";
cin>>bt[i];
pid[i]=i;
}
// sorting jobs according to burst time (ascending )
for(i=0;i<n;i++)
{
for(k=i+1;k<n;k++)
{
if( bt[i] > bt[k] )
{
//swapping process id
temp=pid[i];
pid[i]=pid[k];
pid[k]=temp;
//swapping burst time
temp=bt[i];
bt[i]=bt[k];
bt[k]=temp;
}
}
}
// caculating waiting time and turn around time for each process
wt[0]=0;
for(i=0;i<n;i++)
{
wt[i+1]=wt[i]+bt[i];
tat[i]=wt[i]+bt[i];
}
//calculating total turn around time and total waiting time
for(i=0;i<n;i++)
{
twt=twt+wt[i];
ttat=ttat+tat[i];
}
float awt=(float)twt/n;
float atat=(float)ttat/n;
// displaying output
cout<<"Process ID          :";
for(i=0;i<n;i++)
cout<<pid[i]<<"\t";
cout<<"\nBurst Time        :";
for(i=0;i<n;i++)
cout<<bt[i]<<"\t";
cout<<"\nWaiting time      :";
for(i=0;i<n;i++)
cout<<wt[i]<<"\t";
cout<<"\nTurn around time  :";
for(i=0;i<n;i++)
cout<<tat[i]<<"\t";

cout<<"\n";

cout<<"Avg waiting time:"<<awt<<endl;
cout<<"Avg turn around time"<<atat<<endl;
getch();
}

Saturday, 25 August 2012

Wish me Good luck

So, here I am with another blog dealing with programming problems. This blog is yet only for c and c++ since I am yet a student only, but I will later extend it's scope. Till then I will concentrate only on C and C++. So, every one is welcome with their problems or homework.
I will try my best and ya I will be posting some programs on my own. It might be of some help for our followers. And suggestions are always welcome.

HAPPY  PROGRAMMING...!!