使用函数指针时返回值(也使用OpenMP)

时间:2017-03-22 22:35:30

标签: c

当我更改快速短功能代码时,例如输出始终为0,程序进入case0更改quicksortarr'值变为0这是okey但是当其他情况(“case1”)启动shellSortarr更改时quicksortarr也会更改并变为与(shellsortarr)相同,因此我不会比较这个数组。 我想我的错误指针。

我的第二个问题(不太重要)是我想要omp函数使用%100 CPU我发现这个代码但是我想,这只是在循环之前工作

#pragma omp parallel for schedule(dynamic)

omp_get_wtime();

你有建议吗

 double sort(int *(*func)(int*, int), int *a, int len,int i) {
        time_t end, begin;
        int *b = malloc(len * sizeof *b);
        if (!b) { perror("sort: malloc"); return 0; }
        memcpy(b, a, len * sizeof b[0]);    

//for example b = 10,3,15,6,123

        int *selectionSortarr;
        int *quicksortarr;

        switch( k%2 ) {
        case 0:
        begin = clock();

//b = 10,3,15,6,123 

        quicksortarr = func(b, len);
//for example func(quicksort) is wrong and quicksortarr become 0,0,0,0,0
        end = clock();  
        k++;
        break;
        case 1:     
        begin = clock();
//b is again b = 10,3,15,6,123  
        shellSortarr = func(b, len);
// after that shellsortarr and quicksortarr become 3,6,10,15,123
//therefore compare function is print "File's sorted arrays are equal"
        end = clock();      
        compare(quicksortarr,shellSortarr,len,i);
        k=0;    
        break;  
        }   
        free(b);
        return (double)(end - begin) / CLOCKS_PER_SEC;
    }

。我这样调用函数:

while (fscanf(f, "%d", &input) == 1) a[len++] = input;
    quick[i] = sort(quickSort,     a, len,i);
    shell[i] = sort(shellSort,     a, len,i);

。我的排序​​功能如下:

int *shellSort(int x3[], int n){

    //sorting code
    return x3;
}

。我比较这样的函数:

void compare(int *arr1,int *arr2,int len,int y) {
    int i=0;
    int check=0;
        for (i=0; i<len; i++){
        if (arr1[i] != arr2[i])     
                check++;
                break;

    }
    if (check !=0) printf("%i . File's sorted arrays are not equal difference start at %i . variable in array\n",y,i);
    else printf("%i. File's sorted arrays are equal\n",y);
}

1 个答案:

答案 0 :(得分:1)

你需要额外的空间用于quicksortarr和shellSortarr

double sort(void (*func)(int*, int), int *a, int len,int i) {
    time_t end, begin;
    int *b = malloc(len * sizeof *b);
    if (!b) { perror("sort: malloc"); return 0; }
    memcpy(b, a, len * sizeof b[0]);    
    switch( k%2 ) {
    case 0:
    begin = clock();    
    func(b, len);
    end = clock();  
    int *quicksortarr = malloc(len * sizeof *quicksortarr);
    if (!quicksortarr) { perror("sort: malloc"); return 0; }
    memcpy(quicksortarr, b, len * sizeof quicksortarr[0]);
    k++;
    break;
    case 1:     
    begin = clock();
    func(b, len);
    int *shellSortarr = malloc(len * sizeof *shellSortarr);
    if (!shellSortarr) { perror("sort: malloc"); return 0; }
    memcpy(shellSortarr, b, len * sizeof shellSortarr[0]);
    end = clock();
    k++;
    break;

    compare(quicksortarr,shellSortarr,len,i);
    k=0;    
    break;  
    }   

    free(b);
    return (double)(end - begin) / CLOCKS_PER_SEC;
}

你不再需要返回了;

 void shellSort(int x3[], int n){

    //sorting code
       }

#pragma omp parallel for schedule(dynamic)如果你的排序代码使用,你只能使用for循环来添加<omp.h>