I am working on a program in C which is supposed to dynamically allocate memory to a 2d array of [x] rows and [y] columns with the help of malloc(). The things I was supposed to to do for this program are :
This is how I have approached the problem
#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;
}
As far as the code is, it works properly when compiled by allocating 2 bytes per integer for each slot in the integer array, but when I submit this same code to Bottlenose (Web based assignment submission for CS based courses), I am given these two prompts
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.
What exactly am I doing wrong in my program?
答案 0 :(得分:0)
当我阅读您的问题陈述时,您似乎对指示感到困惑。具体做法是:
Each cell needs to be assigned with the number i + j
您还被告知&#34;在单个下标中记录您自己的偏移量i * c + j&#34; 。这意味着将i + j
的值简单分配给偏移array[i * c + j]
,这意味着:
array[i * c + j] = i + j;
此外,如评论中所示,您没有分配2D数组,而是分配足够大小的内存块来保存r * c
整数值。是否按顺序索引该块中的值(作为简单数组)或将它们索引为模拟 2D数组完全取决于您,但底层内存块只是{{1 }} bytes。
将这些部分放在一起,你可以做类似以下的事情:
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;
}
如果您在模拟的2D数组中打印值,您将获得:
$ ./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
还要注意要求:
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'); }
可能意味着单独的嵌套初始化和求和循环需要你走遍整个阵列&#34;使用指针对值求和。在这种情况下,你可以做类似的事情:
Walk the entire array and add up each element to form the sum.
这更多的是语义问题,但使用单独的初始化和求和循环并使用指针来遍历数组更符合您的指令。