Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Charlie just started his new job as a parking attendant. Part of his job is to a

ID: 3905864 • Letter: C

Question

Charlie just started his new job as a parking attendant. Part of his job is to assign parking spaces from left to right in an n x n parking lot. Write a function that finds the row with the least cars and assigns the next available space in that row for Charlie to assign to the drivers as they enter the lot. Ex n 5, the parking spots are denoted by row and space number. In your answer, indicate the Row number (1, 2, 3, 4,5, , n) and the Space number (1, 2, 3, 4, 5, ,n) that is next available Space Row 1: 1 2 345 Row 2: 1 2 345 Row 3: 1 2 345 Row 4: 1 2 345 Row 5: 1 2 345 Cars are parked from left to right, in the row with the least number of spaces. There will never be a gap in the full spaces (such as 1, 0, 0, 1, 0) in any row. In the event of a tie, the first free spot is chosen. 11100 11000 11100 10000 In this example, the next available spot is Row: 5 Spot: 2. Inputs are as follows: first line n, which is the nx n grid second and third lines define the 2D array sizes. All subsequent inputs denote if the spot is empty or full, as 1-full and 0- empty Write a function that prints the Row and Space of the next space Charlie should assign to his customers and returns an array with the answer, array(0-row, array[1-space In the event there are no spaces available, the lot is full, return the array with values (0, 0) Constraints- 1

Explanation / Answer

Hello Student!

Here goes the solution

#include <iostream>

using namespace std;

int *carParking(int n, int availabe_size_rows, int available_size_cols, int **available, int *result_size) {

if (availabe_size_rows == 0 || available_size_cols == 0) {

return 0;

}

int ans = INT_MAX;

int idx = -1;

for (int i = 0 ; i < availabe_size_rows ; i++) {

int count = 0;

for (int j = 0 ; j < available_size_cols ; j++) {

if (available[i][j] == 1) {

count++;

}

}

if (ans > count) {

ans = count;

idx = i;

}

}

int a[2] = {idx+1, ans+1};

result_size = (int*)malloc(sizeof(int *));

*result_size = 2;

cout << ans+1 << " " << idx+1 << endl;

return a;

}

int main(int argc, const char * argv[]) {

// insert code here...

int n;

cin >> n ;

int availabe_size_rows, available_size_cols ;

int *result_size ;

cin >> availabe_size_rows >> available_size_cols ;

int **mat = (int **)malloc(availabe_size_rows * sizeof(int *));

for (int i = 0 ; i < availabe_size_rows ; i++) {

mat[i] = (int *)malloc(available_size_cols * sizeof(int));

}

for (int i = 0 ; i < availabe_size_rows ; i++) {

for (int j = 0 ; j < available_size_cols ; j++) {

cin >> mat[i][j];

}

}

int *result = carParking(n, availabe_size_rows, available_size_cols, mat, result_size);

return 0;

}

Test Case :

5

5

5

1 1 1 0 0

1 1 1 1 0

1 1 0 0 0

1 1 1 0 0

1 0 0 0 0

2 5

Program ended with exit code: 0

Thank you. Feel free to ask anything. If you like the answer. Please upvote it. It means a lot. Thank you again.