c - 在下面显示的代码中,我无法以相反的顺序打印数组
问题描述
在下面显示的代码中,我无法以相反的顺序打印数组。除了相反的部分外,其余代码都可以正常工作。我该如何解决这个问题?
#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] );
}
我无法弄清楚如何扭转它。您的帮助将不胜感激。
解决方案
阅读您的代码:
for (i = 0; i > 10; i --)
它说:只要 i>10,就从 i=0 运行。这当然不是你的意思。
尝试将其更改为
for (i = 9; i >= 0; i--)
推荐阅读
- google-maps - Flutter - AnimateCamera 无法与 newLatLngBounds 一起使用
- visual-studio-code - 是否可以通过 Visual Studio 代码运行 #[tokio::test] 标记的单元测试?
- python - Python。TypeError:“str”和“int”的实例之间不支持“<”
- android - 如何在 ViewBinding 中使用片段?
- python-3.x - OpenPyXl - TypeError:无法解压不可迭代的 NoneType 对象
- uwp - TabView 和 UWP 项目
- onclick - 未捕获的类型错误:无法使用 onclick 将属性“innerHTML”设置为 null
- scaling - 理解不同训练数据缩放方案的 GPy 标准差?
- android - Facebook Deferred Deep Link Android - AppLinks.getAppLinkData 返回 null
- reactjs - React - 将 Formik 函数组件迁移到类组件