Skip to content

Commit e13e6ac

Browse files
New Commit
2 parents 32e3075 + a9b6520 commit e13e6ac

File tree

3 files changed

+271
-0
lines changed

3 files changed

+271
-0
lines changed

activity_select.c

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
<<<<<<< HEAD
12
#include <stdio.h>
23
int start[100], end[100], acti[100];
34

@@ -81,4 +82,89 @@ int main ()
8182
printf ("Do you want to continue? (1/0): ");
8283
scanf ("%d", &wish);
8384
}while (wish!=0);
85+
=======
86+
#include <stdio.h>
87+
int start[100], end[100], acti[100];
88+
89+
void sort (int act) //Sorting the activities wrt finish time
90+
{
91+
int temp, i, j, temp2;
92+
for (i= act-1;i>=1; i--)
93+
{
94+
for (j=1;j<=i;j++)
95+
{
96+
if (end[j-1]>end[j])
97+
{
98+
temp = end[j];
99+
end[j]= end[j-1];
100+
end[j-1] = temp;
101+
102+
temp2 = start[j];
103+
start[j] = start[j-1];
104+
start[j-1] = temp2;
105+
}
106+
}
107+
}
108+
}
109+
110+
111+
void activity_sel (int act)
112+
{
113+
int i,j;
114+
acti[0] = 0;
115+
j = 0;
116+
for (i=1;i<act;i++)
117+
{
118+
if (start[i]>=end[j])
119+
{
120+
acti[i] = i;
121+
j=i;
122+
}
123+
}
124+
125+
printf ("Activity set: ");
126+
for (i = 0; i<act; i++)
127+
printf ("%d ", acti[i]);
128+
printf ("\n");
129+
}
130+
131+
132+
int main ()
133+
{
134+
int i, act, wish;
135+
do
136+
{
137+
printf ("Enter the number of Activities to be done: ");
138+
scanf ("%d", &act);
139+
140+
printf ("Enter the start and end time of the activities: \n");
141+
for (i =0;i<act;i++)
142+
{
143+
printf ("%d-->", i+1);
144+
printf ("Start Time: ");
145+
scanf ("%d", &start[i]);
146+
printf ("\tEnd Time: ");
147+
scanf ("%d", &end[i]);
148+
}
149+
150+
for(i=0;i<act;i++)
151+
acti[i] = -1; // Setting all values of the acti array to -1
152+
153+
sort (act);
154+
155+
printf ("Start set: ");
156+
for (i = 0; i<act; i++)
157+
printf ("%d ", start[i]);
158+
printf ("\n");
159+
printf ("End set: ");
160+
for (i = 0; i<act; i++)
161+
printf ("%d ", end[i]);
162+
printf ("\n");
163+
164+
activity_sel (act);
165+
166+
printf ("Do you want to continue? (1/0): ");
167+
scanf ("%d", &wish);
168+
}while (wish!=0);
169+
>>>>>>> a9b6520f1aa074c45bc777fd42beb569ca960413
84170
}

fractional_knapsack.c

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
<<<<<<< HEAD
12
#include <stdio.h>
23
float wt[100], pt[100];
34
float pw[100];
@@ -98,4 +99,106 @@ int main()
9899

99100

