-1
I’m asking the question Low Frequency I have tested the cases of Udebug but the online judge only returns me Runtime error, for what I know when this happens and when it tries to use memory position that was not allocated, but I allotted more than the question asked and still the problem continues
My code
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
double resultado(char *frase);
int main(int argc, char** argv)
{
int teste, tam, cont, cont2, indice, i;
char frase[900], presenca[900];
char *ptr_1, *ptr_2;
double vetor[900];
char **nomes = malloc(sizeof(char) * 300);
char **presencas = malloc(sizeof(char) * 300);
for(i = 0; i < 300; i++)
{
nomes[i] = malloc(sizeof(char) * 900);
}
for(i = 0; i < 300; i++)
{
presencas[i] = malloc(sizeof(char) * 900);
}
scanf("%d", &teste);
while(teste--)
{
cont2 = cont = 0;
getchar();
scanf("%d", &tam);
scanf(" %[^\n]", frase);
scanf(" %[^\n]", presenca);
ptr_1 = strtok(frase, " ");
while(ptr_1 != NULL)
{
strcpy(nomes[cont], ptr_1);
cont++;
ptr_1 = strtok(NULL, " ");
}
ptr_2 = strtok(presenca, " ");
while(ptr_2 != NULL)
{
strcpy(presencas[cont2], ptr_2);
cont2++;
ptr_2 = strtok(NULL, " ");
}
for(indice = 0; indice < tam ; indice++)
{
vetor[indice] = resultado(presencas[indice]);
}
for(i = 0; i < tam; i++)
{
if(vetor[i] <= 75.0)
{
printf("%s ", nomes[i]);
}
}
memset(vetor, 0, sizeof(vetor));
printf("\n");
}
for(i = 0; i < 300; i++)
{
free(nomes[i]);
}
free(nomes);
for(i = 0; i < 300; i++)
{
free(presencas);
}
free(presencas);
return 0;
}
double resultado(char *frase)
{
int i, tam = strlen(frase);
double cont = 0;
for(i = 0; i < tam; i++)
{
if(frase[i] == 'M' || frase[i] == 'P')
{
cont++;
}
}
return (cont * 100) / tam;
}
Run time error? Maybe it has to do with variable allocation
nomes
andpresencas
. They’re pointers, but you’re allocating space only forchar
, not tochar*
.– Jefferson Quesado
It takes away a doubt, personal curiosity, what is the purpose of making these issues of competitive programming?
– Maniero
@Jefferson Quesado on Stackoverflow himself I asked a question how I would do that kind of allocation I just did what they passed me
– rafael marques
Cool is a way I have to train
– rafael marques
Friend I just saw !! that can have up to N names of students ... I’ve done this exercise in Uri, ie !! N names if N= 100 and each name is size 50, the allocation size is 5000 !! That is to say you are allocating little space for what the problem asks for.
– ViTAO
Dude, why don’t you declare these vectors as
char vetor[300][900];
? Is suffering there :/– Marcelo Shiniti Uchimura
@rafaelmarques sorry to tell you that is not working, nor is training taking place. You need to review your strategy.
– Maniero