Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a programming segment/function (in .ccp for example) that uses a stack and

ID: 3757585 • Letter: W

Question

Write a programming segment/function (in .ccp for example) that uses a stack and a queue of integers. Use the random # generator and a loop to generate 100 integers

between 0 and 101. As the integers are being generated, place the odd #’s in a stack and the even #s in a queue. After the #’s have been placed in the appropriate stack and queue, your code will need to go back through the stack and queue:

o Compare each #, until the stack or queue is empty, and count the # of times the #’s the number being removed from the stack is within 10 of the number being removed from the queue. (Example: if the stacktop stores 17 and queue front stores 22, then that is one occurrence, then move on to the next element in the stack and queue. Etc...)

// Random # Example Program

#include <iostream>

#include <cstdlib>

#include <ctime>

using namespace std;

int main()

{

int num,max_number;

cout<<"This program will generate 10 random numbers"<<endl;

cout<<"The range is from 1 to what ever number you select."<<endl;

cout<<"What is the highest integer you will allow?"<<endl;

cin>>max_number;

int i = 1;

while ( i <= 10 )

{

num = ((rand() + time(0)) % max_number) + 1;

cout<< num<<endl;

i++;

}

system("PAUSE");

Explanation / Answer

C++:

#include <iostream>
#include<bits/stdc++.h>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
    queue < int > q;        // initializing queue and stack.
    stack < int > s;
    int i=1;
    cout<<"This program will generate 100 random numbers"<<endl;
    cout<<"The range is from 1 to 101."<<endl;
    cout<<"highest integer will be 101?"<<endl;
    while(i<=100)           // loop for generating 100 random numbers.
    {
        int num = ((rand() + time(0)) % 101) + 1;
        if(num%2==0) q.push(num);
        else s.push(num);
        i++;
    }
    int count=0;              // variable to count the desired answer.
    while(s.empty()==false && q.empty()==false){
        int num1=s.top();
        int num2=q.front();
        s.pop();
        q.pop();
        if(abs(num1-num2)<=10) count++;
    }
    cout<<"number being removed from the stack is within 10 of the number being removed from the queue is "<<count<<endl;       // printing the answer.
return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote