我正在使用C语言编写一个程序,该程序应该借助malloc()动态地将内存分配给[x]行和[y]列的二维数组。我应该为此程序做的事情是:
创建空间来容纳[r] x [c]个整数。
使用嵌套的for循环来填充数组空间。
每个单元需要分配编号i + j
您需要在单个下标中跟踪i * c + j的偏移量。
遍历整个数组,并将每个元素加起来形成和。
这就是我解决问题的方式
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char const *argv[])
{
int r;
int c;
int sum = 0;
int i;
int j;
int *A;
printf("Enter number of rows : \n");
scanf("%d", &r);
printf("Enter number of columns : \n");
scanf("%d", &c);
A = (int *)malloc(sizeof(int) * r * c);
printf("Enter the array elements : ");
for (i = 0; i < r; i++)
{
for (j = 0; j < c; j++)
{
scanf("%d", &(*(A + i * c + j)));
sum += (*(A + i * c + j));
}
}
printf("The sum is : %d", sum);
return 0;
}
就代码而言,在通过为整数数组中的每个插槽分配每个整数2个字节进行编译时,它可以正常工作,但是当我将相同的代码提交给Bottlenose(针对基于CS的课程的基于Web的作业提交)时,我得到了这些代码两个提示
not ok 1 - Test 1 FAILED: Did not print sum properly.
not ok 2 - Test 2 FAILED: Did not allocated and free
correct amount of memory with one call to malloc and one call to free.
我的程序到底在做什么错?
最佳答案
当我阅读您的问题陈述时,似乎您对指导感到困惑。特别:
Each cell needs to be assigned with the number i + j
您还被告知“在单个下标中以i * c + j跟踪自己的偏移量”。这意味着将
i + j
的值简单分配给偏移量array[i * c + j]
,这意味着:array[i * c + j] = i + j;
此外,如注释中所示,您不是在分配2D数组,而是在分配具有足够SIZE大小以容纳
r * c
整数值的内存块。究竟是按顺序(作为一个简单的数组)索引该块中的值还是作为一个模拟的2D数组对其进行索引完全取决于您,但是内存的基础块只是一个r * c * sizeof *array
个字节的分配块。将这些部分放在一起,您可以执行以下操作:
#include <stdio.h>
#include <stdlib.h>
int main (void) { /* you don't use any argument, use void */
int *array, r, c, sum = 0; /* declare, initialize variables */
printf ("enter number of rows: ");
if (scanf ("%d", &r) != 1) { /* alway validate ALL input */
fprintf (stderr, "error: invalid row value.\n");
return 1;
}
printf ("enter number of cols: ");
if (scanf ("%d", &c) != 1) {
fprintf (stderr, "error: invalid column value.\n");
return 1;
}
/* allocate & VALIDATE -- often useful to initialize to zero with calloc */
if (!(array = calloc (r * c, sizeof *array))) {
fprintf (stderr, "error: virtual memory exhausted.\n");
return 1;
}
for (int i = 0; i < r; i++) /* nested loops */
for (int j = 0; j < c; j++) {
array[i * c + j] = i + j; /* assign i + j to correct address */
sum += array[i * c + j]; /* add value to sum */
}
printf ("The sum is : %d\n", sum);
free (array); /* if you allocate it, track it and free it when done */
return 0;
}
使用/输出示例
$ ./bin/arrayalloc
enter number of rows: 4
enter number of cols: 4
The sum is : 48
如果在模拟的2D数组中打印值,将得到:
$ ./bin/arrayalloc
enter number of rows: 4
enter number of cols: 4
0 1 2 3
1 2 3 4
2 3 4 5
3 4 5 6
The sum is : 48
仔细检查一下,如果您还有其他问题,请告诉我。如果您希望使用单个下标索引将值输出为模拟的2D数组,则可以执行以下操作:
for (int i = 0; i < r; i++) { /* nested loops */
for (int j = 0; j < c; j++) {
array[i * c + j] = i + j; /* assign i + j to correct offset */
sum += array[i * c + j]; /* add value to sum */
printf (" %3d", array[i * c + j]);
}
putchar ('\n');
}
另请注意以下要求:
Walk the entire array and add up each element to form the sum.
可能意味着嵌套的初始化和求和循环需要您使用指针对值求和的“遍历整个数组”。在这种情况下,您可以执行以下操作:
#include <stdio.h>
#include <stdlib.h>
int main (void) { /* you don't use any argument, use void */
int *array, *p, r, c, sum = 0; /* declare, initialize variables */
printf ("enter number of rows: ");
if (scanf ("%d", &r) != 1) { /* alway validate ALL input */
fprintf (stderr, "error: invalid row value.\n");
return 1;
}
printf ("enter number of cols: ");
if (scanf ("%d", &c) != 1) {
fprintf (stderr, "error: invalid column value.\n");
return 1;
}
/* allocate & VALIDATE */
if (!(array = calloc (r * c, sizeof *array))) {
fprintf (stderr, "error: virtual memory exhausted.\n");
return 1;
}
p = array; /* assign array to pointer */
for (int i = 0; i < r; i++) /* nested loops used to initialize */
for (int j = 0; j < c; j++)
array[i * c + j] = i + j; /* assign i + j to correct offset */
for (int i = 0; i < r * c; i++, p++) /* separate loop to walk the array */
sum += *p; /* sum values using pointer */
printf ("The sum is : %d\n", sum);
free (array); /* if you allocate it, track it and free it when done */
return 0;
}
这更多的是语义问题,但是使用单独的初始化和求和循环以及使用指针遍历数组更符合您的指令。
关于c - 持久性值在代码中不正确,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/43314173/