Question
Each program prompts the user to enter an integer n and takes as input a file containing a list of numbers (I have the file) . Each program then enqueues n numbers into a priority queue then dequeues and prints the numbers to a file.
The two programs implement this functionality in the following ways:
1) The first program inserts elements into a priority queue by using an unsorted list implemented by an array. To dequeue, the program searches the array for the smallest number and then removes it by replacing it with the last element in the list. For example, if the list is {20, 1, 2, 5, 8}, then after the smallest number is extracted, the list becomes {20, 1, 8, 5}.
2) The second program makes a min-heap of the numbers by inserting the numbers one at a time into the heap and then dequeues the numbers from the heap (while maintaining the heap). The program uses an array to implement the heap. (Do not implement in-place heap-sort for this part.)
Use this to measure execution time in C:
/* The following header file defines clock_t datatype, clock() function, as well
as the constant CLOCKS_PER_SEC */
#include <time.h>
clock_t start = clock();
/* Code you want timed here */
double timeElapsed = ((double)clock() - start) / CLOCKS_PER_SEC;
Use this to read from a file:
FILE *fp;
fp = fopen("test_dat.txt", "r");
if (fp == NULL) {
printf("Error opening test_dat.txt");
exit(1);
}
//create array size according to user input and read values into array
int array[someValue];
int i;
for (i = 0; i < someValue; i++)
{
fscanf(fp, "%d", &array[i]);
}
Solution Preview
This material may consist of step-by-step explanations on how to solve a problem or examples of proper writing, including the use of citations, references, bibliographies, and formatting. This material is made available for the sole purpose of studying and learning - misuse is strictly forbidden.
#include <stdio.h>#include <stdlib.h>
#include <time.h>
void enqueue(int array[], int val, int *size);
int dequeue(int array[], int *size);
void rebulid(int array[], int size, int root);
int main(int argc, char** argv) {
int someValue;
int tmp;
int size;
FILE *fp;
int i;
double timeElapsed;
printf("nNter number value to read: ");
scanf("%d", &someValue);
clock_t start = clock();
fp = fopen("test_dat.txt", "r");
if (fp == NULL) {
printf("Error opening test_dat.txt");
exit(1);
}...