#include <stdio.h>
#include <string.h>
#define NUM_OF_BITS 16
#define BIT_ROW 0
#define DEC_VALUE_FOR_BIT_ROW 1
void GetInput(char *inputBinaryValue, int NUMBER_OF_BITS);
int ValidateInputLength(char *inputBinaryValue, int NUMBER_OF_BITS);
int ValidateInputValues(char *inputBinaryValue, int NUMBER_OF_BITS);
void ConvertArray(char *inputBinaryValue, char *myArray, int NUMBER_OF_BITS);
int main()
{
// some of these vaiables are redundant when I finish, I will clean it up
int index = 0, validTestOne = 0, validTestTwo = 0;
int column = 0;
int decValueForBit = 1;
int totalDecimalValue = 0;
int myArray [2] [NUM_OF_BITS] = {0}; //final array for comparrisons
char inputBinaryValue[NUM_OF_BITS] = {0}; // initial input read in here
int arrayLength = 0;
int NUMBER_OF_BITS = NUM_OF_BITS;
// loop while input is invalid
do
{
GetInput(&inputBinaryValue, NUMBER_OF_BITS);
// eg 16 chars for 16 bits
validTestOne = ValidateInputLength(&inputBinaryValue, NUMBER_OF_BITS);
//make sure input is in binary, 1 or 0
validTestTwo = ValidateInputValues(&inputBinaryValue, NUMBER_OF_BITS);
}while((!validTestOne)||(!validTestTwo));
//Add input array into myArray for later calculations in the program
ConvertArray(&inputBinaryValue, &myArray, NUMBER_OF_BITS);
return (0);
}
// get input from keyboard add into array
void GetInput(char *inputBinaryValue, int NUMBER_OF_BITS)
{
printf("Please enter the %i bit binary code: ", NUMBER_OF_BITS);
scanf("%s", &*inputBinaryValue);
return;
}
// check input is the correct length, error message if not valid, return boolean
int ValidateInputLength(char *inputBinaryValue, int NUMBER_OF_BITS)
{
int arrayLength = 0, valid = 0;
arrayLength = strlen(*inputBinaryValue);
if ((NUMBER_OF_BITS - arrayLength) >= 0)
{
valid = 1;
}
else
{
printf("Invalid input!\nYour input is larger then the current bits.\n");
printf("Please limit the length to %i places.\n", NUMBER_OF_BITS);
}
printf("");
return(valid);
}
// check input is in binary, error message if not valid, return boolean
int ValidateInputValues(char *inputBinaryValue, int NUMBER_OF_BITS)
{
int index = 0, valid = 0;
for(index = 0; index < NUMBER_OF_BITS; index++)
{
if((*inputBinaryValue[index] !='1')&&(*inputBinaryValue[index] !='0')&&
(*inputBinaryValue[index] != NULL))
{
printf("Invalid input!!!\nYou can only enter 0's or 1's\n"
"Try again.....\n");
break;
}
else
{
valid = 1;
}
}
return(valid);
}
/* This method takes in the user input if shorter then the total bits the
* function will add zeros to the front of the original input value when
* adding to myArray for the final calculations. Eg For 8 bits, if the user
* inputs 11 after this method myArray will have 00000011 input into it's first
* row.
*
* You can not see this as the final functions are currently missing but
* later on in the 2d array called myArray, the first row of the 2d array will
* have the binary value and the second row will have the related value for each
* bit obviously increasing by the power of 2.
*
* I have not got this code in there as i have to finish the functions and to
* do that I need to understand pointers. once these functions are working i can
* apply the syntax to the other functions and finish
*
*/
void ConvertArray(char *inputBinaryValue, char *myArray, int NUMBER_OF_BITS)
{
int idx = 0, index = 0;
int arrayLength = strlen(inputBinaryValue);
int locToWriteFrom = (NUMBER_OF_BITS - arrayLength);
/* This FOR loop starts writing the input string in the correct position in
* myArray for the final calculations (to come). eg in a 16 bit input if the
* user originally inputs 1000 (length being 4) this function will start
* writing this in te 12th position of myArray 16 - 4 = 12, 12 being the
* position to start writing to end up with 0000000000001000 in row 1 of
* myArray after the function id finished
*/
for(index = locToWriteFrom; index < NUMBER_OF_BITS; index++)
{
if(*inputBinaryValue[idx] == '1')
{
*myArray[0][index] = 1;
}
idx++;
}
return;
}