Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to declare the array that stores the finish times of activities.
DSA C
int finish[] = {1, 3, 6, 5, 8, 9};
int start[] = {0, 1, 3, 4, 6, 8};
int n = sizeof(finish)/sizeof(finish[0]);
// The array to store finish times is called [1]; Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Confusing start times with finish times.
Using an undefined variable name.
✗ Incorrect
The array that stores the finish times is named 'finish'.
2fill in blank
mediumComplete the code to select the first activity by initializing the index.
DSA C
int i = 0; // Select the first activity int count = 1; // Count of selected activities for (int j = 1; j < n; j++) { if (start[j] >= finish[[1]]) { i = j; count++; } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'j' instead of 'i' to check finish time.
Comparing with 'count' or 'n' which are not indices.
✗ Incorrect
We compare the start time of the current activity with the finish time of the last selected activity, which is at index 'i'.
3fill in blank
hardFix the error in the loop condition to correctly iterate over all activities.
DSA C
for (int j = 1; j [1] n; j++) { if (start[j] >= finish[i]) { i = j; count++; } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<=' causes out-of-bounds access.
Using '>' or '>=' causes the loop to never run.
✗ Incorrect
The loop should run while j is less than n to avoid going out of array bounds.
4fill in blank
hardFill both blanks to complete the function header and return statement for counting selected activities.
DSA C
int activitySelection(int [1][], int [2][], int n) { int count = 1; int i = 0; for (int j = 1; j < n; j++) { if ([1][j] >= [2][i]) { i = j; count++; } } return count; }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping start and finish arrays.
Using incorrect parameter names.
✗ Incorrect
The function takes start and finish arrays as parameters to select activities.
5fill in blank
hardFill all three blanks to complete the main function that calls activitySelection and prints the result.
DSA C
#include <stdio.h> int activitySelection(int [1][], int [2][], int n); int main() { int start[] = {1, 3, 0, 5, 8, 5}; int finish[] = {2, 4, 6, 7, 9, 9}; int n = sizeof(start)/sizeof(start[0]); int result = activitySelection(start, finish, [3]); printf("Number of activities selected: %d\n", result); return 0; }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Passing arrays in wrong order.
Passing wrong variable for number of activities.
✗ Incorrect
The main function passes start and finish arrays and the number of activities 'n' to the activitySelection function.