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 thanint
as None of the values will be Less than zero.– user3629249
the arrays
M[]
andL[]
are only 100 Elements long BUTN
can be up to10*10*10*10
I.E. 10000 in size, so anytime a value forN
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.– user3629249
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.
– user3629249