-1

I am working on a project where I have a binary number as a variable that I am trying to make into an array so I can move through the index and test for 0 or 1. I have tried looking up other questions on here but can not find exactly what I am looking for. Right now I have the array predefined so I could check to see if my if statements work properly and they do. The decimal that is coming in through the parameter will be sent to function decToBin to be converted to binary and it returns the binary number. I want something like this to happen:

binA[size] = decToBin(decimal);

The size variable is the size of the binary number.

void relation (int decimal)
{
    int size = ((floor(log2(decimal)))+ 1);

    //char binA[size];
    int binA[] = {1,1,0,0,1,0,0};
    if (size > 1)
    {
            for( int i = 1; i < (size - 1) ; i++)
            {
                    if (binA[i] == 0)
                            printf("Father's ");
                    if (binA[i] == 1)
                            printf("Mother's ");
            }//end for

            for(int i = (size -1); i < size; i++)
            {
                    if (binA[i] == 0)
                            printf("Father ");
                    if (binA[i] == 1)
                            printf("Mother ");
            }//end for
    }//end if
    else
            printf("Self");
    printf("\n");
}       //end relation

Comments: The binary number is of type int. The reason I am doing the second loop is so the last word does not have "'s". Here is an example of output with binary number 1010: Father's Mother's Father. Self is printed when the binary number is just size of 1. Here is the code for my decToBin function

int decToBin (int decimal)
{
    int rem = 0;    //sets remainder to 0
    int binary = 0; //sets answer to 0
    int x = 1;      //sets x to 1

    while(decimal != 0)
    {
            rem = decimal % 2;      //lets remainder = whatever is left after diving by 2
            decimal = decimal / 2;  //lets input = input divided by 2
            binary = binary + (rem * x);    //answer = answer + the remainder times x
            x = x * 10;                     //x now = itself times 10 for the next loop
    }       //end while

    //printf("Ahnentafel number in binary: %d\n", binary);
    return binary;
}       //end decToBin
ShelbyJ
  • 33
  • 4
  • What is "binary number"? What type of variable is this? – Eugene Sh. Sep 30 '16 at 15:10
  • In `for( int i = 1; i < (size - 1) ; i++)` why are you starting at the array index `1` and finishing at a size that is unrelated to the array size? – Weather Vane Sep 30 '16 at 15:12
  • On a **binary** digital computer every variable is a "binary variable". You cannot put variables somewhere. But you can assign its value to another variable. – too honest for this site Sep 30 '16 at 15:13
  • I hope you can find the help and solution [here](http://stackoverflow.com/a/14104287/6874831) which convert an integer into binary and store it in an integer array of specified size Interesting code [here](http://stackoverflow.com/a/14104275/6874831) `int x = 0xdeadbeef; // Your integer? int arr[sizeof(int)*CHAR_BIT]; for(int i = 0 ; i < sizeof(int)*CHAR_BIT ; ++i) { arr[i] = (x & (0x01 << i)) ? 1 : 0; // Take the i-th bit }` – P.Bra Sep 30 '16 at 15:14
  • There really is no need for all of this, you can just check the bits of the integer variable directly using e.g. `(decimal & (1 << i)) != 0` where `i` is the index (starting from 0, of course) of the bit to check. This expression will be 1 if the bit is set. – unwind Sep 30 '16 at 15:20
  • @unwind: That shilft will invoke UB for a specific value. – too honest for this site Sep 30 '16 at 15:40
  • Give a few examples of expected output. What output are you expecting for an input of say 3, 6, 8 or 15? – work.bin Sep 30 '16 at 15:51
  • Is the desired output for 1010 "Father's Mother's Father" (or is it just the output of your current program)? Shouldn't you be expecting "Father's Mother's Father's Mother" or "Mother's Father's Mother's Father" depending on the direction of traversal? – work.bin Sep 30 '16 at 19:24

3 Answers3

0

Your specifications are a bit unclear, but if I take what you have and use my freshly polished crystal ball on it I get the following:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

// ALL CHECKS OMMITTED!

void relation(int decimal)
{
  // wee need to feed log() a positive number
  // log(-x) = log(x) + pi*I ; |x| >= 1 and principal branch of log
  int size = ((floor(log2(abs(decimal)))) + 1);
  printf("size = %d\n", size);
  if (size > 1) {
    for (int i = 1; i < (size - 1); i++) {
      // please do yourself a favor and always use braces
      // even if superfluent
      printf("\nFIRST i = %d, bit = %d\n",i,(decimal>>i)&0x1);
      // the shift is always defined because 0 < i < (sizeof(int)*CHAR_BIT)
      if ( ((decimal>>i)&0x1) == 0) {
        printf("Father's ");
      }
      if ( ((decimal>>i)&0x1) == 1) {
        printf("Mother's ");
      }
    }
    puts("");
    // This loop runs only one time, is that correct?
    for (int i = (size - 1); i < size; i++) {
      printf("\nSECOND i = %d, bit = %d\n",i,(decimal>>i)&0x1);
      if ( ((decimal>>i)&0x1) == 0) {
        printf("Father ");
      }
      if ( ((decimal>>i)&0x1) == 0) {
        printf("Mother ");
      }
    }
  }
  else{
    printf("Self");
  }
  printf("\n");
}

int main(int argc, char **argv)
{
  int dec;
  if (argc != 2) {
    fprintf(stderr, "Usage: %s integer\n", argv[0]);
    exit(EXIT_FAILURE);
  }
  // TODO; use strtol() instead and check all errors
  dec = atoi(argv[1]);
  relation(dec);
  exit(EXIT_SUCCESS);
}

If it doesn't, well... use the comment section below.

deamentiaemundi
  • 5,502
  • 2
  • 12
  • 20
0

Let's try to solve it the way you envisioned by defining decToBin():

void decToBin(unsigned decimal, size_t size, unsigned char *array)
{
    for (unsigned i = 0, bit = 1 << (size - 1); i < size; i++, bit >>= 1)
    {
        array[i] = (decimal & bit) ? 1 : 0;
    }
}

It takes the unsigned integer to decode, the number of bits to decode, and an array to fill in with zeros and ones:

unsigned size = floor(log2(decimal)) + 1;

unsigned char binA[size];

decToBin(decimal, size, binA);

You could add an error check in decToBin() to make sure size is greater than zero. We can also simplify the relation() function a bit:

void relation (unsigned decimal)
{
    unsigned size = floor(log2(decimal)) + 1;

    unsigned char binA[size];

    decToBin(decimal, size, binA);

    if (size > 1)
    {
        for (int i = 1; i < (size - 1); i++)
        {
            printf((binA[i] == 0) ? "Father's " : "Mother's ");
        } // end for

        printf((binA[size - 1] == 0) ? "Father" : "Mother");
    } // end if
    else
    {
            printf("Self");
    }

    printf("\n");
} // end relation
cdlane
  • 40,441
  • 5
  • 32
  • 81
-1

Quick and dirty solution

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

void relation (int decimal)
{
    int size = ((floor(log2(decimal))));
    int c, d;

    if (size > 1)
    {
           for ( c = size ; c >= 0 ; c-- )
           {
              d = decimal >> c;

              if ( d & 1 )
                printf("Mother's ");
              else
                printf("Father's ");
           }

            printf("\n");

           for ( c = size ; c >= 0 ; c-- )
           {
              d = decimal >> c;

              if ( d & 1 )
                printf("Mother ");
              else
                printf("Father ");
           }
    }//end if
    else
        printf("Self");

    printf("\n");
}       //end relation

main()
{
    relation(77);
    printf("\n\n");
    relation(0);

    return 0;
}
hknust
  • 1,251
  • 12
  • 11