You are on page 1of 2

//Program to schedule activities

#include<iostream.h>
using namespace std;
class activity
{
int a[20];
int k;
int count;
public:
int n;
int f[20];
int s[20];
int key[20];
activity()
{ k=1;
}
void setdata();
void sort();
void setcount()
{
count=0;
}
void getcount();
void activity_selector();
void getdata();
};
//to input array on console
void setdata()
{
cout<<"\nEnter the no of activities ";
cin>>n;
for(int i=1;i<=n;i++)
{
cout<<"\nEnter the start time of activity "<<i<<" ";
cin>>s[i];
cout<<"\nEnter the finish time of activity "<<i<<" ";
cin>>f[i];
key[i]=i;
}
}
//to get number of comparisons
void getcount()
{
cout<<"\nThe number of comparisons is "<<count;
}
//function to sort arrays according to their finish times
void sort()
{
int temp;
for(int i=2;i<=n;i++)
{
for(int j=i-1;j>0;j--)
{
if(f[j]>f[j+1])
{
temp=f[j];
f[j]=f[j+1];
f[j+1]=temp;
temp=s[j];
s[j]=s[j+1];
s[j+1]=temp;
temp=key[j];
key[j]=key[j+1];
key[j+1]=temp;
}
}
}
cout<<"\nActivities with their sorted finish times are :";
cout<<"\nActivity Start Finish";
for(int i=1;i<=n;i++)
cout<<"\n"<<key[i]<<" "<<s[i]<<" "<<f[i];
}
//function to schedule activities
void activity_selector()
{
a[1]=1;
int i=1;
for(int m=2;m<=n;m++)
{ count++;
if(s[m]>=f[i])
{
a[++k]=m;
i=m;
}
}
}
//to output array on console
void getdata()
{
cout<<"\nSelected activities are";
for(int i=1;i<=k;i++)
cout<<" "<<a[i];
}
void main()
{
setdata();
setcount();
sort();
activity_selector();
getdata();
getcount();
system("pause");
}

You might also like