Friday, September 5, 2008

Design and Analysis of Algorithms

//MAGIC SQUARE

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

void main()
{

int sq[50][50],sum=0;
int n,i,j,k,l,key;

clrscr();
printf("\nEnter the size (odd no.) of magic square : ");
scanf("%d",&n);

if(!(n%2))
{
printf("\nNumber is even");
printf("\nTry again with an odd number");
getch();
return;
}
else
{
for(i=0; i<n; i++)
for(j=0; j<n; j++)
sq[i][j] = 0;

sq[0][(n-1)/2] = 1;
j = (n-1)/2;

for(key=2; key <= n*n; key++)
{
k = (i)?(i-1):(n-1);
l = (j)?(j-1):(n-1);

if(sq[k][l])
i = (i+1)%n;
else
{
i = k;
j = l;
}
sq[i][j] = key;
}

for(j=0; j<n; j++)
sum+=sq[0][j];

printf("\nCommon Sum = %d\n",sum);
printf("\nMAGIC SQUARE of %dx%d is:\n",n,n);

for(i=0; i<n; i++)
{
printf("\n");
for(j=0; j<n; j++)
printf("%d\t",sq[i][j]);
}

}
getch();
}









//MATRIX ADDITION

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

void main()
{
int a[10][10],b[10][10],c[10][10];
int i, j, m, n;

clrscr();

printf("\nEnter the no. of rows and columns: ");
scanf("%d %d",&m,&n);

printf("\nEnter the elements of first matrix:\n");

for(i=0; i<m; i++)
for(j=0; j<n; j++)
scanf("%d",&a[i][j]);

printf("\nEnter the elements of second matrix:\n");

for(i=0; i<m; i++)
for(j=0; j<n; j++)
scanf("%d",&b[i][j]);

for(i=0; i<m; i++)
for(j=0; j<n; j++)
c[i][j] = a[i][j] + b[i][j];

printf("\nResultant matrix is:\n");

for(i=0; i<m; i++)
{
for(j=0; j<n; j++)
printf("%d\t",c[i][j]);
printf("\n");
}
getch();
}









//MATRIX MULTIPLICATION

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

void main()
{
int a[10][10],b[10][10],c[10][10];
int i, j, k, m1, n1, m2, n2;

clrscr();

printf("\nEnter the rows and columns for matrix I: ");
scanf("%d %d",&m1,&n1);

printf("\nEnter the rows and columns for matrix II: ");
scanf("%d %d",&m2,&n2);

if(m1!=n2)
{
printf("\nMatrix Multiplication is not possible.");
printf("\n\tTry again!");
getch();
exit();
}

printf("\nEnter the elements of matrix I:\n");

for(i=0; i<m1; i++)
for(j=0; j<n1; j++)
scanf("%d",&a[i][j]);

printf("\nEnter the elements of matrix II:\n");

for(i=0; i<m2; i++)
for(j=0; j<n2; j++)
scanf("%d",&b[i][j]);

for(i=0; i<m1; i++)
for(j=0; j<n1; j++)
for(k=0; k<n2; k++)
c[i][j] += a[i][k] * b[k][j];

printf("\nResultant matrix is:\n");

for(i=0; i<m1; i++)
{
for(j=0; j<n2; j++)
printf("%d\t",c[i][j]);
printf("\n");
}
getch();
}









//EXPONENTIATE

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

void exponentiate(float,int);

void main()
{
float x;
int n=0;
clrscr();
printf("\nEnter number(may be float) & its power(integer):");
scanf("%f %d",&x,&n);
exponentiate(x,n);
getch();

}

void exponentiate(float x,int n)
{
int m=n;
float z=x, power=1;
while(m>0)
{
while((m%2)==0)
{
m = floor(m/2);
z = z*z;
}
m = m-1;
power = power * z;
}
printf("\n%f ^ %d = %f",x,n,power);
}









//TO PRINT THE NTH TERM OF FIBONACCI SERIES

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

void main()
{
int n=0;
clrscr();
printf("\nEnter the value of n: ");
scanf("%d",&n);
if(n<=1)
{
printf("\nThe nth term of fibonacci series is : %d",n);
}
else
{
float t1=0,t2=1,t3;
int i;
for(i=2; i<=n; i++)
{
t3 = t1 + t2;
t1=t2;
t2=t3;
}
printf("\nThe nth term of fibonacci series is : %f",t3);

}

getch();
}









//TOWER OF HANOI

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

void toh(int, char, char, char);

void main()
{
int n;
printf("\nHow many disks?\t");
scanf("%d",&n);
toh(n,'A','B','C');
getch();
}

void toh(int n, char x, char y, char z)
{
if(n)
{
toh(n-1,x,z,y);
printf("\nMove top disk from tower %c ”,x);
printf(“to top of tower %c\n",y);
toh(n-1,z,y,x);
}
}