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

×

Life,The universe and everything

What is wrong with the following code. It shows wrong answer in codechef.

include <stdio.h>

include <stdlib.h>

int main() { int a;

while(a!=42)
{
    scanf("%d",&a);
     if(a<0&&a>100)

            printf("%d",a);

}

return 0;}

asked 22 Apr '15, 17:27

zephy's gravatar image

zephy
11
accept rate: 0%

retagged 22 Apr '15, 22:07

arun_as's gravatar image

arun_as
7232618


The line:
if (a < 0 && a > 100) will never be true, since a number can never simultaneously be both less than 0 and greater than 100.

Further, the input specification clearly mentions that all the numbers are strictly 2-digit numbers, and hence you need not attempt to verify this within your program.

Also, initialize your value a, since at the line while (a != 42), for the first iteration, a is undefined and can have an unpredictable outcome.

To summarize:

  • Remove the "if" condition
  • Initialize a at the time of declaration, say to -1.

Taking care of the above two points should solve the problem. :)

link

answered 22 Apr '15, 17:36

caprico's gravatar image

caprico
861
accept rate: 25%

Plus, use "\n" after printing your value in a otherwise output will not be displayed on the new line.

(10 Jan, 23:24) pankajkhan

Could anyone please explain me what is wrong with my code

include<stdio.h>

int main() { int n,i,a[1000]; for(i=0;i<n;i++) { { scanf("%d",&a[i]); if(a[i]==42) { ` exit(0); }printf("%d\n",a[i]); } } }

link

answered 21 Sep '15, 23:38

anil_s_p's gravatar image

anil_s_p
1
accept rate: 0%

edited 21 Sep '15, 23:38

There is no n in the question. You have to keep taking the input from the user, until you encounter a 42, which is when you break. Also, it is possible that there are more than 1000 numbers, so storing them in a 1000 sized array will give a segmentation fault.

link

answered 22 Sep '15, 13:10

harshvk5's gravatar image

harshvk5
313
accept rate: 0%

include<iostream>

using namespace std;

int main() { int len=5; int a[len],i,j; for(i=0;i<len;i++) {="" <br=""/> cin>>a[i];

}

for(j=0; j<len; j++)
{
    if(a[j]!=42)
    {
        cout<<a[j]<<"\t";
    }     
    else
    {
        break;
    }

}

return 0;

}

is it correct??

link

answered 02 Aug '16, 00:21

damini_19's gravatar image

damini_19
1
accept rate: 0%

How about this

#include <iostream>

using namespace std;

int main() { int n;

while(cin>>n)

    {

    if(n==42)break;


    else

    cout<<n<<endl;
}

}

link

answered 02 Aug '16, 01:46

atulmaster's gravatar image

atulmaster
151
accept rate: 0%

include<stdio.h>

int main() { int i,a[5]; printf("enter the nos"); for(i=0;i<=4;i++) { scanf("%d",&a[i]); } for(i=0;i<=4;i++) { if(a[i]!=42) { printf("%d",a[i]); } else { break; } } return 0; }

link

answered 30 Aug '16, 20:28

anjali_3298's gravatar image

anjali_3298
1
accept rate: 0%

What is wrong with this code?

include<stdio.h>

int main() { int i=0,j=0,storage[]={0}; //while(j>=0 && j<100) while(j==0) { //j++; scanf("%d",&storage[i]); printf("i=%d\n",i); if(storage[i]!=42) printf("arr = %d\n",storage[i]); else break; } return 0; }

link

answered 10 Jan, 20:20

vamsi3296's gravatar image

vamsi3296
1
accept rate: 0%

include <stdio.h>

int main(void) { int i; while (1) { scanf("%d", &i); if (i > 99) continue; if (i == 42) break; printf("%d", i); } return 0; }

link

answered 11 Jan, 00:19

dhingra12345's gravatar image

dhingra12345
1
accept rate: 0%

So,what exactly is wrong with my code?

link

answered 11 Jan, 22:12

vamsi3296's gravatar image

vamsi3296
1
accept rate: 0%

toggle preview

Follow this question

By Email:

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

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "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

Tags:

×1,177
×25

Asked: 22 Apr '15, 17:27

Seen: 1,857 times

Last updated: 11 Jan, 22:12