Thread: FIFO question (Please help)

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    21

    FIFO question (Please help)

    Hi ,

    I doing my assignment and got struck with my programming. The following source code take in data(1,2,3,4,5) and then display it. My program display the following result which
    is not my expected result.
    ----------------------------------
    First data = 1
    Second data = 2
    Third data = 3
    Fourth data = 4
    Fifth data = 5
    ----------------------------------

    Then i use the if function to store the data to the desire position. it works but will not work if i change the position of the source code from main{}. That means it is not flexible.
    For example ,shift "push(3)" from it's current position to after the "printf("First data = %d\n", d);" , i will not be able to get the desirable result again

    i want the output to be like this :
    First data = 3
    Second data = 2
    Third data = 4
    Fourth data = 5
    Fifth data = 1

    Can anyone give me some clue how to solve this. ( Note : source code from the main{}
    cannot be altered.)

    Code:
    #include <stdio.h>
    
    int buffer[1024];
    int inp = 0;
    int outp = 0;
    
    push(int v)
    {
     if (inp < 1024)
      {
       if(v==1)
        {
         buffer[4] = v;
         inp++;
        }
       else if(v==2)
        {
         buffer[1] = v;
         inp++;
        }
       else if(v==3)
        {
         buffer[0] = v;
         inp++;
        }
       else if(v==4)
        {
         buffer[2] = v;
         inp++;
        }
       else if(v==5)
        {
         buffer[3] = v;
         inp++;
        }
      }
    }
    
    int pop()
    {
     int data = 0;
     
      if(outp < inp)
       {
        data = buffer[outp];
        outp+=1;
       }
    
     return data;
    }
    
    
    
    
    main()
    {
        int d;
    
        push(1);
        push(2);
        
        d = pop();
        push(3);
        printf("First  data = %d\n", d);
        d = pop();
        printf("Second data = %d\n", d);
        push(4);
        d = pop();
        printf("Third  data = %d\n", d);
        push(5);
        d = pop();
        printf("Fourth data = %d\n", d);
        d = pop();
        printf("Fifth  data = %d\n", d);
    }

  2. #2
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    I dont understand what you're trying to do but for the first time when pop() is called, data=buffer[outp] is executed, where outp=0 but what is buffer[0]? I mean which value will be stored in data?
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  3. #3
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Are you sure it's not supposed to be:
    First data = 2
    Second data = 3
    Third data = 4
    Fourth data = 5
    Fifth data = 1

    ???

    This code will give me that. Otherwise you need to translate 2 and 3 for seemingly no reason:
    Code:
    int buffer[1024];
    int top = 0;
    
    push(int v)
    {
      buffer[top++] = v;
    }
    
    int pop()
    {
      return buffer[--top];
    }
    
    
    main()
    {
    ...
    }
    If you understand what you're doing, you're not learning anything.

  4. #4
    Registered User
    Join Date
    Nov 2009
    Posts
    21
    I understand the code will give me the answer i want. It is because i program it directly.
    For example :

    if the value is 3 then store it in buffer[0] and so on....

    But this program is not flexible. If i move the "push(3)" from it's current position to after the "printf("First data = %d\n", d);" , i will not be able to get the desirable result again.

    I understand that i got my result.

    But as i stated , it is not flexible.

  5. #5
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Your desired results indicate that you're working with a stack. In a stack, values are popped in the reverse order in which they're pushed. Stacks are LIFO (Last In First Out). If you want FIFO, you want to use a queue instead.
    If you understand what you're doing, you're not learning anything.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. select system call with FIFO fd.
    By vlrk in forum Linux Programming
    Replies: 0
    Last Post: 05-11-2009, 04:27 AM
  2. simultaneously waiting for data on FIFO and UDP using select call
    By yogesh3073 in forum Networking/Device Communication
    Replies: 2
    Last Post: 01-05-2007, 09:53 AM
  3. working with FIFO files
    By icebabe in forum C Programming
    Replies: 6
    Last Post: 05-06-2006, 11:35 AM
  4. Alice....
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 06-20-2005, 02:51 PM
  5. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM