简体   繁体   English

以相反的顺序输出数组的元素

[英]Output the elements of an array in reverse order

User puts unknown amount of integers in an array (dynamic). 用户将未知数量的整数放入数组中(动态)。 Program has to output those numbers in reverse order, for instance: Integers in reverse order. 程序必须以相反的顺序输出这些数字,例如:以相反的顺序输出整数。 Exmp. 例题 324,12,5987 --> 423, 21, 7895 Exmp. 324,12,5987-> 423,21,7895示例 123, 100 --> 321, 1 (two 0's get removed) 123,100-> 321,1(删除两个0)

I'm not sure if I'm on the right path, but I sincerely hope that someone could suggest me a solution. 我不确定自己走的路是否正确,但我衷心希望有人可以为我提供解决方案。 Right now, I get the error of ''Segmentation fault'' - could it be related with array space? 现在,我收到“分段错误”的错误-它可能与数组空间有关吗?

Here's my code for the moment: 这是我目前的代码:

int main()
{

    int n,sk,rez;


    int *a;


    cout << "How many integers will there be? : " << endl;
    cin >> sk;

    a = new int[sk];


    for (int i = 0; i < sk; i++)
    {

        cout << "Write an integer: " << endl;
        cin >> n;
        a[i] = n;
    }


    for (int i = 0; i < sk; i++)
    {
        // Gets amount of the digits of every integer.

        int count, new_num = 0;
        int* skaits;

        while(a[i] > 0)
        {
        count++;
        a[i] = a[i]/10;
        }
        skaits = new int [count];
        for (int j = 0; j<count; j++)
            {
                skaits[j] = count; //  Exmp. I write 324,12 , loop calculates 3 and 2 and puts that amount in an array.

                // Loop to output integers in reverse order. Exmp. 324,12,5987 --> 423, 21, 7895

                for (int k = 0; k<skaits[j]; k++)
                {
                    new_num = new_num * 10 + (a[k] % 10);
                    a[k] = a[k]/10;


                }
                cout << new_num << endl;
            }

    }



    delete []a;
    return 0;

}

As a general rule: try to make your code more modular. 作为一般规则:尝试使代码更具模块化。 I would expect to find a function which computes the reverse of a single number. 我希望找到一个计算单个数字反向的函数。 This is easier to write and much more easier to debug, since you separate the two tasks: collect data, reverse the integer. 因为您将两个任务分开了,所以这更容易编写,也更容易调试:收集数据,反转整数。

Anyway, looking deeper in your code: it seems that after you have computed count , the content of the variable a[i] has been destroyed... ( a[i]==0 to exit the while loop). 无论如何,在您的代码中更深入地研究:似乎在计算count ,变量a[i]已被破坏...( a[i]==0退出while循环)。

A simple way to reverse a decimal number: 反转十进制数字的简单方法:

int reverse(int n) {
    int m=0;
    while (n>0) {
        m *= 10;
        m += n%10;
        n /= 10;
    }
    return m;
}

Here segmentation fault occurs as count is not initialized. 由于未初始化count因此发生segmentation fault I have found many errors in your program, for instance: 我在您的程序中发现许多错误,例如:

 int count, new_num = 0;// here count is not initialized also need count=0;
        int* skaits;

        while(a[i] > 0)
        {
        count++; // without initialize how you increase count 
        a[i] = a[i]/10;
        }// after this your number in a[i] is become 0. so what will you find

      skaits = new int [count];// no need extra skaits. you have count already

It is better you can use a function to reverse each number of a[i] . 最好使用函数来反转a[i]每个数字。

long reverse(long n) {
   static long r = 0;

   if (n == 0) 
      return 0;

   r = r * 10;
   r = r + n % 10;
   reverse(n/10);
   return r;
} 

And you can do like as follows: 您可以按如下所示进行操作:

for(int i=0;i<sk;i++)
 {
  int new_num=reverse(a[i);
   a[i]=new_num;
   cout<<a[i]<<endl;
}

Or you can just do it just like as follows: 或者,您可以如下所示进行操作:

for (int i = 0; i < sk; i++)
    {

        cout << "Write an integer: " << endl;
        cin >> n;
        a[i] = n;
    }
    for (int i = 0; i < sk; i++)
    {
        // Gets amount of the digits of every integer.
        int reverse=0;
        while (a[i] != 0)
        {
            reverse = reverse * 10;
            reverse = reverse + a[i]%10;
            a[i]= a[i]/10;
        }
        cout<<reverse<<endl;

    }

An another way to do this, first make each number int to string first and then reverse it. 执行此操作的另一种方法是,首先将每个数字int to string首先设置int to string ,然后将其reverse

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM