如何反转数组并将反转数组的值存储在新数组中

How to reverse an array and store the values of the reversed array in a new array

我的程序需要执行以下操作:

  1. 反转数组(f.e。niz[] = { 2, 4, 5, 7, 4, 8, 3 }需要变成niz[] = { 3, 8, 4, 7, 5, 4, 2 }
  2. 将反转数组的值存储到全新数组中
  3. 所有这些都必须在不使用 printf 的函数内完成,该函数反转数组并将值存储到新数组中。此外,需要预定义数组及其大小(因此无需用户输入)。
  4. 结果(在本例中为包含先前数组的反转值的新数组)需要打印在 main

但是,不是反转数组并将其存储到新数组中并返回它。该程序始终打印以下数字:

6356668

有人看到我的代码中的问题了吗?

int koko(int *array, int *array2, int c, int d) {
    for (c = 6; c > -1; c--, d++) {
        array2[d] = array[c];
    }
    return array2;
}

int main() {
    int niz[] = { 2, 4, 5, 7, 4, 8, 3 };
    int niz2[7];
    int a, b, c;
    c = koko(niz, niz2, a, b);
    printf("%d", c);
}

函数的 return 类型不正确。

int koko(int *array,int *array2,int c,int d)
{
for(c=6;c>-1;c--,d++)
{
    array2[d]=array[c];
}
return array2;
}

returned 表达式的类型为 int *,而 return 类型为 int

还有这个电话

c=koko(niz,niz2,a,b);

没有意义,因为变量 ab 未初始化。

也是这个函数的单次调用printf

printf("%d", c);

与输出整个结果数组没有任何共同点。

看来您需要的是以下内容。

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

int * reverse( const int *a, size_t n )
{
    int *result = malloc( n * sizeof( int ) );

    if ( result != NULL )
    {
        for ( size_t i = 0; i < n; i++ )
        {
            result[i] = a[n - i - 1];
        }
    }

    return result;
}

int main(void) 
{
    int a[] = { 2, 4, 5, 7, 4, 8, 3 };
    const size_t N = sizeof( a ) / sizeof( *a );

    int *b = reverse( a, N );

    if ( b != NULL )
    {
        for ( size_t i = 0; i < N; i++ )
        {
            printf( "%d ", b[i] );
        }

        putchar( '\n' );
    }

    free( b );

    return 0;
}

程序输出为

3 8 4 7 5 4 2 

如果你想以相反的顺序将一个数组复制到另一个已经存在的数组中,那么相应的函数可以如下面的演示程序所示。

#include <stdio.h>

void reverse_copy( const int *a, size_t n, int *b )
{
    const int *p = a + n;

    while ( p-- != a )
    {
        *b++ = *p;
    }
}

int main(void) 
{
    enum { N = 7 };
    int a[N] = { 2, 4, 5, 7, 4, 8, 3 };
    int b[N];

    reverse_copy( a, N, b );

    for ( size_t i = 0; i < N; i++ )
    {
        printf( "%d ", b[i] );
    }

    putchar( '\n' );

    return 0;
}

程序输出与上图相同。

3 8 4 7 5 4 2 

在函数中不引入任何额外的变量(实际上是多余的)就可以这样定义

void reverse_copy( const int *a, size_t n, int *b )
{
    while ( n-- )
    {
        *b++ = a[n];
    }
}

正如您在函数内部看到的那样,只使用了它的参数。

顺便说一句,递归函数可以如下所示。:)

#include <stdio.h>

void reverse_copy( const int *a, size_t n, int *b )
{
    if ( n )
    {
        *b = a[n-1];
        reverse_copy( a, n - 1, b + 1 );
    }
}

int main(void) 
{
    enum { N = 7 };
    int a[N] = { 2, 4, 5, 7, 4, 8, 3 };
    int b[N];

    reverse_copy( a, N, b );

    for ( size_t i = 0; i < N; i++ )
    {
        printf( "%d ", b[i] );
    }

    putchar( '\n' );

    return 0;
}

它的输出同样是

3 8 4 7 5 4 2

So does anyone see the problem in my code?

是:

  • d 接收一个单元化参数 c,因为你在使用它之前从未在函数范围内初始化它,它调用 undefined behaviour.

  • koko 的 return 类型不正确,因为您正在尝试 return array2,实际上 niz2,您需要 int* return 类型,而不是 int.

    此外,您不需要这样做,因为作为函数参数传递的数组会衰减为指向其第一个元素的指针,即使参数不是明确的指针,在范围内对其所做的更改也是如此的功能是永久的。

根据您的代码,您可以执行以下操作:

Running sample

#include <stdio.h>

void koko(int *array, int *array2, size_t size) // pass the size of the array
{                                              // size_t more appropriate for object sizes  
    for (int j = 0; size > 0; size--, j++) // initialize j, you can use size as iterator
    {
        array2[j] = array[size - 1]; // changes made to array2 are permanent
    }
}

int main()
{
    int niz[] = {2, 4, 5, 7, 4, 8, 3};
    const size_t SIZE = sizeof(niz) / sizeof(niz[0]); // determine the size of the array
    int niz2[SIZE]; // same size as non reversed array

    koko(niz, niz2, SIZE);

    for (size_t i = 0; i < SIZE; i++) // test print the reversed array
        printf("%d", niz2[i]);
}

因为你使用新数组作为koko函数的参数,所以你不需要return它。如果你想 return 它,你应该将 returning 的类型从 int 更改为 int *

koko函数中,d必须总是从0开始,所以你不需要声明它作为这个函数的参数。让我们在这个函数中将它声明为局部变量。

cfor循环的迭代器之一,所以让我们在这个函数中声明它的初始值等于size - 1size是数组或此数组中的元素数)。

顺便说一句,这个函数变成如下:

void koko(int *array,int *array2,int size)
{
    int d = 0;
    int c;
    for(c = size - 1;c>-1;c--,d++)
    {
        array2[d]=array[c];
    }
}

要在main中使用这个函数,你只需要给出两个数组和数组的大小(在本例中为7):

 koko(niz,niz2,7);

完整测试代码:

#include <stdio.h>

void koko(int *array,int *array2,int size)
{
    int d = 0;
    int c;
    for(c = size - 1;c>-1;c--,d++)
    {
        array2[d]=array[c];
    }
}

int main()
{
    int niz[]={2, 4, 5, 7, 4, 8, 3};
    int niz2[7];
    koko(niz,niz2,7);
    for(int i = 0; i <7; i++)
        printf("%d ",niz2[i]);
}

输出:

3 8 4 7 5 4 2 

您不需要向koko传递4个参数,只需要数组和元素数量就足够了。也不要和-1比较,把循环写成在0处停止的向下循环,这样就可以使用无符号索引类型,比如size_t。最好使用索引从 0 到 length 的经典循环,并将源元素存储到适当的目标元素。

你得到一个无意义的数字作为输出的原因是你 printf koko 的 return 值,它被错误输入为 int 而你 return 一个指针到目标数组,并且无论如何都不能以这种方式打印数组,您可以使用循环来遍历数组元素。

这是修改后的版本:

#include <stdio.h>

void koko(const int *array, int *array2, size_t length) {
    for (size_t i = 0; i < length; i++) {
        array2[length - 1 - i] = array[i];
    }
}

int main() {
    int niz[] = { 2, 4, 5, 7, 4, 8, 3 };
    size_t length = sizeof(niz) / sizeof(niz[0]);
    int niz2[length];
    koko(niz, niz2, length);
    for (size_t i = 0; i < length; i++) {
        printf("%d ", niz2[i]);
    }
    printf("\n");
    return 0;
}