我創建了一個文件。 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); ^
我認爲這是一個愚蠢的錯誤,但我認爲這是一個小時,我找不到錯誤。誰能幫我? 謝謝
你可以顯示tiveval的結構定義嗎?此外,頭文件Sorting.h將是有用的 –
這個問題應該被移動到http://codereview.stackexchange.com/ – IceArdor
爲什麼頭文件包含實現? –