Answers for "c++ program for reversed array"

C++
1

c++ program to reverse an array

#include<iostream>
using namespace std;
int main()
{
   int a[]={1,2,3,4,5};  // Using Pointer and Array Relationship
    for (int i = 4; i>=0; i--)
    cout<<*(a+i);
   return 0;
}
Posted by: Guest on May-12-2021
0

printing an array backwards in c++

#include <iostream>
 
// Print contents of an array in reverse order in C++
// using array indices
int main()
{
    int arr[] = { 10, 20, 30, 40 };
    size_t n = sizeof(arr)/sizeof(arr[0]);
 
    // iterate backwards over the elements of an array
    for (int i = n - 1; i >= 0; i--) {
        std::cout << arr[i] << ' ';
    }
 
    return 0;
}
Posted by: Guest on March-09-2021

Browse Popular Code Answers by Language