首页 > 解决方案 > 在下面显示的代码中,我无法以相反的顺序打印数组

问题描述

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

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

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

标签: carraysfor-loopreverse

解决方案


阅读您的代码:

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

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

尝试将其更改为

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

推荐阅读