[英]I wanted to reverse my array. Why this code gives garbage value?
#include <iostream>
using namespace std;
int main(){
int n;
int a[n];
cin>>n;
for(int i=0;i<n;i++){
cin>>a[i];
}
for(int i=n;i>=0;i--){
cout<<a[i]<<" ";
}
}
Input:- 4 1 2 3 4 Output 4199008 4 3 2 1输入:- 4 1 2 3 4 Output 4199008 4 3 2 1
For starters the program has undefined behavior because the variable n
is not initialized对于初学者,程序具有未定义的行为,因为变量
n
未初始化
int n;
So this declaration所以这个声明
int a[n];
is invalid.是无效的。 Moreover variable length arrays is not a standard C++ feature.
此外,可变长度 arrays 不是标准 C++ 功能。 Instead use the standard class template
std::vector
.而是使用标准 class 模板
std::vector
。
Also within this loop也在这个循环中
for(int i=n;i>=0;i--) {
cout<<a[i]<<" ";
}
you are trying to access of non-existent element with the index n
.您正在尝试使用索引
n
访问不存在的元素。
Also you are not reversing an array.此外,您没有反转数组。 You are trying to output an array in the reverse order.
您正在尝试以相反的顺序 output 数组。
Pay attention to that there are standard algorithms std::reverse
and std::reverse_copy
declared in the header <algorithm>
.请注意,在 header
<algorithm>
中声明了标准算法std::reverse
和std::reverse_copy
。
Here is an example how your program with using your approach could look这是一个使用您的方法的程序看起来如何的示例
#include <iostream>
#include <vector>
int main()
{
size_t n = 0;
std::cout << "Enter the size of an array ";
std::cin >> n;
std::vector<int> v( n );
std::cout << "Enter " << n << " elements: ";
for ( auto &item : v ) std::cin >> item;
std::cout << "The array in the reverse order\n";
for ( size_t i = v.size(); i != 0; )
{
std::cout << v[--i] << ' ';
}
std::cout << '\n';
return 0;
}
The program output might look like程序 output 可能看起来像
Enter the size of an array 10
Enter 10 elements: 0 1 2 3 4 5 6 7 8 9
The array in the reverse order
9 8 7 6 5 4 3 2 1 0
If to use standard algorithms then your program can look the following way如果要使用标准算法,那么您的程序可以如下所示
#include <iostream>
#include <vector>
#include <iterator>
#include <algorithm>
int main()
{
size_t n = 0;
std::cout << "Enter the size of an array ";
std::cin >> n;
std::vector<int> v( n );
std::cout << "Enter " << n << " elements: ";
std::copy_n( std::istream_iterator<int>( std::cin ), n, std::begin( v ) );
std::cout << "The array in the reverse order\n";
std::reverse_copy( std::begin( v ), std::end( v ),
std::ostream_iterator<int>( std::cout, " ") );
std::cout << '\n';
return 0;
}
The program output might look the same way as shown above程序 output 可能看起来与上图相同
Enter the size of an array 10
Enter 10 elements: 0 1 2 3 4 5 6 7 8 9
The array in the reverse order
9 8 7 6 5 4 3 2 1 0
a[n]
will return the element after the last one. a[n]
将返回最后一个元素之后的元素。 When you iterate in reverse order, start with i=n-1
.当您以相反的顺序迭代时,从
i=n-1
开始。
At the beginig of your program there is a mistake:在您的程序开始时有一个错误:
int n; // You declare n with no value
int a[n]; // You use is
cin>>n; // After you used it you get your value-
Now i can suppose that that was just an error while copying it because you give inputs and outputs现在我可以假设这只是复制时的错误,因为您提供了输入和输出
Input:- 4 1 2 3 4 Output 4199008 4 3 2 1
输入:- 4 1 2 3 4 Output 4199008 4 3 2 1
So forgeting about that, you declare an array of size n.所以忘记这一点,你声明一个大小为 n 的数组。 Remember that the elemnts of the array will go from 0 to n-1.
请记住,数组的元素将 go 从 0 到 n-1。 Now look at your second for loop
现在看看你的第二个 for 循环
// the first element you acces is n and you stop at 1
// but the array goes from n-1 to 0
for(int i=n;i>=0;i--){
cout<<a[i]<<" ";
}
So you still get n values as an output but the first element that you access is outside of the array.所以你仍然得到 n 个值作为 output 但你访问的第一个元素在数组之外。 Thats why you get a garbage value, that is a value that was left there.
这就是为什么你会得到一个垃圾值,那是一个留在那儿的值。
A solution will be to change the for loop一个解决方案是更改 for 循环
for(int i=n-1;i>=-1;i--){
cout<<a[i]<<" ";
}
While reversing the array start the loop from n-1 that is i=n-1 (n is the no of elements in array).在反转数组时,从 n-1 开始循环,即 i=n-1(n 是数组中的元素数)。 And run the loop till i>=0.
并运行循环直到 i>=0。 If you will start loop from n it will read illegal index which is out of range and will give you garbage value.
如果您从 n 开始循环,它将读取超出范围的非法索引,并为您提供垃圾值。
for(int i=n-1; i>=0; i++){
cout<<arr[i]<<" ";}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.