2014-04-25 6 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 y 원래, n); ^
  • SortingAlgorithmTest.c : 112 : 8 : 오류 'useSsort' 이중 useSsort (INT 아로 [], INT 않음)에 대한 유형 충돌 ^ {
  • SortingAlgorithmTest.c : 78 : 20 : 참고 : 이전 암시 적으로 'useSsort'선언이 여기에 있습니다 double t_ssort = useSsort (arrayOriginal, n); ^
  • SortingAlgorithmTest.c : 129 : 8 : 오류 'useMsort' 이중 useMsort (INT 아로 [], INT 않음)에 대한 유형 충돌 ^ {
  • SortingAlgorithmTest.c : 79 : 20 : 참고 : 이전 암시 적으로 'useMsort'선언은 여기에 있습니다 double t_msort = useMsort (arrayOriginal, n); ^
  • SortingAlgorithmTest.c 146 : 8 : 오류 'useQsort' 이중 useQsort (INT 아로 [], INT 않음)에 대한 유형 충돌 ^ {
  • 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 함수를 선언하십시오.

관련 문제