Hi, I have a small doubt in C++ assignment. I have been assigned to do seat reservation in a certain foodcourt. I have already done the seat reservation thing using basic array and function. But my program can only do manual seat reservation. Which means the user gets to choose whichever seat he wants. But my lect wants it to be automatic as in like the prog will assign the user with the seat number when he says for e.g. "Seat for 5"... So I am confused in how to start with...how should be using the array to automatically assign the seat numbers? Please help me...Thanks a ton.

My code is reflected below:

#include <iostream>
using namespace std;

int seat,num,num1;
string table[55]={"0","  1 "," 2  ","  3 "," 4  ","  5 "," 6  ","  7 "," 8  ","  9 "," 10 "," 11 "," 12 "," 13 "," 14 "," 15 "," 16 "," 17 "," 18 "," 19 "," 20 "," 21 "," 22 "," 23 "," 24 "," 25 "," 26 "," 27 "," 28 "," 29 "," 30 "," 31 "," 32 "," 33 "," 34 "," 35 "," 36 "," 37 "," 38 "," 39 "," 40 "," 41 "," 42 "," 43 "," 44 "," 45 "," 46 "," 47 "," 48 "," 49 "," 50 "," 51 "," 52 "," 53 "," 54 "};
void Selectseat(string list[],int arraysize);
void Deselectseat(string list[],int arraysize);
void DisplayFC(void);

void Selectseat(string list[],int arraysize)
{
    for(int i=0;i<seat;i++)
    {
        cout<<"Select the seat number that you want :";
        cin>>num;
        if(num==2 || num==4 || num==6 || num==8 || num==10 || num==12)
        {
            list[num]=" O  ";
        }
        else
        {
            list[num]="  O ";
        }
    }
}

void Deselectseat(string list[],int arraysize)
{
    for(int j=0;j<seat;j++)
    {
        string number[55]={"0","  1 "," 2  ","  3 "," 4  ","  5 "," 6  ","  7 "," 8  ","  9 "," 10 "," 11 "," 12 "," 13 "," 14 "," 15 "," 16 "," 17 "," 18 "," 19 "," 20 "," 21 "," 22 "," 23 "," 24 "," 25 "," 26 "," 27 "," 28 "," 29 "," 30 "," 31 "," 32 "," 33 "," 34 "," 35 "," 36 "," 37 "," 38 "," 39 "," 40 "," 41 "," 42 "," 43 "," 44 "," 45 "," 46 "," 47 "," 48 "," 49 "," 50 "," 51 "," 52 "," 53 "," 54 "};
        cout<<"Enter the seat number that you sit just now : ";
        cin>>num;
        list[num]=number[num];
    }
}

void DisplayFC(void)
{
    cout<<"   "<<table[1]<<"|"<<table[2]<<"      "<<table[5]<<"|"<<table[6]<<"       "<<table[9]<<"|"<<table[10]<<endl;
    cout<<"    -------"<<"        -------"<<"        ---------"<<endl;
    cout<<"   "<<table[3]<<"|"<<table[4]<<"      "<<table[7]<<"|"<<table[8]<<"       "<<table[11]<<"|"<<table[12]<<endl;
    cout<<"    Table 1"<<"        Table 2"<<"        Table 3"<<endl;
    cout<<endl;
    cout<<"    "<<table[13]<<"|"<<table[14]<<"|"<<table[15]<<"      "<<table[19]<<"|"<<table[20]<<"|"<<table[21]<<"      "<<table[25]<<"|"<<table[26]<<"|"<<table[27]<<endl;
    cout<<"    --------------"<<"      --------------"<<"      --------------"<<endl;
    cout<<"    "<<table[16]<<"|"<<table[17]<<"|"<<table[18]<<"      "<<table[22]<<"|"<<table[23]<<"|"<<table[24]<<"      "<<table[28]<<"|"<<table[29]<<"|"<<table[30]<<endl;
    cout<<"    Table 4"<<"             Table 5"<<"             Table 6"<<endl;
    cout<<endl;
    cout<<"    "<<table[31]<<"|"<<table[32]<<"|"<<table[33]<<"|"<<table[34]<<"    "<<table[39]<<"|"<<table[40]<<"|"<<table[41]<<"|"<<table[42]<<"    "<<table[47]<<"|"<<table[48]<<"|"<<table[49]<<"|"<<table[50]<<endl;
    cout<<"    -------------------"<<"    -------------------"<<"    -------------------"<<endl;
    cout<<"    "<<table[35]<<"|"<<table[36]<<"|"<<table[37]<<"|"<<table[38]<<"    "<<table[43]<<"|"<<table[44]<<"|"<<table[45]<<"|"<<table[46]<<"    "<<table[51]<<"|"<<table[52]<<"|"<<table[53]<<"|"<<table[54]<<endl;
    cout<<"    Table 7"<<"                Table 8"<<"                Table 9"<<endl;
    return;
}

