所以,我要做的就是创建一个数组,其中包含数字 N 的多个表,即:
"Enter a number = 6"
"1 2 3 4 5 6"
"2 4 6 8 10 12" and so on untill 36
这是我的代码:
int * initiallizeArray(int * rows)
{
int i = 0, j = 0;
int * twoDArray = 0;
printf("Enter a number: ");
scanf("%d", rows);
twoDArray = (int*)malloc(sizeof(int) * (*rows * *rows));
for (i = 0; i < *rows; i++)
{
for (j = 0; j < *rows; j++)
{
//twoDArray[i * *rows + j] =
}
}
return twoDArray;
}
带有“//”的行是我不知道要在其中实现什么 基本上它在整个数组中循环,但我不知道将什么放入特定单元格
最佳答案
由于 twoDArray
实际上不是一个二维数组,因此您最好将其重命名为更清晰的名称。不建议强制转换 malloc
的返回值,因为这是不必要的,并且如果更改要分配的指针的类型,可能会引入错误。 for 循环的主体非常简单:(i + 1) * (j + 1)
。
int* initiallizeArray(int* rows)
{
int i = 0, j = 0;
int* mult_table = NULL;
printf("Enter a number: ");
scanf("%d", rows);
mult_table = malloc((sizeof *mult_table) * (*rows) * (*rows));
for (i = 0; i < *rows; i++)
{
for (j = 0; j < *rows; j++)
{
mult_table[i * (*rows) + j] = (i + 1) * (j + 1);
}
}
return mult_table;
}
关于c - 包含多个表和一个 malloc() 的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43297629/