< Back to forum

any one please help me finding the error for the question i am getting wrong answer https://www.hackerearth.com/practice/data-structures/arrays/1-d/practice-problems/algorithm/range-query-2/description/

  

#include <stdio.h>

int main()
{
    int a,b,c,n,q,i;
    scanf("%d%d",&n,&q);
    char ar[n];
    for(i=0;i<n;i++)
    scanf("%c",&ar[i]);
    for(i=0;i<q;i++)
    {
        scanf("%d%d",&a,&b);
        if(a==1)
        {
            if(ar[b-1]=='1')
                ar[b-1]='0';
            else
                ar[b-1]='1';
        }
        else
        {
            scanf("%d",&c);
            if(ar[c-1]=='1')
                printf("ODD\n");
            else
                printf("EVEN\n");
        }
    }
    return 0;
}
 

Asked by: bandaram_Sumanth on April 7, 2019, 6:34 p.m. Last updated on April 7, 2019, 6:34 p.m.


Enter your answer details below:


Enter your comment details below:




2 Answer(s)

avatar

Input given in this question are the numbers either 0 or 1 , but these numbers are separated by SPACE ,since you are taking input as character ,space will also be stored in your input array since space is also character. So you must always take input in integer array for SPACE seperated integer.

Raghav_Grover last updated on April 7, 2019, 6:34 p.m. 0    Reply    Upvote   

avatar

scanf("%c",&ar[i]); ->this part of your code is causing the error.

This happens beacuse %c also reads the '\n' (new line character) when you hit enter.

To fix this just put a space before %c i.e scanf(" %c",&ar[i]);

be careful while taking char/string input in c.

you may go thorough this article to know more about the cause of the error:

https://gsamaras.wordpress.com/code/caution-when-reading-char-with-scanf-c/

Shubham_Kumar_Gupta last updated on April 7, 2019, 6:34 p.m. 0    Reply    Upvote   

Instruction to write good question
  1. 1. Write a title that summarizes the specific problem
  2. 2. Pretend you're talking to a busy colleague
  3. 3. Spelling, grammar and punctuation are important!

Bad: C# Math Confusion
Good: Why does using float instead of int give me different results when all of my inputs are integers?
Bad: [php] session doubt
Good: How can I redirect users to different pages based on session data in PHP?
Bad: android if else problems
Good: Why does str == "value" evaluate to false when str is set to "value"?

Refer to Stack Overflow guide on asking a good question.