user3425699
user3425699

Reputation:

error: conflicting types for '...'; note: previous implicit declaration of '...' was here

I created a file. C "Sorting.c" that implements several algorithms for sorting an array of integers. Now I have to create a test file that creates random array and performs the various sorting algorithms on these arrays random. Moreover, the time resulting must be written on the terminal and on a text file.

I wrote this code:

#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;
}

But the compiler gives me the following errors:

I think it's a stupid mistake, but I think that is an hour and I can not find the error. Can anyone help me? Thanks

Upvotes: 0

Views: 6835

Answers (2)

tcollart
tcollart

Reputation: 1062

In C, when you call a function, its definition must be above the caller function.

Try to put your use*sort above testReport(), it should fix your problem.

You may also copy all the function definitions into your .h if you don't want to mind about the order of your functions.

Upvotes: 3

user1196549
user1196549

Reputation:

Declare the functions useXsort before you use them in testReport.

Upvotes: 0

Related Questions