二维数组C中的布尔表
我在创建以下数组时遇到一些困难。我的任务是使用递归填充一个2D数组,该数组包含0和1的所有可能组合,按词法顺序执行m次。从数学上讲,有2^m个组合。我的程序只是用相同的顺序0 1 0 1填充数组的前3行,然后只打印其余的行0 0 范例 m=4 这是我迄今为止的代码,如果有人能纠正它并解释我做错了什么,我将不胜感激,因为我自己无法发现错误二维数组C中的布尔表,c,recursion,C,Recursion,我在创建以下数组时遇到一些困难。我的任务是使用递归填充一个2D数组,该数组包含0和1的所有可能组合,按词法顺序执行m次。从数学上讲,有2^m个组合。我的程序只是用相同的顺序0 1 0 1填充数组的前3行,然后只打印其余的行0 0 范例 m=4 这是我迄今为止的代码,如果有人能纠正它并解释我做错了什么,我将不胜感激,因为我自己无法发现错误 #include <stdio.h> #include <stdlib.h> #include <string.h> #in
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <math.h>
void *safeMalloc(int n) {
void *p = malloc(n);
if (p == NULL) {
printf("Error: malloc(%d) failed. Out of memory?\n", n);
exit(EXIT_FAILURE);
}
return p;
}
void combine(int** arrTF,int m,int n,int row,int col){
if(m==0){
if(row<pow(2,m)){
row++;
combine(arrTF,n,n,row,0);
}else{
return;
}
}else{
arrTF[row][col]=0;
col++;
combine(arrTF,m-1,n,row,col);
arrTF[row][col]=1;
col++;
combine(arrTF,m-1,n,row,col);
}
}
int main(int argc, char *argv[]) {
int m
scanf("%d",&m);
int** arrTF;
arrTF = safeMalloc(pow(2,m)*sizeof(int *));
for (int r=0; r < pow(2,m); r++) {
arrTF[r] = safeMalloc(m*sizeof(int));
}
for(int i=0;i<pow(2,m);i++){
for(int j=0;j<m;j++){
arrTF[i][j]=0;
}
}
combine(arrTF,m,m,0,0);
for(int i=0;i<pow(2,m);i++){
for(int j=0;j<m;j++){
printf("%d ",arrTF[i][j]);
}
printf("\n");
}
return 0;
}
#包括
#包括
#包括
#包括
#包括
void*safeMalloc(整数n){
void*p=malloc(n);
if(p==NULL){
printf(“错误:malloc(%d)失败。内存不足?\n”,n);
退出(退出失败);
}
返回p;
}
无效合并(整数**arrTF、整数m、整数n、整数行、整数列){
如果(m==0){
如果(行您想要所有可能的(2^m)
组合0和1的
按词法顺序执行m
次,并且您正在使用2D数组存储结果。
如果您只想打印0
和1
的所有可能组合,而不是将其存储在2D数组中并在以后打印数组,那么事情将非常简单
将0的
和1的
组合存储到2D数组有点棘手,因为每个组合都是2D数组的一个元素。
您希望根据递归算法生成0
和1
的组合。
比如说,在某个阶段,如果您的算法生成组合0010,该组合存储在2D数组中的元素中。
下一个组合是0011,递归算法只需将最后一个组合中的最后一个数字从0更改为1(0010)即可生成该组合
所以,这意味着每次生成组合时,都需要将该组合复制到其在2D数组中的连续位置。
例如,如果在算法开始计算下一个组合之前,将0010存储在2D数组的索引2中,我们需要做两件事:
将索引2的元素复制到索引3
增加行号,使最后一个组合保持完整
(例如,这是2D阵列)
|0 | 0 | 0 | 0 |索引0
|0 | 0 | 0 | 1 |索引1
|0 | 0 | 1 | 0 |索引2--->将其复制到其连续位置(即索引3处)
|0 | 0 | 1 |索引3--->最后一个组合(索引2),最后一个数字从0更改为1
在生成每个组合后,我们需要执行此操作。
现在,我希望你明白了你犯的错误
很少有好的做法可以遵循:
如果要分配内存并将其初始化为0,请使用calloc
而不是malloc
对于同一输入,您反复调用的任何数学函数,最好只调用一次,然后将结果存储在变量中,并在需要时使用该结果
不要包含程序中不需要的任何头文件
完成后,请确保释放程序中动态分配的内存
我已在您的程序中进行了更正:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
void *safeMalloc(size_t n, size_t size) {
void *p = calloc(n, size);
if (p == NULL) {
printf("Error: calloc(%zu) failed. Out of memory!\n", n);
exit(EXIT_FAILURE);
}
return p;
}
void deallocate(int ** ptr, int row) {
for(int i = 0; i<row; i++)
free(ptr[i]);
free(ptr);
}
void combine(int **arrTF, int m, int max_col, int max_row) {
static int row;
if(m==0){
int i;
if (row<(max_row - 1))
{
for(i=0; i<max_col; i++)
arrTF[row+1][i] = arrTF[row][i];
}
row++;
return;
} else {
arrTF[row][max_col-m] = 0;
combine(arrTF, m-1, max_col, max_row);
arrTF[row][max_col-m] = 1;
combine(arrTF, m-1, max_col, max_row);
}
}
int main(int argc, char *argv[]) {
int** arrTF;
int m, max_row;
printf ("Enter number: \n");
scanf("%d", &m);
max_row = pow(2, m);
arrTF = safeMalloc(max_row, sizeof(int *));
for (int r=0; r<max_row; r++) {
arrTF[r] = safeMalloc(m, sizeof(int));
}
combine(arrTF, m, m, max_row);
for(int i=0; i<max_row; i++) {
for(int j=0; j<m; j++) {
printf("%d ", arrTF[i][j]);
}
printf("\n");
}
deallocate(arrTF, max_row);
return 0;
}
希望这有帮助。您使用的是pow(2,m)
和m
可以互换,并且可能会将它们混合在一起。除此之外,解决此类问题的正确工具是调试器。我建议您花一些时间阅读Eric Lippert的文章,学习如何使用调试器在监视变量值的同时逐行检查程序。我不认为调试器我会帮助我,因为我非常确定我的逻辑是关闭的。也许你可以解释我如何从逻辑的角度来处理这个程序,因为我现在认为我在退出第一个递归后,col变量有问题。
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
void *safeMalloc(size_t n, size_t size) {
void *p = calloc(n, size);
if (p == NULL) {
printf("Error: calloc(%zu) failed. Out of memory!\n", n);
exit(EXIT_FAILURE);
}
return p;
}
void deallocate(int ** ptr, int row) {
for(int i = 0; i<row; i++)
free(ptr[i]);
free(ptr);
}
void combine(int **arrTF, int m, int max_col, int max_row) {
static int row;
if(m==0){
int i;
if (row<(max_row - 1))
{
for(i=0; i<max_col; i++)
arrTF[row+1][i] = arrTF[row][i];
}
row++;
return;
} else {
arrTF[row][max_col-m] = 0;
combine(arrTF, m-1, max_col, max_row);
arrTF[row][max_col-m] = 1;
combine(arrTF, m-1, max_col, max_row);
}
}
int main(int argc, char *argv[]) {
int** arrTF;
int m, max_row;
printf ("Enter number: \n");
scanf("%d", &m);
max_row = pow(2, m);
arrTF = safeMalloc(max_row, sizeof(int *));
for (int r=0; r<max_row; r++) {
arrTF[r] = safeMalloc(m, sizeof(int));
}
combine(arrTF, m, m, max_row);
for(int i=0; i<max_row; i++) {
for(int j=0; j<m; j++) {
printf("%d ", arrTF[i][j]);
}
printf("\n");
}
deallocate(arrTF, max_row);
return 0;
}
$ ./a.out
Enter number:
2
0 0
0 1
1 0
1 1
$ ./a.out
4
0 0 0 0
0 0 0 1
0 0 1 0
0 0 1 1
0 1 0 0
0 1 0 1
0 1 1 0
0 1 1 1
1 0 0 0
1 0 0 1
1 0 1 0
1 0 1 1
1 1 0 0
1 1 0 1
1 1 1 0
1 1 1 1