Runtime error on URI Judge Online?

Asked

Viewed 605 times

0

I am unable to find out the Runtime error of this program. The question link: https://www.urionlinejudge.com.br/judge/pt/problems/view/1245

#include <stdio.h>

int main()
{
int i,j,k,count,N,M[100];
char L[100];

while ((scanf("%d",&N))!=EOF)
    {for(i=0;i<N;i++)
        scanf("%d %c",&M[i],&L[i]);
    j=0;
    k=1;
    count=0;
    while(j<N)
        {for(i=k;i<N;i++)
            {if((M[j]==M[i])&&(L[j]!=L[i]))
                ++count;
            if((M[j]==M[i])&&(L[j]==L[i]))
                M[i]=0;
            }
        ++k;
        ++j;
        }
    printf("%d\n",count);
    }

return 0;
}
  • strongly Suggest using size_t rather than int as None of the values will be Less than zero.

  • the arrays M[] and L[] are only 100 Elements long BUT N can be up to 10*10*10*10 I.E. 10000 in size, so anytime a value for N is read that is >= 100, the arrays will overflow (writing Past the end of the arrays) which is Undefined behavior and can lead to a seg fault Event.

  • for Ease of readability and understanding: 1) use Meaningful variable Names. Variable Names should indicate Usage or content (or Better, Both). 2) Select code Blocks (for, if, Else, while, do...while, switch, case, default) via a single Blank line. 3) follow the Axiom: only one statement per line and (at Most) one variable declaration per statement.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.