stigmata2 stigmata2 -4 years ago 104
C Question

Return value when using function pointer (also using OpenMP)

When I change quick short function code for example output always at 0, Program go in case0 change the quicksortarr 'values become 0 this is okey but when other case ("case1") start shellSortarr is changed also quicksortarr is changed and become same as (shellsortarr) therefore I won't compare this array.
I think my mistake pointers.

My second question(less important) is I want to omp functions for using %100 CPU I found this codes but I guess, This is only work before for loop

#pragma omp parallel for schedule(dynamic)

omp_get_wtime();


Do you have suggesiton

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


.I call function like this:

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


.My sorting functions like this:

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

//sorting code
return x3;
}


.My compare functions like this:

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

Answer Source

You need extra space for quicksortarr and 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;
}

you don't need return anymore so;

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

    //sorting code
       }

Also #pragma omp parallel for schedule(dynamic) uses only for loop if your sorting code use for you can add with <omp.h> library

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download