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.
scanfinputs a decimal number15, the expected output is anintarray that has:1 1 1 1as 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 1int * 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]. The16is a guess and could be32. Call with (e.g.)int n[32]; dec2bin(37746,n);#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