Member Avatar for qwazy

Hihi im very new to programming. Need help for this program. Im supposed to create a queue using an integer array.

For input value > 0, program will insert value to queue ; input value = 0, program will remove least recently added value from queue if non empty ; input value < 0, program will return last recently added value from queue if non empty.

The problem is that my bool enqueue function repeats itself twice. Im pretty confused over bool functions too. Apparently the program runs through the bool enqueue function twice. If array size is 0, the next positive input will cause the array to grow to array size 2.

How do i make it go through once?

Will be very grateful for any replies. Thanks :D

#include <stdio.h>
#include <stdbool.h>
#define max 10

void queue_init();
bool enqueue(int val);
void dequeue();
int front();
bool empty();

int r,f,val,SIZE,a,i,h;
int data [max];

int main()
{
    queue_init();

    while(1)
   {
          //int SIZE = sizeof(data) / sizeof(data[0]);
          int SIZE = r-f;
          printf("\nSize at the start");
          printf("%d",SIZE);
          printf("\n");

        printf("\nInput Data: ");
        scanf("%d",&val);

        printf("%d",val);

        if (val>0)
        {
            printf("Initiate enqueue");
            enqueue(val);

        }
        else
        {
            if (empty() == 1 && val == 0)
            {
               printf("\nError - Queue is empty\n");
            }

            else
            {
                if (val < 0)
                {
                    printf("\n");
                    printf("%d",front());
                    printf("\n");
                }
                if (val == 0)
                {
                    dequeue();
                }
            }

        }

         /*  if (r==f)
            {
                SIZE = 0;
            }
            else
            {
                SIZE = r-f;
            }
            */
             if (enqueue(a) == 1)
            {
                printf ("\nTRUE! Array is not full!\n");

            }
            else {printf ("\nFALSE! Array is full!\n");}

            printf("\n");
            printf("Array size: ");
                if (r==f)
                {
                    printf("0");
                }
                else
                {
                    printf("%d",r-f);
                }
            printf("\n");
            printf("%d",r);
            printf("%d",f);

    }
}

void queue_init()
{
    r = f = -1;
}
bool enqueue(val)
{
        printf("Start of boon enqueue!");
        i=0;

        if (empty())
        {
            r=f=0;
        // printf("\nError - Queue is empty\n");
        }

        else if(SIZE != max && i ==0)
        {
            i=i+1;
            printf("Value of i");
            printf("%d",i);

            if(i==3)
            {
                printf("\nSecond Time Liao!\n");
            }
            r = r + 1;
             printf("\n");
             printf("Value of R here");
            printf("%d",r);

             printf("\n");

            if(r==max)
            {
                r=0;
                printf ("\nExecute Wraparound here\n");
            }
        else if (SIZE == max)
        {
        data[r]=val;
        printf("\n");
        printf("Value of Val here");
        printf("%d",val);
         printf("\n");
         printf("End Enqueue Liao!");

          if (r==f)
        {
            SIZE = 0;
        }
        else
        {
            SIZE = r-f;
        }


        printf("Before the Return");
         return SIZE != max;
        }
        }

         if (r==f)
        {
            SIZE = 0;
        }
        else
        {
            SIZE = r-f;
        }

        printf("Before the Return");
         return SIZE != max;



}
void dequeue()
{
    if(r==f)
    {
        r=f=-1;
    }
    else
    {
        f=f+1;
        if (f==max)
        {
            f = 0;
        }
    }
}
int front()
{
    return data[f];
}
bool empty()
{
    return r==-1;
}

Im pretty confused over bool functions too.

They're functions that return a boolean value. Really not that different from a void function except for the return value.

Apparently the program runs through the bool enqueue function twice. If array size is 0, the next positive input will cause the array to grow to array size 2.

That's because you treat an empty queue as an error condition and fail to insert the item. This is somewhat nonsensical since adding to an empty queue shouldn't be a problem except in unrealistic degenerate cases like a queue of size 0.

Member Avatar for qwazy

okay i see :D Thanks for your help :D

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.