CPE0033L
Formative Assessment
3
STUDENT NAME : Daniel Josef C. Pleno
STUDENT NUMBER: 201911871
DATE : 1/27/2025
Format &
SIGNATURE : ___________ Presentation ( 5% ) : ________
Timeliness (20%) : ________
Correctness ( 15% ) : ________
Machine Code
Instruction used (25%) : ________
Output (25%) : ________
Time (10%) : ________
Grade :
I. OBJECTIVES
At the end of this exercise, students must be able to:
a) Learn about stacks.
b) Understand the different operations on stacks
c) Understand how to implement a static and dynamic stack.
II. BACKGROUND INFORMATION
Computer systems use stacks during a program’s execution to store function return
addresses, local variables etc.
Some calculators use stacks for performing mathematical operations.
III. LABORATORY ACTIVITY
Directions:
Encode and run the program in Dev C++
Copy and paste the source code to this word file.
Screenshot the result/output of the program, then paste it in the word file.
Submit this word file to canvas.
Case problem 1.
Instruction:
Try to RUN and debug the program in C++. Observe and analyze the result/ output of the
program.
Provide a screen shot of the output and at the bottom make a comment or analysis of the
program.
//Tests the push, empty, size, pop, and top methods of the stack library.
#include <iostream>
#include <stack> // Calling Stack from the STL
using namespace std;
int main() {
stack<int> newStack;
[Link](3); //Adds 3 to the stack
[Link](8);
[Link](15);
// returns a boolean response depending on if the stack is empty or not
cout << "Stack Empty? " << [Link]() << endl;
// returns the size of the stack itself
cout << "Stack Size: " << [Link]() << endl;
// returns the topmost element of the stack
cout << "Top Element of the Stack: " << [Link]() << endl;
// removes the topmost element of the stack
[Link]();
cout << "Top Element of the Stack: " << [Link]() << endl;
cout << "Stack Size: " << [Link]() << endl;
return 0;
}
SOURCE CODE:
#include <iostream>
#include <stack> // Calling Stack from the STL
using namespace std;
int main() {
stack<int> newStack;
[Link](3); //Adds 3 to the stack
[Link](8);
[Link](15);
// returns a boolean response depending on if the stack is empty or not
cout << "Stack Empty? " << [Link]() << endl;
// returns the size of the stack itself
cout << "Stack Size: " << [Link]() << endl;
// returns the topmost element of the stack
cout << "Top Element of the Stack: " << [Link]() << endl;
// removes the topmost element of the stack
[Link]();
cout << "Top Element of the Stack: " << [Link]() << endl;
cout << "Stack Size: " << [Link]() << endl;
return 0;
}
OUTPUT:
Case problem 2.
Given the following sequence of stack operations, what is the top item on the stack when the
sequence is complete?
stack<int> m;
[Link](5);
[Link](12);
[Link]();
[Link](27);
cout << [Link]();
SOURCE CODE:
#include <iostream>
#include <stack>
using namespace std;
int main() {
stack<int> m;
[Link](5);
[Link](12);
[Link]();
[Link](27);
cout << "Top item on the stack: " << [Link]() << endl;
return 0;
}
OUTPUT:
Conclusion:
The C++ program successfully demonstrates the fundamental operations
of a stack using the Standard Template Library (STL). It effectively
showcases key stack functionalities, including pushing elements,
checking for emptiness, retrieving the size, accessing the top element,
and popping the top element. The output confirmed the correct execution
of these operations, aligning with the expected behavior of a stack.
Overall, this exercise highlights the ease of using STL for stack
management and reinforces the importance of understanding data
structures in programming.