C++ Programming Complete the incomplete code for the following findSeats functio
ID: 3906459 • Letter: C
Question
C++ Programming
Complete the incomplete code for the following findSeats function that returns an integer result. Its purpose is to check whether its first argument, a 2-dimensional integer array of numbers representing seats taken at a theater, contains enough consecutive empty seats (the second argument) on a desired row (the third argument). If the desired number of consecutive empty seats are available, it returns the position in the row of the first available seat in the group. If there are not enough empty seats available, it returns -1. Assume an empty seat is represented by 0 and an occupied seat is represented by 1 in the seats array. The algorithm for the program is to find an empty seat in the desired row, then see if it is the first of n consecutive empty seats. Assume global integer constants ROWS and COLS exist that are the dimensions of the seats array.
Explanation / Answer
//function declaration and definition along with main program to check the required functionality
#include<iostream>
#define ROWS 4
#define COLS 10
using namespace std;
int findSeats(int seats[][COLS], int empty, int row);
int main()
{
//to remonstrate the findseats assigns 2-d array
int seats[][COLS] = { 1,0,1,1,1,0,1,1,1,1, 1,0,0,0,0,1,1,1,1,1, 1,1,1,1,1,0,0,1,1,1, 0,0,1,1,1,0,0,1,1,1 };
cout << "Starting of first avaialabel seat: " << findSeats(seats, 4, 0) << endl;
cout << "Starting of first avaialabel seat: " << findSeats(seats, 4, 1) << endl;
cout << "Starting of first avaialabel seat: " << findSeats(seats, 2, 2) << endl;
cout << "Starting of first avaialabel seat: " << findSeats(seats, 3, 3) << endl;
}
int findSeats(int seats[][COLS], int empty, int row)
{
int AVAILABLE = 0; //assign avaiable variable 0 to indicate seat avaialable
int c,found = 0;
for (c = 0; c < COLS; c++)
{
if (seats[row][c] == AVAILABLE)
{
for (int j = c; j < c+empty && j < COLS; j++)
{
if (seats[row][j] == AVAILABLE)
{
found++;
continue;
}
else
{
found = 0;
break;
}
}
}
if (found)
break;
}
if (found)
return (row * 10 + 1 + c);
else
return -1;
}
/*output:
Starting of first avaialabel seat: -1
Starting of first avaialabel seat: 12
Starting of first avaialabel seat: 26
Starting of first avaialabel seat: -1
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.