簡體   English   中英

如何反轉數組並將反轉數組的值存儲在新數組中

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

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

  1. 反轉一個數組(fe niz[] = { 2, 4, 5, 7, 4, 8, 3 }需要變成niz[] = { 3, 8, 4, 7, 5, 4, 2 }
  2. 將反轉數組的值存儲到一個全新的數組中
  3. 所有這些都必須在不使用printf內的 printf 的情況下完成,該 function 反轉數組並將值存儲到新數組中。 此外,需要預定義數組及其大小(因此無需用戶輸入)。
  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);
}

那么有人在我的代碼中看到了問題嗎?

是的:

  • d接收一個未初始化的參數c ,因為您在使用它之前從未在 function 的 scope 中初始化它,它會調用未定義的行為

  • koko的返回類型不正確,因為您試圖返回array2 ,實際上niz2 ,您需要int*返回類型,而不是int

    此外,您不需要這樣做,因為作為 function 的參數傳遞的數組會衰減到指向它的第一個元素的指針,即使參數不是明確的指針,在 ZC1C415268E17A94 的 scope 中對其所做的更改也是永久的.

根據您的代碼,您可以執行以下操作:

運行示例

#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 function的參數,所以不需要返回。 如果要返回它,則應將返回的類型從int更改為int *

koko function 中, d必須始終從0開始,因此您無需將其聲明為此 function 的參數。 讓我們在這個 function 中將其聲明為局部變量。

cfor循環的迭代器之一,所以讓我們在這個 function 中聲明它,初始值等於size - 1size是數組的大小或該數組中的元素數)。

順便說一句,這個 function 變成如下:

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 中使用此 function,您只需提供兩個 arrays 和數組大小(在本例中為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]);
}

output:

3 8 4 7 5 4 2 

您不需要將 4 arguments 傳遞給koko ,只需 arrays 和元素數量就足夠了。 也不要與-1比較,將循環寫為在0處停止的向下循環,這樣您就可以使用無符號索引類型,例如size_t 更好地使用索引從 0 到排除length的經典循環,並將源元素存儲到適當的目標元素。

你得到一個沒有意義的數字 output 的原因是你 printf 的返回值koko ,它被錯誤輸入為int而你返回一個指向目標數組的指針,無論如何你不能以這種方式打印一個數組,你可以使用循環遍歷數組元素。

這是修改后的版本:

#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;
}

function的返回類型不正確。

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

返回的表達式具有int *類型,而返回類型是int

而這個電話

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

沒有意義,因為變量ab未初始化。

也是 function 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;
}

程序 output 是

3 8 4 7 5 4 2 

如果你想以相反的順序將一個數組復制到另一個已經存在的數組中,那么對應的 function 可以如下面的演示程序所示。

#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;
}

程序 output 將與上圖相同。

3 8 4 7 5 4 2 

在 function 中不引入任何額外的變量(實際上是多余的),它可以這樣定義

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

正如您在 function 內部看到的,僅使用了它的參數。

順便說一句,遞歸 function 可以如下所示。:)

#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;
}

它的 output 再次是

3 8 4 7 5 4 2

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM