basic array problem

So, I'm pretty new to c++, and I'm having trouble with the following function. Its supposed to take an unsigned integer and convert it into it's binary representation. However, I am getting persistent runtime errors, and am at my wits end on how to fix it. can someone help me, or suggest a better way to do this? It would be very much appreciated.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
void binaryOutput(unsigned int x){
  int count;
  int a=x;
  int b=x; 
 while(a>0){
   count++;
   a/=2;
}
 int array [count];
 for(int i=0; i<count; i++){
   if(b>0){
   array[i]=b%2;
   b/=2;}
 }
Last edited on
try this,
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <stdio.h>

int main(){
    int a=10,b[9];

    for(int i=0;i<8;i++){
        b[i] = a%2;
        a /= 2;
    }

    for(int j=7;j>-1;j--){
        printf("%d",b[j]);
    }

}


the function may look like this
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <stdio.h>

int BinaryOutput(int a){
    int b[9];
    
    for(int i=0;i<8;i++){
        b[i]=a%2;
        a/=2;
    }
    
    for(int j=7;j>-1;j--){
    printf("%d",b[j]);
    }
    return 0;
}

int main(){
    int YourInput;
    scanf("%d", &YourInput);
    //You can add the input filter :D
    BinaryOutput(YourInput);
}
Last edited on
Topic archived. No new replies allowed.