Thread: Store a binary number to an array

  1. #1
    Registered User
    Join Date
    Oct 2017
    Posts
    27

    Store a binary number to an array

    I know there are a ton of this problems in google search(decimal to binary) but i cant find something the way I want .
    So this is my code i dont know why it does nt work, can someone help me?
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    #include <string.h>
    
    
    #define MAX 20
    
    
    int input_dec(){
        int x=0;
        scanf("%d", &x);
        if ( x >= 0)
            return x;
        else
            return x = -1;
    }
    
    
    int dec2bin(int m, int b[]){
        int i = MAX - 1 , counter = 0, y=0;
    
    
        b[MAX] = m % 2;//The first binary number
        y = m / 2;
    
    
        while( y != 0){
            b[i] = y % 2 ;
            i--;
            y = y / 2 ;
            counter++;
            if(counter == MAX+1 )break;
        }
        if(counter == MAX+1 )return -1;
        else{
            b[i] = -1 ;
            return 1;
        }
    
    
    }
    
    
    void print_bin(int b[]){
        int i = 0, hold =0;
        printf("\n");
        for(i=0; i <= MAX; i++){
            if ( hold == -1 ){
                printf("%d", b[i]);
            }
            if ( b[i] == -1 ){
                hold = b[i];
            }
        }
    
    
    }
    
    
    int add_bin(int b1[], int b2[], int b_tot[]){
        int i = 0, counter = 0, counter1= 0, counter2 = 0, hold1 = 0, pos1 = 0, hold2 = 0, pos2 = 0, sub = 0;
    
    
        //control for equal binary digits
        for(i = 0; i <= MAX; i++){
            if ( hold1 == -1)counter1++;
    
    
            if ( hold2 == -1)counter2++;
    
    
            if ( b1[i] == -1 ){
                hold1 = b1[i];
                pos1 = i;
            }
            if ( b2[i] == -1){
                hold2 = b2[i];
                pos2 = i;
            }
        }
        //putting the necessary '0' to get equal binary digits
        sub = counter1 - counter2 ;
        //when second binary number is bigger
        if ( sub < 0) {
            sub = abs(sub);
            for( i = MAX; i >= 0; i--){
                if ( hold1 == 0 ) {
                    b1[i] = 0;
                    sub--;
                    if ( sub == 0)break;
                }
                if (i  == pos1){
                    b1[i] = 0;
                    hold1 = 0;
                    sub--;
                    if( sub == 0)break;
                }
            }
            b1[ i-1 ] = -1;
        }
        else{
            for( i = MAX; i >= 0 ; i--){
                if ( hold2 == 0 ) {
                    b2[i] = 0;
                    sub--;
                    if ( sub == 0)break;
                }
                if (i  == pos2){
                    b2[i] = 0;
                    hold2 = 0;
                    sub--;
                    if( sub == 0)break;
                }
            }
            b2[ i-1 ] = -1;
        }
    
    
        //ADD
        for( i = MAX ; i >= 0 ; i--){
            if ( b1[i] == 0 && b2[i] == 0 ){
                b_tot[i] = 0;
                if ( hold1 ) b_tot[i] = 1;
            }
            if ( b1[i] == 0 && b2[i] == 1){
                b_tot[i] = 1;
                if ( hold1 ) b_tot[i] = 0;
            }
            if ( b1[i] == 1 && b2[i] == 0){
                b_tot[i] = 1;
                if ( hold1 ) b_tot[i] = 0;
            }
            if ( b1[i] == 1 && b2[i] == 1){
                b_tot[i] = 0;
                if ( hold1 ) {
                    b_tot[i] = 1;
                    continue;
                }
                hold1 = 1;
            }
            counter++;
            if ( counter == MAX+1 )break;
        }
        if(counter == MAX+1 )return -1;
        else{
            b_tot[ i-1 ] = -1;
            return 1;
        }
    
    
    }
    
    
    int bin2dec(int b[]){
        int i = 0, add = 0, temp = 0, power = 0;
        for( i = MAX ; i >= 0 ; i--){
            temp = b[i] * pow(2, power);
            add = add + temp;
            power++;
            if ( b[i] == -1)break;
        }
        return add;
    }
    
    
    int main()
    {
        int i = 1, var1, var2, b1[MAX], b2[MAX], b_tot[MAX], x=0, y=0 ;
    
    
    
    
        while ( i != 0){
    
    
            var1 = input_dec();
            if( var1 < 0)printf("%d\n", var1);
            var2 = input_dec();
            if( var2 < 0)printf("%d\n", var2);
            dec2bin(var1, b1[MAX]);
            dec2bin(var2, b2[MAX]);
            printf("\nThe 2 binary numbers are: " );
            print_bin(b1[MAX]);
            printf("\t");
            print_bin(b2[MAX]);
            add_bin(b1[MAX], b2[MAX], b_tot[MAX]);
            printf("\nThe Add of the 2 numbers is in binary form: " );
            print_bin(b_tot[MAX]);
            printf("\tand in decimal form is: " );
            bin2dec(b_tot[MAX]);
            print_bin(b_tot[MAX]);
    
    
            printf("\nDo you want to play again?IF Yes type 1, IF No type 0:");
            scanf("%d", &i);
        }
    
    
    
    
        return 0;
    }
    for example if I put as input two 5,the program crashes

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Code:
    b[MAX] = m % 2;//The first binary number
    The index MAX in array b is likely illegal because the max legal index is likely MAX-1

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Binary number from string to int array help
    By Trey Brumley in forum C++ Programming
    Replies: 3
    Last Post: 09-22-2013, 04:21 PM
  2. how number of two-dimensional array store ?
    By zcrself in forum C Programming
    Replies: 8
    Last Post: 11-28-2009, 12:15 AM
  3. store binary data in program...
    By Abda92 in forum C Programming
    Replies: 9
    Last Post: 03-23-2008, 10:33 AM
  4. binary number in array? urgently help
    By shteo83 in forum C Programming
    Replies: 4
    Last Post: 02-25-2006, 12:28 PM
  5. How to store n write binary file?
    By megablue in forum C Programming
    Replies: 4
    Last Post: 10-23-2003, 03:53 AM

Tags for this Thread