Tutorial for Input,Output |
This post will try and introduce newcomers to the first and most basic thing they need to learn before submitting programs -- How to properly accept Input and print Output (I/O). I'll start off with a few guidelines and then conclude with an example from the CodeChef site.
For all the questions on our site, the input will be given to you from standard input (stdin for C users) and the result is expected on standard output (stdout for C users). This is the same as writing a program that accepts input from the keyboard and displays the results on the screen. I'm sure most of you have been programming that way already. However, a major difference is that you don't need to prompt the user for input, so stuff like:
"Please enter your name: "
are a big NO NO! You need to assume that the user entering the data at the other end knows what to enter and when to enter it. Similarly, while displaying the results, you need to display it in the exact format as is specified in the problem statement.
For figuring out the format in which data will be entered (given to you) and how you should display the results, you need to refer to the "Input" and “Output” section of the problem statement respectively. Please make sure that you are following the output specification to the decimal point. If you have one character out of place, your submission will be marked as incorrect
After reading the problem statement, you will be given a sample "Input” and the resulting expected “Output” your program should submit. Typically, you will be asked to run your program against multiple test cases. The number of test cases and the format of each test case will be clearly mentioned in the problem statement's "Input" section. More often than not, you will be required to display the result for each test case, so you will have as many results displayed as there are test cases.
Why so many test cases?
You must have studied about the different types of tests that you can run your program against in school. Some of them are:
- Boundary value testing
- Negative testing
- Stress testing
- and so on....
Now, each of these have a specific purpose, so the problem setter has kept many different types of tests to test the correctness and performance of your program for different types of inputs.
Let us move on to a specific example to see what is happening.
The problem we shall be tacking here is: Life, the Universe, and Everything
The problem statement:
Your program is to use the brute-force approach in order to find the Answer to Life, the Universe, and Everything. More precisely... rewrite small numbers from input to output. Stop processing input after reading in the number 42. All numbers at input are integers of one or two digits.
Input:
A list of integers separated by a new line.
Output:
All the integers before the integer '42'.
Sample Input:
1
2
88
42
99
Sample Output:
1
2
88
Here, the constraint on the input we have is that any input number n will be such that (0 <= n < 100).
The problem statement is pretty straightforward, but for this toy program, the problem setter may construct test cases where:
- The 1st number is 42
- The last number if 42
- There are 10000000 numbers before 42
The first 2 tests are boundary value tests, and the 3rd one is a performance test. Generally these problems have a time limit associated with them. You are required to solve the problem with the given constraints in the given time limit. This time limit is chosen such that a good solution in any language would be accepted.If you are stuck, the answers to this problem, in over 25 programming languages can be found at Sample Solutions
You can now go ahead and try out the problems in the easy set.
Comments


