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;
}