Tuesday, 25 June 2013

A matchstick game



Q.Matchstick game being played between the computer and a user. Your program should ensure that the
computer always wins. Rules for the game are as follows:
− There are 21 matchsticks.
− The computer asks the player to pick 1, 2, 3, or 4 matchsticks.
− After the person picks, the computer does its picking.
− Whoever is forced to pick up the last matchstick loses the game. 


   #include<stdio.h>
   #include<conio.h>
void main()
{
int ms=21,ply,sys=0;
clrscr();
fflush(stdin);
printf("Select between 1-4\n otherwise computer wins\n");
while(ms>=1)
{
   if(ms==1)
    {
       printf("Computer wins\n");
       break;
    }
printf("Avilable Matchsticks = %d\n",ms);
printf("Player= ");
scanf("%d",&ply);
printf("\nComputer= %d\n",sys=5-ply);
ms=ms-sys-ply;
}
getch();
}

Monday, 24 June 2013

How to shuffle the array elements..?

#include<iostream>
#include<stdlib.h>
#include<time.h>
using namespace std;
int main()
{
int size,i;

cout<<"Enter the size of array:";
cin>>size;

int a[20];
cout<<"\nEnter the elements:";

for(i=0;i<size;i++)
cin>>a[i];

srand(time(0));
int k,t;


for(i=0;i<size;i++)
{
k=rand()%size;
t=a[i];        //a[i]=a[i]*a[k];
a[i]=a[k];     //a[k]=a[i]/a[k];
a[k]=t;        //a[i]=a[i]/a[k];
}

cout<<"\nShuffeled array:";
for(i=0;i<size;i++)
cout<<a[i]<<"\t";
cout<<endl;
return 0;
}