Queue problem

This is a discussion on Queue problem within the C Programming forums, part of the General Programming Boards category; I need to make a queue using only ADT, tha calculates and prints the sum and the average of integers ...

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    12

    Queue problem

    I need to make a queue using only ADT, tha calculates and prints the sum and the average of integers in the queue Without changing the contexts of the queue. ( no dequeue can be use) using C
    I made program to work but i can't fix that my instructor pointed out in my program. He wants calculations and print function to be used in main and not separate function( sum and printf in FILL QUEUE).. Please help, i gave up..
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<stdbool.h>
    #include "queue.h"
    int main(void)
    {  
     QUEUE* numbers;
     int sum,average;
     int i,number;
     int* dataPtr;
     numbers= createQueue ();
     printf(" This program will randomly choose 20 integers,\n"
              " fill the queue and calculate the sum and average\n"
              " without changing the contents of the queue.");
     
     
     fillQueue(numbers);
     queueFront (numbers ,  dataPtr);
     
     
       
       
       
      system("PAUSE");
      return 0;
    }
    
    
    
    //Queue ADT Type Definations
    typedef struct node
    {
    void* dataPtr;
    struct node* next;
    }QUEUE_NODE;
    typedef struct
    {
    QUEUE_NODE* front;
    QUEUE_NODE* rear;
    int count;
    }QUEUE;
    //Prototype declarations
    QUEUE* createQueue (void);
    bool enqueue   (QUEUE* queue,void*  itemPtr);
    int queueCount (QUEUE* queue);
    void fillQueue(QUEUE* numbers);
    bool emptyQueue(QUEUE* queue);
    bool fullQueue(QUEUE* queue);
    bool queueFront (QUEUE* queue,void** itemPtr);
    //End of Queue ADT definations
    // Create 
    
    Queue******************************************************************
    QUEUE* createQueue(void)
    {
    QUEUE* queue;
    queue = (QUEUE*) malloc (sizeof(QUEUE));
    if(queue)
    {
    queue->front = NULL;
    queue->rear = NULL;
    queue->count = 0;
    }//if
    return queue;
    }
    
    //ENQUEUE ***************************************************************************
    bool enqueue(QUEUE* queue,void* itemPtr)
     {
     QUEUE_NODE* newPtr;
     if (!(newPtr =(QUEUE_NODE*)malloc(sizeof(QUEUE_NODE))))
    return false;
     newPtr->dataPtr = itemPtr;
     newPtr->next = NULL;
     if(queue->count == 0)
     //Inserting into Null queue
     queue->front = newPtr;
     else
     queue->rear->next = newPtr;
     (queue->count)++;
     queue->rear = newPtr;
     return true;
     }
    
    
    // EMPTY QUEUE ****************************************************
    bool emptyQueue(QUEUE* queue)
    {
    return(queue->count == 0);
    }
    
    // FULL QUEUE *******************************
    bool fullQueue(QUEUE* queue)
    {
    QUEUE_NODE* temp;
    temp = (QUEUE_NODE*)malloc(sizeof(*(queue->rear)));
    if(temp)
    {
    free (temp);
    return true;
    }//if
    // Heap Full
    return false;
    }
    
    // FILL QUEUE *****************************************************************
    void fillQueue (QUEUE* numbers)
     {
     int category=0;
     int item;
     int* dataPtr;
     int sum=0;
     int average=0;
     printf("\n Nubers that has been choosen are:\n");
     srand(79);
     int i;
     for (i=1;i<=20;i++)
     {
     if (!(dataPtr = (int*) malloc (sizeof (int))))
     printf("Overflow in fillQueues\a\n"),
     exit(100);
     *dataPtr = item = rand() % 100;
     
     printf("%3d",item);
     if(!(i % 10))
     printf("\n");
     sum = sum + item;
     enqueue (numbers, dataPtr);
     }
     average = sum / 25;
     printf ("\n The sum of integers is: %d   ", sum);
     printf ("\n The average of integers is: %d \n", average);
     return;
     }

  2. #2
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    Quote Originally Posted by denisa View Post
    I need to make a queue using only ADT, tha calculates and prints the sum and the average of integers in the queue Without changing the contexts of the queue. ( no dequeue can be use) using C
    I made program to work but i can't fix that my instructor pointed out in my program. He wants calculations and print function to be used in main and not separate function( sum and printf in FILL QUEUE).. Please help, i gave up..
    I am not really sure what your problem is? This is what you need to do:
    1. Create your abstract queue
    2. Fill it with integers ( I guess 20 based on your comments)
    3. Create a variable called sum in main.
    4. Iterate through your queue, from first to last element and add the ints up storing them in sum. Use a loop.
    5. Divide sum by number of entries and store that in a variable called average
    6. Print sum and average

    What exactly is the problem you are having with this?
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  3. #3
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    OK, I just read your other thread and I think I see your confusion. Yes, you do store your data as an ADT. All that means is that when you make your assignment you need to cast it to the right value first.

    Here is a very simple answer since I would prefer you to finish this yourself. However it shows how to cast the return type back which is what your problem is:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    struct ADT{
    	void *data;
    	struct ADT *next;
    };
    
    int main(void){
    
    	int sum=0;
    	//always check return of malloc
    	int *newdata = malloc(sizeof(*newdata));
    	struct ADT *root =malloc(sizeof(*root));
    	
    	*newdata = 5;
    	root->data = newdata;
    	root->next = NULL;
    
    	while(root){
    		//the important part: cast from void* to int* to int
    		sum+= *((int *)(root->data));
    		root = root->next;
    	}
    
    	printf("sum %d",sum);
    	return(0);
    }
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem about stack and queue in C
    By neyul in forum C Programming
    Replies: 4
    Last Post: 04-07-2009, 10:34 PM
  2. Queue Problem
    By SMurf in forum C Programming
    Replies: 3
    Last Post: 04-26-2008, 11:30 AM
  3. Queue Project Problem
    By DarkDot in forum C++ Programming
    Replies: 9
    Last Post: 04-04-2007, 01:26 AM
  4. queue problem using c
    By doom83 in forum C Programming
    Replies: 3
    Last Post: 10-16-2005, 07:03 AM
  5. queue problem
    By Yelagin in forum C Programming
    Replies: 1
    Last Post: 07-24-2002, 11:04 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21