随机数组中有很多数字出现错误 [英] Random array with many numbers getting error
问题描述
我有一个家庭作业要做:我需要测量随机数组中100000个数字的时间。当我尝试随机生成数字时出现错误。如果我不随机生成数字,则每次都得到0。
我到目前为止完成了这项工作:
main.c
int main()
{
int * a,n = 0;
srand(time(0));
beolvas(& a,& n,be.txt);
clock_t开始,停止;
start = clock();
bubblesort(a,n);
stop = clock();
float timespent =(stop - start)/ CLOCKS_PER_SEC;
printf(%f\\\
,timespent);
kiir(a,n);
免费(a);
返回0;
kibe.c(对不起,我写的很糟糕)
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; (i = 0; i for(j = 0; j
{
if(a [j]> a [j + 1]){
csere = a [j];
a [j] = a [j + 1];
a [j + 1] = csere;
$ b void kiir(int * a,int n)
{
int i;
for(i = 0; i printf(%i,a [i]);
$ b 正如你所看到的,我需要使用头文件......它真的很无聊......
$ b $ p 编辑
<现在我完全重写所有程序没有错误没有警告,但它不打印数组,并且排序时间仍然是0.我忘记了什么?为什么我的写功能什么都不做?
sema.c
void read(int * a,int n)
{
int i;
scanf(%d,& n);
a =(int *)malloc(n * sizeof(int));
if(a == 0){printf(Error); return 0;}
for(i = 0; i a [i] = rand()%100;
void bubblesort(int * a,int n)
{
int i,j,csere; (i = 0; i for(j = 0; j
{
if(a [j]> a [j + 1]){
csere = a [j];
a [j] = a [j + 1];
a [j + 1] = csere;
void write(int * a,int n)
{
int i;
for(i = 0; i printf(%i,a [i]);
$ block $ $ b $ se $ h
void read(int *,int *);
void write(int *,int);
void bubblesort(int *,int);
main.c
int main()
{
double * a = NULL,n = 0;
读(& a,& n);
clock_t开始,停止;
start = clock();
bubblesort(a,n);
stop = clock();
float elapsedTime =(stop - start)/ CLOCKS_PER_SEC;
printf(%f,elapsedTime);
write(a,n);
免费(a);
返回0;
解决方案 void beolvas(int ** a,int * n,const char * file);
声明 a
作为指针
但是这行:
$ b $ p $ a $ rand $()% 100;
仅将其取消引用一次,并为指针赋值(实际导致内存泄漏它是 malloc
-ed之前)
所以你会收到各种未定义的行为。 b
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.c(sorry I write it bad)
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...
EDIT
Now I completely rewrite all program no errors no warnings, but it doesn't prints the array out and the sorting time is still 0.What I forgot to do ? And why my write function does nothing ?
sema.c
void read(int *a,int n)
{
int i;
scanf("%d",&n);
a = (int*)malloc(n*sizeof(int));
if(a == 0){printf("Error");return 0;}
for(i = 0; i < n; ++i){
a[i] = rand() % 100;
}
}
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 write(int *a,int n)
{
int i;
for(i = 0; i < n; ++i){
printf("%i ",a[i]);
}
}
sema.h
void read(int*,int*);
void write(int*,int);
void bubblesort(int*,int);
main.c
int main()
{
double *a = NULL ,n = 0;
read(&a,&n);
clock_t start,stop;
start = clock();
bubblesort(a,n);
stop = clock();
float elapsedTime = (stop - start)/CLOCKS_PER_SEC;
printf("%f",elapsedTime);
write(a,n);
free(a);
return 0;
}
解决方案 The line void beolvas(int **a,int *n,const char * file);
is declaring a
as a pointer to pointer.
But this line:
*a = rand() % 100;
is dereferencing it only once and assigning a value to a pointer (effectively causing a memory leak as it was malloc
-ed before)
So you are getting various undefined behaviors.
这篇关于随机数组中有很多数字出现错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文