【发布时间】:2012-09-11 02:43:41
【问题描述】:
如果这令人困惑,我很抱歉......到目前为止,我正在将十进制数转换为二进制数。这样做时,我将二进制表示的数字存储到一个 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);
Dec2bin:
#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");
}
【问题讨论】:
-
您好,请编辑,主要发布两次 :)\
-
当。刚离开家。将不得不在几个中添加正确的东西
-
好吧,我现在可以在没有完整代码的情况下进行:为什么不声明/存储为 int **?也就是说,直观地说,是一个整数数组。如果您有不同大小的数字,则可能无法预先声明大小,这会导致 mallocs,导致巨大的麻烦,因为您假设了数组大小之一等,但是...
-
... 我要做的是声明一个 int** 来保存您的所有数据以及存储每个 int 数组大小的 int*(或 int[])。这样您就可以(相对)安全地遍历您的数据
-
谢谢 AK,我会尝试一下,我得从比赛中休息一下!..lol,我会回来看看你的答案是否会根据添加的正确的信息:)
标签: c arrays pointers multidimensional-array