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