Home > Back-end >  Consult converts decimal positive integer to a binary when the cause of the error
Consult converts decimal positive integer to a binary when the cause of the error

Time:06-02

My freshman year, suddenly thought of wrote this source, but don't know where I went wrong,
#include
Int main () {
int a;
Void DecimalToBinaryConversion (int n);
Printf (" please enter a decimal positive integers: ");
The scanf (" % d ", & amp; A);
Printf (" the corresponding binary number is: ");
DecimalToBinaryConversion (a);
return 0; }

Void DecimalToBinaryConversion (int n)
[32] {short int x={0};
int i=0;
for(i=0; i<=31; I++)
[I] {x=n % 2;
If (n/2! N=n=1)/2;
The else x + 1] [I=n/2; }
For (I=31; I>=0; I -) {
If (x [I]==0) continue;
Else for (; I>=0; I -)
Printf (" % d ", x [I]); }
}

CodePudding user response:

Inside the function static variables should be used

CodePudding user response:

The
reference 1/f, academic probation (R) response:
function should adopt static variables inside

At first I was defined as static variables, but in a few number found that the result is the same, the static statement

CodePudding user response:

The
reference 1/f, academic probation (R) response:
function should adopt static variables inside

#include
Int main () {
int a;
Void DecimalToBinaryConversion (int n);
Printf (" please enter a decimal positive integers: ");
The scanf (" % d ", & amp; A);
Printf (" the corresponding binary number is: ");
DecimalToBinaryConversion (a);
return 0; }

Void DecimalToBinaryConversion (int n)
{the static short int x [32]={0};
int i=0;
for(i=0; i<=31; I++)
[I] {x=n % 2;
If (n/2! N=n=1)/2;
The else x + 1] [I=n/2; break; }
Printf (" % d \ n ", x [1]).
For (I=31; I>=0; I -) {
If (x [I]==0) continue;
Else for (; I>=0; I -)
Printf (" % d ", x [I]); }
}

CodePudding user response:

reference qq_45979829 reply: 3/f
Quote: refer to 1st floor probation (R) response:
function should adopt static variables inside

#include
Int main () {
int a;
Void DecimalToBinaryConversion (int n);
Printf (" please enter a decimal positive integers: ");
The scanf (" % d ", & amp; A);
Printf (" the corresponding binary number is: ");
DecimalToBinaryConversion (a);
return 0; }

Void DecimalToBinaryConversion (int n)
{the static short int x [32]={0};
int i=0;
for(i=0; i<=31; I++)
[I] {x=n % 2;
If (n/2! N=n=1)/2;
The else x + 1] [I=n/2; break; }
Printf (" % d \ n ", x [1]).
For (I=31; I>=0; I -) {
If (x [I]==0) continue;
Else for (; I>=0; I -)
Printf (" % d ", x [I]); }
}

I was in the first statement followed by break; After the statement at least the first three number is no problem

CodePudding user response:

I hope it can help you: https://blog.csdn.net/it_xiangqiang/category_10581430.html
I hope it can help you: https://blog.csdn.net/it_xiangqiang/category_10768339.html
  • Related