You are here:

# C++/c++simulator

Question
QUESTION: could u pls help mi.i realli desperate for your help.could u pls c my code,i'm nt sure whether i am doing it right or wrong.could u pls guide mi step by step on tis program.i will be so appreciated for all the help that u give mi.

my qns is the e-mail simulator that processes mail at an average of 40 messages per minute. As messages are received, they are placed in a queue. For the simulation, assume that the messages arrive at an average rate of 30 messages per minute.The messages must arrive randomly, so must need to use a random number generator to determine when messages are received.

Each minute, i can dequeue up to 40 messages and send them.
Assume that 25% of the messages in the queue cannot be sent in
any processing cycle. Again, i need to use a random number
to determine whether a given message can be sent. If it cannot be
sent, put it back at the end of the queue or enqueue it.
i hv to run my simulator for 10 to 15 minutes, tracking the number of times each message had to be requeued. At the end of the
simulation, print the statistics that show:
1. The total messages processed
2. The average arrival rate
3. The average number of messages sent per minute
4. The average number of messages in the queue in a minute
5. The number of messages sent on the first attempt, the number
sent on the second attempt, and so forth
6. The average number of times messages had to be requeued (do
not include the messages sent the first time in this average.)

code:
#include <ctime>//For seeding random generator
#include <cstdlib>
#include <iostream>

using namespace std;

void enqueue(int);

int main()
{
int random_email = 0;

srand((unsigned)time(0));//Seed the random number for not having the same sequence everytime depending on time

for(int a = 0; a < 40;++a)
{
random_email=(rand()%40)+1;
cout << random_email << endl;
}
return 0;
}

void enqueue(int random_email)
{
enqueue(random_email);
}

ANSWER: the running of a discrete-event simulation program is represented as a chronological sequence of events. Each event occurs at an instant in time and marks a change of state in the system - for example, in the email simulator,  an event would be "a messages arrives", with the resulting system state of "the queue has one more message".

In this example, the principal system entity is the "message queue" which holds the messages which have arrived, but are yet to be sent. The system events are "message arrived" and "message dequeued and sent" or "message dequeued and could not be sent". The system state, which is changed by these events is "number of messages in the queue (an integer from 0 to n)". The random variables that are required to model this system stochastically are "message interarrival time" and "message processing (attempt to send) time".

The simulations also needs:
a clock: You must keep track of the current simulation time, in seconds for the system - the events are instantaneous and the clock skips to the next event start time as the simulation proceeds.

a pending events list: You also need a list of simulated events. In this list, an event is described by the time at which it occurs, and a type - "message arrived" or "message dequeued and sent" or "message dequeued and could not be sent". This list is conveniently ordered by the time of occurance of the event.

The program logic would be:

Initialize: set clock to zero, initialize system state variables, schedule the initial events (messages arrive) and add them to the pending events list.

Loop till clock shows the end time: set clock to next event time, process next event and remove from the pending events list, add events that are generated as a part of this to the the pending events list, update statistics

---------- FOLLOW-UP ----------

QUESTION: for the Initialize: set clock to zero the code izzit correct.as for the initialize system state variables, schedule the initial events (messages arrive) and add them to the pending events list.

Loop till clock shows the end time: set clock to next event time, process next event and remove from the pending events list, add events that are generated as a part of this to the the pending events list, update statistics.i realli gt no idea hw to do it.could u pls help mi!i will be so appreciated for all the help frm you.

code:
void SetTime(int min);
{
min =0;
}

Here is a snippet which will give you an idea of what you have to do - this deals with just the arrival of messages (for simplicity all arrival events are generated as part of initialization) and putting them into the message queue. It collects statistics and reports the number of messages and average inter-arrival time, running the simulation fo 10 minutes. Sending of messages, enqueueing messages that were not sent etc. are not handled.

This appears to be a homework question - I would be harming you if I gave you the complete answer. Ask a follow-up if and only if you have made a genuine effort to write the program yourself, you are stuck at a particular point, and you have a specific question about your code.

#include <queue>
#include <cstdlib>
#include <iostream>

struct event
{
enum event_type { MESSAGE_ARRIVED, MESSAGE_SENT } ;

event( event_type ty, int tm ) : type(ty), event_time(tm) {}

bool operator< ( const event& that ) const { return event_time > that.event_time ; }

event_type type ;
int event_time ;
// ...
};

struct message
{
explicit message( int tm ) : time_of_arrival(tm), time_sent(-1) {}

int time_of_arrival ;
int time_sent ;
// ...
};

std::priority_queue< event > pending_events_queue ;

std::queue< message > message_queue ;

int curr_time = 0 ; // clock

const int finish_time = 10 * 60 ; // 10 minutes, 600 seconds

void generate_arrival_events()
{
// average rate of 30 messages per minute, or one every two seconds
// say, uniformly distributed between 0-4 seconds

int time = 0 ; // temporary clock for generating all arrival events

do
{
time += std::rand() % 5 ;
pending_events_queue.push( event( event::MESSAGE_ARRIVED, time ) ) ;
} while( time < finish_time ) ;
}

int main()
{
std::srand( unsigned( time(0) ) ) ;
generate_arrival_events() ;

int nmsgs = 0 ;
int last_msg_time = 0 ;
double total_interarrival_time = 0 ;

while( ( curr_time < finish_time ) && ( !pending_events_queue.empty() ) )
{
event ev = pending_events_queue.top() ;
pending_events_queue.pop() ;
curr_time = ev.event_time ;

switch( ev.type )
{
case event::MESSAGE_ARRIVED :
{
message msg( ev.event_time ) ;
++nmsgs ;
total_interarrival_time += msg.time_of_arrival - last_msg_time ;
last_msg_time = msg.time_of_arrival ;
message_queue.push( msg ) ;
}
break ;

case event::MESSAGE_SENT : ;
// ...
}
}
std::cout << "#messages: " << nmsgs << "  average inter-arrival time: "
<< total_interarrival_time / nmsgs << "seconds.\n" ;
}

C++

Volunteer

#### vijayan

##### Expertise

my primary areas of interest are generic and template metaprogramming, STL, algorithms, design patterns and c++11. i would not answer questions about gui and web programming.