You are on page 1of 26

1. Few kids play a game forming a circular round.

Each kid is assigned a secret


number. A kid will hold the secret number of his neighbours on both sides. A kid
who is not in the circle will call out a number.The kid who is assigned the called out
number must get removed himself from the circle by exchanging the secret number
he hold which is his neihbours .He must remove such the now all the kids in the
circle will hold their neighbours secret numbers.
#include<iostream.h>
#include<conio.h>
class list
{
struct node
{
int info;
node *next;
}*head;
public:
list()
{
head=NULL;
}
void append(int data)
{
node *temp,*temp1;
if(head==NULL)
{
temp=new node;
temp->info=data;
head=temp;

temp->next=head;
}
else
{
temp=head;
while(temp->next!=head)
temp=temp->next;
temp1=new node;
temp1->info=data;
temp1->next=head;
temp->next=temp1;
}
}
void beg_insert(int data)
{
node *temp,*temp1;
temp=new node;
temp1=head;
if(temp1==NULL)
cout<<"\n Firstely create using 1 , list not exist";
else
{
while(temp1->next!=head)
temp1=temp1->next;

temp->info=data;

temp->next=head;
head=temp;
temp1->next=head;
}

}
void in_insert(int pos,int data)
{
node *temp,*t;
int a;
a=pos;
temp=head;
while(pos>1)
{
temp=temp->next;
pos--;
}
if(temp==NULL)
cout<<"\n\t firstely create using 1 , List not exist ";
else if(a==0)
beg_insert(data);

else
{
t=new node;
t->info=data;

t->next=temp->next;
temp->next=t;
}
}
void del( )
{
node *temp,*t;
temp=head;
if(temp==NULL)
cout<<"\n \tList not Exists ";
else if(temp->next==head)
{
delete temp;
head=NULL;
}
else
{
t=head->next;
while(t->next!=head)
{
t=t->next;
temp=temp->next;
}
// t=temp->next->next;
temp->next=head;
delete t;

}
}

void beg_del()
{
node *temp,*temp1;
temp=head;
if(head==NULL)
cout<<"\n\t Link list has no nodes";
else
{
temp1=head;
while(temp1->next!=head)
temp1=temp1->next;
head=temp->next;
temp1->next=head;
delete temp;
}
}
void mid_del(int pos)
{
node *temp,*t;
int a=pos;
temp=head;
while(pos>1)

{
temp=temp->next;
pos--;
}
if(a==1 )
beg_del();

else if(temp==NULL)
cout<<"\n\t List not Exist ..";
else
{
t=temp->next;
temp->next=temp->next->next;
delete t;
}
}

void show()
{
node *temp;
temp=head;
cout<<"\n\t";
if(temp==NULL)
cout<<"\n \tList not exist";
cout<<temp->info;
temp=temp->next;

while(temp!=head)
{
cout<<" - "<<temp->info;
temp=temp->next;
}
}
};
void main()
{
list l;
clrscr();
int ch,pos;
while(1)
{
cout<<"\n\t\tENter your Choice : ";
cout<<"\n 1: To append\n 2: To insert at Begining\n 3: To insert at any
position";
cout<<"\n 4: To delete\n 5: To del at begining\n 6: To Delete at any position
";
cout<<"\n 7: To show\n 8: To exit ";
cin>>ch;
if(ch==1)
{
cout<<"\n\t Enter data to append : ";
cin>>ch;
l.append(ch);
}

else if(ch==2)
{
cout<<"\n\t Enter element to insert at begining : " ;
cin>>ch;
l.beg_insert(ch);
}
else if(ch==3)
{
cout<<"\nEnter element to insert at specific position : ";
cin>>ch;
cout<<"\n Enter position : ";
cin>>pos;
l.in_insert(pos,ch);
}
else if(ch==4)
l.del();
else if (ch==5)
l.beg_del();
else if(ch==6)
{
cout<<"\n Enter position where u have to delete";
cin>>pos;
l.mid_del(pos);
}
else if(ch==7)
l.show();

else if(ch==8)
{
cout<<"\n Press Enter to Exit";
break;
}
else
cout<<"\n\t Wrong Choice ";
}
getch();
}

