c语言:写一个函数,生成10000 个随机整数,保存在数组中,随机生成10个范围在1到100的整数,保存在数组中,输出这1...

该楼层疑似违规已被系统折叠 隐藏此楼查看此楼

#include

#include

#include

#define SIZE 10000

void frand();

int ffindmax( int* const array,const int size);

void saveArray(int []);

int cmp(const void* p,const void* q);

void sortArray();

int randArray[SIZE];

int arr[SIZE];

int main(){

clock_t start1,finish1,start2,finish2;

double Thistime,thistime;

char a;

frand();

start1=clock();

ffindmax(randArray,SIZE);

finish1=clock();

Thistime=(double)(finish1-start1)/CLOCKS_PER_SEC;

printf("%.9f Seconds\n",Thistime);

saveArray(randArray);

start2=clock();

sortArray();

finish2=clock();

thistime=(double)(finish2-start2)/CLOCKS_PER_SEC;

printf("%f Seconds.\n",thistime);

printf("\n\n");

saveArray(arr);

return 0;

}

void frand(){

int i;

srand(time(NULL));

for(i=0;i

randArray[i]=rand();

printf("%d%c",randArray[i],(i+1)%10?*\t*:*\n*);

}

}

int ffindmax( int* const array,const int size){

int max=0;

int i=0;

while(i

if(array[i]>max){

max=array[i];

i++;

}

else

i++;

}

printf("\n\nFind the max==%d.\n\n",max);

return max;

}

void saveArray(int[]){

FILE *fPtr;

int i;

if((fPtr=fopen("File_Array.txt","w"))==NULL)

printf("File could not be opened.\n");

else{

for(i=0;i

fprintf(fPtr,"%5d%c",randArray[i],(i+1)%5?*\t*:*\n*);

}

fclose(fPtr);

}

int cmp(const void* p,const void* q){

return (*(int*)p)-(*(int*)q);

}

void sortArray(){

FILE *fPtr;

clock_t start,finish;

int i=0,j=0;

if((fPtr=fopen("File_Array.txt","r"))==NULL)

printf("File could not be opened.\n");

else{

while(!feof(fPtr)){

fscanf(fPtr,"%d",&arr[i]);

++i;}

for(j=0;j

printf("%d%c",arr[j],(j+1)%10?*\t*:*\n*);

qsort(arr,SIZE,sizeof(int),cmp);

for(j=0;j

printf("%d%c",arr[j],(j+1)%10?*\t*:*\n*);

}

}

}

fclose(fPtr);

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值