用c++按升序和降序打印数组值
printing a array values in ascending and descending order in c++
我在下面编写了这个程序,通过从用户那里获取I/p来打印数组,但显示了一个编译错误。我想要一个程序按升序和降序打印这些输入。
#include <iostream>
using namespace std;
int main()
{
int a[10] = {}; // ![enter image description here][1]
for (int i = 0; a[i] <= 9; i++)
{
cout << "Enter the numbers:" << endl;
cin >> a[i];
}
cout << a[i] << endl;
}
错误:
![error by codeblocks][1]
||=== Build: Debug in example (compiler: GNU GCC Compiler) ===|
H:c++examplemain.cpp||In function 'int main()':|
H:c++examplemain.cpp|12|error: name lookup of 'i' changed for ISO 'for' scoping [-fpermissive]|
H:c++examplemain.cpp|12|note: (if you use '-fpermissive' G++ will accept your code)|
||=== Build failed: 1 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|
您应该在循环中打印数字,而且您的循环条件似乎是错误的。这将起作用:
#include <iostream>
using namespace std;
int main()
{
int a[10];
int i;
cout << "Enter the numbers:" << endl;
for (i = 0; i <= 9; i++)
{
cin >> a[i];
}
for (i = 0; i <= 9; i++)
{
cout << a[i] << endl;
}
}
如果你想显示它们的排序,你必须首先使用sort()。
相关文章:
- 如何使用Crypto++并为RSA返回可打印的字节/字符数组
- 在一定长度后从数组中打印时缺少整数
- 数组元素打印的递归方法
- C++ 无法在字符数组中使用 for 循环打印字母模式
- 打印矢量数组中的所有元素
- 打印问题.我在数组中打印值时遇到一些问题
- 数组类 阵列的打印输出
- 动态分配的聊天数组打印缺失的数据和空
- 如何打印 2D 字符数组C++
- 如何修改代码以打印出数组 v2
- 在C++中初始化和打印 2D 数组
- 将浮点数转换为无符号字符数组并打印出来
- C++ 打印包含数据结构的数组
- 如何让字符数组在一行而不是多行中打印出来?
- 打印字符数组地址的正确方法
- 按字母顺序打印存储在字符数组中的信息
- 使用递归反转和打印出数组的内容
- 如何将文本文件读取到二维数组中并以 c++ 打印
- 如何在运行时在对象数组中动态追加新对象C++并打印它们
- 加载有空指针的字符数组的打印值