can the input have any no.of
can the input have any no.of numbers
is same input can be reapted
is same input can be reapted
#include<stdio.h> int
#include<stdio.h>
int main()
{
while (1)
{
int i;
scanf("%d", &i);
if i == 42 break;
else
printf("%d/n",i);
}
}
oops.. didnt intent to post
oops.. didnt intent to post that. sorry :)
#include<stdio.h> int
#include<stdio.h>
int main()
{
int i;
while (1)
{
scanf("%d", &i);
if (i!= 42)
{
printf("%d/n",i);
}
}
#include<stdio.h>int main(){
#include<stdio.h>
int main()
{
int num;
while(scanf("%d",&num)>0 && num!=42)
printf("%dn",num);
return 0;
}
My code is not uploading ..
My code is not uploading .. Run time eroor is coming .. its working very fine GCC compiler..
When stops the input?
When stops the input?
IF 2 TIMES 42 REPEAT THEN
IF 2 TIMES 42 REPEAT THEN WHATWE DO?
lol why choose an easy
lol why choose an easy problem like life & the universe? How do you terminate input on TurboSort (Easy) ?
I'm entering my data into vectors but I don't know how to terminate it when the input is done. For example, I know max input is 1000000 integers, what do I do if the user wants to stop before that?
That problem tells you
That problem tells you exactly how many numbers you should read before stopping, on the first line.
hi please tell me what is the
hi please tell me
what is the problem in
#include <stdio.h>
int main()
{
int i;
do{
scanf("%d",&i);
if(i<42)
{
printf("%dn",i);
}
}while(i<42);
}
Describe what you are doing
Describe what you are doing in words. Then read the problem. Your error is obvious.
#include<stdio.h> int
#include<stdio.h>
int main()
{
int i;
scanf("%d",&i);
if(i!=42)
{
printf("%d",i);
}
}
#include<stdio.h> int
#include<stdio.h>
int main()
{
int i;
scanf("%d",&i);
do
{
printf("%d",&i);
}while(i!=42)
}
FAQ
FAQ
@Gaurav it straight forward
@Gaurav
it straight forward that the number read should not be 42. we dont have to compare it with lesser than or greater than 42. This is what u r doing.
Also the horrible thing i found in your code is checking number in while() as well as inside while. Why cant you just check once, then continue loop and print it every time u are able to read coz of condn met.
like this
cin>>n;
while(n!=42)
{
cout<<"n"<<n;
cin>>n;
}
Its pretty simple. Isn't it????
continued from prev
continued from prev post...
logic is ok but
cin and cout wouldnt work. try stream reading using fread(), fgets()
#include<stdio.h> void
#include<stdio.h>
void main()
{
int i;
scanf("%d",&i);
for(i=1;i<42;i++)
printf("%d",i);
getch();
}
why we can not use conio.h
why we can not use conio.h
Because it is not a standard
Because it is not a standard header file.
import
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Life_the_Universe_and_Everything {
public static void main(String str []) throws NumberFormatException, IOException
{ System.out.println("Enter no of digit u wann enter ");
BufferedReader ob1= new BufferedReader(new InputStreamReader(System.in));
int user_choice= Integer.parseInt(ob1.readLine());
// Enter number in array
int num_hold[]=new int[user_choice];
System.out.println("Entre "+user_choice+" numbers ");
for(int i=0;i<num_hold.length;i++)
{
BufferedReader ob2= new BufferedReader(new InputStreamReader(System.in));
int num= Integer.parseInt(ob2.readLine());
num_hold[i]=num;
}
System.out.println("------------------------");
for(int m=0;m<num_hold.length;m++)
{
int temp=num_hold[m];
if(temp<=88)
{ System.out.println(temp);
}
else{
break;
}
}
}
}
whats wrong with this?
I am new here, can you please
I am new here, can you please tell where I am going wrong :-
void main()
{
int a;
while (1)
{
cin>>a;
if (a==42)
{
break;
}
cout<<"n"<<a<<"n";
}
}
" The problem statement: Your
"
The problem statement:
Your program is to use the brute-force approach in order to find the Answer to Life, the Universe, and Everything. More precisely... rewrite small numbers from input to output. Stop processing input after reading in the number 42. All numbers at input are integers of one or two digits."
i think the given example is wrong
No it isn't.
No it isn't.
please buddy tell me how this
please buddy tell me how this is working... how the input is being given.... i jus cant understand plz tell me....
FAQ
FAQ
Hi can someone tell how to
Hi can someone tell how to take input with Python?
ALSO TELL ME TOOO THAT HOW TO
ALSO TELL ME TOOO THAT HOW TO TAKE INPUT IN PYTHON AND PHP ???????????
dont we need an array for
dont we need an array for storng?
Hello, I am new in this
Hello, I am new in this website.Where is the problem at this code.I compiled it and run it in my Dev C++ 4.9.9.2.It worked but Codechef is showing it is wrong.I am getting upset to see that...You know this is a simple problem...:(
//Solution of Life, the Universe, and Everything
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
using namespace std;
int main()
{
system("cls");
int n,*a=NULL,i=0;
cout<<"How many inputs do you have?"<<endl;
cin>>n;
a=new int[n];
cout<<"Input:"<<endl;
do
{
cin>>a[i];
i++;
}while(i<n);
cout<<"Output:"<<endl;
i=0;
while(i<n && a[i]!=42)
{
cout<<a[i]<<endl;
i++;
}
delete []a;
a=NULL;
system("pause");
return 0;
}
See the FAQ.
See the FAQ.
I want to know that.. how is
I want to know that.. how is it supposed to work? like.. first user enter some inputs serially and then the outputs are shown ...if that is so and we cannot ask the user "enter input"..then how do I know when to stop taking input and show output??p
or is it like...enter one input, process it if it is not 42.. ?
which one ??
I agree with Israt.I am also
I agree with Israt.I am also confused how do the we make the compiler stop taking input and start to process????!!!!
There is also a problem that
There is also a problem that I saw successful submissions:
How could it be a successful submissions.It input output shouldn't match with the I/O given there...this simple program makes me crazy and stop me to go ahead...:(((
That program is perfectly
That program is perfectly correct. Why do you think it gives the wrong output?
plz help..... my codes
plz help.....
my codes are:
#include<stdio.h>
int main()
{
int a[5],i;
for (i=0;i<5;i++)
{
scanf("%d",&a[i]);
if (a[i]!=42)
{
printf("%dn",a[i]);
}
else
{
break;
}
}
return 0;
}
i am getting correct output.
but here when i submit it shows wrong answer.
why..??
Your code will give the wrong
Your code will give the wrong answer if there are more than 5 numbers in the input.
#include<stdio.h>int main(){
#include<stdio.h>
int main()
{
int c,a[10000],i;
for(i=0;i<10000;i++)
{
scanf("%d",&c);
if(c>=0 && c<=99)
a[i]=c;
else
i--;
}
i=0;
while(a[i]!=42&&i<20000)
{
printf("%dn",a[i]);
i++;
}
return 0;
}
Is this code optimal?
#include int main() { int
Why did you put if (x>0) in
This simple puzzle is
There is nothing 'ill
There is nothing 'ill organised' about the problem at all.
All of the sample codes I have read allow for 99 as an input; why do you think they don't?
The problem statement doesn't say you can't enter 42 twice, so of course you aren't meant to assume you can't.
The sample input shows numbers appearing after 42 so it is clear that the input doesn't have to stop there.
As for 'mixing' input and output, I don't think you understand how output works. The input does not appear in the output stream whatsoever, and storing all numbers in a collection is a bad idea. Read the FAQ which is very clear about that.
i hav a code which runs
i hav a code which runs otherwise but give a run-time error after submission
the code is in python
def f():
x=raw_input("Enter a number")
l1=[]
l1.append(x)
while(x!=""):
x1=int(x)
l1.append(x1)
x=raw_input("Enter a number")
l=len(l1)
i=1
while(i<l):
if l1[i]!=42:
print l1[i]
else:
break
i=i+1
f()
I m New 2 codechef JAVA error
I m New 2 codechef
JAVA error ===
Can any1 specify more on NZEC runtime error . i read the details given on the site about this error .
So, i placed the code ::::
System.exit(0);
and i also did handled the error with try , catch but the code still gives the error while uploading .
this code is compiling fine on my end .
i am really stuck with this error with past 3 days .!!!
I'm also using HashSet , TreeSet stuctures . Do i need to replace these data structures with ARRAY ...??
Help needed.,....
Is there any limit on the no.
Is there any limit on the no. of inputs???
time limit exceeded error for
time limit exceeded error for following code......plz help me reduce time consumption
#include<stdio.h>
int main(void)
{
unsigned int num;
for(;printf("%d",scanf("%d",&num)!=42););
return(0);
}
I CAN NOT UNDERSTAND WHAT
I CAN NOT UNDERSTAND WHAT SHOULD I DO OR NOT
#include<iostream> using
#include<iostream>
using namespace std;
int main( ) {
int a[5] ;
for( int i = 0; i <= 4; i++ ) {
cin>> a[i];
}
for( int j = 0; j <= 4; j++ ) {
cout<<endl;
if( a[j] == 4 )
break;
cout<< a[j]<<endl;
}
return 0;
}
Are you sure you are trying
Are you sure you are trying to solve the right problem? Your code doesn't seem to do anything that you are asked to do. For example, you are meant to keep reading until you read a 42, but you stop after 5 numbers, or stop after you read the number 4..
@Mohit U've compared the no
@Mohit U've compared the no wit 4 instead of 42!
WHAT IS MY PLOBLEM TELL ME
WHAT IS MY PLOBLEM TELL ME PLESE
#include<stdio.h>
void main()
{
int i=0,a[100],j,d=0;
printf("n enter number(negetive to end.): ");
while(d>=0)
{ scanf("%d",&a[i]);
i++;
d=a[i];
}
printf("output: ");
for(j=0;j<i;j++)
{ if(a[j]==42)
break;
else
printf("%d ",a[j]);
}
}
#include <stdio.h>#include
#include <stdio.h>
#include <string.h>
int main()
{
int num[99];
int i,j,temp,a[99];
i=0;
while(scanf("%d",&num[i]))
{
if(num[i]!=42)
{
a[i]=num[i];
d=i;
i++;
}
else
{
break;
}
}
for(i=0;i<d;i++)
{
for(j=0;j<d;j++)
{
if(a[j]>a[i])
{
temp=a[j];
a[j]=a[i];
a[i]=temp;
}
}
}
return 0;
}
what wrong in this program Please anyone help me
while submitting it giving the error wrong answer
#include<iostream>using
#include<iostream>
using namespace std;
int main()
{
int n;
for(int i=0; i<100;i++)
cin>>n;
for(int i=0; i<100;i++)
{
if(n==42)
break;
cout<<n<<"n";
}
return 0;
}
Smbdy plz tell me wts wrong in dis program...
Does the problem say to stop
Does the problem say to stop after reading 100 numbers?
#include<stdio.h>int
#include<stdio.h>
int main()
{
int a;
while(1)
{
scanf("%d",&a);
if(a==42||a>=99)
break;
printf("%d",a);
}
return 0;
}
I want to submit it.... compile it using trubo c.....
then which language i select for it.
#include<iostream>#include<co
#include<iostream>
#include<conio.h>
int main()
{
int n;
for(int i=0; i<100;i++)
cin>>n;
for(int i=0; i<100;i++)
{
if(n==42)
break;
cout<<n<<"n";
}
return 0;
}
I'm new to this site and just
I'm new to this site and just testing how things work.
what does M represents, because while doing Life universe problem, the first one in easy, it takes 177M while an almost same solution in the successful submission has 0.0M ?
Also, the input and output shown in the example, has to be in that order or we can take input then show output, then again run loop and take input then output. Because, I submitted the solution as shown in the example, but other submitted in other order and still got successful submission?
#include<stdio.h> #include<
#include<stdio.h> #include<
#include<stdio.h>#include<con
#include<stdio.h>
#include<conio.h>
void main()
{
int n;
clrscr();
printf("PLEASE ENTER ANY INTEGER NUMBER OF ONE OR TWO DISITS ");
do
scanf("%d",&n);
while(n!=42);
printf("nn42 found!!!nnPLEASE PRESS ANY KEY TO EXIT");
getch();
}
Here, the constraint on the
Here, the constraint on the input we have is that any input number n will be such that (0 <= n < 100).
What shall i do if the number inputed is not within the range? Shall i exit the programme or ask user to input again?
If the problem statement
If the problem statement tells you that n will always be between 0 and 100, then n will always be between 0 and 100.
thnks Stephen
thnks Stephen
import java.io.*; public
#include int main() { int
#include void main() { int
#include void main() { int x;
#include int main() { int
main() { int i; while(1) {
hey, m getting a runtime time
#include #include void
Can anybody tell me wats the
#include #include void
The equivalent code in Java
where is the
How do I know when to stop
whats wrong with
#include #include void
#include #include void
this is not fair :(( why the
int a[10],k,n,i; clrscr();
using System; namespace
Help help help what is error
plz tell the problem with
#include int main() {
anybody tell me this in C#
class name should be Main
Is it ok if cin and cout r
#include int main() { short
Do i need to assume that
#include void main() { int
For those doing these tasks
#include main() { int