i want to take all the different inputs and print their corresponding outputs after the line with the last input…do i have to use an array …i don’t get this test cases concept can somebody help me please???
I’ll start my answer little widely…
Why there are such test cases when there are multiple tests in one file?
I do not know for sure, but my opinion is, that there is sometimes small difference between expected and unwanted solution by problem setter. For example setter wants coders to find n*log(n) solution, but n^2 solution is unwanted. If the n is small, let say 10.000, n^2 can pass (with some optimization) the 1s time limit. In such case the “multiple test cases in one file” approach is very handy, because when there are 100 test cases in one file n^2 solution will never pass timelimit.
How to solve such problems?
I saw a lot of solutions where the coders read the input, store the values to array and then solved all values to another array and finally printed the values.
scanf( "%d", &n ); // number of test cases
int in[n];
int out[n];
for ( int i = 0; i < n; ++i ) scanf( "%d", &in[i] );
for ( int i = 0; i < n; ++i ) out[i] = solve( in[i] );
for ( int i = 0; i < n; ++i ) printf( "%d\n", out[i] );
Probably everyone can see, that these 3 for loops can be easily merged to one
for ( int i = 0; i < n; ++i ) {
scanf( "%d", &in[i] );
out[i] = solve( in[i] );
printf( "%d\n", out[i] );
}
But why to store value, that is processed (read from input never used again and also printed and never used again too) already? When I overdraw it, I can throw away arrays and use
int x;
for ( int i = 0; i < n; ++i ) scanf( "%d", &x ), printf( "%d\n", solve( x ) );
You can try this with following “problem”:
read the number of test cases from the input (t) and for each following nonnegative integer x (x < 1.000.000.000) print the double value (2*x)
Testing the solution
Probably the motivation for storing the input and output is this: when you manually test such program, it prints the output while you enter another input, that can be confusing for newcomers… That’s because input and output streams are separate streams, so your program can read from one stream and write to other one.
The best testing approach I can recommend is to use input and output files as it is described in FAQ, in section How should I test my program.
Hope that helps
but i want to code in the format:
test cases
all inputs
all outputs
thanks for this
It’ll save a lot of space for me now
@betlista
The solution you’ve mentioned takes in value of each test case evaluates it, produces a result and then takes next set of values as input & so on. In case the problem demands we take all test case values as input first and then display result of all of them together, here storing values becomes necessary. Could you suggest any other low memory requirement approach ?
I am a beginner and I don’t get the concept of test cases. Can somebody explain to me little bit in detail whether how should I include number of test cases in my program, and how do I get my program to print the output after the number of testcases is reached?