Binod's Blog

Tag: DSA

TOH: Tower of Hanoi

by on Jan.03, 2011, under C & C++, DSA in C, Tutorial

Towers of Hanoi (aka Tower of Hanoi) is a mathematical puzzle invented by a French Mathematician Edouard Lucas in 1983. Initially the game has few discs arranged in the increasing order of size in one of the tower like shown above. The number of discs can vary, but there are only three towers. The goal is to transfer the discs from one tower another tower. However you can move only one disk at a time and you can never place a bigger disc over a smaller disk. It is also understood that you can only take the top most disc from any given tower.While TOH doesn’t require any general knowledge to be solved it does require an astute mind and logical thinking

#include<iostream.h>
#include<conio.h>
int i=0;
void move(int n,char *s,char *t,char *d)

// S : is  source peg
//D : destination peg
// T : temp peg
//i : Number  of steps
{
if(n>0)
{
move(n-1,s,d,t);  // move n-1 disks from S to T
i++;
cout<<”Step:”<<i<<” disk “<<n<<” is moved from “<<s<<” to “<<d<<endl;
// move the disk from to S to D
move(n-1,t,s,d); //move n-1 disc from T to D
}
}
void main()
{
clrscr();
cout<<”\n**********************************************************\n”;
cout<<”This C++ program is to solve the towers of hanoi problem”;
cout<<”\n**********************************************************\n”;
cout<<”Enter the no. of disks “;
int n;
cin>>n;
move(n,”Source”,”Temp”,”Destination”);
getch();
}

Leave a Comment :, , more...

Insert Element in Array

by on Dec.15, 2010, under C & C++, DSA in C, Tutorial

//WAP to INSERT element in an array using C and algorithm
#include<stdio.h>
#include<conio.h>
main()
{
clrscr();
int a[8]={1,2,3,4,5,6,7,0},pos=0,val;
printf(“befor INSERT: “);
for(int i=0;i<8;i++){
printf(“%d”,a[i]);
}
printf(“\n Enter the value and position of the element to INSERT in array: “);
scanf(“%d %d”,&val,&pos);
printf(“after INSERT: “);
pos=pos-1;
for(i=6;i>=pos;i–)
a[i+1]=a[i] ;

a[pos]=val;
for(i=0;i<8;i++){
printf(“%d”,a[i]);
}
getch();
return 0;
}

Leave a Comment :, , more...

Matrix Multiplication

by on Dec.14, 2010, under C & C++, DSA in C, Tutorial

#include<stdio.h>
#include<conio.h>
#define N 3
main(){
clrscr();
int a[N][N]={{1,2,3},{4,5,6},{7,8,9}};
int b[N][N]={{7,8,9},{4,5,6},{1,2,3}};
int result[N][N]={{0,0,0},{0,0,0},{0,0,0}};
int i,j,k;
//display matrix a
printf(“matrix A: \n”);
for(i=0;i<N;i++) {
for(j=0;j<N;j++) {
printf(“%d\t”,a[i][j]);
}
printf(“\n”);
}
printf(“matrix B: \n”);
//display matrix b
for(i=0;i<N;i++) {
for(j=0;j<N;j++) {
printf(“%d\t”,b[i][j]);
}
printf(“\n”);
}
//calculate result
for (i=0; i<N; i++) {
for (j=0; j<N; j++) {
for (k=0; k<N; k++) {
result[i][j] += a[i][k] * b[k][j];
}
}
}
printf(“matrix Result: \n”);
for(i=0;i<N;i++) {
for(j=0;j<N;j++) {
printf(“%d\t”,result[i][j]);
}
printf(“\n”);
}
getch();
return 0;
}

Leave a Comment :, , more...

Array Update

by on Dec.14, 2010, under C & C++, DSA in C, Tutorial

//WAP to UPDATE element in an array using C and algorithm
#include<stdio.h>
#include<conio.h>
main()
{
clrscr();
int a[8]={1,2,3,4,5,6,7, },pos=0,val;
printf(“befor UPDATE: “);
for(int i=0;i<8;i++){
printf(“%d”,a[i]);
}
printf(“\n Enter the value and position of the element to UPDATE in array: “);
scanf(“%d %d”,&val,&pos);
printf(“after UPDATE: “);
pos=pos-1;
for(i=8;i<=pos;i–){
a[i+1]=a[i] ;
}
a[pos]=val;
for(i=0;i<8;i++){
printf(“%d”,a[i]);
}
getch();
return 0;
}

Leave a Comment :, , more...

Delete Element in Array

by on Dec.14, 2010, under C & C++, DSA in C, Tutorial

//WAP to DELETE element in an array using C and algorithm
#include<stdio.h>
#include<conio.h>
main()
{
clrscr();
int a[8]={1,2,3,4,5,6,7, },pos=0,val;
printf(“befor DELETE: “);
for(int i=0;i<8;i++){
printf(“%d”,a[i]);
}
printf(“\n Enter the position of the element to DELETE in array: “);
scanf(“%d”,&pos);
printf(“after DELETE: “);
pos=pos-1;
for(i=pos;i<8;i++){
a[i]=a[i+1] ;
}
for(i=0;i<8;i++){
printf(“%d”,a[i]);
}
getch();
return 0;
}

Leave a Comment :, , more...

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

Blogroll

A few highly recommended websites...