2. You are given a number of

queens to place it in a chessboard.You already

know ,the queen is the most powerful piece in the game of chess, able to move any
number of squares vertically, horizontally, or diagonally. The condition given is that
no two queens share the same row, column, or diagonal.The scenario is already
solved but certain errors in the code make it unexecutable. Find the errors so that
you could place the n queens according to the conditons above.
#include<stdio.h>
#include<conio.h>
#include<math.h>
int a[30],count=0;
int place(int pos)
{
int i;
for(i=1;i<pos;i++)
{
if((a[i]==a[pos])||((abs(a[i]-a[pos])==abs(i-pos))))
return 0;
}
return 1;
}
void print_sol(int n)
{
int i,j;
count++;
printf("\n\nSolution #%d:\n",count);
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)

if(a[i]==j)
printf("Q\t");
else
printf("*\t");

}
printf("\n");

}
}
void queen(int n)
{
int k=1;
a[k]=0;
while(k!=0)
{
a[k]=a[k]+1;
while((a[k]<=n)&&!place(k))
a[k]++;
if(a[k]<=n)
{
if(k==n)
print_sol(n);
else
{
k++;
a[k]=0;
}
}
else
k--;
}
}
void main()
{
int i,n;
clrscr();
printf("Enter the number of Queens\n");
scanf("%d",&n);
queen(n);
printf("\nTotal solutions=%d",count);
getch();
}

3.

5 people sitting at a round table doing 2 things, eating and thinking. While eating they are not thinking and

while thinking they are not eating. Each people have plates, that is 5 plates. And there is fork places between each
pair of adjacent people, that is total of 5 forks. Each philosopher need 2 forks to eat. Each one can only use the forks
on his immediate left and immediate right. The solution for the problem is given above bt it doesnt seem to work
.Find the errors if there is any or add or delete statements to make the code executable.

#include<stdio.h>
#include<semaphore.h>
#include<pthread.h>
#define N 5

#define
#define
#define
#define
#define

THINKING 0
HUNGRY 1
EATING 2
LEFT (ph_num+4)%N
RIGHT (ph_num+1)%N

sem_t mutex;
sem_t S[N];
void * phil(void *num);
void take_fork(int);
void put_fork(int);
void test(int);
int state[N];
int phil_num[N]={0,1,2,3,4};
int main()
{
int i;
pthread_t thread_id[N];
sem_init(&mutex,0,1);
for(i=0;i<N;i++)
sem_init(&S[i],0,0);
for(i=0;i<N;i++)
{
pthread_create(&thread_id[i],NULL,phil,&phil_num[i]);
printf("People %d is thinking\n",i+1);
}
for(i=0;i<N;i++)
pthread_join(thread_id[i],NULL);
}
void *phil(void *num)
{
while(1)
{
int *i = num;
sleep(1);
take_fork(*i);
sleep(0);
put_fork(*i);
}
}
void take_fork(int ph_num)
{
sem_wait(&mutex);
state[ph_num] = HUNGRY;
printf("People %d is Hungry\n",ph_num+1);
test(ph_num);
sem_post(&mutex);
sem_wait(&S[ph_num]);
sleep(1);
}
void test(int ph_num)

