Starting Out with Java: From Control Structures through Data Structures (4th Edition) (What's New in Computer Science)
Starting Out with Java: From Control Structures through Data Structures (4th Edition) (What's New in Computer Science)
4th Edition
ISBN: 9780134787961
Author: Tony Gaddis, Godfrey Muganda
Publisher: PEARSON
Expert Solution & Answer
Book Icon
Chapter 20, Problem 1FTE

Explanation of Solution

Program purpose:

The given program is for implementing stack using an array.

Compile-time error:

An error which occurs on the source code of a program when user violate the rules of writing syntax are called as compile-time error. If the code fails to prove its language syntax format, then the compiler will throw an error.

Error in the given code:

The flow of execution never reaches the statement top--; The execution of code will never reach the line written after the return statement. So the top--; should give before return statement to get affected.

Correct statement:

//Decrementing the value of top by 1

top--;

//returning the value at position top-1 in the stack

return s[top];

Corrected code:

/*An array implementation of a stack

function for popping a value from the stack.*/

int pop()

{

/*checks whether the value of top is equal to zero*/

        if (top == 0)

/*if the value of top is equal to zero function throws an exception to indicate the stack is empty*/

            throw new EmptyStackException( );

...

Blurred answer
Students have asked these similar questions
Examine the stack's performance when it is allowed to operate in its natural state.
Find out how effectively the stack operates when it is given the opportunity to work in line with its inherent inclinations.
Find out how productive the stack can be if it is left to be itself.
Knowledge Booster
Background pattern image
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
C++ Programming: From Problem Analysis to Program...
Computer Science
ISBN:9781337102087
Author:D. S. Malik
Publisher:Cengage Learning