An array is a series of elements of the same type placed in
contiguous memory locations that can be individually referenced by
adding an index to a unique identifier.
Declaration:
Declaration:
int arr[10]; //Declares an array named arr of size 10, i.e; you can store 10 integers.
Accessing elements of an array:Indexing in arrays starts from 0.So the first element is stored at arr[0],the second element at arr[1]...arr[9]
Ex: Accept array elements and display in reverse order
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
int N;
cin>>N;
int *arr =new int[N];
for(int i=0;i<N;i++)
{
cin>>arr[i];
}
for(int i=N-1;i>=0;i--)
{
cout<<arr[i]<<" ";
}
return 0;
}
Input (stdin)
4
1 4 3 2
Your Output (stdout)
2 3 4 1
No comments:
Post a Comment