Im getting the wrong output. In my while loop for the else statement I want it to perform the subtraction operation and then put that data on the queue pq2. I put a cout statement to make sure that the data is on the pq2 queue. However Im getting weird numbers.

This is my output:
PID: 3 Initial Priority: 32 Service Time: 0.52
Process is done
PID: 45 Initial Priority: 12 Service Time: 1.1
Process is done
PID: 0 Initial Priority: 1 Service Time: 5.51
Changed Quantum: 1.01
PID: -1073741821Priority: -1073741821 Changed Service Time: 2.8026e-45
PID: 1 Initial Priority: 1 Service Time: 6
Changed Quantum: 1.5
PID: -1073741821Priority: -1073741821 Changed Service Time: 2.8026e-45


The output should be:
PID: 3 Initial Priority: 32 Service Time: 0.52
Process is done
PID: 45 Initial Priority: 12 Service Time: 1.1
Process is done
PID: 0 Initial Priority: 1 Service Time: 5.51
Changed Quantum: 1.01
PID: 0 Priority: 1 Changed Service Time: 1.01
PID: 1 Initial Priority: 1 Service Time: 6
Changed Quantum: 1.5
PID: 1 Priority: 1 Changed Service Time: 1.5

This is what I have for my main:

int main(){
priority_queue<Event, vector<Event>, ComparePriority> pq;

	float quantum = 4.5;

Event events[4]= { {0, 1, 5.51}, {1, 1, 6.00}, {45, 12, 1.10}, {3, 32, 0.52}};

for (int i = 0; i < 4; ++i)
	pq.push(events[i]);
	
while (! pq.empty()) {
	Event events2=pq.top();
	cout<<"PID: "<<events2.pid<<" Initial Priority: "<<events2.priority<<" Service Time: "<<events2.service<<endl;
	if (events2.service<quantum){
	cout<<"Process is done"<<endl;
		//pq.pop();
	}else{
		events2.service-=quantum;
		cout<<"Changed Quantum: "<<events2.service<<endl;
		priority_queue<Event, vector<Event>, ComparePriority> pq2;
		Event events2=pq2.top();
		cout<<"PID: "<<events2.pid<<"Priority: "<<events2.priority<<" Changed Service Time: "<<events2.service<<endl;
		//cout<<"PID: "<<events2.pid<<" Initial Priority: "<<events2.priority<<" Service Time: "<<events2.service<<endl;
//put at the end of the line to go back through the loop next cycle
	}
	pq.pop();
}
	return 0;
}

Recommended Answers

All 2 Replies

Can you post a compilable snippet? I.e. you're Event class is missing.

Dave

thanks for the input. I have fixed it, it was the way I was implementing push();. However now I am having trouble with something else. What I want is after the comparison, and the subtraction, the line of data is added onto the second queue. The loop continues to run and the data is either popped off after the comparison or it is added onto the second queue. After all the data is popped off the first queue. It does the same thing with the second queue, instead this time putting the data back on the first queue. Basically it is flip-flopping data between two queues. Can you show me where in this while loop Im going wrong?

Here is the entire code

#include <iostream>
#include <queue>
#include <deque>
#include <vector>
#include <iomanip>
#include <fstream>

using namespace std;

struct Event {
int pid;
int priority;
float service;
};

class ComparePriority {
public:
bool operator() (Event& events1, Event& events2)	{
if (events1.priority < events2.priority) return true;
	if(events1.priority == events2.priority && events1.service>events2.service)return true;

	return false;
}
};

while (! pq.empty()) {
	while(! pq2.empty()){
	Event events2=pq.top();
	cout<<"PID: "<<events2.pid<<" Initial Priority: "<<events2.priority<<" Service Time: "<<events2.service<<endl;
	if (events2.service<quantum){
	cout<<"Process is done"<<endl;
		//pq.pop();
	}else{
		events2.service-=quantum;
		cout<<"Changed Quantum: "<<events2.service<<endl;
		pq2.push(events2);
		cout<<"PID: "<<events2.pid<<"Priority: "<<events2.priority<<" Changed Service Time: "<<events2.service<<endl;
		//cout<<"PID: "<<events2.pid<<" Initial Priority: "<<events2.priority<<" Service Time: "<<events2.service<<endl;
//put at the end of the line to go back through the loop next cycle
	}
	pq.pop();
}
		Event events2=pq2.top();
		cout<<"PID: "<<events2.pid<<" Initial Priority: "<<events2.priority<<" Service Time: "<<events2.service<<endl;
		if (events2.service<quantum){
			cout<<"Process is done"<<endl;
			//pq.pop();
		}else{
			events2.service-=quantum;
			cout<<"Changed Quantum: "<<events2.service<<endl;
			pq.push(events2);
			cout<<"PID: "<<events2.pid<<"Priority: "<<events2.priority<<" Changed Service Time: "<<events2.service<<endl;
 
}
	pq.pop();
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.