在下面显示的代码中,我无法以相反的顺序打印数组

问题描述 投票:0回答:1

在下面显示的代码中,我无法以相反的顺序打印数组。除了相反的部分,其余代码工作正常。我该如何解决这个问题?

#include <stdio.h>
int main()
{
    int arr[10]={1,2,3,4,5,6,7,8,9,10};
    int i;
    printf("Enter the values into the array and see them in normal and reverse order\n");
    printf("------------------------------------------------------------------------\n");
    printf("Enter the number of elements into the array\n");
    scanf("%d", &i);
    printf("Input %d elmements into the array:\n", i);
    for (i = 0; i < 10; i ++)
    {
        printf("elemenet - %d: ", i);
        scanf("%d", &arr[i]);
    }
    printf("\nThe values stored into the array are:\n");
    for (i = 0; i < 10; i ++)
    {
        printf("%3d",arr[i] );
    }
    printf("\nThe values stored into the array in reverse order are:\n");
    for (i = 0; i > 10; i --) \*something wrong here*\
    {
        printf("%5d",arr[i] );
    }
    printf("\n\n");
}

特别是下面的代码中应用了数组反转的部分:

printf("\nThe values stored into the array in reverse order are:\n");
    for (i = 0; i > 10; i --) \*something wrong here*\
    {
        printf("%5d",arr[i] );
    }

我不知道如何扭转它。您的帮助将不胜感激。

c arrays for-loop reverse
1个回答
1
投票

如果要以相反的顺序打印,则需要从最大的索引处开始i,并且循环条件应确保i为正:


1
投票

阅读您的代码:

© www.soinside.com 2019 - 2024. All rights reserved.