You are on page 1of 7

ASSIGNMENT NUMBER: 7

Name :Mridul KANTI SIKDER


Roll no:21CS8002
Subject :Design analysis and algorithm

// 7-part 1

#include <stdio.h>

int schedule_orders_a(int n, int a[], int b) {


int schedule[b][n];
int counter_time[b];
int total_time = 0;

for (int j = 0; j < b; j++) {


counter_time[j] = 0;
for (int i = 0; i < n; i++) {
schedule[j][i] = -1;
}
}

for (int i = 0; i < n; i++) {


int earliest_counter = 0;
for (int j = 1; j < b; j++) {
if (counter_time[j] <
counter_time[earliest_counter] ||
(counter_time[j] ==
counter_time[earliest_counter] && j < earliest_counter)) {
earliest_counter = j;
}
}

schedule[earliest_counter][i] = i;
counter_time[earliest_counter] += a[i];

if (counter_time[earliest_counter] > total_time) {


total_time = counter_time[earliest_counter];
}
}

for (int j = 0; j < b; j++) {


printf("Counter %d:", j + 1);
for (int i = 0; i < n; i++) {
if (schedule[j][i] != -1) {
printf(" %d", schedule[j][i] + 1);
}
}
printf("\n");
}

return total_time;
}

int main() {
int n;
printf("Enter number of customers:");
scanf("%d", &n);

int a[n];
printf("Time for each customers \n");
for (int i = 0; i < n; i++) {
printf("Time for customer %d:", i+1);
scanf("%d", &a[i]);
}

int b;
printf("Enter number of counters:");
scanf("%d", &b);
printf("\n");

int closing_time = schedule_orders_a(n, a, b);


printf("Cafe Closing Time: %d\n", closing_time);
printf("\n");
return 0;
}

Output:

PS C:\Users\HP\daa lab> cd "c:\Users\HP\daa lab\" ; if ($?) { gcc lab7.c -


cd "c:\Users\HP\daa lab\" ; if ($?) { gcc lab7.c -o lab7 } ; if ($?) { .\lab7 }

Enter number of customers:5


Time for each customers
Time for customer 1:5
Time for customer 2:4
Time for customer 3:5
Time for customer 4:7
Time for customer 5:5
Enter number of counters:6

Counter 1: 1
Counter 2: 2
Counter 3: 3
Counter 4: 4
Counter 5: 5
Counter 6:
Cafe Closing Time: 7

// 7_part 2

#include<stdio.h>
#include<stdlib.h>

int compare_integers(const void *A, const void *B) {


return (*(int *)B - *(int *)A);
}

void schedule_orders_b(int *processing_times, int n, int q) {


qsort(processing_times, n, sizeof(int), compare_integers);
}

int schedule_orders_a(int n, int p[], int q) {


int schedule[q][n];
int counter_time[q];
int total_time = 0;
schedule_orders_b(p,n,q);

for (int j = 0; j < q; j++)


{
counter_time[j] = 0;
for (int i = 0; i < n; i++)
{
schedule[j][i] = -1;
}
}

for (int i = 0; i < n; i++)


{
int earliest_counter = 0;
for (int j = 1; j < q; j++)
{
if (counter_time[j] <
counter_time[earliest_counter] ||
(counter_time[j] ==
counter_time[earliest_counter] && j < earliest_counter))
{
earliest_counter = j;
}
}

schedule[earliest_counter][i] = i;
counter_time[earliest_counter] += p[i];

if (counter_time[earliest_counter] > total_time)


{
total_time = counter_time[earliest_counter];
}
}

for (int j = 0; j < q; j++)


{
printf("Counter %d:", j + 1);
for (int i = 0; i < n; i++)
{
if (schedule[j][i] != -1)
{
printf(" %d", schedule[j][i] + 1);
}
}
printf("\n");
}

return total_time;
}

int main()
{
int n;
printf("number of customers:");
scanf("%d", &n);

int t[n];
printf("Time for per customers \n");
for (int i = 0; i < n; i++) {
printf("Time for customer %d:", i+1);
scanf("%d", &t[i]);
}

int k;
printf(" number of counters:");
scanf("%d", &k);
printf("\n");
int closing_time = schedule_orders_a(n, t, k);
printf("Cafe Closing Time: %d\n", closing_time);
printf("\n");

return 0;
}

OUTPUT :

PS C:\Users\HP\daa lab> cd "c:\Users\HP\daa lab\" ; if ($?) { gcc lab7_part2.c -o lab7_part2


} ; if ($?) { .\lab7_part2 }

number of customers:8
Time for per customers
Time for customer 1:5
Time for customer 2:5
Time for customer 3:5
Time for customer 4:4
Time for customer 5:5
Time for customer 6:4
Time for customer 7:5
Time for customer 8:4
number of counters:5

Counter 1: 1 6
Counter 2: 2 7
Counter 3: 3 8
Counter 4: 4
Counter 5: 5
Cafe Closing Time: 9

You might also like