You are here:

C++/c++ Mail simulator

Advertisement


Question
could u pls help mi.i realli desperately need your help.could u pls c my code,i'm nt sure whether i am doing it right or wrong for the whole program.i will be so appreciated for all the help that u give mi.

my qns is,how to write an 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. Remember, the messages must arrive randomly, so you will need to use a random number generator to determine when messages are received.

Each minute, you 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, you will 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.
Run the 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 <iostream> //iostream
#include <cstdlib> //stdlib
#include <ctime> //time
#include <queue>
#include <conio.h>

using namespace std;

const int max_email=40;


void generate_arrival_events();

class Queue
{
  private:
  int queue[max_email];
  int rear;
  int front;

   public:
  Queue(void);
  void Insert();

};

/*************************************************************************/
//--------------------------  Queue( )  ---------------------------------//
/*************************************************************************/
Queue::Queue()
{
  rear=-1;
  front=-1;

  for(int count=0;count<max_email;count++)
     queue[count]=0;
}


/*************************************************************************/
//------------------------------  Insert( )  ----------------------------//
/*************************************************************************/
void Queue::Insert()
{
      int random_email;

      cout<<"Enter random email to insert into the Queue : ";
      cin>>random_email;

      if((rear==front-1) || (front==0 && rear==max_email-1))
         cout<<"Queue is full. \n"<<endl;

      else if(rear==-1 && front==-1)
    {
       rear++;
       queue[rear]=random_email;
       front=rear;

       cout<<random_email<<" is inserted into the Queue."<<endl;
    }

      else if(rear==max_email-1 && front!=0)
    {
       rear=0;
       queue[rear]=random_email;

       cout<<random_email<<" is inserted into the Queue."<<endl;
    }

      else
    {
       rear++;
       queue[rear]=random_email;

       cout<<random_email<<" is inserted into the Queue."<<endl;
    }
}

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()
{
  
  int random_email=0;

   srand((unsigned)time(0));

    for(int i=0; i<random_email; i++)
    {
       random_email=(rand()%40)+1;
     cout << random_email << endl;
    }
   
  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" ;
         
         
  
   system("PAUSE");
  return 0;
 
}  

Answer
Hi dear Sabrina !

   I've went through your code which meets the requirement partially , and it requires some tune up activities.

Things to be noted before tuning up!

  Don't Run the simulator equivalent to the speed of the processor , make it parallel to the system time!
  Since you have fixed the size of the message queue is 40 and also the simulator should process an average of 40 messages per minutes. As per the requirement you must have to restrict the count of incoming message to be 30/min, this can be done by pausing the message generating mechanism for 2seconds after generating a message (also ensure that it should send 30 messages per min )

>> pending_events_queue.push( event( event::MESSAGE_ARRIVED, time ) ) ;
Once after pushing the message into the message queue suspend the message generating event for 2 sec's  this care should be taken in generate_arrival_events().

Notice the following code , inside the for loop will never get a chance to get executed

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

since the condition variable int random_email=0; will never be true as per your logic.

Alteration needed with the generate_arrival_events()., not all the messages to be generated at once , it should be generated with respect to time on regular intervals.

Presenting you the simple pseudo code for your task


1.Make essential Initializations
2 Start_Time = 0; // Durations to be handled in minutes
  End_Time = Start_Time + 15 mins
  while Start_Time < End_Time
  begin
     process the message generator and insert into the queue
     if( queue is exhausted )
     {
        process the messages in the queue
     }
  End
3 Compute all the results relevant to the requirements

Note: make sure don't process the computation process inside the while loop , instead store the message sending and receiving time in a separate data structure and perform the computation finally.

The infrastructure seems  good and requires minimal modification if requires , but the flow of process should be treated above ( the pseudo code what ive given above alone not a solution for the given problem , we can modify for efficiency that's just a basic flow )

Thanks and Regards!
Prince M. Premnath  

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Prince M. Premnath

Expertise

Years of research in C/C++ Will bring you all facts regarding , OOPS , SVGA , MOUSE , BIOS , IVT , Feel free to query me :-)

Experience

More than 5 years

Education/Credentials
MCA

©2016 About.com. All rights reserved.