Troller
Troller

Reputation: 1138

Program produce Wrong output

I have written a program on queues and dynamic memory allocation. This is what my program needs to do - insert values in to the queue and remove it from the queue; that simple. But my problem is that it just prints the names of the variables the values are assigned to and the program goes not responding.

Here is my program :

#include <stdio.h>
#define MAX 180

struct cakes{
        int spongecake;
        int meringue;
        int chocalate;
        int red_velvet;
        struct newcake *next;
};

struct Queue{
        int front;
        int rear;
        int count;
        int cake[10];
};

void init(struct Queue *);
int isFull(struct Queue *);
void insert(struct Queue *,int);
int isEmpty(struct Queue *);
int removes(struct Queue *);

void cake_order(struct cakes *);
void order_out(struct cakes *);

main()
{
        struct cakes *head;

        head=(struct cakes *)malloc(sizeof(struct cakes ));
        cake_order(&head); //this is a seperate function and it works perfectly
        head->next=(struct cakes *)malloc(sizeof(struct cakes));
        order_out(&head->next);
}
void init(struct Queue *q)
{
        q->front=0;
        q->rear=10-1;
        q->count=0;
}

int isFull(struct Queue *q)
{
        if(q->count==10)
        {
                return 1;
        }
        else 
        {
                return 0;
        }
}

void insert(struct Queue *q,int x)
{
        if(!isFull(q))
        {
                q->rear=(q->rear+1)%10;
                q->cake[q->rear]=x;
                q->count++;
        }

}

int isEmpty(struct Queue *q)
{
        if(q->count==0)
        {
                return 1;
        }
        else
        {
                return 0;
        }
}

int removes(struct Queue *q)
{
        int caked=NULL;

        if(!isEmpty(q))
        {
                caked=q->cake[q->front];
                q->front=(q->front+1)%10;
                q->count--;
                return caked;
        }
}

void order_out(struct cakes *order)
{
        struct Queue s;
        int i;  

        order->spongecake=20;
        order->meringue=75;
        order->chocalate=40;
        order->red_velvet=30;

        init(&s);

        for(i=0;i<10;i++)
        {
                insert(&s,order->chocalate);
                insert(&s,order->spongecake);
                insert(&s,order->meringue);
                insert(&s,order->red_velvet);
    }

        while(!isEmpty(&s)) 
        {   
                printf("%d",removes(&s));
        }
}

What seems to be the problem here? I am new to C, so yea am a bit slow when debugging in this language.

Thank you for your time.

Here is the output:

enter image description here

Upvotes: 1

Views: 96

Answers (1)

Nobilis
Nobilis

Reputation: 7448

Lots of problems here, first it would be better if main was declared properly as in int main() and then it returned a value at the end e.g. return 0; like:

int main()
{
    .... // code

    return 0; // normally 0 is returned if execution has been successful
}

There seem to be other problems with the code as I wasn't able to compile it, for example there's no closing brace at the end of order_out() (right after the while loop).

Also would be good if you provided the cake_order() function.

It's also missing the includes for say stdlib.h, and on line 45 (head=(struct cakes *)malloc(sizeof(struct cakes ));) I've noticed you cast the result of malloc, which is not necessary.

And if I may further add, don't remember to free() the memory you've allocated with malloc(). I didn't see a single free() statement in your code.

Upvotes: 1

Related Questions