Thread: Priority Queue(no-premption) Implementation

  1. #1
    Registered User
    Join Date
    Dec 2013
    Posts
    5

    Priority Queue(no-premption) Implementation

    code is not working properly for priorities .what changes should i do?? failing if priority of 1st process is less
    Code:
    #include<stdio.h>
    
    
    int wt=0;
    float sum=0;
    int main()
    {
            int pr[5];
            int wait[5];
            int bt[5],tat[5];
            int n,prt,brt,i,j,temp;
            printf("enter the no. of processes");
            scanf("%d",&n);
    
    
            for(i=0;i<n;i++)
            {
                          printf("enter the burst time for %d th process",(i+1));
                          scanf("%d",&brt);
                          bt[i]=brt;
            }
    
    
            for(i=0;i<n;i++)
            {
                    printf("\n Burst time of[%d] process:%d",(i+1),bt[i]);
            }
    
    
            //priority
            for(i=0;i<n;i++)
            {
                    printf("enter the priority for %dth processes::",(i+1));
                    scanf("%d",&prt);
                    pr[i]=prt;
            } 
    
    
    
    
            for(i=0;i<n;i++)
            {
              for(j=0;j<n-i-1;j++)
               {
                    if(pr[j]>pr[j+1])
                    {
                            temp=bt[j];
                            bt[j]=bt[j+1];
                            bt[j+1]=temp;
                    }
               }
            }
            for(i=0;i<n;i++)
            {
              for(j=0;j<n-i-1;j++)
               {
                    if(pr[j]>pr[j+1])
                    if(pr[j]>pr[j+1])
                    {
                            temp=bt[j];
                            bt[j]=bt[j+1];
                            bt[j+1]=temp;
                    }
               }
            }
            for(i=0;i<n;i++)
            {
              for(j=0;j<n-i-1;j++)
               {
                    if(pr[j]>pr[j+1])
                    {
                            temp=wait[j];
                            wait[j]=wait[j+1];
                            wait[j+1]=temp;
                    }
               }
            }
    
    
            //waiting time loop
            for(i=0;i<n;i++)
            {
                          wait[i]=wt;
                                printf("\nwaiting time of [%d] is ::%d",(i+1),wait[i]);
    
    
                            wt=wt+bt[i];
    
    
            }
            //calculate sum for avg
            for(i=0;i<n;i++)
            {
                     sum=sum+wait[i];
            }
            sum=sum/n;
            printf("average waiting time=%d",sum);
    
    
            for(i=0;i<n;i++)
            {
                    tat[i]=wait[i]+bt[i];
                    printf("\nturn around time of %dth process:%d",(i+1),tat[i]);
            }
    return 0;
    }

  2. #2
    Registered User
    Join Date
    Nov 2013
    Posts
    31
    Seems to work for me. What input are you using?

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    1th It's int main(void), i.e. you need the "void" in there to be valid C.
    2th I would clearly describe the problem, giving example input, expected output, and actual output.
    3th I would remove the duplicated bubble sort with the duplicated if-statement.
    4th, I think you get the hint.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Priority queue STL
    By dpp in forum C++ Programming
    Replies: 2
    Last Post: 01-08-2009, 02:21 AM
  2. c++ stl priority queue
    By dpp in forum C++ Programming
    Replies: 8
    Last Post: 01-01-2009, 11:43 AM
  3. Priority Queue Help
    By cjwenigma in forum C++ Programming
    Replies: 6
    Last Post: 11-15-2007, 12:48 AM
  4. Priority Queue
    By Tostado in forum C++ Programming
    Replies: 1
    Last Post: 03-24-2004, 02:01 PM
  5. Priority Queue
    By evotron in forum C Programming
    Replies: 1
    Last Post: 11-02-2001, 01:46 PM