Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /******************************************************************************
- Online C Compiler.
- Code, Compile, Run and Debug C program online.
- Write your code in this editor and press "Run" button to compile and execute it.
- *******************************************************************************/
- #include <stdio.h>
- #define min(a,b) a<=b ? a : b;
- typedef struct map
- {
- int weight;
- int index;
- } map;
- int
- main ()
- {
- int queueLength[5] = { 1, 2, 3, 20, 5 };
- int radioLinkQuality[5] = { 3, 3, 1, 2, 1 };
- int timeSinceLastServed[5] = { 2, 1, 0, 4, 2 };
- map userList[5], aux;
- int freeChannels = 10, servedAmount = 0;
- int i = 0, j = 0, n = 5;
- for (i = 0; i < n; i++)
- {
- userList[i].index = i;
- userList[i].weight = 0;
- }
- for (i = 0; i < n; i++)
- userList[i].weight = radioLinkQuality[i] * timeSinceLastServed[i];
- printf ("\n\n");
- for (i = 0; i < n; i++)
- printf ("Computed weight of %d is %d.\n", i + 1, userList[i].weight);
- for (i = 0; i < n - 1; i++)
- for (j = i; j < n; j++)
- if (userList[i].weight <= userList[j].weight)
- {
- aux = userList[i];
- userList[i] = userList[j];
- userList[j] = aux;
- }
- printf ("\n\n");
- printf ("Current order of users is: %d (%d)", userList[0].index + 1,
- userList[0].weight);
- for (i = 1; i < n; i++)
- printf (", %d (%d)", userList[i].index + 1, userList[i].weight);
- printf (".\n");
- printf ("\n\n");
- for (i = 0; i < n; i++)
- {
- servedAmount = min (userList[i].weight, queueLength[userList[i].index]);
- if (freeChannels == 0)
- {
- timeSinceLastServed[userList[i].index]++;
- }
- else
- {
- if (freeChannels >= servedAmount)
- {
- printf ("Served user %d %d channels.\n", userList[i].index + 1,
- servedAmount);
- timeSinceLastServed[userList[i].index] = 0;
- freeChannels -= servedAmount;
- }
- else
- {
- printf ("Served user %d %d channels.\n", userList[i].index + 1,
- freeChannels);
- timeSinceLastServed[userList[i].index] = 0;
- freeChannels -= freeChannels;
- }
- }
- }
- printf ("\n\n");
- for (i = 0; i < n; i++)
- printf ("Time since last served of %d is %d.\n", i + 1,
- timeSinceLastServed[i]);
- return 0;
- }
Add Comment
Please, Sign In to add comment