int main()
{
    do
    {
        system("cls");
        cout<<endl;
        cout<<"Welcome to FC4"<<endl;
        cout<<"--------------"<<endl;
        cout<<endl;
        cout<<"FC4 Layout"<<endl;
        cout<<endl;
        cout<<"O means the seat is occupied"<<endl;
        cout<<"Please wait or go to other FC if all the table shown O."<<endl;
        cout<<"Bags are not allow to occupy seats"<<endl;
        cout<<endl;
        DisplayFC();
        cout<<endl;
        cout<<"Press 1 for selecting seats,Press 2 for deselecting seats : ";
        cin>>num1;
        cout<<endl;
        if(num1==1)
        {
            cout<<"Number of Seats you need : ";
            cin>>seat;
            cout<<endl;
            DisplayFC();
            cout<<endl;
            Selectseat(table,54);
        }
        else
        {
            cout<<"Number of people who leaving : ";
            cin>>seat;
            cout<<endl;
            Deselectseat(table,54);
        }
    }while(1);
    return 0;
}

Dani AI

Generated

Brief answer: keep a simple occupancy map separate from your display strings, then search each table for a run of K adjacent free seats. For a request like "Seat for 5" check each table (or logical row) and assign the first or best-fitting contiguous block you find. This is exactly the sequential/search idea mentioned, but scoped to table boundaries so groups stay together.

Suggested approach in steps:

  1. Keep a parallel boolean array (or vector<bool>) where false = free, true = occupied. Also keep a list of table ranges (start,end indices) that define which seat indices belong to each table.
  2. For a request K, iterate tables and run a contiguous-seat search (sliding window) inside each table range. Assign when you find a run of length K.
  3. If no single-table run exists, decide policy: fail, split across tables, or try to find nearest grouped seats. Splitting is a fallback only if your requirements allow it.

Minimal C++-style helper (1-based or 0-based indices as you choose):

int find_contiguous(const vector<bool>& occ, int start, int end, int k) {
    int run = 0;
    for (int i = start; i <= end; ++i) {
        if (!occ[i]) { if (++run == k) return i - k + 1; }
        else run = 0;
    }
    return -1;
}

After assignment mark occ[i]=true for the range and update your display array.

Notes and cautions: watch off-by-one errors (your display may use 1-based labels), validate K against maximum table sizes, and choose a fit policy (first-fit vs best-fit). If this runs in a multi-user context persist data and lock assignments to avoid races. If you still want randomness (as mentioned), apply it only to pick between multiple valid contiguous blocks, not to ignore contiguity.

Recommended Answers

All 5 Replies

1) Ask user if he wants a seat, if so then
2) Check if there are seats to fill
3) If full, then give an error, else
4) get any random seat from the list of unfilled seat array

1) Ask user if he wants a seat, if so then
2) Check if there are seats to fill
3) If full, then give an error, else
4) get any random seat from the list of unfilled seat array

Hi, but i forgot to state i can't assign any random seat... Let's say the user inputs he wants 6 seats we have to try and assign him to the table that has 6 seats together or at least side by side... Btw if i wanna do your 4) statement what is command I must use?Thanks

rand() will give you random number from 0 to 0x7fff.
You can manipulate rand the give you what you want. To get random
number every time you will need to seed the random number
generator.

If you can't use random number then try something like this :
1) Get user input for how many seats
2) From 0 till seatRecivedCounter reaches seat, search the
array for empty seat.

That would be sequential.

rand() will give you random number from 0 to 0x7fff.
You can manipulate rand the give you what you want. To get random
number every time you will need to seed the random number
generator.

If you can't use random number then try something like this :
1) Get user input for how many seats
2) From 0 till seatRecivedCounter reaches seat, search the
array for empty seat.

That would be sequential.

Hi...Thanks for the constant help...But need to clarify more things...sorry...

I'm not too sure regarding ur 2) From 0 till seatRecivedCounter reaches seat, search the array for empty seat....Is it I must have a new counter to check the Input "Seat" the user type and compare it with seatRecivedCounter? After tt assign the seat number to the user...

I mean ask the user for input, and then have a counter variable
that determines how many seats the user has gotten. If none
then search from Array[0] to Array[MAX], until the counter has reached
the needed number of seats.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.