0
votes

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:

  • SortingAlgorithmTest.c:95:8: error: conflicting types for 'useIsort' double useIsort(int arO[], int n) { ^
  • SortingAlgorithmTest.c:77:20: note: previous implicit declaration of 'useIsort' was here double t_isort = useIsort(arrayOriginal, n); ^
  • SortingAlgorithmTest.c:112:8: error: conflicting types for 'useSsort' double useSsort(int arO[], int n) { ^
  • SortingAlgorithmTest.c:78:20: note: previous implicit declaration of 'useSsort' was here double t_ssort = useSsort(arrayOriginal, n); ^
  • SortingAlgorithmTest.c:129:8: error: conflicting types for 'useMsort' double useMsort(int arO[], int n) { ^
  • SortingAlgorithmTest.c:79:20: note: previous implicit declaration of 'useMsort' was here double t_msort = useMsort(arrayOriginal, n); ^
  • SortingAlgorithmTest.c:146:8: error: conflicting types for 'useQsort' double useQsort(int arO[], int n) { ^
  • SortingAlgorithmTest.c:80:20: note: previous implicit declaration of 'useQsort' was here double t_qsort = useQsort(arrayOriginal, n); ^

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

2
can you show tiveval's struct definition? Also the header file Sorting.h would be useful - Marshall Tigerus
This question should be moved to codereview.stackexchange.com - IceArdor
Why does the header file contain implementations? - Eric Lippert

2 Answers

3
votes

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.

0
votes

Declare the functions useXsort before you use them in testReport.