Thread: make a new entry in C

  1. #1
    Registered User
    Join Date
    Nov 2021
    Posts
    11

    make a new entry in C

    Code:
    struct pqueue
    {
        int numEntries;
        struct entry entries[10];           
        }
    pqueue;
     
    case 'e':
    void enqueue(struct pqueue*pqueue, struct entry entry)
             {
        if (pqueue->numEntries == 10)
        {
            printf("priority queue already full!");
            return;
        }
        else
    ...
    i try to make a priorityqueue and after the else there should follow a code where i create a new entry and obviously numentries should increase by 1 and if it worked well a text should be displayed "a new entry has successfully been added"
    how do i do this?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    How do you plan to implement the priority queue?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Nov 2021
    Posts
    11
    What do you exactly mean by that? I tryna make a enqueue, a dequeue, print queue and exit. and output should look something like that

    Choose action: print queue (p), enqueue entry (e), dequeue entry (d) or exit (x): e

    Choose priority: lowest (L), low (l), normal (n), high (h), highest (H):
    a

    Input invalid! Try again:
    L

    Choose message:
    Do_something!

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Maybe you could pick a better font colour scheme.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Nov 2021
    Posts
    11
    Choose action: print queue (p), enqueue entry (e), dequeue entry (d) or exit (x): e

    Choose priority: lowest (L), low (l), normal (n), high (h), highest (H): a

    Input invalid! Try again: L

    Choose message: Do_something!

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    So does your 'struct entry' have a member called priority, which can take values represented by "lowest (L), low (l), normal (n), high (h), highest (H)" ?

    More to the point, you've only shown the user interface.
    The real meat of the problem is how implement the priority queue, of which there is very little to go on.

    For example, if your queue has just one 'n' entry, and you try to add a 'H' entry, do you know how to do that?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Nov 2021
    Posts
    11
    i have problems with understand these things. yes my struct entry does have a member called priority. and no i dont know how to do that.
    probably something like =
    Code:
    struct entry getNewEntry(){
            struct entry newEntry;
            newEntry.prio = 'H';
            newEntry.message = "msg1";
            return newEntry;
    }
    do you have discord so i could ask some questions if you are not busy right now? Alan-FH#6689

  8. #8
    Registered User
    Join Date
    Oct 2019
    Posts
    82
    Quote Originally Posted by awox View Post

    do you have discord so i could ask some questions if you are not busy right now? Alan-FH#6689
    Just ask right here...

  9. #9
    Registered User
    Join Date
    Nov 2021
    Posts
    11
    Quote Originally Posted by ghoul View Post
    Just ask right here...
    already did i dont know how to make enqueue an entry for the priorityqueue

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    One way perhaps
    Code:
    struct pqueue
    {
        int numEntries[5];
        struct entry entries[5][10];           
        }
    pqueue;
    Adding an entry is simply adding to the right queue, where H=0, h=1, n=2 etc.

    If you just have the one queue, then you have to search all the entries to find out where the newEntry.prio should go, by comparing with all the pqueue.entries[i].prio.

    Was your previous homework just 'queue'?
    Because if you haven't managed to understand and implement a regular queue to begin with, then priority queue is just an unnecessarily large jump from your current experience.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  11. #11
    Registered User
    Join Date
    Nov 2021
    Posts
    11
    Quote Originally Posted by Salem View Post
    One way perhaps
    Code:
    struct pqueue
    {
        int numEntries[5];
        struct entry entries[5][10];           
        }
    pqueue;
    Adding an entry is simply adding to the right queue, where H=0, h=1, n=2 etc.

    If you just have the one queue, then you have to search all the entries to find out where the newEntry.prio should go, by comparing with all the pqueue.entries[i].prio.

    Was your previous homework just 'queue'?
    Because if you haven't managed to understand and implement a regular queue to begin with, then priority queue is just an unnecessarily large jump from your current experience.




    It do be like that for us: We learn Structs, Enums, Pointers, whatsoever. and then we get a massive homework where everything is mixed up crazy, then you lose overview for everything. Thats where I am at right now.


    the numEntries should be there to track the number of entries? it should be max 10. so doesnt i have to be numentries[10]? if i use an int array. is it better to use an array or just the int variable?


    struct pqueue{
    int numEntries;
    struct entry entries[10];
    }pqueue;

    this is what the entry should contain:

    struct entry{
    char message[32];
    priority priority;
    }entry;



    Data Structure - Priority Queue

    tried something like this but ehh dont understand the steps at all
    Last edited by awox; 11-11-2021 at 11:41 AM.

  12. #12
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    It's all there in PriorityQueueDemo.c

    Focus on what this is doing.
    // if data is larger, shift existing item to right end
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  13. #13
    Registered User
    Join Date
    Nov 2021
    Posts
    11
    Quote Originally Posted by Salem View Post
    It's all there in PriorityQueueDemo.c

    Focus on what this is doing.
    // if data is larger, shift existing item to right end
    in this priorityqueuedemo.c "entry" is my "data"?

  14. #14
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Quote Originally Posted by awox View Post
    in this priorityqueuedemo.c "entry" is my "data"?
    Yes.
    All you need is some way to compare one entry with another.

    You need to be able to do this with entries.
    if(data > intArray[i])
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to make blank entry = default
    By Zzaacchh in forum C++ Programming
    Replies: 3
    Last Post: 07-04-2008, 12:32 PM
  2. validation entry
    By Fizz in forum C++ Programming
    Replies: 5
    Last Post: 06-28-2004, 04:28 PM
  3. DNS entry changes
    By Thantos in forum Tech Board
    Replies: 4
    Last Post: 09-02-2003, 08:57 PM
  4. Salary for entry level C position- How much can you make?
    By Terrance in forum A Brief History of Cprogramming.com
    Replies: 9
    Last Post: 08-10-2002, 11:54 PM
  5. Registry entry from AIM help!
    By SyntaxBubble in forum Windows Programming
    Replies: 3
    Last Post: 01-02-2002, 05:00 PM

Tags for this Thread