I have just made a simple code to taken in some inputs (as testcases and values), but this code is just running and completing execution on both codeblocks and the onlinegdb compiler. I am not able to understand why it isn't letting me input anything despite having cin included.
The program has undefined behaviour (UB) because you use n (to specify the size of A) before being initialized.
Note that using a non-constant value to specify the size of a local array is not a standard feature, but some compilers allow it anyway so this shouldn't cause any problems as long as the compiler doesn't complain.
What are you trying to achieve? Assuming that your compiler allows a non-const value to be used as the size of a local array, A is defined at L6 but n isn't obtained until L10. Remove A[n] from L6 and after L10 insert int A[n];
But even when this works as coded, it really does nothing apart from obtaining sets of values for A t times. Nothing is actually done with the entered values...
Thanks Peter87 I got it. And @seeplus, I was actually trying to use it to sort a no of arrays (based on their testcases) in reverse order. I was able to rectify the mistake thanks to both of you. :)
#include <iostream>
#include <utility>
int main() {
int A[1000] {};
int t {};
std::cin >> t;
while (t--) {
int n {};
std::cin >> n;
//int A[n];
for (int i = 0; i < n; i++)
std::cin >> A[i];
for (int i = 0; i < n / 2; i++)
std::swap(A[i], A[n - 1 - i]);
for (int i = 0; i < n; i++)
std::cout << A[i] << " ";
std::cout << '\n';
}
}
#include <iostream>
#include <algorithm>
#include <functional>
int main() {
int A[1000] {};
int t {};
std::cin >> t;
while (t--) {
int n {};
std::cin >> n;
//int A[n];
for (int i = 0; i < n; i++)
std::cin >> A[i];
std::sort(A, A + n, std::greater<int>());
for (int i = 0; i < n; i++)
std::cout << A[i] << " ";
std::cout << '\n';
}
}