2014-04-25 28 views
0

我创建了一个文件。 C“Sorting.c”,它实现了用于对整数数组进行排序的几种算法。 现在我必须创建一个测试文件来创建随机数组,并随机对这些数组执行各种排序算法。 此外,所产生的时间必须写在终端和文本文件上。错误:'...'的冲突类型;注:以前的隐式声明'...'在这里

我写了这个代码:

#include <stdio.h> 
#include <time.h> 
#include <stdlib.h> 
#include <sys/time.h> 
#include "Sorting.h" //file thath contains the implementation of the sorting method like iinsertion sort, selection sort, merge sort and quick sort 

#define N 100 
#define STEP 5 

int arrayOriginal[N]; 
int arrayToSort[N]; 
int arrayTemp[N]; 

void fillArray(int a[], int n, int max) { 
    srand(time(NULL)); 
    int i; 
    for(i = 0; i < n; i++) 
     a[i] = rand() % max; 
} 

void copyInto(int a[], int b[], int n) { 
    int i; 
    for(i = 0; i < n; i++) 
     b[i] = a[i]; 
} 

void testReport() { 
    FILE* pFile = fopen("Times.txt", "a"); 
    int n; 
    for(n = STEP; n < N; n += STEP) { 
     fillArray(arrayOriginal, n, 9*n/10); 
     double t_isort = useIsort(arrayOriginal, n); 
     double t_ssort = useSsort(arrayOriginal, n); 
     double t_msort = useMsort(arrayOriginal, n); 
     double t_qsort = useQsort(arrayOriginal, n); 
     fprintf(pFile, "Size = %d, t_isort = %.6f, t_ssort = %.6f, t_msort = %.6f, t_qsort = %.6f \n", n, t_isort, t_ssort, t_msort, t_qsort); 
     printf("Size = %d, t_isort = %.6f, t_ssort = %.6f, t_msort = %.6f, t_qsort = %.6f \n", n, t_isort, t_ssort, t_msort, t_qsort); 
    } 
    printf("\n\n"); 
    fclose(pFile); 
} 

double useIsort(int arO[], int n) { 
    copyInto(arO, arrayToSort, n); 
    struct timeval t1, t2; 
    gettimeofday(&t1, NULL); 
    isort(arrayToSort, n); 
    gettimeofday(&t2, NULL); 
    double timediff = (t2.tv_sec - t1.tv_sec) * 1000.0;  // sec to ms 
    timediff += (t2.tv_usec - t1.tv_usec)/1000.0; // us to ms 
    return timediff; 
} 

double useSsort(int arO[], int n) { 
    copyInto(arO, arrayToSort, n); 
    struct timeval t1, t2; 
    gettimeofday(&t1, NULL); 
    ssort(arrayToSort, n); 
    gettimeofday(&t2, NULL); 
    double timediff = (t2.tv_sec - t1.tv_sec) * 1000.0;  // sec to ms 
    timediff += (t2.tv_usec - t1.tv_usec)/1000.0; // us to ms 
    return timediff; 
} 

double useMsort(int arO[], int n) { 
    copyInto(arO, arrayToSort, n); 
    struct timeval t1, t2; 
    gettimeofday(&t1, NULL); 
    msort(arrayToSort, n); 
    gettimeofday(&t2, NULL); 
    double timediff = (t2.tv_sec - t1.tv_sec) * 1000.0;  // sec to ms 
    timediff += (t2.tv_usec - t1.tv_usec)/1000.0; // us to ms 
    return timediff; 
} 

double useQsort(int arO[], int n) { 
    copyInto(arO, arrayToSort, n); 
    struct timeval t1, t2; 
    gettimeofday(&t1, NULL); 
    qisort(arrayToSort, n); 
    gettimeofday(&t2, NULL); 
    double timediff = (t2.tv_sec - t1.tv_sec) * 1000.0;  // sec to ms 
    timediff += (t2.tv_usec - t1.tv_usec)/1000.0; // us to ms 
    return timediff; 
} 

int main() { 

    testReport(); 
    return 0; 
} 

但是,编译器给了我以下错误:

  • SortingAlgorithmTest.c:95:8:错误:冲突的类型 'useIsort' 双useIsort (int arO [],int n){ ^
  • SortingAlgorithmTest.c:77:20:note:以前的'useIsort'的隐式声明在这里 double t_isort = useIsort(arra yOriginal,n); ^
  • SortingAlgorithmTest.c:112:8:错误:冲突的类型 'useSsort' 双useSsort(INT ARO [],INT N){ ^
  • SortingAlgorithmTest.c:78:20:注:先前隐式声明'useSsort'在这里 double t_ssort = useSsort(arrayOriginal,n); ^
  • SortingAlgorithmTest.c:129:8:错误:冲突的类型 'useMsort' 双useMsort(INT ARO [],INT N){ ^
  • SortingAlgorithmTest.c:79:20:注:先前隐式声明'useMsort'在这里 double t_msort = useMsort(arrayOriginal,n); ^
  • SortingAlgorithmTest.c:146:8:错误:冲突的类型 'useQsort' 双useQsort(INT ARO [],INT N){ ^
  • SortingAlgorithmTest.c:80:20:注:先前隐式声明'useQsort'在这里 double t_qsort = useQsort(arrayOriginal,n); ^

我认为这是一个愚蠢的错误,但我认为这是一个小时,我找不到错误。谁能帮我? 谢谢

+0

你可以显示tiveval的结构定义吗?此外,头文件Sorting.h将是有用的 –

+0

这个问题应该被移动到http://codereview.stackexchange.com/ – IceArdor

+0

为什么头文件包含实现? –

回答

3

在C中,当你调用一个函数时,它的定义必须在调用者函数之上。

尝试把你的使用*排序上面testReport(),它应该解决你的问题。

如果您不想介绍函数的顺序,您也可以将所有函数定义复制到.h中。

0

在testReport中使用它们之前,声明函数useXsort。