Write two programs in C, which simulate priority queues, yet use different algorithms. Use the method at the bottom of this page to time the execution of the algorithms.

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");

//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 PreviewSolution 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");
$60.00 for this solution

PayPal, G Pay, ApplePay, Amazon Pay, and all major credit cards accepted.

Find A Tutor

View available C-Family Programming Tutors

Get College Homework Help.

Are you sure you don't want to upload any files?

Fast tutor response requires as much info as possible.

Upload a file
Continue without uploading

We couldn't find that subject.
Please select the best match from the list below.

We'll send you an email right away. If it's not in your inbox, check your spam folder.

  • 1
  • 2
  • 3
Live Chats