--- In [email protected], "meeda91" <meed...@...> wrote:
>
> The program ask the user to enter 5 numbers +ve and negative ...
> and the program will put the positive numbers in a queue
> named pos and negative numbers in queue named neg the program
> works but when it reach the funciton push it stop working 
> need help please :)
> 
> #include<stdio.h>
> #include<stdlib.h>
> 
> 
> struct queue /struct the queue

Was that meant to be // instead of /?

> {
>  int count;
>  int front;
>  int rear;
>  int maxsize;
>  int *qarray;
> };
> 
> 
> 
> queue * initialize(struct queue * q, int m) //state size of queue

Given the lack of struct on the return type, I presume you're
using a C++ compiler. You should not be using a C++ compiler
to learn C. If you're learning C++, then you should be using
the <queue> STL.

But your problem is here. Parameters like q are _local_.

> {
>  q=(struct queue*)malloc(sizeof(struct queue));

Alternatively...

  q = malloc(sizeof *q);

But you are not assigning memory to pos and neg in main().
Their memory is already allocated, so all you're doing is
discarding the pointers to those that you pass in, and
reassigning a newly allocated pointer, which you then
return (but ignore).

>  if(q!=NULL)
>  {
>   q->qarray=(int*)malloc(sizeof(int)*m);

  q->array = malloc(m * sizeof *q->array);

>   q->count=0;
>   q->front=0;
>   q->rear=-1;
>   q->maxsize=m;
>   return q;
>  }
> }
> 
> void push(struct queue*q,int x) //push number in queue
> {
>  q->count++;
>  q->rear=(q->rear+1)%(q->maxsize);
>  q->qarray[q->rear]=x; <----------------here the program stops
> }
> 
> int pop(struct queue*q) //take out a number frim queue
> {
>  q->count--;
>  int x;

C90 won't let you mix declarations and statements.

>  x=q->qarray[q->front];
>  q->front=(q->front+1)%q->maxsize;
>  return(x);
> }
> 
> main()

C++ _requires_ return types. The return type for main is int.
So you're not really using a C _or_ C++ compiler.

  int main()

> {
>  struct queue pos,neg;
>  int countpos=0,countneg=0,i,x,a[5];
>  printf("please enter 30 number:\n");
>  for(i=0;i<5;i++)

You ask for 30 numbers, but only read 5.

>  {
>   scanf("%d",&a[i]);

Always check the return value of input functions to see if
you actually read anything.

>   if(a[i]>0)
>   countpos++;
>   else
>   countneg++;
>  }
>  initialize(&pos,countpos);
>  initialize(&neg,countneg);

Here you're passing the address of _already_ allocated objects,
then subsequently ignoring that address and allocating a new
queue object which you then discard.

There are two options. Either stick with pos and neg being
objects (not that common), or make them pointers have your
initialize function be a 'constructor'...

  struct queue *pos;
  pos = queue_new(countpos);
  queue_free(pos);

>  for(i=0;i<5;i++)
>  {
>   if(a[i]>0)
>   push(&pos,a[i]);

Here you've passed a pointer to pos, but you never changed
the undetermined values that pos had to begin with. Hence,
push() merrily goes ahead using pos.qarray despite it being
a garbage value.

>   else
>   push(&neg,a[i]);
>   }
>   for(i=1;i<=countpos;i++)

Stick to the more idiomatic way you used earlier...

  for (i = 0; i < countpos; i++)

>   {
>    x=pop(&pos);
>    printf("%d",x);
>   }
>   for(i=1;i<=countneg;i++)
>   {
>    x=pop(&neg);
>    printf("%d",x);
>   }
>  }

Have a look at the C FAQ

  http://c-faq.com/ptrs/passptrinit.html

That isn't exactly your issue, but it should give you a clue.
What you're doing is little different to...

  void foo(int x)
  {
    x = 5;
  }

  int main(void)
  {
    int y = 3;
    foo(y);  /* y remains at 3. It isn't assigned 5 */
  }

-- 
Peter

Reply via email to