100101
knapsack(capacity, n);
102+
=======
103+
#include <stdio.h>
104+
float wt[100], pt[100];
105+
float pw[100];
106+
int capacity;
107+
108+
void sort(int n)
109+
{
110+
int i, j;
111+
float t, t1, t2;
112+
for(int i=0;i<n-1;i++)
113+
{
114+
for(int j=0;j<n-1-i;j++)
115+
{
116+
if(pw[j]<pw[j+1]){
117+
t= pw[j];
118+
t1 = wt[j];
119+
t2 = pt[j];
120+
121+
pw[j] = pw[j+1];
122+
wt[j] = wt[j+1];
123+
pt[j] = pt[j+1];
124+
125+
pw[j+1] = t;
126+
wt[j+1] = t1;
127+
pt[j+1] = t2;
128+
}
129+
}
130+
}
131+
}
132+
133+
void knapsack(int capacity, int n)
134+
{
135+
int i,k=0;
136+
float mul, profit = 0;
137+
// Calculating the unit profit values
138+
for(i = 0; i<n; i++)
139+
{
140+
pw[i] = pt[i]/wt[i];
141+
}
142+
143+
sort(n); // Sorting the arrays wrt unit profit pw
144+
145+
/*for (i=0;i<n;i++)
146+
{
147+
printf("%f ", pw[i]);
148+
}
149+
printf ("\n");
150+
for (i=0;i<n;i++)
151+
{
152+
printf("%f ", pt[i]);
153+
}
154+
printf ("\n");
155+
for (i=0;i<n;i++)
156+
{
157+
printf("%f ", wt[i]);
158+
}
159+
printf ("\n");
160+
*/
161+
162+
while(capacity != 0)
163+
{
164+
if(capacity >= wt[k])
165+
{
166+
capacity = (float)capacity - wt[k];
167+
profit = profit+pt[k];
168+
k++;
169+
}
170+
else
171+
{
172+
mul = capacity/wt[k];
173+
wt[k] = wt[k]*mul;
174+
pt[k] = pt[k]*mul;
175+
capacity = (float)capacity - wt[k];
176+
profit = profit + pt[k];
177+
k++;
178+
}
179+
}
180+
printf("The Maximum profit that can be made is: %f", profit);
181+
}
182+
183+
184+
int main()
185+
{
186+
int n,i;
187+
printf("Enter the number of weights and profit: ");
188+
scanf("%d", &n);
189+
190+
printf("Enter the capacity of the knapsack: ");
191+
scanf("%d", &capacity);
192+
193+
for(i = 0; i< n; i++)
194+
{
195+
printf("Weight: ");
196+
scanf("%f", &wt[i]);
197+
printf("Profit: ");
198+
scanf("%f", &pt[i]);
199+
}
200+
201+
202+
knapsack(capacity, n);
203+
>>>>>>> a9b6520f1aa074c45bc777fd42beb569ca960413
101204
}

job_scheduling.c

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
<<<<<<< HEAD
12
#include<stdio.h>
23

34
int p[100], jobs, d[100], final_dead[100];
@@ -77,4 +78,85 @@ int main(){
7778
input();
7879
jobSelection();
7980
return 0;
81+
=======
82+
#include<stdio.h>
83+
84+
int p[100], jobs, d[100], final_dead[100];
85+
86+
void input(){
87+
printf("Enter the profits and deadlines\n\n");
88+
for(int i=0;i<jobs;i++){
89+
printf("Job %d\n", i+1);
90+
printf("Profit : ");
91+
scanf("%d", &p[i]);
92+
printf("Deadline : ");
93+
scanf("%d", &d[i]);
94+
}
95+
printf("\n");
96+
}
97+
98+
void sort(){
99+
int i, j, t1, t2;
100+
for(i=0;i<jobs-1;i++){
101+
for(j=0;j<jobs-1-i;j++){
102+
if(p[j]<p[j+1]){
103+
t1 = p[j];
104+
t2 = d[j];
105+
106+
p[j] = p[j+1];
107+
d[j] = d[j+1];
108+
109+
p[j+1] = t1;
110+
d[j+1] = t2;
111+
}
112+
}
113+
}
114+
}
115+
116+
void jobSelection(){
117+
sort();
118+
int i, j;
119+
printf("Profit Array: ");
120+
for(i=0;i<jobs;i++){
121+
printf("%d ", p[i]);
122+
}
123+
124+
printf("\nDeadlines: ");
125+
for(i=0;i<jobs;i++)
126+
printf("%d ", d[i]);
127+
128+
int max = d[0];
129+
for(i=0;i<jobs;i++)
130+
if(d[i]>max) max = d[i];
131+
132+
int profit = 0; //Stores the maximum profit
133+
134+
for(i=0;i<max;i++)
135+
final_dead[i] = 0;
136+
137+
for(i=max;i>=0;i--){
138+
if(final_dead[i]!=0)
139+
continue;
140+
else{
141+
//Searching for the deadline index in array d
142+
for(j=0;j<jobs;j++){
143+
if(d[j]==i){
144+
final_dead[i] = p[j]; //Store the profits in the final deadline list
145+
profit += p[j];
146+
break;
147+
}
148+
}
149+
}
150+
}
151+
printf("\nMaximum Profit = %d\n", profit);
152+
153+
}
154+
155+
int main(){
156+
printf("Enter number of jobs: ");
157+
scanf("%d", &jobs);
158+
input();
159+
jobSelection();
160+
return 0;
161+
>>>>>>> a9b6520f1aa074c45bc777fd42beb569ca960413
80162
}

0 commit comments

Comments
 (0)