Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Segmentation fault (core dumped). I can't figure out why it's doing this. #include using namespace std; int size = 1001; struct priority_element{ int command; int
Segmentation fault (core dumped). I can't figure out why it's doing this. #includeusing namespace std; int size = 1001; struct priority_element{ int command; int id; int priority; }; priority_element *array_elements = new priority_element[size]; void heapify_Up(int heapSize, priority_element *array_elements){ if(heapSize > 0){ int parent = (heapSize -1) /2; if(array_elements[heapSize].priority > array_elements[parent].priority){ priority_element temp = array_elements[parent]; array_elements[parent] = array_elements[heapSize]; array_elements[heapSize] = temp; heapify_Up(heapSize, array_elements); } } } void add_element(priority_element *array_elements, int id, int priority, int heapSize){ priority_element new_element; new_element.id = id; new_element.priority = priority; array_elements[heapSize] = new_element; if(heapSize > 0){ heapify_Up(heapSize, array_elements); } heapSize++; } void heapify_Down(int heapSize, priority_element *array_elements){ int parent = (heapSize -1)/2; int right = (2*parent) + 1; int left = right + 1; if((left > 0) && (right > 0) && (array_elements[left].priority > array_elements[right].priority)){ left = right; } if(left > 0){ priority_element temp = array_elements[heapSize]; array_elements[heapSize] = array_elements[left]; array_elements[left] = temp; heapify_Down(left, array_elements); } } void delete_element(priority_element *array_elements, int id, int heapSize){ priority_element last_element = array_elements[heapSize]; priority_element removing = array_elements[id]; last_element = removing; heapSize--; int parent = (heapSize -1)/2; if(array_elements[heapSize].priority < array_elements[parent].priority){ heapify_Up(heapSize, array_elements); } else{ heapify_Down(heapSize,array_elements); } } void reset(int id, int newPriority, int heapSize){ for(int i=0; i array_elements[parent].priority){ heapify_Up(heapSize, array_elements); } else{ heapify_Down(heapSize,array_elements); } } void read(int priority, priority_element *array_elements, int heapSize, int temp){ int highest; array_elements[0].priority = highest; int element; element = array_elements[0].id; } void pop(int priority, priority_element *array_elements, int heapSize){ int highest; array_elements[0].priority = highest; for(int i=0; i highest){ highest = array_elements[i].priority; } } priority_element last_element = array_elements[heapSize]; priority_element removing = array_elements[highest]; last_element = removing; heapSize--; int parent = (heapSize -1)/2; if(array_elements[heapSize].priority < array_elements[parent].priority){ heapify_Up(heapSize, array_elements); } else{ heapify_Down(heapSize,array_elements); } } void finalize(priority_element *array_elements, int heapSize, int priority, int temp){ while(heapSize > 0){ read(priority, array_elements,heapSize, temp); pop(priority , array_elements, heapSize); } } int main(int argc, const char * argv[]) { int count =0; int heapSize = 0; priority_element element[1001]; int i=0; while(cin >> element[i].command){ if(element[i].command == 1){ cin >> element[i].id>>element[i].priority; } if(element[i].command == 2){ cin >> element[i].id; } if(element[i].command == 3){ cin >> element[i].id>>element[i].priority; } i++; } int addTotal=0, deleteTotal=0, resetTotal=0, readTotal=0, popTotal=0, finalizeTotal=0, total=0; while(count <= i){ if(element[count].command == 1){ add_element(element, element[count].id, element[count].priority, heapSize); addTotal++; } if(element[count].command == 2){ delete_element(element, element[count].id, heapSize); deleteTotal++; } if(element[count].command == 3){ reset(element[count].id, element[count].priority, heapSize); resetTotal++; } if(element[count].command == 4){ int temp = addTotal + deleteTotal + resetTotal + popTotal; cout << element[0].id << element[0].priority << endl; read(element[count].priority, element, heapSize, temp); readTotal++; } if(element[i].command == 5){ pop(element[count].priority, element, heapSize); popTotal++; } if(element[count].command == 6){ finalizeTotal++; int temp = addTotal + deleteTotal + resetTotal + popTotal +readTotal +finalizeTotal; finalize(element, heapSize, element[count].priority, temp); } count++; } total = addTotal + deleteTotal + resetTotal + readTotal + popTotal + finalizeTotal; cout<< " "; cout<< " "; cout<< "=== ================================ "; cout<< addTotal << " total add commands processed. "; cout<< deleteTotal << " total delete commands processed. "; cout<< resetTotal << " total reset commands processed. "; cout<< readTotal << " total read commands processed. "; cout<< popTotal << " total pop commands processed. "; cout<< finalizeTotal << " total finalize commands processed. "; cout<< total << " total commands processed. "; }
Example input:
1 1 1 1 2 2 1 3 3 1 4 4 1 5 5 1 6 6 1 7 7 1 8 8 1 9 9 1 10 10 3 10 1 3 9 2 3 8 3 3 7 4 3 6 5 3 5 6 3 4 7 3 3 8 3 2 9 3 1 10 6
Key:
1 - add element 12 - delete element 2 3 - reset priority 3 4 - read 4 (query top element) 5 - pop 5 (remove top element) 6 - nalize 6 (print and remove all current queue contents)
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started