Hello friends,
I am currently working on a homework. But the code i wrote is not works as expected what i mean is that when i run the code sometimes i get the "Process exited with value 3221225477" error and integer values get unreasonable values.
Where do i do wrong.
Can you help me. Thanks in advance
#include <stdio.h>
#include <stdlib.h>
#include <queue>
#include <cstdlib> //contains prototypes for functions srand and rand
using std::srand;
using std::rand;
#include <ctime> //contains prototypes for function time
using std::time;
usingnamespace std;
#define N 30
#define MIN_SD 1
#define MAX_SD 5
struct Customer_Info {
int customer_number;
int arrive_time;
int service_duration;
};
struct Customer_Info CustomerInfo;
int main() {
int customernumber = 0;
queue<Customer_Info> Q;
srand(time(NULL));
printf("SIMULATION LOOP STARTED\n\n");
for (int currentTime = 0; currentTime <= N; currentTime++) {
//with 0.5 random probability
int p = rand() % 2;
if (p) { //If there is customer arrival
CustomerInfo.customer_number = ++customernumber;
CustomerInfo.service_duration = (rand()%MAX_SD) + MIN_SD;
CustomerInfo.arrive_time = currentTime;
printf("TIME:%02d Customer %d arrived.\n", currentTime, CustomerInfo.customer_number);
Q.push(CustomerInfo); // Add (enqueue) structure to queue
}
//Check the front element of Queue to determine whether the front element is eligible to remove from Queue.
if (Q.front().arrive_time + Q.front().service_duration <= currentTime) {
printf("TIME:%02d Customer %d departed. Service duration:%d\n", currentTime, Q.front().customer_number, Q.front().service_duration);
Q.pop();
}
}
printf("\nSIMULATION LOOP FINISHED.");
printf("\n\nREMOVING REST OF THE CUSTOMERS FROM QUEUE.\n");
while (!Q.empty()) {
printf("TIME: %2d Customer %2d departed. Service duration: %d\n", (Q.front().arrive_time + Q.front().service_duration), Q.front().customer_number, Q.front().service_duration);
Q.pop();
}
//system("pause");
return 0;
}
Hello, I think I have found your error.
First, Q is empty at first, and when the for loop begin, p's value is 0 or 1. As you say , with 0.5 random probability.But, if p is 0 at first, the second 'if' will execute, and in the condition part, Q.front() will execute , but you ought to note that Q is empty. You can't call front() to an empty queue. So, after I correct your code like this:
if (!Q.empty() && Q.front().arrive_time + Q.front().service_duration <= currentTime)
I run your code continuously and there is no error.I think that's correct.
#include <iostream>
#include <queue>
#include <random>
#include <chrono>
struct Customer_Info
{
int customer_number;
int arrive_time;
int service_duration;
};
int main()
{
constint N { 30 };
constint MIN_SD { 1 };
constint MAX_SD { 5 };
std::default_random_engine prng { std::random_device {} () };
std::uniform_int_distribution<int> dis1(0, 1);
std::uniform_int_distribution<int> dis2(MIN_SD, MAX_SD);
Customer_Info CustomerInfo;
int customernumber { };
std::queue<Customer_Info> Q;
std::cout << "SIMULATION LOOP STARTED\n\n";
for (int currentTime { }; currentTime <= N; currentTime++)
{
//with 0.5 random probability
int p { dis1(prng) };
if (p)
{ //If there is customer arrival
CustomerInfo.customer_number = ++customernumber;
CustomerInfo.service_duration = dis2(prng);
CustomerInfo.arrive_time = currentTime;
std::cout << std::format("TIME: {:2} Customer {:2} arrived.\n", currentTime, CustomerInfo.customer_number);
Q.push(CustomerInfo); // Add (enqueue) structure to queue
}
//Check the front element of Queue to determine whether the front element is eligible to remove from Queue.
if (!Q.empty() && Q.front().arrive_time + Q.front().service_duration <= currentTime)
{
std::cout << std::format("TIME: {:2} Customer {:2} departed. Service duration:{}\n",
currentTime, Q.front().customer_number, Q.front().service_duration);
Q.pop();
}
}
std::cout << "\nSIMULATION LOOP FINISHED.";
std::cout << "\n\nREMOVING REST OF THE CUSTOMERS FROM QUEUE.\n";
while (!Q.empty())
{
std::cout << std::format("TIME: {:2} Customer {:2} departed. Service duration: {}\n",
(Q.front().arrive_time + Q.front().service_duration), Q.front().customer_number, Q.front().service_duration);
Q.pop();
}
}
SIMULATION LOOP STARTED
TIME: 1 Customer 1 arrived.
TIME: 2 Customer 2 arrived.
TIME: 3 Customer 1 departed. Service duration:2
TIME: 4 Customer 3 arrived.
TIME: 7 Customer 2 departed. Service duration:5
TIME: 8 Customer 4 arrived.
TIME: 8 Customer 3 departed. Service duration:4
TIME: 10 Customer 5 arrived.
TIME: 13 Customer 4 departed. Service duration:5
TIME: 15 Customer 6 arrived.
TIME: 15 Customer 5 departed. Service duration:5
TIME: 18 Customer 6 departed. Service duration:3
TIME: 22 Customer 7 arrived.
TIME: 24 Customer 7 departed. Service duration:2
TIME: 25 Customer 8 arrived.
TIME: 27 Customer 9 arrived.
TIME: 29 Customer 10 arrived.
TIME: 29 Customer 8 departed. Service duration:4
TIME: 30 Customer 9 departed. Service duration:1
SIMULATION LOOP FINISHED.
REMOVING REST OF THE CUSTOMERS FROM QUEUE.
TIME: 34 Customer 10 departed. Service duration: 5
*std::format requires C++20, everything else is C++11 or later. If your compiler isn't C++20 compliant yet, only Visual Studio is currently, you could use the {fmt} 3rd party library. https://fmt.dev/latest/index.html