how to print an array backwards

后端 未结 6 1437
太阳男子
太阳男子 2021-01-17 09:20

The user enteres a number which is put in an array and then the array needs to be orinted backwadrds

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

    for (i         


        
相关标签:
6条回答
  • 2021-01-17 09:35

    this one is more simple

    #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;
    
    }
    
    0 讨论(0)
  • 2021-01-17 09:35
    #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;
    }
    
    0 讨论(0)
  • 2021-01-17 09:36
    #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;
    
    }
    

    of course you can just delete the cout statements and modify to your liking

    0 讨论(0)
  • 2021-01-17 09:43
    #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;
    }
    
    0 讨论(0)
  • 2021-01-17 09:44

    You're very close. Hope this helps.

    #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;
    }
    
    0 讨论(0)
  • 2021-01-17 09:56

    You are very close to your result but you did little mistakes, the following code is the correct solution of the code you have written.

    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;
    }
    
    0 讨论(0)
提交回复
热议问题