Email simulation using queues

Here is the assignment I get:
*****
Write an email simulator that processes mail at an average of 40 messages per minute. As messages are received, they are placed in a queue.assume that the messages arrive at an average rate of 30 messages per minute.messages must arrive randomly.
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.use a random number to determine whether a given message can be sent. If it can’t be sent, enqueue it.

Run the simulation for 24 hours, At the end of the simulation, print the statistics that show:

-The total messages processed.
-The average arrival rate.
-The average number of messages sent per minute.
-The average number of messages in queue in a minute.
-The number of messages sent on the first attempt, the number sent on the second attempt, and so forth.
-The average number of times messages had to be requeued (do not include the messages sent the first time in this average)
********
I did the following part but have no idea to continue it, I appreciated it if you help me with a piece of pseudo code or something

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <iostream>
using namespace std;
#define SIZE 40

class Queue {
  int queue[SIZE];
  int head, tail;
public:
  Queue();
  void Enq(int num);
  void Deq();
  void Display();
};
Queue::Queue(){
  head = tail = 0;
}
void Queue::Enq(int num){
    int newtail = (tail+1)%SIZE;
    if(newtail!=head) //queue is'nt full
    {
        queue[tail]=num;
        tail=newtail;
    }
    else
    {
        cout<<"\n ***Queue is full***\n";
    }
}
void Queue::Deq(){
  if(head == tail) {
    cout << "Queue is empty\n";
  }
  head = (head+1) % SIZE;
}
void Queue::Display(){
    for(int i=head;i!=tail;i=(i+1)%SIZE)
    cout<<queue[i]<<" ";
    cout<<endl;
}
int main(){
    Queue EmailSim;




    return 0;
}

any help :( ?
Hmm, that's a bit above a total beginner. Here's the pseudo code for the simulation. Let's see if you get it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
message_array[all_possible_msgs] <- 0
loop over all minutes (minutes per 24 hrs)
{
  // receiver part
  loop random number per minute 0 - 59 (leads to avg 30)
  {
    push index of message = loop index + max index from last cycle + 1
  }
  // sender part
  loop until 40 or queue empty
  {
    pop index of message
    if random number 0 - 3 is 0
    {
      add 1 to message_array[index of message]
      push index of message
    }
  }
}


random number -> rn = rand() % max_num; // 0 - 59 -> max_num = 59
that's a bit above a total beginner


I wish lecturer could understand this too ;)

BTW, first let me show u what i did to manage the loops if its Ok then I move to next step

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
//0 or 1 to fire the action or not
int GenRandomNum(){
    srand(time(NULL));
    return rand()%1+1;
}
//loops
while(difftime(time(0),start)<=60*60*24){ // 24 hours
    //inner loop to manage each minute
    while(difftime(time(0),ToHoldOneMinute)<=60){
        //Enqueueing
		for(int i=0;i<30;i++){ //problem 1
			if(GenRandomNum()){
                EmailSim.Enq(/*some int should added but not sure what yet!*/);
            }
		}
        //Dequeueing
        for(int i=0;i<40;i++){ //problem 2
            if(GenRandomNum()){
                EmailSim.Deq();
            }
            else{
                EmailSim.Enq(EmailSim.Deq());
                }
        }
        while(difftime(time(0),ToHoldOneMinute)<=60){}//wait till one minute finished coz the
                       //above will take a fraction of second to be done
    }
}


problem 1: here it does not take care of average it just get 30 and put in queue if as you said i make it loop random number per minute 0 - 59 (leads to avg 30) then how to take care of the max size of 40 of the array suppose the random number will be 50??

problem 2: its similar to problem 1, it just randomly dequeue out of 40 elements which are queued before and dose not apply the only 25% (10 message) should remains.

and thanks for your help dude

[EDIT] The loops are not completely wrong, but they must be revamped so that they do what you want. The structure is ok and it's not that complicated like you might think right now. [/EDIT]


Well, sorry to say that, but your loops are completely wrong. It's a simulation and not real time! I don't think that your teacher wants to wait a day for the result.

The first loop must only loop over the amount of the minutes (no difftime at all!)
The second and last loop are wrong. Remove them.

'srand()' must be called only once at the beginning of the program (main()). 'rand()%1+1' returns only 1 or 2. That makes no sense.

problem 1: You need an array that holds all message data (here an int array for the resend count). The size of that array is the amount of minutes * max amount of values per minute. Normally you don't have such a large array, but in this case it's ok (I guess).
Important: The current message index to enque is the last message index + 1. Hold that variable outside the loop plus you need that for the statistics later.

problem 2: You always en/deque the index of the message. Then decide (like if((rand() % 4) == 0)) whether to enque it again. if enque you must increase the resend count by using the index of the message dequed.

You need to think about what variables you need to produce the output. Better declare those variables first. So you might see it somewhat clearer what has to be done for it.
Last edited on
Topic archived. No new replies allowed.