簡體   English   中英

以相反的順序輸出數組的元素

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

用戶將未知數量的整數放入數組中(動態)。 程序必須以相反的順序輸出這些數字,例如:以相反的順序輸出整數。 例題 324,12,5987-> 423,21,7895示例 123,100-> 321,1(刪除兩個0)

我不確定自己走的路是否正確,但我衷心希望有人可以為我提供解決方案。 現在,我收到“分段錯誤”的錯誤-它可能與數組空間有關嗎?

這是我目前的代碼:

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;

}

作為一般規則:嘗試使代碼更具模塊化。 我希望找到一個計算單個數字反向的函數。 因為您將兩個任務分開了,所以這更容易編寫,也更容易調試:收集數據,反轉整數。

無論如何,在您的代碼中更深入地研究:似乎在計算count ,變量a[i]已被破壞...( a[i]==0退出while循環)。

反轉十進制數字的簡單方法:

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

由於未初始化count因此發生segmentation fault 我在您的程序中發現許多錯誤,例如:

 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

最好使用函數來反轉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;
} 

您可以按如下所示進行操作:

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

或者,您可以如下所示進行操作:

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;

    }

執行此操作的另一種方法是,首先將每個數字int to string首先設置int to string ,然后將其reverse

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM