Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to declare a stack pointer variable.
Embedded C
int [1] = -1; // Stack pointer initialized
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a variable name that is not descriptive of the stack pointer.
Initializing the pointer with a wrong value.
✗ Incorrect
The variable 'top' is commonly used as the stack pointer to track the top element index.
2fill in blank
mediumComplete the code to check if the stack is full.
Embedded C
if (top == [1] - 1) { // Stack is full }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong constant name for stack size.
Checking for equality with the wrong value.
✗ Incorrect
The stack is full when the top index equals STACK_SIZE - 1, the last valid index.
3fill in blank
hardFix the error in the stack overflow detection condition.
Embedded C
if (top [1] STACK_SIZE) { // Overflow detected }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '==' which misses cases where top is greater than STACK_SIZE.
Using '<=' which is incorrect for overflow detection.
✗ Incorrect
The stack overflows if top is greater than or equal to STACK_SIZE, which is beyond the last valid index.
4fill in blank
hardFill both blanks to correctly update the stack pointer and detect overflow.
Embedded C
top [1] top + 1; if (top [2] STACK_SIZE) { // Handle overflow }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '+' instead of '=' for assignment.
Using '>' instead of '>=' for overflow check.
✗ Incorrect
First, increment the top pointer with '=', then check if it is greater than or equal to STACK_SIZE to detect overflow.
5fill in blank
hardFill all three blanks to implement a safe push function with overflow detection.
Embedded C
void push(int stack[], int value) {
if (top [1] STACK_SIZE - 1) {
// Stack overflow error
return;
}
top [2] top + 1;
stack[[3]] = value;
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '>' instead of '>=' in overflow check.
Using '+' instead of '=' for increment.
Using wrong index for stack assignment.
✗ Incorrect
Check if top is greater or equal to STACK_SIZE - 1 to detect full stack, then increment top with '=', and assign value to stack[top].