Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7713771
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T01:57:26+00:00 2026-06-01T01:57:26+00:00

struct node { int id; float weight; }; int find_last(struct node Prio_Q[]) { int

  • 0
struct node
{
    int id;
    float weight;
};

int find_last(struct node Prio_Q[])
{
    int i = 1;
    while (Prio_Q[i].id != -1)
    {
        i += 1;
    }
    return i;
}

void initialize_Q(struct node Prio_Q[], int size)
{

    int i;

    for (i = 0; i < size; i ++)
    {
        Prio_Q[i].id = -1;
        Prio_Q[i].weight = -1;

    }
    //printf("The queue is %f", Prio_Q[3].weight);
}

void enque_Q(struct node Prio_Q[], struct node node, int size)
{
    int i = find_last(Prio_Q);

    Prio_Q[i].id = node.id;
    Prio_Q[i].weight = node.weight;
    printf("The last index is %d\n", i);
    heapify_up(Prio_Q, i);
}

void heapify_up(struct node Prio_Q[], int i)
{    

    if (Prio_Q[i/2].weight > Prio_Q[i].weight)
    {
        swap_node(Prio_Q,i/2, i);
        heapify_up(Prio_Q, i/2);
    }
}

void swap_node(struct node Prio_Q[], int i, int j)
{
    struct node temp;
    temp = Prio_Q[i];
    Prio_Q[i] = Prio_Q[j];
    Prio_Q[j] = temp;
    //printf("smething has been swapped.\n");
} 

int main(int argc, char *argv[])
{
    struct node node;
    struct node Prio_Q[10];

    int size = 10;
    initialize_Q(Prio_Q, 11);

    node.id = 5;
    node.weight = 11;

    for(int m = 0; m < size+1; m++)
    {
        printf("The %dth element in Que is %d with weight %f.\n", m, Prio_Q[m].id, Prio_Q[m].weight);
    }

}

This is a priority queue I built, but if you test the code, you will see that the queue will automatically add a node to its last index before I actually ask the function to do so.

In the main function, I only made the node to have two values, but I didnt enqueue the node into the priority queue array. the array will automatically add the node to the last index, can anyone help me please?

Thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T01:57:28+00:00Added an answer on June 1, 2026 at 1:57 am

    Welcome to C; the language does not help you avoid array access out-of-bounds, which is a particular problem with your code:

    From main():

    struct node Prio_Q[10];
    
    int size = 10;
    initialize_Q(Prio_Q, 11);
    

    Note that you’re calling initialize_Q() with a size of 11. That means your for loop will cause an array access beyond the end of your Prio_Q array:

    void initialize_Q(struct node Prio_Q[], int size)
    {
    
        int i;
    
        for (i = 0; i < size; i ++)
        {
            Prio_Q[i].id = -1;      /* BUG when i == 10 */
            Prio_Q[i].weight = -1;
    

    You should probably use a #define, enum, or const variable to store the size of your array in one location. That will drastically reduce the chances of writing little bugs like this.

    Your find_last() routine should do some bounds checking on the size of the array. This code should work fine IFF the rest of your code has no bugs. You might as well re-write this function to also ensure it has not walked off the end of the array. (How will it handle a completely-full array? Hint: Poorly. 🙂

    int find_last(struct node Prio_Q[])
    {
        int i = 1;
        while (Prio_Q[i].id != -1)
        {
            i += 1;
        }return i;
    }
    

    And for your output (which ought to be in its own function, so you can sprinkle it throughout your program liberally):

    for(int m = 0; m < size+1; m++)
    {
        printf("The %dth element in Que is %d with weight %f.\n", m, Prio_Q[m].id, Prio_Q[m].weight);
    }
    

    Again, you’ve accessed beyond the end of the array, when m == 10.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

void addNewNode (struct node *head, int n) { struct node* temp = (struct node*)
I have the following code struct Node { int accnumber; float balance; Node *next;
I have the below struct: struct node { float val; int count; } I
#include<stdio.h> #include<conio.h> #include<alloc.h> struct node { int data; struct node*link; }; void push(struct node*,int);
struct Node { int value Node* next; } typedef List Node* const Set operator
struct node { node(int _value, node* _next) : value(_value), next(_next) {} int value; node*
struct Edge; struct Node { int id; vector<Edge> *edges; }; struct Edge { int
I've got a node struct struct Node{CString text, int id;}; in a sorted vector.
Having construction in a form: struct Node { Node():left_(nullptr), right_(nullptr) { } int id_;
// Variables typedef struct node { int value; struct node *next; }mynode; // Globals

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.