|
[x]
Posted via EE Mobile
|
||
Search, ask, and monitor your questions on the go with EE Mobile. Visit Experts Exchange from your mobile device and never be out of touch again. |
||
| Question |
|
[x]
Attachment Details
|
||
|
[x]
The Solution Rating System
|
||
With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.
Your Input Matters If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support. Thank you! |
||
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: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184: 185: 186: 187: 188: 189: 190: 191: 192: 193: 194: 195: 196: 197: 198: 199: 200: 201: 202: 203: 204: 205: 206: 207: 208: |
#include <stdio.h>
#include <stdlib.h>
struct tasks
{
int priority;
char command[24];
};
struct heap
{
struct tasks* store;
int size; // Number of values in the heap
int capacity; // Maximum number of values before we
// expand the heap
};
struct heap* heapify(int array[], int num_elements);
struct heap *new_heap(int initial_capacity);
void percolate_down(struct heap* h, int position);
void percolate_up(struct heap* h, int position);
void expand_heap(struct heap* h);
void deletemax(struct heap* h);
void insert(struct heap* h, int value);
struct heap *new_heap(int initial_capacity)//new data is stored
{
struct heap *h;
h = malloc(sizeof(struct heap));
h->store = calloc(initial_capacity, sizeof(struct tasks));
h->size = 0;
h->capacity = initial_capacity;
return h;
}
void percolate_down(struct heap* h, int position)
{
int temp;
if(position * 2 > h->size)
return;
if(position * 2 == h->size)
{
if(h->store[position*2].priority <= h->store[position].priority)
return;
else
{
temp = h->store[position].priority;
h->store[position].priority = h->store[position*2].priority;
h->store[position*2].priority = temp;
return;
}
}
if(h->store[position*2].priority <= h->store[position].priority
&& h->store[position*2 + 1].priority <= h->store[position].priority)
return;
// Percolate to the left
if(h->store[position*2].priority >= h->store[position*2 + 1].priority)
{
temp = h->store[position].priority;
h->store[position].priority = h->store[position*2].priority;
h->store[position*2].priority = temp;
percolate_down(h, position * 2);
return;
}
// Percolate to the right
temp = h->store[position].priority;
h->store[position].priority = h->store[position*2 + 1].priority;
h->store[position*2 + 1].priority = temp;
percolate_down(h, position * 2 + 1);
}
void percolate_up(struct heap* h, int position)
{
int temp;
if(position == 1)
return;
if(h->store[position/2].priority >= h->store[position].priority)
return;
temp = h->store[position].priority;
h->store[position].priority = h->store[position/2].priority;
h->store[position/2].priority = temp;
percolate_up(h, position/2);
}
void expand_heap(struct heap* h)
{
struct tasks* newstore;
int i;
newstore = calloc(h->capacity * 2, sizeof(struct tasks));
for(i = 1; i <= h->size; i++)
{
newstore[i] = h->store[i];
}
free(h->store);
h->store = newstore;
h->capacity *= 2;
}
struct heap* heapify(int array[], int num_elements)// sorts data
{
struct heap* h;
int i;
h = new_heap((num_elements + 1) * 2);
for(i = 0; i < num_elements; i++)
{
h->store[i+1].priority = array[i];
}
h->size = num_elements;
for(i = h->size / 2; i >= 1; i--)
percolate_down(h, i);
return h;
}
void deletemax(struct heap* h)
{
printf("Program %s with priority %d has been executed.\n", h->store[1].command, h->store[1].priority);
strcpy(h->store[1].command, h->store[h->size].command);
h->store[1].priority = h->store[h->size].priority;
h->size--;
percolate_down(h, 1);
}
void insert(struct heap* h, int value)
{
printf("Program %s with priority %d has arrived.\n", h->store->command, h->store->priority);
h->size++;
if(h->size >= h->capacity)
expand_heap(h);
h->store[h->size].priority = value;
percolate_up(h, h->size);
}
int main(void)
{
FILE *ifp;
char infile[48];
char command[24];
int valid = 0, priority;
int i=0;
int n = 0;
char action;
struct tasks p;
struct heap *h;
(h)=new_heap(48);
while(!valid)
{
printf("What is the name of the input file?\n");
scanf("%s", infile);
ifp = fopen(infile, "r");
if(ifp == NULL)
printf("File not found!\n");
else
{
valid = 1;
printf("*******_______-------*******\n");//locate crash
for(i = 0; i < n; i++)
{
fscanf(ifp, "%s", action);
if(strcmp(action, "new")==0)
{
fscanf(ifp, "%d %s", p.priority, p.command);
printf("Program %s with priority %d has arrived.\n", p.command, p.priority);
}
else if(strcmp(action, "execute")==0)
{
if (h->size==0)
printf("Heap is empty, no programs executed!\n");
else
{
deletemax(&h);
}
}
}
}
}
valid = 0;
fclose(ifp);
system("PAUSE");
return 0;
}
|
Advertisement
| Hall of Fame |