Thread: Function Pointer Problem

  1. #1
    Registered User
    Join Date
    Mar 2017
    Posts
    3

    Function Pointer Problem

    I have a some problem using function pointer

    Above code start to case0 when I change the quicksorting algorthm quicksortarr is changed but after that when program go to other cases quicksortarr is changed therefore I can not compare sorting type result Why it is happened I don't have idea. Maybe it is a pointer problem

    This is my problem part:


    Code:
    switch( k%4 ) {
        case 0:
        begin = clock();    
        quicksortarr = func(b, len);
        end = clock();    
        k++;
        break;
        case 1:        
        begin = clock();
        shellSortarr = func(b, len);
        end = clock();
        k++;
        break;
        case 2:        
        begin = clock();
        bubbleSortarr = func(b, len);
        end = clock();    
        k++;
        break;
        case 3:        
        begin = clock();
        selectionSortarr=func(b, len);    
        end = clock();
        compare(quicksortarr,shellSortarr,bubbleSortarr,selectionSortarr,len,i);
        k=0;    
        break;    
        }



    This is my full code:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    #include <string.h>
    void print(int *arr, int len) {
        int i=0;
        for (i=0; i<len; i++){
            printf("%d ", arr[i]);
        }
        printf("\n");
    }
    int k=0;
    int *selectionSort(int x1[],int len){
        int j,k,temp;
        for (j=0; j<len; j++) {
            int min=x1[j], ind=j;
            for (k=j+1; k<len; k++) {
                if(x1[k]<min){
                    min=x1[k];
                    ind=k;
                }
            }
            if(ind!=j) {
                temp=x1[ind];
                x1[ind]=x1[j];
                x1[j]=temp;
            }
        }
    return x1;
    }
    
    
    int *bubbleSort(int x2[],int len){
     int pass,k,temp;
        for (pass=0; pass<len-1; pass++) {
     for (k=0; k<(len-pass-1); k++) {
     if(x2[k]>x2[k+1]){
     temp=x2[k];
     x2[k]=x2[k+1];
     x2[k+1]=temp;
    
    
     }
      
     }
     
     }
    return x2;
    }
    int *shellSort(int x3[], int n){
        int temp,i,last,is_sorted;
        int jump=n/2;
        while(jump >=1){
            last= n-jump;
            is_sorted=0;
            while(!is_sorted){
                is_sorted=1;
                for(i=0; i<last; i++){
                    if(x3[i]>x3[i+jump]){
                        temp=x3[i];
                        x3[i]=x3[i+jump];
                        x3[i+jump]=temp;
                        is_sorted=0;
                    }
                }
            }
            jump=jump/2;
        }
        return x3;
    }
    
    
    
    
    void quicksort2(int x[],int first,int last){
        int pivot,j,temp,i;
             if(first<last){
             pivot=first;
             i=first;
             j=last;
    
    
             while(i<j){
                 while(x[i]<=x[pivot]&&i<last)
                     i++;
                 while(x[j]>x[pivot])
                     j--;
                 if(i<j){
                     temp=x[i];
                      x[i]=x[j];
                      x[j]=temp;
                 }
             }
    
    
             temp=x[pivot];
             x[pivot]=x[j];
             x[j]=temp;
             quicksort2(x,first,j-1);
             quicksort2(x,j+1,last);
            
        }
        
    }
    int *quickSort(int x[], int len) {
        quicksort2(x, 0, len - 1);
        return x;
    }
     
    void check_sort(int *a, int len) {
        int i;
        for (i = 1; i < len; i++)
            if (a[i - 1] > a[i])
                break;
        if (i < len) printf("!!!! BAD SORT !!!!\n");
    }
    void compare(int *arr1,int *arr2,int *arr3,int *arr4, int len,int y) {
        int i=0;
        int check=0;
            for (i=0; i<len; i++){
            if (arr1[i] != arr2[i] && arr1[i] != arr4[i] && arr1[i] != arr3[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);
    }
    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]);    
        int *selectionSortarr;
        int *quicksortarr;
        int *bubbleSortarr;
        int *shellSortarr;
        switch( k%4 ) {
        case 0:
        begin = clock();    
        quicksortarr = func(b, len);
        end = clock();    
        k++;
        break;
        case 1:        
        begin = clock();
        shellSortarr = func(b, len);
        end = clock();
        k++;
        break;
        case 2:        
        begin = clock();
        bubbleSortarr = func(b, len);
        end = clock();    
        k++;
        break;
        case 3:        
        begin = clock();
        selectionSortarr=func(b, len);    
        end = clock();
        compare(quicksortarr,shellSortarr,bubbleSortarr,selectionSortarr,len,i);
        k=0;    
        break;    
        }    
        check_sort(b, len);
        free(b);
        return (double)(end - begin) / CLOCKS_PER_SEC;
    }
     
    
    
    
    
    int main(int argc, char *argv[]) {
        int i,lenght[10];
        float quick[10],shell[argc],bubble[argc],selection[argc];
        for (i = 1; i < argc; i++) {
            FILE *f = fopen(argv[i], "r");
            if (!f) { perror(argv[i]); continue; }
                      
        int *a, len = 0, input;
        while (fscanf(f, "%d", &input) == 1) len++;
        a = malloc(len * sizeof *a);
        if (!a) { perror("do_sorts: malloc"); break; }
        len = 0;
        rewind(f);
        while (fscanf(f, "%d", &input) == 1) a[len++] = input;
        quick[i] = sort(quickSort,     a, len,i);
        shell[i] = sort(shellSort,     a, len,i);
        bubble[i] = sort(bubbleSort,    a, len,i);
        selection[i] = sort(selectionSort, a, len,i);
        lenght[i]=len;
        free(a);
        fclose(f);
        
        }
        printf("\nFilename:         ");
        for(i=1;i<argc;i++) {
        printf("\t%s\t", argv[i]);
        }
        printf("\nInput Numbers:    ");
        for(i=1;i<argc;i++) {
        printf("\t%i\t\t", lenght[i]);
        }
    
    
        printf("\nQuick Sort:       ");
        for(i=1;i<argc;i++)
        {
        printf("\t%f\t", quick[i]);
            
        }
        printf("\nShell Sort:      ");
        for(i=1;i<argc;i++)
        {
        printf("\t%f\t", shell[i]);
        }
        printf("\nBubble Sort:     ");
        for(i=1;i<argc;i++)
        {
           printf("\t%f\t", bubble[i]);
           
        }
        printf("\nSelection Short:");
        for(i=1;i<argc;i++)
        {
           printf("\t%f\t", selection[i]);
        }
        return 0;
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Your function named sort is implemented with the wrong idea: you wrote the switch thinking it would handle a bunch of different sorting algorithm cases, but that is completely unnecessary: the function for sorting is passed to the function named sort via the function pointer, so you don't have to consider cases.

    You may have thought that you needed to do this in order to compare the different results for the sorting algorithms to check that they are identical, but that's bogus: you only need to check for correctness with your check_sort function. As long as check_sort does not report a problem, the results would be identical since you are sorting copies of the same array of integers.

    Therefore, your function named sort could be simplified to something like this:
    Code:
    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]);
        begin = clock();
        func(b, len);
        end = clock();
        check_sort(b, len);
        free(b);
        return (double)(end - begin) / CLOCKS_PER_SEC;
    }
    Get rid of the global variable named k. You don't need it, and it is probably confusing you especially since you also have local variables named k. If you did need a global variable, it must have a much more descriptive name than k. k is not a good name for a local variable that is not a loop/array index or has some special typically mathematical purpose that is clear from context; for a global variable it is exceedingly bad.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Function and Pointer problem
    By austin.18 in forum C Programming
    Replies: 4
    Last Post: 11-03-2014, 11:16 AM
  2. Pointer to function problem
    By vincent01910 in forum C Programming
    Replies: 6
    Last Post: 08-29-2009, 08:14 AM
  3. Problem with function's pointer!
    By Tirania in forum C Programming
    Replies: 5
    Last Post: 11-28-2008, 04:50 AM
  4. Pointer in Function problem
    By lilhawk2892 in forum C++ Programming
    Replies: 7
    Last Post: 12-08-2007, 02:41 AM
  5. Replies: 4
    Last Post: 11-05-2006, 02:57 PM

Tags for this Thread