By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,804 Members | 1,631 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,804 IT Pros & Developers. It's quick & easy.

Plz Help Me To Debug This Prog!!!!

momotaro
100+
P: 357
This is what am i asked to do:

Implement a priority queue to manage patients in an emergency room
You MUST use the files “patient.h” and “ER.c” as given.
You should write the files “patientqueue.c” and “patientqueue.h” so that they work correctly with the two files above.
For most credit:
Submit two implementations of patientqueue.c, using the two approaches described above.
Make sure that the queue is never full, by allowing it to grow

And this is my prog:

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include<string.h>
  4. #define MAXQ 20
  5.  
  6. typedef struct patient
  7. {
  8.     char name[30];
  9.     int patientID,
  10.         severity,
  11.         ArrivalTime;
  12. }Patient;
  13.  
  14. typedef Patient QueueEntry;
  15.  
  16. typedef struct queue
  17. {
  18.     int count,
  19.         rear,
  20.         front,
  21.         Maxseverity;
  22.     QueueEntry entry[MAXQ];
  23. }PatientQueue;
  24.  
  25. void getPatient(Patient *newPatient) ;
  26. int PQFull(PatientQueue *q);
  27. int PQEmpty(PatientQueue *q);
  28. void PQAppend(Patient p, PatientQueue *q);
  29. void getPatient(Patient *newPatient) ;
  30. void PQMaxServe(QueueEntry *item,PatientQueue *q);
  31. void PQPrint(PatientQueue *q);
  32. PatientQueue *newPQ(int Maxseverity);
  33.  
  34. int time = 0;
  35.  
  36. // Main program, reads one patient, and then processes patients until
  37. // the queue is empty.int main() {
  38.  
  39. int main() {
  40.     PatientQueue *theQueue = newPQ(4);
  41.     Patient thePatient;
  42.     char action;
  43.     int time = 0;
  44.  
  45.     getPatient(&thePatient);
  46.     PQAppend(thePatient, theQueue);
  47.     while(!PQEmpty(theQueue)){
  48.         printf("Enter an action; 'a' for arrive, 'n' for next: ");
  49.         getchar();
  50.         scanf("%c", &action);
  51.  
  52.         switch(action) {
  53.             case 'a':
  54.                 if(PQFull(theQueue)) {
  55.                     printf("Too many patients!  Go somewhere else.\n");
  56.                     break;
  57.                     }
  58.                 else{
  59.                     getPatient(&thePatient);
  60.                     PQAppend(thePatient, theQueue);
  61.                     break;
  62.                 }
  63.             case 'n':
  64.                 if(PQEmpty(theQueue)) {
  65.                     printf("No patients!  Take a break.\n");
  66.                     break;
  67.                     }
  68.                 PQMaxServe(&thePatient, theQueue);
  69.                 printf("Patient %s (%d, severity %d) helped after waiting %d.\n",
  70.                     thePatient.name, thePatient.patientID,
  71.                     thePatient.severity, time - thePatient.ArrivalTime);
  72.             }
  73.         printf("Queue at time %d:\n", time++);
  74.         PQPrint(theQueue);
  75.     }
  76. }
  77. //TOP DOWN FUNCTIONS
  78.  
  79. void PQMaxServe(QueueEntry *item,PatientQueue *q){
  80.     if(PQEmpty(q))
  81.         printf("The queue is empty!\n");
  82.     else{
  83.         *item=q->entry[q->front];
  84.         q->front=(q->front+1)%MAXQ;
  85.         q->count--;
  86.     }
  87. }
  88.  
  89.  
  90. void getPatient(Patient *newPatient) {
  91.     printf("Enter patient name:  ");
  92.     scanf("%s", newPatient->name);
  93.     printf("Enter patient ID:    ");
  94.     scanf("%d", &newPatient->patientID);
  95.     printf("Enter severity (1-4):");
  96.     scanf("%d", &newPatient->severity);
  97.     newPatient->ArrivalTime = time;
  98.     }
  99.  
  100. void PQAppend(Patient p, PatientQueue *q)
  101. {
  102.     int i=q->rear;
  103.     if(PQFull(q))
  104.         printf("queue is full(PQappendfunction)");
  105.     else if(p.severity > q->Maxseverity)
  106.         printf("wrong severity\n");
  107.     else if(PQEmpty(q)){
  108.         printf("Qempty case\n");
  109.         q->rear++;
  110.         strcpy(q->entry[q->rear].name,p.name);
  111.         q->entry[q->rear].patientID=p.patientID;
  112.         q->entry[q->rear].ArrivalTime=p.ArrivalTime;
  113.         q->entry[q->rear].severity=p.severity;
  114.         q->count++;
  115.  
  116.     }
  117.     else{
  118.         while(1){
  119.             if(p.severity > q->entry[i].severity)
  120.                 q->entry[i+1] = q->entry[i];
  121.             else{
  122.                 strcpy(q->entry[i+1].name,p.name);
  123.                 q->entry[i+1].patientID=p.patientID;
  124.                 q->entry[i+1].ArrivalTime=p.ArrivalTime;
  125.                 q->entry[i+1].severity=p.severity;
  126.                 q->count++;
  127.                 q->rear = (q->rear+1)%MAXQ;
  128.                 printf("Apended\n\a");
  129.                 break;
  130.                 }
  131.                 i--;
  132.             }
  133.         }
  134.     }
  135.  
  136.  
  137. PatientQueue *newPQ(int Maxseverity)
  138. {
  139.     PatientQueue *New = (PatientQueue*)malloc(sizeof(PatientQueue));
  140.     New->count = 0;
  141.     New->front = 0;
  142.     New->rear = -1;
  143.     New->Maxseverity = Maxseverity;
  144. return New;
  145. }
  146.  
  147. int PQEmpty(PatientQueue *q)
  148. {
  149.     return q->count <= 0;
  150. }
  151.  
  152. int PQFull(PatientQueue *q)
  153. {
  154.     return q->count >= MAXQ;
  155. }
  156. /*
  157. patient *newPatient() {
  158.     patient *newpatient = (patient *) malloc (sizeof(patient));
  159.     printf("Enter patient name:  ");
  160.     scanf("%s", newpatient->name);
  161.     printf("Enter patient ID:    ");
  162.     scanf("%d", &newpatient->patientID);
  163.     printf("Enter severity (1-4):");
  164.     scanf("%d", &newpatient->severity);
  165.     newpatient->arrivaltime = time;
  166.     return newpatient;
  167.     }*/
  168. void PQPrint(PatientQueue *q){
  169.     int i;
  170.     if(PQEmpty(q))
  171.         printf("Queue is Empty, nothing to print\n");
  172.     else{
  173.         for(i=0;i<q->count;i++){
  174.             printf("Patient %d: %s \nPatientID: %d\nseverity: %d\nArrivalTime: %d\n\n\n",i,q->entry[(q->front+i)%MAXQ].name,
  175.                 q->entry[(q->front+i)%MAXQ].patientID,q->entry[(q->front+i)%MAXQ].severity,q->entry[(q->front+i)%MAXQ].ArrivalTime);
  176.     }
  177.     }
  178. }

PLZ HELP!!!
Mar 18 '07 #1
Share this Question
Share on Google+
2 Replies


100+
P: 1,646
You need to read the posting guidelines particulary with reference to assignments
Mar 18 '07 #2

DeMan
100+
P: 1,806
It is not entirely clear exactly what you would like help with. Rather than posting the assignment question, could you please try to explain what sort of problems you are having. Try to include
what the actual program/class is supposed to do
how/why the behaviour of your program is not what is expected
any ideas you might have on why this is the case
Mar 18 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.