View Single Post
 Newbie Member 21Jul2006,07:22 Hello Everyone, I am new here. I would appreciate any help on a program that i am currently putting together. My program is supposed to convert 8 bit binary numbers to binary coded decimal (BCD) numbers. e.g.: The binary number 1111 1111 (255 in decimal) should equal 0010 0101 0101 in BCD, where 0010 => 2 (for the hundreds position), and 0101 => 5 (for the tens and units position) Conversion is primarily achieved by shifting and addding 3 (0011 in binary) several times. The binary to binary coded decimal conversion follows the follwing rules: #1 Shift the binary number left one bit. #2 If 8 shifts have taken place, the BCD number is in the Hundreds, Tens, and Units column. #3 If the binary value in any of the BCD columns is 5 or greater, add 3 to that value in that BCD column. #4 Go to 1. The program outputs the answer in form units, tens and hundreds in decimal form correct answer should be: (after 8 shifts) e.g.: Units = 2 Tens = 5 Hunds = 5 LINK: http://www.engr.udayton.edu/faculty/...in_to_BCD.html I would appreciate any help. Thank you all. My program: Code: CPP `// 8 BIT BINARY TO BINARY CODED DECIMAL /////////////////////#include #include #include using std::cout;using std::endl;void shifting(int *arr,int max){    int temp;    //int sh;    int i,n;        for(i=0;i<1;i++)   // designates how many spaces u want the binary number shifted e.g.: i<1 => 1 unit shift to the right     {        temp=*(arr+max-1);        for(n=max-2;n>=0;n--)            *(arr+n+1)=*(arr+n);        *arr=temp;    }}int main()//binary number are READ FROM LEFT TO RIGHT !!!  least sig. dig. to most sig. dig. !!!{              //  |8-bit binary #  | Units |  Tens   |Hundreds|    int A[20] =   {1,1,1,1, 1,1,1,1, 0,0,0,0, 0,0,0,0, 0,0,0,0};        //        |8-bit binary #  |    Units  |  Tens   |Hundreds|        int UNITSaddition[20] = {0,0,0,0, 0,0,0,0,   1,1,0,0, 0,0,0,0,  0,0,0,0  }; //for adding 3 to the units BCD    int TENSaddition[20] =  {0,0,0,0, 0,0,0,0,   0,0,0,0, 1,1,0,0,  0,0,0,0  }; //for adding 3 to the units BCD    int HUNDSaddition[20] = {0,0,0,0, 0,0,0,0,   0,0,0,0, 0,0,0,0,  1,1,0,0  }; //for adding 3 to the units BCD        int total = 0; int UNITSsum = 0; int TENSsum = 0; int HUNDSsum = 0;        int doubleval[21]; int HUNDSval[19]; int TENSval[19]; int UNITSval[19];                /*for (int i = 0; i < 21 ; i++)    {    doubleval[i] = (A[i]*pow(2,i));    printf("\n  position value %i in decimal => %d  ", i, doubleval[i]);    total += doubleval[i]; } */        //// DATA OVERVIEW module ////////////////////////////////////////////        // printf("\n the binary number is equal to %d in decimal ", total);    printf("\n original 8 bit binary number  %i %i %i %i %i %i %i %i", A[0],A[1],A[2],A[3],A[4],A[5],A[6],A[7] );    printf("\n the UNITS bits are  %i %i %i %i ", A[8],A[9],A[10],A[11]);    printf("\n the TENS bits are  %i %i %i %i ", A[12],A[13],A[14],A[15]);    printf("\n the HUNDREDS bits are  %i %i %i %i \n", A[16],A[17],A[18],A[19]);        ////////////////////////////////////////////////////////////////////////////    //  int arr[100], N[20];    int max = 20; // max = Max size of the array    int num = 0;        do    {        UNITSsum = 0;          TENSsum = 0;         HUNDSsum = 0;        shifting(A,max); // actual shifting function call        printf("I am in the loop \n");        for(int i=0;i= 5) // add 3 to orignal binary number if hundreds sum >= 5        {            for (i=0; i<20; i++)                 overflow[i] = 0;                        for (i=0; i<20; i++)             {                                  array_value3[i] = A[i] + HUNDSaddition[i] + overflow[i];                                if (array_value3[i] > 1)                {                    overflow[i+1] = 1;                    array_value3[i] %= 2;                }                for (i=0; i<20; i++)                 {                    A[i] = array_value3[i];                    array_value3[i] = 0;                }            }                                }                        if (TENSsum >= 5) // add 3  to orignal binary number if tens sum >= 5        {            for (i=0; i<20; i++)                 overflow[i] = 0;                                for (i=0; i<20; i++)             {                       array_value3[i] = A[i] + TENSaddition[i] + overflow[i];                if (array_value3[i] > 1)                {                    overflow[i+1] = 1;                    array_value3[i] %= 2;                }                for (i=0; i<20; i++)                 {                    A[i] = array_value3[i];                    array_value3[i] = 0;                }            }                    }                if (UNITSsum >= 5) // add 3 to orignal binary number if units sum >= 5        {            for (i=0; i<20; i++)                 overflow[i] = 0;                            for (i=0; i<20; i++)             {                                array_value3[i] = A[i] + UNITSaddition[i] + overflow[i];                if ( array_value3[i] > 1)                {                    overflow[i+1] = 1;                    array_value3[i] %= 2;                }            }            for (i=0; i<20; i++)             {                A[i] = array_value3[i];                array_value3[i] = 0;            }                    }                                                printf("\n the units BCD is equal to %d in decimal ", UNITSsum);     // prints units 4-bit binary# in decimal        printf("\n the tens BCD is equal to %d in decimal ", TENSsum);       // prints tens 4-bit binary# in decimal        printf("\n the hundreds BCD is equal to %d in decimal ", HUNDSsum);  // prints hundreds 4-bit binary# in decimal                    }        while ( num < 5); //designates how many times  the orignal array is shifted (max should be 8 for 8 bits)        for(int i=0;i