#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2011
    Posts
    43
    Rep Power
    3

    Printing values in a column (truth table)


    I should have 5 columns for R, I0, I1, Out0, Out1 respectively.
    Instead I have 8 columns and I dont understand where it is coming from..

    Code:
    #include <stdio.h>
    
    void fsm1(char Reset,char I0, char I1,  char *Out0, char *Out1);
    void fsm2(char Reset,char I0, char I1,  char *Out0, char *Out1);
    
    int main(int argc, char *argv[])
    {
        char I1,I0,Reset, Out1, Out0, i;
    
    
    
        printf("PIC Microcontroller\n\n");
        printf("Operations with Reset=0\n\n");
        printf("R   I0  I1  Out0   Out1\n\n");
    
        Reset=0;
        I0=0;
        I1=0;
        Out0=0;
        Out1=0;
    
        for(i=0;i<17;i++)
        {
            printf("%1d %1d %1d %1d %1d", Reset, I0, I1, Out0, Out1);
            fsm1(Reset,I0,I1,&Out0,&Out1);
            printf("%1d %1d %1d %1d \n",I0,I1,Out0,Out1);
        }
    
        printf("\n\n Operation with R=1\n\n");
        printf("R    I0  I1  Out0   Out1\n\n");
    
        Reset=1;
        I0=0;
        I1=0;
        Out0=0;
        Out1=0;
    
        int j;
        for(j=0;j<17;j++)
        {
            printf("%1d %1d %1d %1d %1d", Reset, I0, I1, Out0, Out1);
            fsm2(Reset,I0,I1,&Out0,&Out1);
            printf("%1d %1d %1d %1d \n",I0,I1,Out0,Out1);
        }
    
        printf("\n\n\n");
    }
    
    void fsm1(char Reset,char I0, char I1,  char *Out0, char *Out1)
    {
    
    
    
        static char I0table[] = {0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1};
        static char I1table[] = {0,0,0,0,1,1,1,1,0,0,0,0,1,1,1,1};
        static char Out0table[] = {1,1,1,1,0,1,0,1,1,0,1,0,0,0,0,0};
        static char Out1table[] = {0,1,0,1,0,0,1,1,1,0,1,0,1,0,1,0};
    
        int index=(I0<<2)+(I1<<1)+(*Out0<<0)+*Out1;
    
    
        I0=I0table[index];
        I1=I1table[index];
        *Out0=Out0table[index];
        *Out1=Out1table[index];
    
    
    }
    
    void fsm2(char Reset,char I0, char I1,  char *Out0, char *Out1)
    {
    
    
    
        static char I0table[] = {0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1};
        static char I1table[] = {0,0,0,0,1,1,1,1,0,0,0,0,1,1,1,1};
        static char Out0table[] = {1,1,1,1,0,1,0,1,1,0,1,0,0,0,0,0};
        static char Out1table[] = {0,1,0,1,0,0,1,1,1,0,1,0,1,0,1,0};
    
        int index=(I0<<2)+(I1<<1)+(*Out0<<0)+*Out1;
    
    
        I0=I0table[index];
        I1=I1table[index];
        *Out0=Out0table[index];
        *Out1=Out1table[index];
    
    
    }
  2. #2
  3. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2009
    Posts
    149
    Rep Power
    36
    shouldn't you have a \n in your first printf? Otherwise the output from your 2nd printf will probably start on the same line making it look like you have more columns.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2011
    Posts
    43
    Rep Power
    3
    Originally Posted by jakotheshadows
    shouldn't you have a \n in your first printf? Otherwise the output from your 2nd printf will probably start on the same line making it look like you have more columns.
    Yeah now I have 5 columns but my values are messed up...
  6. #4
  7. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,379
    Rep Power
    1871
    But you didn't fix the necessary feedback.
    See one of your other posts on the same subject
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper

IMN logo majestic logo threadwatch logo seochat tools logo