Here is the OOP Assignment No 01 for Dynamic Arrays
Question:
Write a C++ program using a dynamic array (or arrays) to assign passengers seats in a Bus and your program will ask the user how many rows the Bus has and will handle that many rows (Assume the Bus does not always have the same rows).
Expected output: Assume a small Bus with seat numbering as follows:
1 A B C D
2 A B C D
3 A B C D
4 A B C D
5 A B C D
6 A B C D
7 A B C D
8 A B C D
9 A B C D
10 A B C D
The program should display the seat pattern, with an X marking the seats already assigned. For example, after seats 1A, 2B, and 4C are taken, the display should look like this:
1 X B C D
2 A X C D
3 A B C D
4 A B X D
5 A B C D
6 A B C D
7 A B C D
8 A B C D
9 A B C D
10A B C D
After displaying the seats available, the program prompts for the desired seat, the user types in a seat specified by row number and column entry separately. Program should then validate the specified seat number, display appropriate error message in case of wrong/invalid seat number, and then update and display the revised seats plan after reserving the correctly specified seat number. This continues until all seats are filled or until the user signals that the program should end. If the user types in a seat that is already assigned, the program should say that the seat is occupied and ask for another choice.
[Note – You must validate seat choice input (both the row number and seat number) via a Recursive function. The function must make use of pointers as parameters.]
Code:
The below is the code for OOP Assignment No 01 for Dynamic Arrays
using namespace std; void Seats_entry(char **,int ,int ); bool Seat_checking(char **,int ,int ); void check(char **,int ,int); int main() { int rows,cols=4; cout<<"ENTER THE NUMBER OF ROWS YOU WANT IN YOUR BUS: "<>rows; system("CLS"); char **table=new char *[rows]; for(int i=0;i<rows;i++) { table[i]=new char [cols]; } Seats_entry(table,rows,cols); int count=0; for(int i=0;i<rows;i++) { cout<<++count<<" "; for(int j=0;j<cols;j++) { cout<<table[i][j]<<" "; } cout<<endl<<endl; } check(table,rows,cols); int counter=0; for(int i=0;i<rows;i++) { cout<<++counter<<" "; for(int j=0;j<cols;j++) { cout<<table[i][j]<<" "; } cout<<endl<<endl; } for(int i=0;i<rows;i++) { delete[] table[i]; } delete[] table; table= NULL; } void Seats_entry(char **ptr,int rows,int cols) { for(int i=0;i<rows;i++) { for(int j=0;j<cols;j++) { if(j==0) ptr[i][j]='A'; else if(j==1) ptr[i][j]='B'; else if(j==2) ptr[i][j]='C'; else if(j==3) ptr[i][j]='D'; } } } bool Seat_checking(char **pointers,int rows,int cols) { for(int i=0;i<rows;i++) { for(int j=0;j<cols;j++) { if(pointers[i][j]!='X') return 0; } } return 1; } void check(char **pointer,int rows,int cols) { bool seats=0; seats=Seat_checking(pointer, rows, cols); if(seats==1) { cout<<endl<<endl<<"ALL THE SEATS ARE OCCUPIED....THANK YOU...."<<endl<<endl<<endl; return ; } else if( seats==0) { char option; int rows_entry,cols_entry; do { cout<<"DO YOU WANT TO BOOK ANY SEAT (Y/N) : "<<endl; cin>>option; } while(option!='y' && option!='Y' && option!='N' && option!='n'); if(option=='y' || option=='Y') { cout<<"ENTER THE ROW NUMBER OF THE SEAT: "<<endl; cin>>rows_entry; cout<<"\nENTER THE COLUMN: "; cout<<"\nEnter 1 FOR A: "; cout<<"\nEnter 2 FOR B: "; cout<<"\nEnter 3 FOR C: "; cout<<"\nEnter 4 FOR D: "<<endl; cin>>cols_entry; if(rows_entry>rows || cols_entry>cols) { cout<<"YOU ENTERED INVALID SEAT NUMBER....PLEASE TRY AGAIN "<<endl<<endl; cout<<"THE NUMBER OF ROWS AND COLUMNS ARE: "<<rows<<" "<<cols <<endl <<endl; } else { if(pointer[rows_entry-1][cols_entry-1]=='X') { cout<<"SORRY, THE SEAT HAS ALREADY BEEN BOOKED. PLEASE ENTER ANOTHER SEAT NUMBER. "<<endl<<endl; } else pointer[rows_entry-1][cols_entry-1]='X'; } check(pointer,rows,cols); } else return; } }
For more details about Huffman coding click here
For other assignments and quizzes click here
Output: