【问题标题】:how to print an array backwards如何向后打印数组
【发布时间】:2022-01-29 18:40:13
【问题描述】:

用户输入一个数字,该数字被放入一个数组中,然后该数组需要反向定位

int main()
{
    int numbers[5];
    int x;

    for (int i = 0; i<5; i++)
    {
        cout << "Enter a number: ";
        cin >> x;
        numbers[x];
    }

    for (int i = 5; i>0 ; i--)
    {
        cout << numbers[i];
    }

    return 0;
}

【问题讨论】:

  • 如果你编译时有足够的警告,你应该得到类似 warning: expression result used [-Wunused-value] 的东西。运行使用 Clang 和 -fsanitize=undefined 编译的程序会出现 runtime error: index 5 out of bounds for type 'int [5]'
  • 我投票决定将此问题作为题外话结束,因为玩得开心。

标签: c++ arrays


【解决方案1】:

你很亲密。希望这会有所帮助。

#include <iostream>
using namespace std;

int main(int argc, char *argv[]) {
    int numbers[5];
    /* Get size of array */
    int size = sizeof(numbers)/sizeof(int);
    int val;

    for(int i = 0; i < size; i++) {
        cout << "Enter a number: ";
        cin >> val;
        numbers[i] = val;
    }

    /* Start index at spot 4 and decrement until k hits 0 */
    for(int k = size-1; k >= 0; k--) {
        cout << numbers[k] << " ";
    }
    cout << endl;

    return 0;
}

【讨论】:

  • @ChrisD 我在一些练习挑战中非常接近。这是我与第二个 for 循环相似的部分,k >= 0。我失去了我的最后一个索引位置,没有大于或等于
【解决方案2】:

你非常接近你的结果但是你犯了一些错误,下面的代码是你编写的代码的正确解决方案。

int main()
{
    int numbers[5];
    int x;

    for (int i = 0; i<5; i++)
    {
        cout << "Enter a number: ";
        cin >> numbers[i];
    }

    for (int i = 4; i>=0; i--)
    {
        cout << numbers[i];
    }

    return 0;
}

【讨论】:

    【解决方案3】:
    #include<iostream>
    
    using namespace std;
    int main()
    {
        //get size of the array
        int arr[1000], n;
        cin >> n;
        //receive the elements of the array
        for (int i = 0; i < n; i++)
        {
            cin >> arr[i];
        }
        //swap the elements of indexes
        //the condition is just at "i*2" be cause if we exceed these value we will start to return the elements to its original places 
        for (int  i = 0; i*2< n; i++)
        {
            //variable x as a holder for the value of the index 
            int x = arr[i];
            //index arr[n-1-i]: "-1" as the first index start with 0,"-i" to adjust the suitable index which have the value to be swaped
            arr[i] = arr[n - 1 - i];
            arr[n - 1 - i] = x;
        }
        //loop for printing the new elements
        for(int i=0;i<n;i++)
        {
            cout<<arr[i];
        }
        return 0;
    }
    

    【讨论】:

    • 虽然此代码可能会回答问题,但提供有关 如何 和/或 为什么 解决问题的附加上下文将改善答案的长期性价值。
    • 感谢您的建议,这是我对这里问题的第一个回答。所以我不知道如何完美回答:)。
    【解决方案4】:
    #include <iostream>
    
    using namespace std;
    
    int main() {
    
    //print numbers in an array in reverse order
    int myarray[1000];
    cout << "enter size: " << endl;
    int size;
    cin >> size;
    cout << "Enter numbers: " << endl;
    for (int i = 0; i<size; i++)
    {
        cin >> myarray[i];
    }
    
    for (int i = size - 1; i >=0; i--)
    {
        cout << myarray[i];
    }
    
    return 0;
    
    }
    

    当然你可以删除 cout 语句并根据自己的喜好修改

    【讨论】:

      【解决方案5】:

      这个比较简单

      #include<iostream>
      
      using namespace std;
      
      int main ()
      
      {
      
      int a[10], x, i;
      
      cout << "enter the size of array" << endl;
      
      cin >> x;
        cout << "enter the element of array" << endl;
      
      for (i = 0; i < x; i++)
          {
      
      cin >> a[i];
      
      }
      
      cout << "reverse of array" << endl;
      
      for (i = x - 1; i >= 0; i--)
      
      cout << a[i] << endl;
      
      }
      

      【讨论】:

        【解决方案6】:

        用 C++ 回答。只使用一个数组。

        #include<iostream>
        
        using namespace std ;
        
        
        
        
        int main()
        {
        
                int array[1000] , count ;
        
                cin >> count ;
        
                for(int i = 0 ; i<count ; i++)
                    {
        
                        cin >> array[i] ;
                    }
        
                for(int j = count-1 ; j>=0 ; j--)
                    {
                        cout << array[j] << endl;
                    }
                    
            return 0 ;
        }
        

        【讨论】:

          【解决方案7】:
          #include <iostream>
          using namespace std;
          
          int main ()
          {
              int array[10000];
              int N;
          
              cout<< " Enter total numbers ";
              cin>>N;
          
              cout << "Enter  numbers:"<<endl;
          
              for (int i = 0; i <N; ++i)                                           
              {
                  cin>>array[i];
              }
              for ( i = N-1; i>=0;i--)
              {                                                             
                  cout<<array[i]<<endl;
              }
              return 0;
          }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2015-09-13
            • 2018-11-27
            • 2016-01-23
            • 1970-01-01
            • 1970-01-01
            • 2017-08-26
            • 2018-07-06
            相关资源
            最近更新 更多