Unit (13.1) Sorting[1]Hw4
Unit (13.1) Sorting[1]Hw4
(Stack &&Que)
1. Assume that we want to find the sum of all elements in a stack of length 10.
Write and algorithm to find that sum?
1. Initialize sum to 0.
2. Repeat until the stack is empty:
o Pop the top element from the stack.
o Add the popped element to sum.
3. Return sum.
2. Assume that we have a stack of length 10. The stack contains integer numbers.
Write an algorithm to count the frequency of the value 3 in this stack.:
1. Initialize count to 0.
2. Repeat until the stack is empty:
o Pop the top element from the stack.
o If the popped element equals 3, increment count.
3. Return count.
#include <iostream>
#include <stack>
using namespace std;
int targetValue = 3;
cout << "Frequency of " << targetValue << " in the
stack: " << countFrequency(s, targetValue) << endl;
return 0;
}