Hi Everyone,
This is my first post and I hope I followed all the guidelines correctly. This is an assignment for a class. I am supposed to read integers from a file, enqueue or dequeue them and then write them out to another file. The main method calls this Work function which in turn calls the dequeue or enqueue function based on what random number the generator gives it. Enqueue or dequeue in turn calls the work function to do it all over. Mutual Recursion. The errors that I am having when I compile are:
queue.c:8: error: expected â)â before â*â token
queue.c:27: error: expected â)â before â*â token
work.c:5: error: expected â)â before â*â token
The errors are all coming from the line with the function(parameters.......) for work, enqueue, and dequeue. I am still struggling with this language, so any help or direction would be appreciated. Thanks!
Also: the first section of code is the Work function and the second is for the Enqueue and Dequeue functions. I didnt include the Main because all it does is open/close the files and then call the work function, but if it is needed to help, just let me know and I will include it.
//put in here the work function that gets called from the main function.
//it will have a random number generator that chooses between 2 numbers.
//these numbers will decide if it enqueues or dequeues
void work(FILE *ifptr, FILE *ofptr, int *queue, const int size, int *tail, int *head)
{
//variable to store the item read
int read;
//check to see if the pointer returns a valid integer or EOF, if valid do all this
if(fscanf(ifptr, "%d", &read)==1)
{
/* initialize random generator */
srand ( time(NULL) );
/* generate random numbers */
int x= rand() % 2;
printf ("A number between 0 and 1: %d\n", x);
//check to see if x = 0 or 1, if 0 then enqueue if 0 and dequeue if 1
if(x==0)
{
//enqueue the read value to the new array
enqueue(ifptr, ofptr, queue, size, &tail, &head, read);
}
else
{
//dequeue the top value from the array
dequeue(ifptr, ofptr, queue, size, &tail, &head, read);
}
}//if(fscanf(fptr, "%d, &read)==1)
else
{
printf("The file is exhausted. The queue will now be dequeued until empty.");
}
//dequeue the array until it is empty
while(empty(head, tail)!=1)
{
dequeue(ifptr, ofptr, queue, size, &tail, &head, read);
}
//return 0;
}//end of work function
//create queue pointer
void init(int *head, int *tail)
{
*head = *tail = 0;
}
//enqueue an element
void enqueue(FILE *ifptr, FILE *ofptr, int *queue, const int size, int *tail, int *head, int read)
{
//check to see if the queue is not full
if(full(tail, size)==0)
{
queue[(*tail)++] = read;
//call the work function
work(ifptr, ofptr, queue, size, &tail, &head);
}
else
{
//print an error to the file
fprintf(ofptr, "The queue is full, this item cannot be enqueued: %d\n", read);
//call the work function
work(ifptr, ofptr, queue, size, &tail, &head);
}
}
//DEQUEUE AN ELEMENT
void dequeue(FILE *ifptr, FILE *ofptr, int *queue, const int size, int *tail, int *head, int read)
{
//check to see if the queue is empty
if(empty(head, tail)==0)
{
//print to the file
fprintf(ofptr, "This item was dequeued: %d\n", queue[(*head)++]);
//call the work function
work(ifptr, ofptr, queue, size, &tail, &head);
}
else
{
//print an error to the file
int error=0;
fprintf(ofptr, "The queue is empty, nothing can dequeued. %d\n", error);
//call the work function
work(ifptr, ofptr, queue, size, &tail, &head);
}
}
//CHECK TO SEE IF QUEUE IS FULL OR NOT
// return 1 if queue is full, else full- return 0
int full(int tail,const int size)
{
return tail == size ? 1 : 0;
}
//CHECK TO SEE IF THE QUEUE IS EMPTY
// return 1 if the queue is empty, else not return 0
int empty(int head, int tail)
{
return head == tail ? 1 : 0;
}