I dont have any idea why this code isnt letting me input. Sorry for being totally naive as a beginner but pls help.

Jul 10, 2022 at 7:51am
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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
  #include <iostream>

using namespace std;

int main() {
	int t,n,i,A[n];
	cin>>t;
	while(t>0)
	{
		cin>>n;
		for(i=0;i<n;i++)
		{
			cin>>A[i];
		}
                t--;
	}
	return 0;
}
Last edited on Jul 10, 2022 at 7:55am
Jul 10, 2022 at 7:56am
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.
Last edited on Jul 10, 2022 at 8:14am
Jul 10, 2022 at 8:44am
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...
Jul 10, 2022 at 8:56am
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. :)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
using namespace std;

int main() {
	int t,i,temp=0;
	cin>>t;
	while(t--)
	{
	    int n;
	    cin>>n;
	    int A[n];
	    for(i=0;i<n;i++)
	   {
		cin>>A[I];
	   }
	    for(i=0;i<n/2;i++)
	    {
		    temp = A[i];
		    A[i] = A[n-1-i];
		    A[n-1-i]=temp;
	    }
	    for(i=0;i<n;i++)
		    cout<<A[I]<<" ";
        }
	return 0;
}


Last edited on Jul 10, 2022 at 9:02am
Jul 10, 2022 at 9:04am
For info, C++ has an in-built swap function to replace L18-20. std::swap()

https://cplusplus.com/reference/utility/swap/

Also, C++ has an inbuilt sort function std::sort()

https://cplusplus.com/reference/algorithm/sort/
Jul 10, 2022 at 9:08am
To reverse order:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#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';
	}
}


To sort by reverse order:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#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';
	}
}

Last edited on Jul 10, 2022 at 10:22am
Jul 10, 2022 at 9:17am
Being a newbie is totally fine, but I would recommend to learn the basics of C++ first before trying to solve this challenge on codechef(?).

A good way to start is: https://www.learncpp.com
Topic archived. No new replies allowed.