if (state[ph_num] == HUNGRY && state[LEFT] != EATING && state[RIGHT] !=


EATING)
{
state[ph_num] = EATING;
sleep(2);
printf("People %d takes fork %d and %d\n",ph_num+1,LEFT+1,ph_num+1);
printf("People %d is Eating\n",ph_num+1);
sem_post(&S[ph_num]);
}
}
void put_fork(int ph_num)
{
sem_wait(&mutex);
state[ph_num] = THINKING;
printf("People %d putting fork %d and %d
down\n",ph_num+1,LEFT+1,ph_num+1);
printf("People %d is thinking\n",ph_num+1);
test(LEFT);
test(RIGHT);
sem_post(&mutex);
}

4.

Here is a game whre youhave a 5*5 square whre numbers are arranged randomly from 1 to 8

#include <iostream>
#include <ctime>
#include <cstdlib>
#include <iomanip>
#include <string>
using namespace std;
const int csize = 5;
void fillCard ( int card [ ] [csize] );
void displayCard ( int card [][csize] );
char drawNum ( int & num, bool checkAray[] );
string check4match ( int num, int cardAray [][csize] );
string check4win ( int cArray [][csize] );
//********************************* MAIN FUNCTION ******************************
int main ( )
{
int bingoCard[csize][csize] = {{0},{0}};
bool chkArray [76] = { false };
int row, col, num ;
bool noWin = true ;
string win ;
srand( time ( NULL ) ) ;

fillCard( bingoCard );
while ( noWin )
{
system("cls");
displayCard( bingoCard );
cout << " " << drawNum( num, chkArray ) << " " << num;
cout << endl << endl;
cout << " " << check4match( num, bingoCard ) << endl << endl ;
win = check4win( bingoCard );
system("pause");
if ( win == "BINGO!!" )
{
system("cls");
noWin = false;
cout << endl << setw(16) << win << endl << endl ;
displayCard( bingoCard );
cout << endl << endl ;
}
else
noWin = true;
}
system("pause");
return 0;
}
//***************************** END of Main ***********************************
//*************************** FillCard Function *******************************
void fillCard ( int card [] [csize] )
{
int num1;
bool flag;
for ( int col = 0; col < csize ; col++ )
{
for ( int row = 0; row < csize ; row++ )
{

do{
flag = false;
num1 = rand ()% 15 + 1 + (15 * col);

for(int check = 0; check < row; check++)


{
if (num1 == card[check][col])
{
flag = true;
}
}
}while (flag);
card[col][row] = num1;
card[2][2] = 0;
}
}
}

//**************************END of FILLCARD ***********************************


//************************ DisplayCard Function *******************************
void displayCard ( int card [][csize] )
{
for ( int col = 0 ; col < csize ; col++ )
{
for ( int row = 0; row < csize ; row++ )
cout <<setw(5)<<card[row][col];
cout<<endl;
}
cout << "The displayCard function has not been defined yet." << endl;
}
//****************************** END of DISPLAYCARD ****************************
//******************************* DrawNum Function *****************************
char drawNum ( int & num, bool checkAray[] )
{
num = 0;
num = rand () % 75 + 1;
checkAray[num] = true;
if ( num > 0 && num < 16 && checkAray[num] == true)
return 'B';

if (num >= 16 && num < 31 && checkAray[num] == true)


return 'I';
if (num >= 31 && num < 46 && checkAray[num] == true)
return 'N';
if (num >= 46 && num < 61 && checkAray[num] == true)
return 'G';
if (num >= 61 && num < 76 && checkAray[num] == true)
return 'O';

}
//******************************END of DRAWNUM ********************************
//**************************** check4match Function ***************************
string check4match ( int num, int cardAray [][csize])
{
int col = 0;
if (num > 0 && num < 16)
col = 0;
else if (num >=16 && num > 31)
col = 1;
else if (num >=31 && num > 46)
col = 2;
else if (num >= 46 && num > 61)
col = 3;
else if (num >= 61 && num > 76)
col = 4;
return "Match check has not been defined yet." ;
}
//************************* END of CHECK4MATCH ********************************
//************************* check4win Function ********************************
string check4win ( int cArray [][csize])
{
int count = 0;
for(int row =0; row < 5; row ++)
{
for(int col =0; col < 5; col++)
{
if(cArray[row][col]==0)

{
count++;
}
if(count==5)
{
return "BINGO!!";
}
}
count = 0;
}
count = 0;

for(int col = 0; col < 5; col ++)


{
for(int row =0; row < 5; row++)
{
if(cArray[row][col]==0)
{
count++;
}
if(count==5)
{
return "BINGO!!";
}
}
count = 0;
}

count = 0;
int rowNum = 0;
for(int col =0; col < 5; col++)
{
if(cArray[col][rowNum]==0)
{
count++;
}
if(count==5)
{
return "BINGO!!";
}
rowNum++;
}
int rowNum2 = 4;
count = 0;
for(int col = 4; col > -1; col--)
{
if(cArray[col][rowNum2]==0)

{
count++;
}
if(count==5)
{
return "BINGO!!";
}
rowNum2 --;
}
return "NOT BINGO!!" ;

return "BINGO!!" ;
}

5.

Lets solve a a mathematical game or puzzle. You have three rods, and a
number of disks of different sizes which can slide onto any rod. The puzzle
starts with the disks in a neat stack in ascending order of size on one rod, the
smallest at the top, thus making a conical shape.The objective of the puzzle
is to move the entire stack to another rod, obeying the following simple
rules:Only one disk can be moved at a time.Each move consists of taking the
upper disk from one of the stacks and placing it on top of another stack i.e. a
disk can only be moved if it is the uppermost disk on a stack.No disk may be
placed on top of a smaller disk. Errors occur while running the program.Remove the
errors so you could solve the puzzle.

import java.util.*;
public class puzzle
{
public static int N;

public static Stack<Integer>[] tower = new Stack[4];

public static void main(String[] args)

{
Scanner scan = new Scanner(System.in);
tower[1] = new Stack<Integer>();
tower[2] = new Stack<Integer>();
tower[3] = new Stack<Integer>();

System.out.println("Enter number of disks");


int num = scan.nextInt();
N = num;
toh(num);
}

public static void puzz(int n)


{
for (int d = n; d > 0; d--)
tower[1].push(d);
display();
move(n, 1, 2, 3);
}

public static void move(int n, int a, int b, int c)


{
if (n > 0)
{
move(n-1, a, c, b);
int d = rod[a].pop();

rod[c].push(d);
display();
move(n-1, b, a, c);
}
}

public static void display()


{
System.out.println(" A | B | C");
System.out.println("---------------");
for(int i = N - 1; i >= 0; i--)
{
String d1 = " ", d2 = " ", d3 = " ";
try
{
d1 = String.valueOf(rod[1].get(i));
}
catch (Exception e){
}
try
{
d2 = String.valueOf(rod[2].get(i));
}
catch(Exception e){
try
{

d3 = String.valueOf(tower[3].get(i));
}
catch (Exception e){
}
System.out.println(" "+d1+" | "+d2+" | "+d3);
}
System.out.println("\n");
}
}

6. This is a game for two players, X and O, who take turns marking the spaces in a 33 grid.
The player who succeeds in placing three respective marks in a horizontal, vertical, or diagonal
row wins the game.
The following example game is won by the first player, X:

#include <iostream.h>
#include <conio.h>

char square[10] = {'o','1','2','3','4','5','6','7','8','9'};


int checkwin();
void board();

int main()
{
int player = 1,i,choice;

char mark;
clrscr();
do
{
board();
player=(player%2)?1:2;
cout << "Player " << player << ", enter a number: ";
cin >> choice;
mark=(player == 1) ? 'X' : 'O';
if (choice == 1 && square[1] == '1')
square[1] = mark;
else if (choice == 2 && square[2] == '2')
square[2] = mark;
else if (choice == 3 && square[3] == '3')
square[3] = mark;
else if (choice == 4 && square[4] == '4')
square[4] = mark;
else if (choice == 5 && square[5] == '5')
square[5] = mark;
else if (choice == 6 && square[6] == '6')
square[6] = mark;
else if (choice == 7 && square[7] == '7')
square[7] = mark;
else if (choice == 8 && square[8] == '8')
square[8] = mark;
else if (choice == 9 && square[9] == '9')

square[9] = mark;
else
{
cout<<"Invalid move ";
player--;
getch();
}
i=checkwin();
player++;
}while(i==-1);
board();
if(i==1)
cout<<"==>\aPlayer "<<--player<<" win ";
else
cout<<"==>\aGame draw";
getch();
return 0;
}
7. A medical representative has his target at many places in the city. He has to visit
every places to complete his work for the day.His focuss would be to visit all places
at minimum cost .He must visit all places without traveeling through the the same
route already traveled by him..The program executes giving some errors.Correct the
errors so you could help the medical rep.

import java.util.*;
import java.text.*;
class TSP
{
int weight[][],n,tour[],finalCost;
final int INF=1000;
public TSP()

{
Scanner s=new Scanner(System.in);
System.out.println("Enter no. of nodes:=>");
n=s.nextInt();
weight=new int[n][n];
tour=new int[n-1];
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
if(i!=j)
{
System.out.print("Enter weight of "+(i+1)+" to "+(j+1)+":=>");
weight[i][j]=s.nextInt();
}
}
}
System.out.println();
System.out.println("Starting node assumed to be node 1.");
eval();
}
public int COST(int currentNode,int inputSet[],int setSize)
{
if(setSize==0)
return weight[currentNode][0];
int min=INF,minindex=0;
int setToBePassedOnToNextCallOfCOST[]=new int[n-1];
for(int i=0;i<setSize;i++)
{
int k=0;
for(int j=0;j<setSize;j++)
{
if(inputSet[i]!=inputSet[j])
setToBePassedOnToNextCallOfCOST[k++]=inputSet[j];
}
int temp=COST(inputSet[i],setToBePassedOnToNextCallOfCOST,setSize1);
if((weight[currentNode][inputSet[i]]+temp) < min)
{

min=weight[currentNode][inputSet[i]]+temp;
minindex=inputSet[i];
}
}
return min;
}
public int MIN(int currentNode,int inputSet[],int setSize)
{
if(setSize==0)
return weight[currentNode][0];
int min=INF,minindex=0;
int setToBePassedOnToNextCallOfCOST[]=new int[n-1];
for(int i=0;i<setSize;i++)
{
int k=0;
for(int j=0;j<setSize;j++)
{
if(inputSet[i]!=inputSet[j])
setToBePassedOnToNextCallOfCOST[k++]=inputSet[j];
}
int temp=COST(inputSet[i],setToBePassedOnToNextCallOfCOST,setSize1);
if((weight[currentNode][inputSet[i]]+temp) < min)
{
min=weight[currentNode][inputSet[i]]+temp;
minindex=inputSet[i];
}
}
return minindex;
}
public void eval()
{
int dummySet[]=new int[n-1];
for(int i=1;i<n;i++)
dummySet[i-1]=i;
finalCost=COST(0,dummySet,n-1);
constructTour();
}
public void constructTour()
{

int previousSet[]=new int[n-1];


int nextSet[]=new int[n-2]; for(int i=1;i<n;i++)
previousSet[i-1]=i;
int setSize=n-1;
tour[0]=MIN(0,previousSet,setSize);
for(int i=1;i<n-1;i++)
{
int k=0;
for(int j=0;j<setSize;j++)
{
if(tour[i-1]!=previousSet[j])
nextSet[k++]=previousSet[j];
}
--setSize;
tour[i]=MIN(tour[i-1],nextSet,setSize);
for(int j=0;j<setSize;j++)
previousSet[j]=nextSet[j];
}
display();
}
public void display()
{
System.out.println();
System.out.print("The tour is 1-");
for(int i=0;i<n-1;i++)
System.out.print((tour[i]+1)+"-");
System.out.print("1");
System.out.println();
System.out.println("The final cost is "+finalCost);
}
}
class TSPExp
{
public static void main(String args[])
{
TSP obj=new TSP();
}
}

You might also like