0

Write a function int* dec2bin(int N, int* n), which, given a natural number 0 ≤ N < 65535, computes and returns its representation in the binary numeral system. The program has to determine the coefficients ai ∈ {0,1}, i = 0,...,n − 1, such that N = (sum->n-1) ai2^i (n ≤ 16).

#include <stdio.h>
#include <math.h>
#include <assert.h>

int decimalToBinary(int N)
{
    int B_Number = 0;
    int c= 0;
    int ctr=0;
    while (N != 0) {
        int rem = N % 2;
        c = pow(10, ctr);
        B_Number += rem * c;
        N /= 2;
        ctr++;
    }
    return B_Number;
}
 
int main()
{
    int N;
    scanf("%d", &N);
    printf("%d", decimalToBinary(N));
    return 0;
}

I know how to make a program that converts the numbers but I don't understand why the pointer is needed for and how to implement it.

4
  • 1
    65535 is 16 bits. Is an int large enough to hold 16 digits? Commented Dec 7, 2022 at 0:44
  • A few sample inputs and expected outputs would help. Just a [large] guess ... If the scanf inputs a decimal number 15, the expected output is an int array that has: 1 1 1 1 as elements. 14 --> 1 1 1 0. 7 --> 0 1 1 1. And how many binary digits do we have to output? Because of the range 65536, which is 0xFFFF, I assume we want 16 bits (i.e.) 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 Commented Dec 7, 2022 at 1:43
  • Here is a crude guess: int * dec2bin(int N,int *n) { int idx; for (idx = 0; idx < 16; ++idx) n[idx] = 0; for (idx = 0; N != 0; N /= 2, ++idx) { if (N & 1) n[idx] = 1; } return n; } This puts the array in little endian order [and doesn't handle negative numbers]. The 16 is a guess and could be 32. Call with (e.g.) int n[32]; dec2bin(37746,n); Commented Dec 7, 2022 at 1:52
  • #include <stdio.h> int* decimalToBinary(int N, int* n) { int i=0; int rem; while (N!=0 && i<16){ rem = N % 2; n[i]= rem; N /= 2; i++; } return n; } int main() { int N; int* n; scanf("%d", &N); printf("%p", decimalToBinary(N,n)); return 0; } I tired doing it like this but I keep getting a bus error Commented Dec 7, 2022 at 2:29

3 Answers 3

1

Another way...
This was written to print the binary representation of a value (left-to-right). Instead of printing, you could simply assign the 0/1 (left-to-right) to a passed array (of 16 integers), then return the number of assigned integers to the calling function to print them from a loop.

int main() {
    for( int i = 253; i <= 258; i++ ) {
        printf( "Decimal %d: ", i );
        unsigned int bitmask = 0;
        bitmask = ~bitmask;
        bitmask &= ~(bitmask >> 1); // High bitmask ready

        // skip over leading 0's (optional)
        while( bitmask && (bitmask & i) == 0 ) bitmask >>= 1;

        // loop using bitmask to output 1/0, then shift mask
        do {
            putchar( (bitmask & i) ? '1' : '0' );
        } while( (bitmask >>= 1) != 0 );

        putchar( '\n' );
    }
    return 0;
}
Sign up to request clarification or add additional context in comments.

Comments

1

Use an integer type capable of encoding the decimal number 1111_1111_1111_1111: use long long.

Do not use pow(), a floating point function for an integer problem. It may generate value just slightly smaller than the integer expected and is slow.

long long decimalToBinary_alt(int N) {
  long long B_Number = 0;
  long long power = 1;

  while (N != 0) {
    int rem = N % 2;  // result: -1, 0, or 1
    B_Number += rem * power;
    N /= 2;
    power *= 10;  // Scale the power of 10 for the next iteration.
  }
  return B_Number;
}

Usage

printf("%lld\n", decimalToBinary(N));

Comments

-1

Your function does not have the required parameters and return value.

int* dec2bin(int N, int* n)
{
    unsigned uN = N;
    
    for(int bit = 15; bit >= 0; bit--)
    {
        *(n + 15 - bit) = !!(uN & (1U << bit));
    }
    return n;
}

2 Comments

Code without an explanation is as bad as code without a question. When he submits this as his homework, and his teacher asks him to explain, what will he say? What does "!!" mean? We know - he doesn't. His teacher probably doesn't.
And so sorry @annnna for saying "he" everywhere. My bad.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.