Thread: Quick Help for Code

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    8

    Quick Help for Code

    Code:
    #include <stdio.h>
    
    #define EMPTY -1
    #define INIT_SIZE 10
    
    // Stores our queue.
    struct queue {
        int* elements;
        int front;
        int numElements;
        int queueSize;
    };
    
    void init(struct queue* qPtr);
    int enqueue(struct queue* qPtr, int val);
    int dequeue(struct queue* qPtr);
    int empty(struct queue* qPtr);
    int front(struct queue* qPtr);
    
    int main() {
         
         int i;
         
        // Allocate space for our queue and initialize it.
        struct queue* MyQueuePtr = (struct queue*)malloc(sizeof(struct queue));
        init(MyQueuePtr);
        
        // Enqueue some items.
        enqueue(MyQueuePtr, 3);
        enqueue(MyQueuePtr, 7);
        enqueue(MyQueuePtr, 4);
        
        // Try one dequeue.
        printf("Dequeue %d\n", dequeue(MyQueuePtr));
        
        // Enqueue one more item, then try several dequeues and one front.
        enqueue(MyQueuePtr, 2);
        printf("Dequeue %d\n", dequeue(MyQueuePtr));
        printf("At Front of Queue Now: %d\n", front(MyQueuePtr));
        printf("Dequeue %d\n", dequeue(MyQueuePtr));
        printf("Dequeue %d\n", dequeue(MyQueuePtr));
        
        // See if we can detect an empty queue.
        printf("Is empty: %d\n", empty(MyQueuePtr));
        
        // Try enqueuing and dequeuing again to make sure that our previous
        // operations didn't "corrupt" the queue.
        enqueue(MyQueuePtr, 5);
        enqueue(MyQueuePtr, 9);
        
        // Try lots of enqueues to test the dynamic capability of the queue.
        for (i=30; i<60; i++)
            enqueue(MyQueuePtr, i);
            
        // Dequeue everything.
        while (!empty(MyQueuePtr))
            printf("Dequeue %d\n", dequeue(MyQueuePtr));
        
        system("PAUSE");
        return 0;
    }
    
    // Pre-condition: qPtr points to a valid struct queue.
    // Post-condition: The struct that qPtr points to will be set up to represent an
    //                 empty queue.
    void init(struct queue* qPtr) {
         
         // The front index is 0, as is the number of elements.
         qPtr->elements = (int*)malloc(sizeof(int)*INIT_SIZE);
         qPtr->front = 0;
         qPtr->numElements = 0;
         qPtr->queueSize = INIT_SIZE;
    }
    
    // Pre-condition: qPtr points to a valid struct queue and val is the value to
    //                enqueue into the queue pointed to by qPtr.
    // Post-condition: If the operation is successful, 1 will be returned, otherwise
    //                 no change will be made to the queue and 0 will be returned.
    
    // Note: Right now, I don't know how to detect that the realloc failed, so 0
    //       does not get returned.
    
    int enqueue(struct queue* qPtr, int val) {
    
        int i;
        
        // Regular case where our queue isn't full.
        if (qPtr->numElements != qPtr->queueSize) {
                 
            // Enqueue the current element. Note the use of mod for the wraparound
            // case. Edit the number of elements.
            qPtr->elements[(qPtr->front+qPtr->numElements)%qPtr->queueSize] = val;
            (qPtr->numElements)++;
             
            // Signifies a successful operation.
            return 1;
        }
        
        // Case where the queue is full, we must find more space before we enqueue.
        else {
             
             // Allocate more space!
             realloc(qPtr->elements, (qPtr->queueSize)*sizeof(int)*2);
             
             // Copy all of the items that are stored "before" front and copy them
             // into their new correct locations.
             for (i=0; i<=qPtr->front-1; i++)
                 qPtr->elements[i+qPtr->queueSize] = qPtr->elements[i];     
             
             // Enqueue the new item, now that there is space. We are guaranteed that
             // no wraparound is necessary here.
             qPtr->elements[i+qPtr->queueSize] = val;    
             
             // More bookkeeping: The size of the queue as doubled and the number of
             // elements has increased by one.
             (qPtr->queueSize) *= 2;
             (qPtr->numElements)++;
             
             return 1;    
        }
            
    }
    
    // Pre-condition: qPtr points to a valid struct queue.
    // Post-condition: If the queue pointed to by qPtr is non-empty, then the value
    //                 at the front of the queue is deleted from the queue and 
    //                 returned. Otherwise, -1 is returned to signify that the queue
    //                 was already empty when the dequeue was attempted.
    int dequeue(struct queue* qPtr) {
        
        int retval;
        
        // Empty case.
        if (qPtr->numElements == 0)
            return EMPTY;
            
        // Store the value that should be returned.
        retval = qPtr->elements[qPtr->front];
        
        // Adjust the index to the front of the queue accordingly.
        qPtr->front = (qPtr->front + 1)% qPtr->queueSize;
        
        // We have one fewer element in the queue.
        (qPtr->numElements)--;
        
        // Return the dequeued element.
        return retval;
    }
    
    // Pre-condition: qPtr points to a valid struct queue.
    // Post-condition: returns true iff the queue pointed to by pPtr is empty.
    int empty(struct queue* qPtr) {
        return qPtr->numElements == 0;
    }
    
    // Pre-condition: qPtr points to a valid struct queue.
    // Post-condition: if the queue pointed to by qPtr is non-empty, the value 
    //                 stored at the front of the queue is returned. Otherwise,
    //                 -1 is returned to signify that this queue is empty.
    int front(struct queue* qPtr) {
        if (qPtr->numElements != 0)
            return qPtr->elements[qPtr->front];
        else
            return EMPTY;
    }
    I need help altering this code to enqueue and dequeue structs instead of ints. Creating a pointer to a struct and returning that pointer.

    This should be a very simple altercation so if you could just return the code altered or help me figure out what should be changed that would be awesome, I don't know why I am having such a difficult time with this but I am and your help would be greatly appreciated. Thank you very much in advance.

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    You figured out how to use code tags, but you just glossed over the homework policy.

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    8
    This is not home work for me, this is simply asking help. This is probably at most 5% of my programming assignment and I am completely stuck on this part, and in order to continue with my program I need this portion of code. So I am asking for help.

    If anybody is willing to help me out, that would be greatly appreciated. Like I said, this should be a simple and quick fix, I just cannot wrap my head around it. I guess coding for the past 6 hours will do that to you.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    How about changing the appropriate int pointers to struct pointers? That'd be a good start.

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Mar 2011
    Posts
    8
    If I could get an example of how to change one or two of them I should be able to change them all, but I'm just having a hard time grasping this concept, please me more specific.

    Code:
    void init(struct queue* qPtr);
    int enqueue(struct queue* qPtr, int val);
    int dequeue(struct queue* qPtr);
    int empty(struct queue* qPtr);
    int front(struct queue* qPtr);
    How would I alter these?

    Code:
    void init(struct queue* qPtr) {
         
         qPtr->elements = (int*)malloc(sizeof(int)*INIT_SIZE);
         qPtr->front = 0;
         qPtr->numElements = 0;
         qPtr->queueSize = INIT_SIZE;
    }
    
    int enqueue(struct queue* qPtr, int val) {
    
        int i;
        
        if (qPtr->numElements != qPtr->queueSize) {
                 
            qPtr->elements[(qPtr->front+qPtr->numElements)%qPtr->queueSize] = val;
            (qPtr->numElements)++;
             
            
            return 1;
        }
        
        else {
             
             realloc(qPtr->elements, (qPtr->queueSize)*sizeof(int)*2);
      
             for (i=0; i<=qPtr->front-1; i++)
                 qPtr->elements[i+qPtr->queueSize] = qPtr->elements[i];     
       
             qPtr->elements[i+qPtr->queueSize] = val;    
             
             (qPtr->queueSize) *= 2;
             (qPtr->numElements)++;
             
             return 1;    
        }
            
    }
    
    
    int dequeue(struct queue* qPtr) {
        
        int retval;
        
     
        if (qPtr->numElements == 0)
            return EMPTY;
            
        retval = qPtr->elements[qPtr->front];
        
        qPtr->front = (qPtr->front + 1)% qPtr->queueSize;
        
        (qPtr->numElements)--;
        
        return retval;
    }
    
    
    int empty(struct queue* qPtr) {
        return qPtr->numElements == 0;
    }
    
    
    int front(struct queue* qPtr) {
        if (qPtr->numElements != 0)
            return qPtr->elements[qPtr->front];
        else
            return EMPTY;
    }
    And there functions to go along with them?
    Last edited by jbonns; 03-24-2011 at 07:49 PM.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You have no idea what this code does on any random line I point at, do you? All you have to do is fine the structure used for the queue, and change where it puts an intenger, to a structure instead.


    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Registered User
    Join Date
    Mar 2011
    Posts
    8
    As I said, I am having a lot of trouble understanding this. Quite frankly I have been coding this same program for the past 30ish hours and am so brain dead. Do I even need to alter this:

    Code:
    void init(struct queue* qPtr);
    int enqueue(struct queue* qPtr, int val);
    int dequeue(struct queue* qPtr);
    int empty(struct queue* qPtr);
    int front(struct queue* qPtr);
    Or do I need to just alter the functions themselves?

    If you could give an example on how to alter one from the first portion of code and one from the second portion of code that is all I need. I need to see an example, I'm more of a visual learner, especially when it comes to code.

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    This reeks of "I'm supposed to make a program, so I found one on the internet and can you fix it for me so I don't have to do any work!"

    If you had any understanding of C at all, you would see how easy this is to switch the contents of the queue from one type to the rest. 90% of this code is just wrapping.


    Quzah.
    Hope is the first step on the road to disappointment.

  9. #9
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Did you copy it from
    http://www.cs.ucf.edu/~dmarino/ucf/c...rogs/queuell.c
    or
    Queues (Linked Lists) Not Running Properly In C - C And C++ | Dream.In.Code

    You lied, cheated and stole. You'll get no help from me jbonns.

  10. #10
    Registered User
    Join Date
    Mar 2011
    Posts
    8
    Considering this is a brand new program that my instructor created, that seems impossible. If you would like for me to PM you the code of what I have along with the program outline so you can search as long as your heart desires to see that I didn't pull code from online, then by all means it will be done.

    Maybe I didn't get proper teachings when it comes to this, i don't know. I'm just having a hard time understanding it and if you could give me an example of how to switch this up that would be great. If not, then get off the thread and quit giving me a hard time. I realize how easy this should be but I'm having trouble understanding it, simple as that. Telling me that I should understand it is something I already know, hence why I came to the board for some help.

    Quote Originally Posted by anduril462 View Post
    Did you copy it from
    http://www.cs.ucf.edu/~dmarino/ucf/c...rogs/queuell.c
    or
    Queues (Linked Lists) Not Running Properly In C - C And C++ | Dream.In.Code

    You lied, cheated and stole. You'll get no help from me jbonns.
    Actually, I did get it from the UCF website, my instructor allows us to use code from our website. Actually, he encourages it.
    Last edited by jbonns; 03-24-2011 at 08:27 PM.

  11. #11
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    You might want to say that up front next time. We have tons of people come through here passing off code they stole from a website as their own. Obviously we take a very negative view towards that. I wouldn't say it's brand new though, since those comments, verbatim, are on a post from 2 years ago at Dream.In.Code.

    You really need to look through your books and notes, and Google around for some good tutorials on line. There is plenty of info out there on how to pass structs to functions and how to return structs.

    Right now, you have a queue of ints. When you enqueue something, you pass in a pointer to your queue structure, and a val to be enqueued. That val is of type int. You need to change that to struct foo, or whatever your structure name is:
    Code:
    int enqueue(struct queue* qPtr, struct foo val);
    Make that change everywhere that's appropriate. I'll let you figure out what that is. Obviously, you have to declare the foo structure.

    Note that without the assignment description, I don't know if you should actually be using struct foo val or struct foo *val, but the basics are this: struct foo is a type name, just like int or char or float.

  12. #12
    Registered User
    Join Date
    Mar 2011
    Posts
    8
    Sorry for not explaining that, and the code itself that I posted isn't brand new, it was made back in 2007, but the program I am to complete is a brand new program, in which I may use any code on the website.. I just might have to alter it to my likings, in which this is one of those cases. Like I said, this queue portion is probably 5% of the entire program. Thank you very much for your help, I appreciate it.

  13. #13
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by jbonns View Post
    Actually, I did get it from the UCF website, my instructor allows us to use code from our website. Actually, he encourages it.
    Then your instructor is a bigger idiot than you are!

    Nobody learns to program by stealing code...

    Picuture this scenario: You have graduated your course with nice high marks that you've obtained without ever writing a program from scratch... Now you've gotten that first, all important job... Your boss comes over and says "We need to reformat these files from this older record to the new one with extra information. We need you to write a program that will go through all of them and automate the transformation." ... So you look at the data structures involved, they are unlike anything you've seen before... OK, now what you gonna do?

    Scoop and poop programmers usually have very short careers... because they never actually learn the problem solving skills needed to be real programmers.
    Last edited by CommonTater; 03-25-2011 at 05:02 AM.

  14. #14
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I believe there's a big difference between "stealing code", and using some open source code.

    If the code is good (clear, commented, etc.), and you study it, how could you NOT learn from it?

    Tests in the class will rank the students according to what they know, on their own. But on their own, how many programmers would come up with something as beautiful as iterative deepening Alpha-beta Minimax or Quicksort, or DLX (aka Dancing Links)?

  15. #15
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Adak View Post
    I believe there's a big difference between "stealing code", and using some open source code.

    If the code is good (clear, commented, etc.), and you study it, how could you NOT learn from it?
    Because they don't study it Adak... The do what buddy's done... they copy the code, stare blankly at it for about 5 minutes then come here and ask us to fix it for them.

    Tests in the class will rank the students according to what they know, on their own.
    You're kidding... right? Tests in class most often do little more than demonstrate how much or how little the instructor knows... Most courses use the same tests over and over again, they get emailed around, photocopied and next year's class already knows all the answers.

    People who are going to use scoop and poop are *already* cheaters, don't expect them to play fair.


    But on their own, how many programmers would come up with something as beautiful as iterative deepening Alpha-beta Minimax or Quicksort, or DLX (aka Dancing Links)?
    And how many scoop and poopers have actually made any contribution whatsoever? Using copied code actually precludes moments of brilliance as they are squelched in a deluge of readily available medocrity.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Do you know...
    By davejigsaw in forum C++ Programming
    Replies: 1
    Last Post: 05-10-2005, 10:33 AM
  2. recursive quick sort - stack overflow
    By Micko in forum C Programming
    Replies: 9
    Last Post: 01-01-2005, 05:51 PM
  3. Questions on basic Quick Sort
    By Weng in forum C++ Programming
    Replies: 4
    Last Post: 12-16-2003, 10:06 AM
  4. Quick Sort Help
    By NavyBlue in forum C Programming
    Replies: 1
    Last Post: 03-02-2003, 10:34 PM
  5. Replies: 0
    Last Post: 04-30-2002, 07:24 PM