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

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

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

我无法弄清楚如何扭转它。您的帮助将不胜感激。

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

阅读您的代码:

for (i = 0; i > 10; i --)

它说:只要i> 10,就从i = 0开始运行。这当然不是你的意思。

尝试将其更改为

for (i = 9; i >= 0; i--)
,

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

for (i=9; i>=0; i--)
{
    printf("%5d",arr[i] );
}
本文链接:https://www.f2er.com/3041204.html

大家都在问