Jump to content

Erro : Excepção de vírgula flutuante (core dumped)


Guest
 Share

Recommended Posts

Boas, estou a começar a resolver os exercicios da USACO. Estou neste momento no segundo,Greedy Gift Givers, elaborei a minha solução mas estou com um erro de excepçao de virgula flutuante.

Codigo:

#include<stdio.h>
#include<string.h>
#include<stdlib.h>

#define MAXNAME 15

struct person{
char name[MAXNAME];
int money;
};

/* Get the index of the person */
int getname(char name[MAXNAME], struct person people[], int n_people)
{
int i;
for(i=0 ; i<n_people; i++)
	if((strcmp(name,people[i].name)) == 0)
		return i;
}



void distribute_money(FILE *fp, struct person people[], int n_people)
{
int total_amount = 0;
int total_people = 0;
int amount_per_people = 0;
char name[MAXNAME];
int i;
/* Get the name */  
fscanf(fp,"%s",name);
/* Get the amount of money */
fscanf(fp,"%d %d",&total_amount, &total_people);
/* How much money does each one gets */  
if(total_amount != 0 || total_people != 0)  
	amount_per_people = total_amount / total_people;
/* Reads the amount of people */
for(i=0; i<total_people; i++){
	/* Reads the people name */	  
	fscanf(fp,"%s",name);
	/* Search for the people name and add the money in there */
	people[getname(name,people,n_people)].money = amount_per_people;
}
}

int main(void)
{
FILE *fp = fopen("gift1.in","r");
int n_people;
/* Get the number of people that will contribute to the gift */  
fscanf(fp,"%d",&n_people);
/* Declare an array of structs */  
struct person *people = malloc(n_people);
int i;
char name[15];


/* Puts the name in the structs */
for(i=0; i<=n_people; i++)
	fscanf(fp,"%s",people[i].name);

for(i=0; i<=n_people;i++)
	distribute_money(fp,people,n_people);

fclose(fp);
fp = fopen("gift1.out","w");
for(i=0; i<n_people; i++)
	fprintf(fp,"%s %d\n",people[i].name,people[i].money);

fclose(fp);
return 0;
}

Com o gdb identifiquei o problema como sendo esta linha

amount_per_people = total_amount / total_people;

Mas não estou a ver uma forma de resolução possivel. Desde já agredeço o tempo dispendido.

Link to comment
Share on other sites

Outros pontos:

Em getname

strcmp(name,people.name) < usa antes strncmp visto que strcmp

não têm bounds checking e pode fazer leituras a overflow e underflow

não faças comparações quando a key é maior que a word

distribute_money

fscanf(fp,"%s",name); < não esta protegido de buffer overflow

fscanf(fp,"%d %d",&total_amount, &total_people);

^ -> risco de não ser um inteiro

amount_per_people = total_amount / total_people;

^ divisão entre inteiros (float ou double se os decimais interessam)

fscanf(fp,"%s",name); < overflow ?

int main(void)

FILE *fp = fopen("gift1.in","r"); < sem verificação de erros

fscanf(fp,"%d",&n_people); < mesma historia de sempre

struct person *people = malloc(n_people); < sem verificação de erro

scanf(fp,"%s",people.name); < preciso dizer mais ?

fclose(fp); < não houve verificação, sera que esta aberto ?

fp = fopen("gift1.out","w"); < sem verificação outra vez

fclose(fp); < n houve verificação, sera que esta aberto ?

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.