Contact Me
Browse » Home » Archives for November 2011
josephus problem solver for generalized n
#include<stdio.h>
int main()
{
int n1,n2,s=0,cnt,i,k;
printf("\n enter how many persons\n");
scanf("%d",&n1);
int a[n1];
k=n1;
for(i=0;i<n1;i++)
a[i]=i+1;
printf("\n enter every which\n");
scanf("%d",&n2);
while(k>1)
{
cnt=0;
while(cnt<n2)
{
if(a[s]!=0)
cnt+=1;
if(cnt!=n2)
s+=1;
if(s==n1)
s=0;
}
a[s]=0;
k=k-1;
}
for(i=0;i<n1;i++)
{
if(a[i]!=0)
printf("\n The %dth person is saved",i+1);
}
getch();
}
[Read More...]
int main()
{
int n1,n2,s=0,cnt,i,k;
printf("\n enter how many persons\n");
scanf("%d",&n1);
int a[n1];
k=n1;
for(i=0;i<n1;i++)
a[i]=i+1;
printf("\n enter every which\n");
scanf("%d",&n2);
while(k>1)
{
cnt=0;
while(cnt<n2)
{
if(a[s]!=0)
cnt+=1;
if(cnt!=n2)
s+=1;
if(s==n1)
s=0;
}
a[s]=0;
k=k-1;
}
for(i=0;i<n1;i++)
{
if(a[i]!=0)
printf("\n The %dth person is saved",i+1);
}
getch();
}
this is for those who prepare for gmat
[Read More...]
best youtube channels for developing your computer knowledge from the scratch
C program for playing tic tac toe
#include<stdio.h>
void assign(int **arr, int k)
{
int i,j;
for(i=0;i<k;i++)
{
for(j=0;j<k;j++)
{
arr[i][j]=0;
}
}
}
void display(int **arr, int k)
{
int i,j;
for(i=0;i<k;i++)
{
for(j=0;j<k;j++)
{
printf("%dt",arr[i][j]);
}
printf("n");
}
}
int input(int **arr,int m)
{
int k,i,j;
k=1;
scanf("%d%d",&i,&j);
if(arr[i][j]!=0)
{
printf("n the position has already occupiedn");
}
else
{
if(m%2==0)
{
arr[i][j]=1;
}
else
{
arr[i][j]=2;
}
k=0;
}
return k;
}
int play(int **arr, int k,int r)
{
int m=1;
printf("n please enter row(i) and column(j)>0 and <%d",k);
if(r%2==0)
{
printf("n player 1 enter your positionn");
}
else
{
printf("n player 2 enter your positionn");
}
while(m)
{
m=input(arr,r);
}
r=r+1;
return r;
}
int check(int **arr, int k)
{
int i,j,m=1,t,r=0;
int cnt1,cnt2,cnt3,cnt4;
while(m<=2)
{
cnt1=0,cnt2=0,cnt3=0,cnt4=0;
for(i=0;i<k;i++)
{
cnt1=0,cnt2=0;
for(j=0;j<k;j++)
{
if(arr[i][j]==m)
{
cnt1+=1;
}
if(arr[j][i]==m)
{
cnt2+=1;
}
if(i==j)
{
if(arr[i][j]==m)
{
cnt3+=1;
}
} //end of i==j
if((i+j)==(k-1))
{
if(arr[i][j]==m)
{
cnt4+=1;
}
} //end of i+j
} //end of j loop
printf("n value of m =%d",m);
printf("n cnt1=%d cnt2=%d cnt3=%d cnt4=%dn",cnt1,cnt2,cnt3,cnt4);
if((cnt1==k)||(cnt2==k)||(cnt3==k)||(cnt4==k))
{
r=1;
t=1;
break;
}
else
{
t=0;
}
} //end of i loop
// printf("n value of m =%dn",m);
if(r==1)
{
break;
}
m=m+1;
} //end of while
if(m==3)
{
m=m-1;
}
if(t==1)
{
return m+t;
}
else
{
return t;
}
}
int main()
{
int n,move=0,flag=0;
printf("Tic Tac Toe gamen");
printf("please enter the odd no for which square matrix has to be generatedn");
scanf("%d",&n);
if(n%2==0)
{
printf("please enter the valid odd no >0");
}
else
{
int i,j;
int **a=(int **)malloc(n*sizeof(int *));
for( i=0;i<n;i++)
{
a[i]=(int *)malloc(n*sizeof(int));
}
assign(a,n);
while(move<(n*n))
{
display(a,n);
move=play(a,n,move);
if(move>2)
flag=check(a,n);
printf("n flag value is %dn",flag);
if(flag!=0)
break;
}
display(a,n);
printf("n flag value is %dn",flag);
if(flag==0)
printf("n The game is draw");
else if(flag==2)
printf("n The player 1 has won the game");
else
printf("n The player 2 has won the game");
}
getch();
}
[Read More...]
void assign(int **arr, int k)
{
int i,j;
for(i=0;i<k;i++)
{
for(j=0;j<k;j++)
{
arr[i][j]=0;
}
}
}
void display(int **arr, int k)
{
int i,j;
for(i=0;i<k;i++)
{
for(j=0;j<k;j++)
{
printf("%dt",arr[i][j]);
}
printf("n");
}
}
int input(int **arr,int m)
{
int k,i,j;
k=1;
scanf("%d%d",&i,&j);
if(arr[i][j]!=0)
{
printf("n the position has already occupiedn");
}
else
{
if(m%2==0)
{
arr[i][j]=1;
}
else
{
arr[i][j]=2;
}
k=0;
}
return k;
}
int play(int **arr, int k,int r)
{
int m=1;
printf("n please enter row(i) and column(j)>0 and <%d",k);
if(r%2==0)
{
printf("n player 1 enter your positionn");
}
else
{
printf("n player 2 enter your positionn");
}
while(m)
{
m=input(arr,r);
}
r=r+1;
return r;
}
int check(int **arr, int k)
{
int i,j,m=1,t,r=0;
int cnt1,cnt2,cnt3,cnt4;
while(m<=2)
{
cnt1=0,cnt2=0,cnt3=0,cnt4=0;
for(i=0;i<k;i++)
{
cnt1=0,cnt2=0;
for(j=0;j<k;j++)
{
if(arr[i][j]==m)
{
cnt1+=1;
}
if(arr[j][i]==m)
{
cnt2+=1;
}
if(i==j)
{
if(arr[i][j]==m)
{
cnt3+=1;
}
} //end of i==j
if((i+j)==(k-1))
{
if(arr[i][j]==m)
{
cnt4+=1;
}
} //end of i+j
} //end of j loop
printf("n value of m =%d",m);
printf("n cnt1=%d cnt2=%d cnt3=%d cnt4=%dn",cnt1,cnt2,cnt3,cnt4);
if((cnt1==k)||(cnt2==k)||(cnt3==k)||(cnt4==k))
{
r=1;
t=1;
break;
}
else
{
t=0;
}
} //end of i loop
// printf("n value of m =%dn",m);
if(r==1)
{
break;
}
m=m+1;
} //end of while
if(m==3)
{
m=m-1;
}
if(t==1)
{
return m+t;
}
else
{
return t;
}
}
int main()
{
int n,move=0,flag=0;
printf("Tic Tac Toe gamen");
printf("please enter the odd no for which square matrix has to be generatedn");
scanf("%d",&n);
if(n%2==0)
{
printf("please enter the valid odd no >0");
}
else
{
int i,j;
int **a=(int **)malloc(n*sizeof(int *));
for( i=0;i<n;i++)
{
a[i]=(int *)malloc(n*sizeof(int));
}
assign(a,n);
while(move<(n*n))
{
display(a,n);
move=play(a,n,move);
if(move>2)
flag=check(a,n);
printf("n flag value is %dn",flag);
if(flag!=0)
break;
}
display(a,n);
printf("n flag value is %dn",flag);
if(flag==0)
printf("n The game is draw");
else if(flag==2)
printf("n The player 1 has won the game");
else
printf("n The player 2 has won the game");
}
getch();
}
C program for odd order magic squares generalized
#include<stdio.h>
void main()
{
int n;
// int i=0,j,k=1,a[15][15];
//clrscr();
printf("n Enter any odd number>1 t");
scanf("%d",&n);
if(n%2==0)
{
printf("This program works only for odd numbersn");
}
else
{
int i=0,j,k=1,a[n][n];
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
a[i][j]=0;
}
}
i=0;
j=n/2;
a[i][j]=k;
while(k<(n*n))
{
i=i-1;
j=j+1;
if((i<0)&&(j>=n))
{
j=j-1;
i=i+2;
}
else if(i<0)
{
i=n-1;
}
else if(j>=n)
{
j=0;
}
// else
// {
if(a[i][j]!=0)
{
j=j-1;
i=i+2;
}
k=k+1;
a[i][j]=k;
// }
}
printf("n the magic squares for given order isn");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
printf("%dt",a[i][j]);
}
printf("n");
}
}
getch();
}
[Read More...]
void main()
{
int n;
// int i=0,j,k=1,a[15][15];
//clrscr();
printf("n Enter any odd number>1 t");
scanf("%d",&n);
if(n%2==0)
{
printf("This program works only for odd numbersn");
}
else
{
int i=0,j,k=1,a[n][n];
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
a[i][j]=0;
}
}
i=0;
j=n/2;
a[i][j]=k;
while(k<(n*n))
{
i=i-1;
j=j+1;
if((i<0)&&(j>=n))
{
j=j-1;
i=i+2;
}
else if(i<0)
{
i=n-1;
}
else if(j>=n)
{
j=0;
}
// else
// {
if(a[i][j]!=0)
{
j=j-1;
i=i+2;
}
k=k+1;
a[i][j]=k;
// }
}
printf("n the magic squares for given order isn");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
printf("%dt",a[i][j]);
}
printf("n");
}
}
getch();
}
Subscribe to:
Posts (Atom)