1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

c++ Queue Simulation

Discussion in 'C++' started by pkjm17, Nov 28, 2010.

  1. pkjm17

    pkjm17 New Member

    Joined:
    Nov 28, 2010
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    Hi, my problem here is I can't get my program to output if another person enters the queue at a minute which the person before them is still being served. I think I need to put something after my while statement? Here's an example of what I'm talking about. On one of my runs, it skipped person #4, because they must have entered somewhere at a minute when person #3 was still being served (counting down to zero). Please see attached screen shot image of what I mean.

    Code:
    case '5' :
          {
            int a[q.getCount()];
            int out[q.getCount()];
            cout<<"Enter simulator time: ";
            cin>>time;
            cout<<"Enter max minutes to enter queue: ";
            cin>>minute;
            cout<<"Enter max minutes to serve: ";
            cin>>serve;
            cout<<"Queue Size: " <<q.getCount() <<endl <<endl;
            cout<<"MINUTE"<<setw(10)<<"PERSON #"<<setw(3)<<"IN"<<setw(10)<<"SERVE" <<endl;
             a[0] = rand() % (minute - 1) + 1 + 1;
            for(int i=1; i<q.getCount(); i++) //get arrival time
                 a[i] = a[i - 1] + rand() % (minute - 1) + 1 +1;
    
            for(int s=0; s<q.getCount(); s++)//get serve minutes
              out[s] = rand()%(serve-1)+1+1;
    
            for(int t=0; t<time; t++){//begin loop start with minute 1
             cout<<t+1;
    
            for(int j=0; j<q.getCount(); j++){
                if(a[j] == t){ //minute equals minute entering queue
                 cout<<setw(5)<<j+1 <<setw(5) <<a[j]+1 <<setw(5) <<out[j]<<endl; //<<rand()%(serve-1)+1+1;//print current person
            while(out[j] != 0){//print until serve minute is at zero
             t++;
             --out[j];
             cout<<t+1 <<setw(5)<<j+1 <<setw(5) <<a[j]+1 <<setw(5) <<out[j]<<endl;}}}
            cout<<endl;
    }
    
          }
          break;
     

    Attached Files:

Share This Page