You are not logged in. Please login at to post your questions!


how do i input test cases??

i want to take all the different inputs and print their corresponding outputs after the line with the last i have to use an array .....i don't get this test cases concept can somebody help me please???

asked 04 Jul '12, 18:51

khitish's gravatar image

accept rate: 0%

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 < 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 ;-)


answered 04 Jul '12, 19:38

betlista's gravatar image

3★betlista ♦♦
accept rate: 11%


but i want to code in the format: test cases all inputs all outputs

(22 Jul '12, 08:08) khitish3★

thanks for this It'll save a lot of space for me now :)


answered 22 Jul '12, 12:01

bladeshark's gravatar image

accept rate: 0%

@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 ?


answered 20 Sep '17, 21:47

pronomita's gravatar image

accept rate: 0%

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?


answered 06 Oct '17, 14:57

parthice03's gravatar image

accept rate: 0%

toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here



Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text]( "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported
  • mathemetical formulas in Latex between $ symbol

Question tags:


question asked: 04 Jul '12, 18:51

question was seen: 12,350 times

last updated: 06 Oct '17, 14:57