0

when i call my multi() it will calculate the binary number just fine, but won't calculate the adding(). If i switch the two in order, it will calculate the adding() and not calculate the multi(). Is there a reason why the second function call isn't displaying its value? It will print the printf, but not the value. Any help would be great.

#include <stdio.h>

long binary1, binary2, binary3, binary4, multiply = 0;
int binaryproduct(int, int);
int digit, factor = 1;
int multi();
int adding();


int main()
{
    binary3 = binary1;
    binary4 = binary1;

    printf("Enter the first binary number: ");
    scanf("%ld", &binary1);

    printf("Enter the second binary number: ");
    scanf("%ld", &binary2);

    multi();   
    printf("\n");
    adding();
}

int adding()
{
    int i = 0, remainder = 0, sum[20];

    while (binary3 != 0 || binary4 != 0) {
        sum[i++] =(binary3 % 10 + binary4 % 10 + remainder) % 2;
        remainder =(binary3 % 10 + binary4 % 10 + remainder) / 2;
        binary1 = binary3 / 10;
        binary2 = binary4 / 10;
    }

    if (remainder != 0) {
        sum[i++] = remainder;
    }

    --i;

    printf("Sum of two binary numbers: ");

    while (i >= 0) {
        printf("%d", sum[i--]);
    }

    return 0;
}


int multi()
{
    while (binary2 != 0) {
        digit =  binary2 % 10;
        if (digit == 1) {
            binary1 = binary1 * factor;
            multiply = binaryproduct(binary1, multiply);
        } else {
            binary1 = binary1 * factor;
        }
        binary2 = binary2 / 10;
        factor = 10;
    }

    printf("Product of two binary numbers: %ld", multiply);
    return 0;
}

int binaryproduct(int binary3, int binary4)
{
    int i = 0, remainder = 0, sum[20];
    int binaryprod = 0;

    while (binary1 != 0 || binary2 != 0) {
        sum[i++] =(binary1 % 10 + binary2 % 10 + remainder) % 2;
        remainder =(binary1 % 10 + binary2 % 10 + remainder) / 2;
        binary1 = binary1 / 10;
        binary2 = binary2 / 10;
    }

    if (remainder != 0) {
        sum[i++] = remainder;
    }

    --i;

    while (i >= 0) {
        binaryprod = binaryprod * 10 + sum[i--];
    }

    return binaryprod;
}
Sean Bright
  • 118,630
  • 17
  • 138
  • 146
Dev123Dev
  • 194
  • 2
  • 13
  • probably because you made everything global variables. Use parameters instead. – Red Alert Apr 24 '14 at 18:07
  • By the way, your product function is giving `10 * 10 = 101`, which does not seem quite right. – merlin2011 Apr 24 '14 at 18:08
  • The while loop in adding() will never exit once entered, as the guard values aren't modified within the loop. – Ross Apr 24 '14 at 18:08
  • `long binary1, binary2, binary3, binary4, multiply = 0;` <-- Those multiple variables are [not initialized like you may be thinking](http://stackoverflow.com/questions/6838408/declare-set-multiple-variables). – crashmstr Apr 24 '14 at 18:10
  • Sum never prints properly, even without the `multi();` call. – Utkan Gezer Apr 24 '14 at 18:17
  • @crashmstr Either way, those variables are initialized to zero, even if that last `multiply` was not specifically initialized to zero. Global variables are initialized to zero by default. – Utkan Gezer Apr 24 '14 at 18:18
  • You're declaring binary* to be long ints, but your functions take and return ints. There's no guarantee that a long int and an int are the same size, or that something unexpected isn't going to happen when casting from int to long or vice-versa. Use consistent integer sizes. – Phil Perry Apr 24 '14 at 18:42

1 Answers1

-1

Your globals aren't being initialized. In you adding() function you use:

binary1 = binary3/10

binary2 = binary4/10

and in the binaryproduct function you have two local ints named binary3, binary4 that you don't use.

You should be using unique names, initializing you variables, use local variables in places where globals are unnecessary.

109
  • 137
  • 1
  • 10
  • Globals don't need initialization, if zero is the number they are desired to be initialized with. – Utkan Gezer Apr 24 '14 at 18:30
  • you didn't look at the question did you? The `adding()` and `multi()` both check the variables to see if they are 0. If you wanted to test to test the functions, you would need to initialize the variables before calling `adding()` and `multi()`. – 109 Apr 24 '14 at 18:44