#include<stdio.h>
int main()
{
int n,i;
int task[100],start[100],end[100],j,t,g;
printf("Enter no task");
scanf("%d",&n);
int a=1;
for(i=0;i<n;i++)
{
task[i]=a;
printf("Enter start time for t%d\n",a);
scanf("%d",&start[i]);
printf("Enter End time for t%d\n",a);
scanf("%d",&end[i]);
a++;
}
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(start[i]>start[j])
{
t=start[i];
start[i]=start[j];
start[j]=t;
t=end[i];
end[i]=end[j];
end[j]=t;
t=task[i];
task[i]=task[j];
task[j]=t;
}
}
}
//printing sorting
printf("Sorted start time\n");
for(i=0;i<n;i++)
{ printf("%d",task[i]);
printf("\t%d",start[i]);
printf("\t%d\n",end[i]);
}
printf("\n");
//calculating machine
int z;
int m[100];int f=0;
for(i=0;i<n;i++)
{
z=0;g=0;
if(i==0) //creating 1st machine
{
m[f]=end[i];
printf("Task %d is submitted to machine m%d\n",task[i],f);
}
else
{ //comparing machine end time with start
for(j=0;j<=f;j++)
{
if(m[j]<=start[i]&&g==0)
{
m[j]=end[i];
printf("Task %d is submitted to machine m%d\n",task[i],j);
z=1;g=1;
}
} //loop for creating machine (not 1st machine)
if(z==0)
{
f++;
m[f]=end[i];
printf("Task %d is submitted to machine m%d\n",task[i],f);
}
}
}
return 0;
}
int main()
{
int n,i;
int task[100],start[100],end[100],j,t,g;
printf("Enter no task");
scanf("%d",&n);
int a=1;
for(i=0;i<n;i++)
{
task[i]=a;
printf("Enter start time for t%d\n",a);
scanf("%d",&start[i]);
printf("Enter End time for t%d\n",a);
scanf("%d",&end[i]);
a++;
}
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(start[i]>start[j])
{
t=start[i];
start[i]=start[j];
start[j]=t;
t=end[i];
end[i]=end[j];
end[j]=t;
t=task[i];
task[i]=task[j];
task[j]=t;
}
}
}
//printing sorting
printf("Sorted start time\n");
for(i=0;i<n;i++)
{ printf("%d",task[i]);
printf("\t%d",start[i]);
printf("\t%d\n",end[i]);
}
printf("\n");
//calculating machine
int z;
int m[100];int f=0;
for(i=0;i<n;i++)
{
z=0;g=0;
if(i==0) //creating 1st machine
{
m[f]=end[i];
printf("Task %d is submitted to machine m%d\n",task[i],f);
}
else
{ //comparing machine end time with start
for(j=0;j<=f;j++)
{
if(m[j]<=start[i]&&g==0)
{
m[j]=end[i];
printf("Task %d is submitted to machine m%d\n",task[i],j);
z=1;g=1;
}
} //loop for creating machine (not 1st machine)
if(z==0)
{
f++;
m[f]=end[i];
printf("Task %d is submitted to machine m%d\n",task[i],f);
}
}
}
return 0;
}
No comments:
Post a Comment