简体   繁体   English

我想反转我的数组。 为什么这段代码给出垃圾值?

[英]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::reversestd::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.

相关问题 为什么我对数组代码的反向操作不起作用 - Why is my reverse of an array code not working as it should 为什么我的数组在初始化时被一个垃圾值填充? - Why is my array being filled with a garbage value when I have initialized it? 程序为name [i]给出垃圾值,i = 1到3 - The Program gives garbage value for name[i], i= 1 to 3 该问题要求打印数组中所有整数的总和。 为什么我不能运行以下代码? - The question asks to print the sum of all integers in an array. Why cant I run the following code? 为什么我的输出给出了一些垃圾值,即使我输入了它们 - Why my output gives some garbage values, even though I input them 将char数组复制到另一个数组中会产生垃圾值 - copying char array into another array gives garbage value 创建传递值的数组大小会产生垃圾值 - Creating array size of passed value gives garbage value 每次我将我的(播放旋律)代码上传到 arduino 并按下按钮时,它都会跳过数组中的相同音符。 怎么来的? - Every time I upload my (play melody-)code to the arduino and I press the button, it skips the same notes in the array. How come? 我写了一段代码来查找数组元素的反向,但是它没有给出所需的输出 - I have written a code to find reverse of array elements but it doesnot gives required output 为什么我的代码用于在阶乘返回垃圾值中查找尾随零的数量 - Why is my code for finding number of trailing zeros in a factorial returning garbage value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM