增加数组的大小

increase size of array

你能帮我做这个练习吗?

Write a C program that reads 6 integers from the keyboard and assigns the first 5 values at the first 5 positions of an array; store the sixth value in a variable N. Write a function that, given input the array initialized with the first 5 values from the keyboard and the integer N, returns the array resized to contain 5 + N elements, such that each one of the new N elements corresponds to the sum of the numbers before it in the array. In main, print the content of the array returned by the function.

main函数中也可以。

当我必须使用函数 realloc 将数组从 size = 5 递增到 5 + N 时遇到问题。

这是我的代码:

int N, a, i;
int *ptr;
int arr[6];

for (i = 0; i < 5; i++) {
    printf("Insert number in array, position(%d): ", i);
    scanf("%d", &arr[i]);
}

N = arr[4];

a = 5 + N;

ptr = (int *)realloc(arr, sizeof(int) * a);

for (i = 4; i < a; i++) {
    ptr + i = N * N; //<--- **problem!!**
}

for (i = 0; i < a; i++) {
    printf("%d\n", ptr[i]);
}

free(ptr);

您不能重新分配在函数中局部定义或全局定义的数组。您只能对先前使用 malloc()calloc()realloc()NULL 指针分配的对象调用 realloc。所以你必须在 main() 中分配 5 个元素的初始数组,并在函数中重新分配它。

#include <stdio.h>
#include <stdlib.h>

int *extend_array(int *arr, int N) {
    int a = 5 + N;
    arr = realloc(arr, a * sizeof(int));
    if (arr != NULL) {
        int sum = 0;
        for (int i = 0; i < 5; i++) {
            sum += arr[i];
        }
        for (int i = 5; i < a; i++) {
            arr[i] = sum;
            sum += sum;
        }
    }
    return arr;
}

int main() {
    int N;

    int *arr = malloc(5 * sizeof(int));
    if (arr == NULL) {
        printf("allocation failed\n");
        return 1;
    }
    for (int i = 0; i < 5; i++) {
        printf("Insert number in array, position(%d): ", i);
        if (scanf("%d", &arr[i]) != 1) {
            printf("invalid input\n");
            return 1;
        }
    }

    printf("Insert the value of N: ");
    if (scanf("%d", &N) != 1) {
        printf("invalid input\n");
        return 1;
    }

    int *ptr = extend_array(arr, N);
    if (ptr == NULL) {
        printf("reallocation failed\n");
    } else {
        arr = ptr;
        for (int i = 0; i < 5 + N; i++) {
            printf("%d\n", arr[i]);
        }
    }
    free(arr);
    return 0;
}

赋值指定函数应将数组和数字 N 作为参数,但最好将初始大小设为变量并将其传递给函数以生成代码由于常量 5 出现在许多地方,因此更通用、更易于扩展且更不容易出错。

如果您需要重新分配一个数组,那么它最初必须是动态分配的。 所以这段代码

int arr[6];
//...
ptr = (int *)realloc(arr, sizeof(int) * a);

无效。

注意根据赋值需要写一个重新分配数组的函数

请记住,像 5 一样使用 "magic numbers" 是一种非常糟糕的编程风格。而是使用命名常量或将此类数字分配给变量并使用它们。

程序可以如下所示。

#include <stdio.h>
#include <stdlib.h>

int * resize( int *a, size_t n, size_t m )
{
    int *tmp = realloc( a, ( n + m ) * sizeof( int ) );

    if ( tmp != NULL )
    {
        int sum = 0;
        size_t i = 0;

        while ( i < n ) sum += tmp[i++];
        while ( i < n + m )
        {
            tmp[i] = sum;
            sum += tmp[i++];
        }
    }

    return tmp;
}

int main(void) 
{
    size_t n = 5;
    int *a = malloc( n * sizeof( int ) );
    size_t m = 0;

    printf( "Enter %zu numbers. The last number shall be greater than 0: ", n + 1 );

    for ( size_t i = 0; i < n; i++ )
    {
        scanf( "%d", a + i );
    }

    scanf( "%zu", &m );

    int *tmp  = resize( a, n, m );

    if ( tmp != NULL )
    {
        a = tmp;
    }
    else
    {
        m = 0;
    }

    for ( size_t i = 0; i < n + m; i++ )
    {
        printf( "%d ", a[i] );
    }

    putchar( '\n' );

    free( a );

    return 0;
}

程序输出可能看起来像

Enter 6 numbers. The last number shall be greater than 0: 1 2 3 4 5 6
1 2 3 4 5 15 30 60 120 240 480 960 1920 3840 7680