Job Sequencing With Deadline

#include<stdio.h>

int job[50];

void JSWD(int n,int profit[],int dead[])
{
    int i,j,d,count=0,tp=0;
    d=dead[1];
    for(i=1;i<=n;i++)
        if(d<dead[i])
            d=dead[i];
    for(i=1;i<=n;i++)
    {
        if(count==d)
            break;
        if(job[dead[i]]==0)
        {
            job[dead[i]]=profit[i];
            count++;
        }
        else
        {
            j=dead[i];
            while(j>1)
            {
                j--;
                if(job[j]==0)
                {
                    job[j]=profit[i];
                    count++;
                    break;
                }
            }
        }
    }
    for(i=1;i<=d;i++)
        tp=tp+job[i];
    printf("Total profit is %d",tp);
}

void main()
{
    int n,dead[50],profit[50],i,j,temp;
    printf("Enter number of jobs ");
    scanf("%d",&n);
    printf("Enter profit of jobs ");
    for(i=1;i<=n;i++)
        scanf("%d",&profit[i]);
    printf("Enter deadline of each job ");
    for(i=1;i<=n;i++)
        scanf("%d",&dead[i]);
    for(i=1;i<=n;i++)
    {
        for(j=1;j<=n-i;j++)
        {
            if(profit[j+1]>profit[j])
            {
                temp=dead[j];
                dead[j]=dead[j+1];
                dead[j+1]=temp;

                temp=profit[j];
                profit[j]=profit[j+1];
                profit[j+1]=temp;
            }
        }
    }
    printf("Profit of job is\n");
    for(i=1;i<=n;i++)
        printf("%d ",profit[i]);
    printf("\nDeadline of job is\n");
    for(i=1;i<=n;i++)
        printf("%d ",dead[i]);
    printf("\n");
    JSWD(n,profit,dead);
}

/*Output
Enter number of jobs 5
Enter profit of jobs 60 70 80 90 100
Enter deadline of each job 5 5 1 1 4
Profit of job is
100 90 80 70 60
Deadline of job is
4 1 1 5 5
Total profit is 320
*/

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s