- Prepare
- C++
- Introduction
- Arrays Introduction
- Discussions
Arrays Introduction
Arrays Introduction
+ 1 comment include
include
include
include
include
using namespace std; void reversearrays(int arr[], int n) { int s = 0; int e = n - 1; while (s <= e) { swap(arr[s], arr[e]); s++; e--; }
} void printarray(int arr[], int n) { for (int i = 0; i < n; i++) { cout << arr[i] << ' '; } }
int main() { int n; cin >> n; int* arr = new int[n]; for (int i = 0; i < n; i++) { cin >> arr[i]; }
reversearrays(arr, n); cout << endl; cout << "Array elements after reversing " << endl; printarray(arr, n); return 0;
}
+ 0 comments //Dynamic allocation of an array
int *arrPtr{}, userInput{}; cin >> userInput; arrPtr = new int[userInput]; //allocate array at runtime for(size_t i{1}; i<=userInput; i++) { cin >> arrPtr[i-1]; //store elements starting at index 0 } for(size_t j{1}; j<=userInput; j++) { cout << arrPtr[userInput-j] << " "; } delete [] arrPtr; //free allocated storage (good practice)
//Pre-defined array with a fixed size (based on constraints)
int userInput{}, arrFixed[1000]{}; cin >> userInput; for(size_t i{1}; i<=userInput; i++) { cin >> arrFixed[i-1]; } for(size_t j{1}; j<=userInput; j++) { cout << arrFixed[userInput-j] << " "; }
+ 0 comments C++ 20
include
using namespace std;
int main() { int N, arr[1000];
cin >> N; for (int i = 0; i < N; i++) { cin >> arr[i]; } for (int i = N-1; i >= 0; i--) { cout << arr[i] << " "; } return 0;
}
+ 0 comments int main() { /* Enter your code here. Read input from STDIN. Print output to STDOUT */ int n; cin>>n; int arr[n]; for (int i =0; i>arr[i]; } for(int i = n-1; i >= 0; i--){ cout<
return 0;
}
+ 0 comments C++20:
#include <iostream> using namespace std; int main() { int N, arr[1000]; cin >> N; for (int i = 0; i < N; i++) { cin >> arr[i]; } for (int i = 0; i < N / 2; i++) { int temp = arr[i]; arr[i] = arr[N - 1 - i]; arr[N - 1 - i] = temp; } for (int i = 0; i < N; i++) { cout << arr[i] << " "; } return 0; }
Sort 1216 Discussions, By:
Please Login in order to post a comment