0% found this document useful (0 votes)
24 views5 pages

Stack and Queue Algorithms in C++

stack_queue_hw1

Uploaded by

yamash koshofali
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views5 pages

Stack and Queue Algorithms in C++

stack_queue_hw1

Uploaded by

yamash koshofali
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

(Practical)

Problem Solving in Computing

(Stack && queue)

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?
Algorithm:
1. Define a variable sum and initialize it to 0.
2. While the stack is not empty:
o Remove the top element from the stack (pop).
o Add the removed element's value to sum.
3. After the loop, sum will contain the total sum of all elements in the stack.

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.:
Algorithm:
1. Define a variable count and initialize it to 0.
2. While the stack is not empty:
o Remove the top element from the stack (pop).
o If the removed element equals 3, increment count by 1.
3. After the loop, count will hold the frequency of the value 3 in the stack.

3 -Covert algorithm in question 2 into a program using C++ or Java?


C++ Program to count the frequency of the value 3 in a stack:

#include <iostream>
#include <stack>
using namespace std;

int main() {
stack<int> s;

// Push values into the stack ( with 10


elements)
for (int i = 0; i < 10; i++) {
int num;
cout << "Type number " << (i + 1) << ": ";
cin >> num;
[Link](num);
}

// Count the frequency of the value 3


int count = 0;
while (![Link]()) {
int topElement = [Link]();
[Link]();
if (topElement == 3) {
count++;
}
}

cout << "Frequency of 3: " << count << endl;


return 0;
}

Output Screenshot:
4. A queue of length 20 contains 20 even and odd numbers. Write an
algorithm to find the sum of those even numbers in that queue?
Algorithm:
1. Define a variable evenSum and initialize it to 0.
2. While the queue is not empty:
o Remove the front element from the queue (dequeue).
o If the removed element is even, add its value to evenSum.
3. After the loop, evenSum will contain the total sum of all even numbers in the
queue.
Example Program (C++):
#include <iostream>
#include <queue>
using namespace std;

int main() {
queue<int> q;
// Push values into the queue (example with
20 elements)
for (int i = 0; i < 20; i++) {
int num;
cout << "Type number " << (i + 1) << ":
";
cin >> num;
[Link](num);
}

// Calculate the sum of even numbers


int evenSum = 0;
while (![Link]()) {
int frontElement = [Link]();
[Link]();
if (frontElement % 2 == 0) {
evenSum += frontElement;
}
}

cout << "The sum of non-odd (even) numbers:


" << evenSum << endl;
return 0;
}
Output Screenshot:

You might also like