0

I have an assignment to make a program that should convert a number from it's integer value to a binary value. For some reason my array is always filled with zeroes and won't add "1"'s from my if statements. I know there are probably solutions to this assignment on internet but I would like to understand what is problem with my code. Any help is appreciated.

Here is what I tried:

#include <iostream>

/*Write a code that will enable input of one real number in order to write out it's binary equivalent.*/

int main() {

    int number;
    int binaryNumber[32] = { 0 };
    std::cout << "Enter your number: ";
    std::cin >> number;

    while (number > 0) {
        int i = 0;

        if ((number / 10) % 2 == 0) {
            binaryNumber[i] = 0;
        }
        if ((number / 10) % 2 != 0) {
            binaryNumber[i] = 1;
        }

        number = number / 10;
        i++;
    }

    for (int i = 31; i >= 0; i--) {

        std::cout << binaryNumber[i];
        
    }

    return 0;
}
  • A binary number is base 2 not base 10, so you need to divide by 2 in every iteration. Also even in decimal representation the division before applying the modulo is incorrect. – fabian Dec 26 '20 at 12:45
  • 2
    why do you dive by 10 before to do modulo 2 ? then why do you divide by 10 rather than 2 ? why not using else ? why reseting i to 0 inside the loop ? – bruno Dec 26 '20 at 12:45
  • If you have an assignment just to print the binary representation of a number, you can use `std::bitset`. See https://stackoverflow.com/a/7349767/9363996 – Sergey Kolesnik Dec 26 '20 at 12:52
  • Keep in mind that "binary" and "decimal" describe **text representations** of a value. The value is always the same; it's just the text that changes. – Pete Becker Dec 26 '20 at 15:28

3 Answers3

0

... is always filled with zeroes ... I would like to understand what is problem with my code

int i = 0; must be before the while, having it inside you only set the index 0 of the array in your loop because i always values 0.

But there are several other problems in your code :

  • using int binaryNumber[32] you suppose your int are on 32bits. Do not use 32 but sizeof(int)*CHAR_BIT, and the same for your last loop in case you want to also write 0 on the left of the first 1
  • you look at the value of (number / 10) % 2, you must look at the value of number % 2
  • it is useless to do the test then its reverse, just use else, or better remove the two ifs and just do binaryNumber[i] = number & 1;
  • number = number / 10; is the right way when you want to produce the value in decimal, in binary you have to divide by 2
  • in for (int i = 31; i >= 0; i--) { except for numbers needing 32 bits you will write useless 0 on the left, why not using the value of i from the while ?
bruno
  • 32,421
  • 7
  • 25
  • 37
0

The first thing is the variable 'i' in the while loop. Consider it more precisely: every time you iterate over it, 'i' is recreated again and assigned the value of zero. It's the basics of the language itself.

The most relevant mistake is logic of your program. Each iteration we must take the remainder of division by 2, and then divide our number by 2.

The correct code is:

#include <iostream>

int main()
{
  int x = 8;
  bool repr[32]{};
  int p = 0;
  while(x)
    {
      repr[p] = x % 2;
      ++p;
      x /= 2;
    }
  for(int i = 31; i >= 0; --i)
    std::cout << repr[i];
    return 0;
}
mathway
  • 143
  • 11
0

There are some logical errors in your code.

  1. You have taken (number/10) % 2, instead, you have to take (number %2 ) as you want the remainder.
  2. Instead of taking i = 31, you should use this logic so you can print the following binary in reverse order:
    for (int j = i - 1; j >= 0; j--)
    { 
            cout << BinaryNumb[j]; 
    }

Here is the code to convert an integer to its binary equivalent:

    #include <iostream> 
    #include <bits/stdc++.h>
    
    using namespace std; 
      
    // function to convert integer to binary 
    void DecBinary(int n) 
    { 
        // Array to store binary number 
        int BinaryNumb[32]; 
       
        int i = 0; 
        while (n > 0)
        { 
            // Storing remainder in  array 
            BinaryNumb[i] = n % 2; 
            n = n / 2; 
            i++; 
        } 
      
        // Printing  array in reverse order 
        for (int j = i - 1; j >= 0; j--)
        { 
            cout << BinaryNumb[j];
        } 
    } 
      
    // Main Program
    int main() 
    { 
        int testcase;
        
        //Loop is optional 
        
        for(int i = 0; i < testcase; i++)
        {
            cin >> n; 
            DecToBinary(n); 
        }
    
        return 0; 
    }
KaiserKatze
  • 1,521
  • 2
  • 20
  • 30
Shreesh
  • 1
  • 2