如果这令人困惑,我很抱歉......到目前为止,我正在将十进制数转换为二进制数。这样做时,我将二进制表示的数字存储到一个 int 数组中。
EX:对于数字 4。(这是在下面的 dec2bin 中完成的)
temp[0] = 1
temp[1] = 0
temp[2] = 0
我想将此数组存储到另一个数组(例如 BinaryArray)中,该数组将包含多个“临时”数组。
我希望 BinaryArray 声明为 main,传递给 dec2bin,并保存当前临时数组的副本。然后转到下一个号码。
我无法弄清楚指针和不需要的东西。如果有人可以帮助我如何在 main 中声明所需的数组以及如何从 dec2bin 添加到它。
谢谢!主要的:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
void dec2bin(int term, int size);
int size, mincount;
int * ptr;
int x;
x=0;
scanf("%d %d", &size, &mincount);
printf("Variables: %d\n", size);
printf("Count of minterms: %d\n", mincount);
int input[mincount+1];
while(x < mincount){
scanf("%d", &input[x]);
x++;
}
x = 0;
while(x < mincount){
dec2bin(input[x], size);
十二月二箱:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SIZE 32
void
dec2bin(int term,int size){
int i, j, temp[size], remain, quotient;
quotient = term;
i = size-1;
// set all temp to 0
for(j=size-1;j>=0; j--){
temp[j] = 0;
}
//change to binary
while(quotient != 0){
remain = quotient % 2;
quotient/=2;
if(remain != 0){
temp[i] = 1;
} else {
temp[i] = 0;
}
i--;
}
//print array
for(i=0; i<size; i++)
printf("%d", temp[i]);
printf("\n");
}