خرید بک لینک

Hi everyone I'm practicing C with this little program that takes numbers in input but in string and divided with commas ("1,2,3,4") and the output is the average of the numbers. The function Split, splits a string in numbers and converts them in float and puts them in an array, then the function average makes the average. THe compiler gives me Segmentation Fault, but I can't undestand why, i'm on it from hours, can anybody helps me?

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

float* split(char* string){

int dim=0;
int i=0;
float numero;

char* sep = "," ;
char* tokena;
char* token;
char stringa[20];
strcpy (stringa, string);

for(token=strtok(stringa,sep); token!=NULL; token=strtok(NULL,sep))
{
dim++;
}

float *vet = (float*)malloc (dim*sizeof(float));

for(tokena=strtok(string,sep); tokena!=NULL; tokena=strtok(NULL,sep))
{

vet[i]=tokena;
i++;
}

retu vet;
}

float average(char* stringa){

int i;
int dim=0;
char* token;
char* sep = ",";
float sum=0;
float risultato=0;
char stringas[20];
strcpy(stringas, stringa);

for(token=strtok(stringas,sep); token!=NULL; token=strtok(NULL,sep))
{
dim++;
}

float* vettore = split(stringa);
for(i=0;i<dim;i++){

sum = sum + vettore[i];
}

risultato = (sum/dim);

retu risultato;

}

int main()
{
char* test="3,2,1";
float a=average(test);
printf("%fn", a);
retu 0;
}

- - , .
.

برچسب: نویسنده: استخدام کار تاريخ: يکشنبه 27 تير 1395 ساعت: 1:51

صفحه بندی