34 / 92
Nov 2013

can any geek tell me why the following code is giving a wrong answer.......

include

int main()
{
int x,k=0;

scanf("%d",&x);
while(x>0)
{
if( x==42)
k=1;
if(k==0 && x<100)
printf("%d\n",x);
scanf("%d",&x);
}
return 0;
}

4 months later

You are assuming that the file ends in '\n'. In reality you should not care how the file ends. You should ony care about finding "42". Use scanf.

7 months later

i am running this code and it is getting me a run time error.. why ?> plz help me. i m new to programming

include

int main()
{
int num;
scanf("%d", &num);
while(num!=42)
{
printf("%d\n", num);
scanf("%d", &num);
}
}

Please use code tags when posting code.

You always need to return 0; from main in C.

5 months later

Just thought I'd share mine since I haven't seen it in this topic yet.

#include <stdio.h>
int main(void) {
    char x[3];
    while(atoi(gets(x))!=42)puts(x);
    return 0;
}

Alternatively with fgets() and fputs():

#include <stdio.h>
int main(void) 
{
    char x[4];
    while (atoi(fgets(x,4,stdin))!=42)
    {
    	fputs(x,stdout);
    }
    return 0;
}

@leppy, is it the size of the character array? I believe I made the size of my array 3 in my original version, but I was just playing around to see if I could get away with buffer overflow. ^^

Sorry I forgot to change it back. I edited the post to make the size of the array 3, which should account for the max 2 characters per line (not counting newline).

2 months later

include

int main(){
int i,x,c;
int a[100];
i=0;

	while(x>=0 && x<100 && i<100)
	{ 
		scanf("%d",&x);
		a[i]=x;
		i++;

	}
	c=i;	

for(i=0;i { if(a[i]!=42)
printf("%d\n",a[i]);
else
break;
}

return 0;
}

whats the error

15 days later

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{
    int arr[10000];
    int count=0,i=0,flag=1;
    printf("Enter Input:");
    while(flag)
    {
        scanf("%d",&arr[i]);
        if(arr[i]==42)
        flag=0;
    else
        flag=1;

    i++;
    count++;
}
for(i=0;i<count-1;i++)
{
    printf("%d\n",arr[i]);
}
return 0;
}

can you plz explain me what is wrong with this code because it gives the same output that is required but i do no understand they considered it as wrong answer.

25 days later

The judge considers all output for the solution.

    printf("Enter Input:");

As soon as you print this line the judge has received the wrong answer.

Please post back if you have more questions.

3 months later

output is correct but it was showing run-time error

include

void main()
{
int j,i;
for(i=0;i<100;i++)
{
scanf("%d",&j);
if(j==42) break;
printf("%d",j);
}
}

Please use code tags when posting code.

For the input:

10
23
42
3
10

Your code returns:

1023

It should return:

10
23

Why are you using 100? There is no indication as to how many numbers are in the input.

2 months later

Why i am getting compilation error??
[code]

include

include

int main()
{
int n;
while(1)
{
scanf("%d",&n);
if(n>=0&&n<100&&n!=42)

        printf("%d\n",n);
    else
        exit(0);

}

return 0;

}

If you click on where it says "Compilation Error" it will actually list you the compilation error.

1 month later

Every one is writing code only for the above input only.
It should not be like that na.
We have to sort all the numbers before 42 in ascending order (as mentioned in the question...."More precisely... rewrite small numbers from input to output").
Then we have to print all the numbers.
why everyone is writing the code without sorting?
or
Any problem with my way of understanding the question(let me know the correct way)??

and

why admin did consider the two digit input case as the program can accept any number na..????

Thanks in advance smile smile

There is nothing in this problem statement that says to sort the numbers.

"More precisely..." means that the statement to follow will make the previous statement more precise.

"rewrite small numbers from input to output"
Input is where the numbers are read from. Output is where the numbers get printed to. Rewrite means to copy. The problem is asking you to read the numbers from input and write them to output. Stop when the number is 42.

Keeping the numbers one or two digits keeps the problem simple. It also allows for some creative solutions.

If you need more clarification, please post again.

I have only one doubt now.
Can i give the input sequence like 88 1 99 or 99 55 1 (small number followed by bigger number)..??
if not then y??

Thanks in Advance

Yes those are legal input. The numbers can be in any order. The only requirement is that they be only one or two digits and the input must contain a 42.

Know that you do not choose the input that the judge will use. Your solution must work for all possible input that the judge will give you.