-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main Proposed Optimized on timing.c
86 lines (86 loc) · 1.8 KB
/
Main Proposed Optimized on timing.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include<stdio.h>
#include<stdlib.h>
#define MIN(x,y) (x<y)?x:y
int main()
{
int orate,drop=0,cap,x,count=0,inp[10]={0},i=0,j,nsec,ch;
int bucketCount = 1, buckets[100]={0};
printf("\n enter bucket size : ");
scanf("%d",&cap);
printf("\n enter output rate :");
scanf("%d",&orate);
do{
printf("\n enter number of packets coming at second %d :",i+1);
scanf("%d",&inp[i]);i++;
printf("\n enter 1 to contiue or 0 to quit..........");
scanf("%d",&ch);
}while(ch);
nsec=i;
printf("\n second \t recieved \t sent \t dropped \t remained \n");
int totalIn = 0, totalOut = 0, totalTime = 0;
for(i=0;totalIn > totalOut || i<nsec;i++) {
printf(" %d",i+1);
printf(" \t%d\t ",inp[i]);
printf(" \t %d\t ",MIN((inp[i]+count),orate));
int totalSent = 0;
totalIn += inp[i];
for(j = 1; j <= bucketCount; j++){
if(cap - buckets[j] >= inp[i]){
buckets[j] += inp[i];
inp[i] = 0;
} else {
inp[i] -= cap - buckets[j];
buckets[j] = cap;
}
}
while(inp[i] > 0){
bucketCount++;
if(inp[i] <= 0) {
inp[i] = 0;
break;
}
if(cap >= inp[i]){
buckets[ bucketCount ] = inp[i];
inp[i] = 0;
} else {
buckets [ bucketCount ] = cap;
inp[i] -= cap;
}
}
for(j = 1; j <= bucketCount; j++){
if(buckets[j] == 0){
bucketCount = j - 1;
break;
}
if(buckets[j] > orate) {
buckets[j] -= orate;
totalSent += orate;
}
else {
totalSent += buckets[j];
buckets[j] = 0;
}
}
//if((x=inp[i]+count-orate)>0)
//{
//if(x>cap)
//{
//count=x-cap;
//bucketCount++;
//}
//else
//{
//count=x;
//drop=0;
//}
//}
//else
//{
//drop=0;
//count=0;
//}
totalOut += totalSent;
printf(" \t %d \t %d \n",bucketCount,totalSent);
}
return 0;
}