Interview Question
Country: United States
#include <iostream>
using namespace std;
char seatRows[10] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J'};
int main()
{
//bool seatAvailable[10][20] = {0};
unsigned int uiAvailibility = 200;
unsigned int uiRowID= 0;
unsigned int uiSeatID = 1;
//bool bAvailibility = true;
while (uiAvailibility != 0)
{
unsigned int uiBookCount = 0;
std::cout << endl << uiAvailibility << " Seats Available \n";
std::cout << "Enter number of seats to book; Max 20 tickets can be booked at a time \n";
std::cin >> uiBookCount;
std::cout << "Request to book " << uiBookCount <<" tickets \n";
if(uiBookCount)
{
if((uiBookCount <= uiAvailibility) && (20 >= uiBookCount))
{
unsigned int uiRowCount = uiBookCount / 20;
unsigned int uiSeatCount = uiBookCount % 20;
for(unsigned int uiCounter = 0; uiCounter < uiRowCount; uiCounter++)
{
uiAvailibility -= 20;
}
uiAvailibility -= uiSeatCount;
std::cout << "Booked Tickets are: ";
for(unsigned int uiCounter = 0; uiCounter < uiBookCount; uiCounter++)
{
if(20 >= uiSeatID)
{
std::cout << seatRows[uiRowID] << uiSeatID << ", ";
uiSeatID++;
}
else
{
uiSeatID = 1;
uiRowID++;
}
}
}
else
{
std::cout << "No Seats booked \n";
}
}
else
{
std::cout << "No Seats booked \n";
}
}
cout << " \n BOOKINGS CLOSED" << endl;
return 0;
}
#include<iostream.h>
#include<string.h>
class theatre
{
private:
char rows[10][20];
public:
theatre()
{
for(int i=0;i<10;i++)
{
for(int j=0;j<20;j++)
{
rows[i][j]='E';
}
}
}
void display()
{
char ch='A';
for(int i=0;i<10;i++)
{
cout << ch++ << " ";
for(int j=0;j<20;j++)
{
int jj=j;
if(rows[i][j]=='E')
cout << ++jj << " ";
else
cout << "R ";
}
cout << endl;
}
}
bool choose(int r, int c)
{
if(rows[r][c]=='E')
{
cout << " SEAT is booked " ;
rows[r][c]='R';
return true;
}
else
cout << " SEAT was already booked, please choose another seat " << endl;
return false;
}
};
int main()
{
int ro,co,count;
theatre th;
cout << " How many seats ? : " << endl;
cin >> count;
for(int book_count=0;book_count<count;)
{
th.display();
cout << " Choose from the above seats " << endl;
cout << " Row to be selected : " << endl ;
cin >> ro;
cout << " Column to be selected : " << endl ;
cin >> co ;
if(th.choose(ro,co))
{
book_count=book_count+1;
}
else
{
cout << " SEAT SELECTED WAS ALREADY RESERVED: " << endl << " PLEASE TRY AGAIN.!!! " << endl;
}
}
}
//DEVELOPED BY ASHISH ANI
// THEATRE SEAT RESERVATION PROGRAMME
class SeatAllocation
{
private int a[][];
private int ls[];
private char sr[]={'A','B','C','D','E','F','G','H','I','J'};
public SeatAllocation()
{
a=new int[10][20];
ls=new int[10];
for(int i=0;i<10;i++)
{
for(int j=0;j<20;j++)
a[i][j]=0;
ls[i]=20;
}
}
public static void main(String[] arg)
{
SeatAllocation sa=new SeatAllocation();
//CHANGE ACCORDING TO YOUR REQUIREMENT
sa.book(Integer.parseInt(arg[0]));
sa.book(Integer.parseInt(arg[1]));
sa.book(Integer.parseInt(arg[2]));
sa.book(Integer.parseInt(arg[3]));
sa.display();
}
public void display()
{
System.out.println("Current Seat Status : ");
for(int i=0;i<10;i++)
{
System.out.println("");
for(int j=0;j<20;j++)
{
System.out.print(" "+a[i][j]);
}
}
System.out.println("");
}
public void book(int n)
{
if(n<=20)
{
int ir=-1;
for(int i=9;i>=0;i--)
{
if(ls[i]>=n)
{ ir=i;
break;}
}
if(ir!=-1)
{
System.out.print("BOOKED SEATS ARE : ");
for(int j=0;j<n;j++)
{
System.out.print(" "+sr[ir]+""+(21-ls[ir]+j));
a[ir][20-ls[ir]+j]=1;
}
ls[ir]=ls[ir]-n;
System.out.println("");
}
else
{
System.out.println("Sorry !! Required Seats Not Available");
}
}
else
{
System.out.println("Only 20 tickets can be booked at a time");
}
}
}
USE enum to store the values from A to J
- Komal February 09, 2014enum
{A,//0 value
B,
....
J//19
Max//20
};
declare an array seat [Max][20];
seat [Max][20]={0};// initialize all elements to 0