该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
#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);
}