1

Here is the input:

First line of input file contains a single integer T which denotes the number of test cases. For each test case, there will be two lines. First line contains N which denotes number of elements in an array, and second line contains N space seperated integers.

I know how to denote the number of test cases but not how to denote numbers of elements in an array. I'm pretty new to c++ , so it would be nice if you answer noob friendly.

Kedog
  • 29
  • 3

2 Answers2

1

It should be something like this :

cin>>test;                                // Taking the number of test cases 
    while(test--){                        // For each test case
        cin>>n;                           // Taking input n
        vector<int> a(n);
        for(i=0;i<n;i++){
            cin>>a[i];                   // Taking input as n integers
        }
    }

OR

cin>>test;                                     // Taking the number of test cases  
    for(int i=0;i<test;i++){                   // For each test case
        cin>>n;                                // Taking input n
        vector<int> v;                         // declaring a vector
        for(int j=0;j<n;j++){
            cin>>x;
            v.push_back(x);                    // Taking input as n integers one by one
        }
    }

KL_KISNE_DEKHA_HAI
  • 649
  • 11
  • 26
  • I did not get what you are trying to state.. The question asked is common way to input in many competitive coding sites, so i answered accordingly, also c++11 , c++14 ,c++17 all these compilers accept this format. I myself have done it. Also i have stated a vector method using push_back as another way. – KL_KISNE_DEKHA_HAI Jun 09 '20 at 18:29
  • ok , so basically in this https://www.onlinegdb.com/online_c++_compiler ,are we using an extension? Also many of the red coders in codeforces too used the way i stated , so i did not doubt it. Anyways i have updated the answer. Please have a look. – KL_KISNE_DEKHA_HAI Jun 09 '20 at 18:39
  • Yes it's using an extension - and I couldn't find a way to turn the extension off. With the online compiler I linked to you can, so if you'd like to get more help from the compiler to stay within the standard I suggest that you try that one out. – Ted Lyngmo Jun 09 '20 at 18:44
  • 1
    Is it fine now? please see. – KL_KISNE_DEKHA_HAI Jun 09 '20 at 18:47
  • Yes. You got my vote and I removed my old comments regarding the VLA. – Ted Lyngmo Jun 09 '20 at 18:50
  • 1
    Thank you @Ted Lyngmo , i will let my comments remain as it may help others who may be in the same misconception . :) – KL_KISNE_DEKHA_HAI Jun 09 '20 at 18:51
-1

To denote the number of elements in an array, you just do

int arr[N];

Where N represents the number of elements in an array. If you're using a std::vector instead, you would initialize it like this:

vector <int> vec(N);

Then, you can use a for loop to input values into the arrays.

Telescope
  • 2,068
  • 1
  • 5
  • 22
  • 1
    "_First line of input file contains a single integer T which denotes the number of test cases_" - That means that `int arr[N];` (or `T`) won't work unless the compiler has a non-standard extension for VLA:s. The standard only permits `int arr[N];` if `N` is `constexpr`. – Ted Lyngmo Jun 09 '20 at 17:46