I have a homework to do: I need to measure the time for the bubblesort in a random array with 100000 numbers. I get an error when I try to randomly generate number.Also if I DON'T randomly generate numbers I get 0 everytime. I done this so far:
main.c
int main()
{
int *a,n = 0;
srand(time(0));
beolvas(&a,&n,"be.txt");
clock_t start,stop;
start = clock();
bubblesort(a,n);
stop = clock();
float timespent = (stop - start)/CLOCKS_PER_SEC;
printf("%f\n",timespent);
kiir(a,n);
free(a);
return 0;
}
kibe.h
void beolvas(int **a,int *n,const char * file)
{
int i;
FILE * fin;
fin = fopen("be.txt", "rt");
*a = (int*)malloc(*n*sizeof(int));
if(a == 0){printf("Error");return 0;}
for(i = 0; i < 100000; ++i){
*a = rand() % 100;
}
fclose(fin);
}
void bubblesort(int *a, int n)
{
int i,j,csere;
for(i = 0; i < n-1; ++i){
for(j = 0; j < n - i -1; ++j){
if (a[j] > a[j + 1]){
csere = a[j];
a[j] = a[j + 1];
a[j + 1] = csere;
}
}
}
}
void kiir(int *a,int n)
{
int i;
for(i = 0; i < n; ++i){
printf("%i ",a[i]);
}
}
As you see I need to use headers...It is really boring...
Aucun commentaire:
Enregistrer un commentaire