Array printing help needed

Discussion in 'C' started by steve walker, Mar 9, 2021.

  1. steve walker

    steve walker New Member

    Joined:
    Mar 8, 2021
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    1
    Gender:
    Male
    I have a program to create and fill a 2 dimensional array with numbers up to 2**8 in the first column, and the binary equivalent in the next 8 columns. When I print the array twice, as in the program, I get the results expected. But if I print the array only once, using the goto skipprint statement, I get all ones. The program works for Bits equals 3 through 7, but when Bits = 8, it fails unless I print the array twice. Very frustrated, as I'm missing some nuance, and I can't find any errors. BTW, if anyone knows of a better algorithm to do what I am attempting, please post a link. I've found all sorts of binary bit counting stuff, but nothing to do what I am attempting.
    Code:
    #include <stdio.h>                                  /* for screen display */
    #include <stdlib.h>                                 /* utilities library */
    #include <math.h>                                   /* for math functions */
    
    int main()
        {
    
    
    /* GLOBAL VARIABLES DECLARED HERE */
    
    int i;                                                  /* initialize various counters for loops */
    int j;
    int k;
    int l;
    int m;
    
    int Bits = 8;                                           /* set curve count */
    
    int Count = (1 << Bits);                                /* calculate Count (2 ^^ Bits) */
    
    int Bin[Count][Bits];                                   /* set binary count array size */
    
            for (i = 0; i<=Count ; i++) {                   /* fill binary count array */
                for (j = 0; j<=Bits ; j++) {                /* with 0's */
                    Bin[j] = 0;
                }                                           /* end j loop */
            }                                               /* end i loop */
    
    
    
    /* FILL ARRAYS WITH BINARY NUMBERS UP TO 2^^COUNT */
    
    i = 0;                                                  /* Bits counter */
    j = 0;                                                  /* Bits bit number */
    
    
    checkBit:
    
        if (i==(Count-1))  {                                /* check if finished */
            goto done;
        }
    
    /* COPY PREVIOUS ARRAY */
    
        i = i + 1;
                    for (k = 0 ; k<Bits; k++) {
                    Bin[k] = Bin[i-1][k];
                    }
    
    nextBit:
    
               if (Bin[j]==0)  {
                   Bin[j] = 1;                         /* change 0 bit to 1 */
                   j=0;                                   /* reset bit counter to 0 */
               }
    
                else {
                     Bin[j] = 0;                       /* change 1 bit to 0 */
                     j = j + 1;                           /* increment bit counter */
                     goto nextBit;                        /* check next higher bit */
                }
    
    goto checkBit;
    
    done:
    
    
    
    
    // goto skipprint;
    
    /* PRINT ARRAYS */
    
        for (k = 0; k<Count ; k++){
    
          printf(" k=%d " ,  k);                            /* print  k(Bits) */
    
            for (l = Bits ; l-- > 0 ; ) {
    
    
          printf(" bit %d =%d ",l,Bin[k][l]);               /* print Bin[k][l] (bit value) */
            }                                               /* end l loop */
              printf("\n" );
        }                                                   /* end k loop */
              printf(" blank line 1 \n" );
    
    
              printf(" Count = %d  Bits=%d ", Count, Bits);
    
              printf(" k = %d  l =%d \n", k, l);
    
    
    
    // skipprint:
    
    
    
    
       /* PRINT ARRAYS */
    
        for (k = 0; k<Count ; k++){
    
          printf(" k=%d " ,  k);                            /* print  k(Bits) */
    
            for (l = Bits ; l-- > 0 ; ) {
    
    
          printf(" bit %d =%d ",l,Bin[k][l]);               /* print Bin[k][l] (bit value) */
            }                                               /* end l loop */
              printf("\n" );
        }                                                   /* end k loop */
              printf(" blank line 2 \n" );
    
    
              printf(" Count = %d  Bits=%d ", Count, Bits);
    
              printf(" k = %d  l =%d \n", k, l);
    
    
        }   /* end main */
    
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice