linking heapsort file and main file

This is a discussion on linking heapsort file and main file within the C Programming forums, part of the General Programming Boards category; I have a main.c file and a heap.c file. The heap.c file sorts data but it gets it's data from ...

  1. #1
    Registered User
    Join Date
    Dec 2009
    Posts
    5

    linking heapsort file and main file

    I have a main.c file and a heap.c file. The heap.c file sorts data but it gets it's data from a pre-set size in the main function. I am trying to have it so that when I compile with:

    Code:
     gcc -std=c99 -DRAND -DPRNT -DTYPE=(float | double) -D(BUBB | HEAP | INSR | MERG) *.c
    so for example:

    Code:
      gcc -std=c99 -DRAND -DPRNT -DTYPE=float -DHEAP *.c
    and run with:

    Code:
      ./a.out 34000
    It will use that input of 34000 with main.c and I believe pass the base address of the array, number of elements, size of each element, and the compare function into my heapsort function of heap.c

    The main.c file has an if statement to pass this:

    Code:
        #elif defined HEAP
        srtheap(a, nelem, sizeof(TYPE), compare);

    So basically I want to get rid of the main function in my heap.c file and have it run off of the main.c file instead, hope this makes sense. The main.c file cannot and should not be changed, only the heap.c file.


    main.c file:

    Code:
        #include <limits.h>
        #include <stdio.h>
        #include <stdlib.h>
        #include "srt.h"
        
        int compare(const void *, const void *);
        
        int main(int argc, char *argv[]) {
        
            int nelem = argc == 2 ? atoi(argv[1]) : SHRT_MAX;
        
            TYPE *a = calloc(nelem, sizeof(TYPE));
        
        #ifdef RAND
            for (int i = 0; i < nelem; ++i) {
        
                a[i] = (TYPE)rand() / RAND_MAX;
            }
        #else
            for (int i = 0; i < nelem; ++i) {
        
                a[i] = i;
            }
        #endif
        
        #if defined BUBB
            srtbubb(a, nelem, sizeof(TYPE), compare);
        #elif defined HEAP
            srtheap(a, nelem, sizeof(TYPE), compare);
        #elif defined INSR
            srtinsr(a, nelem, sizeof(TYPE), compare);
        #elif defined MERG
            srtmerg(a, nelem, sizeof(TYPE), compare);
        #else
            qsort(a, nelem, sizeof(TYPE), compare);
        #endif
        
        #ifdef PRNT
            for (int i = 0; i < nelem; ++i) {
        
                printf("%f\n", a[i]);
            }
        #else
            for (int i = 0; i < nelem - 1; ++i) {
        
                if (a[i] > a[i + 1]) {
        
                    printf("fail\n");
                    goto end;
                }
            }
        
            printf("pass\n");
        #endif
        
        end:
        
            free(a);
        
            return 0;
        }
        
        int compare(const void *p1, const void *p2) {
        
            if (*(TYPE *)p1 < *(TYPE *)p2) {
        
                return -5;
            }
            else if (*(TYPE *)p1 > *(TYPE *)p2) {
        
                return +5;
            }
        
            return 0;
        }
    heap.c file with main function:

    Code:
        #include <stdio.h>
        #include <stdlib.h>
        
        void swap(void* ap, void* bp, size_t s){
        	 int temp;
        	 temp = *(int*)ap;
        	 *(int*)ap = *(int*)bp;
        	 *(int*)bp = temp;
        }
        
        int compar(void* ap, void* bp){
           if(*(int*)ap < *(int*)bp){
        		return -5;			 
           }
           else if(*(int*)ap > *(int*)bp){
        		return 5;	 
           }
        	return 0;
        }
        
        void printarray(void* root, size_t nelem){
        	int i;  
        	for(i = 0; i < nelem; ++i){
        			printf(" %i ", *(int*)(root + 4*i));		
        	}
        	printf("\n\n");	
        }
        
        void shiftdown(void* root, void* base, size_t nelem, size_t size){
        	 int leftIndex = ((base - root)/size * 2 + 1);
        	 int rightIndex = ((base - root)/size * 2 + 2);
        	 void* left;
        	 void* right;
        	 
        	 
        	 
        	 if(leftIndex < nelem){
        				  left = leftIndex*size + root;
        	 }
        	 if(rightIndex < nelem){
        				   right = rightIndex*size + root;
        	 }
        	if(rightIndex < nelem){
        			if(compar(right, base) > 0){
        							 if(compar(left, right) > 0){
        							 swap(base, left, size);
        							
        							 }
        							 else{
        								swap(base, right, size);
        							
        								shiftdown(root, right, nelem, size);
        							  
        							 }
        			}		   
        	 }
        	 if(leftIndex < nelem){
        			if(compar(left, base) > 0){
        							swap(base, left, size);
        							
        							shiftdown(root, left, nelem, size);	  
        			}
        	 }
        
        	 return;
        	 
        }
        
        void heapify(void* root, void* base, size_t nelem, size_t size){
        	 int heap;
        	 heap = 1;
        	 int baseIndex = (base - root)/size;
        	 int leftIndex = ((base - root)/size * 2 + 1);
        	 int rightIndex = ((base - root)/size * 2 + 2);
        	 
        	 void* left;
        	  
        	 void* right;
        	 
        	 if(leftIndex < nelem)
        	  left = leftIndex*size + root;
        	  if(rightIndex < nelem)
        	  right = rightIndex*size + root;
        	 
        
        	 
        	 if(rightIndex < nelem){
        			if(compar(right, base) > 0){
        							 swap(base, right, size);
        							 heap = 0;
        			}		   
        	 }
        	 if(leftIndex < nelem){
        			if(compar(left, base) > 0){
        							swap(base, left, size);
        							heap = 0;	   
        			}
        	 }
        	 
        	
        	 
        	 if(baseIndex + 1 < nelem)
        	 heapify(root, base + size,nelem, size);
        	 
        	 if(heap == 0)
        	 heapify(root, root, nelem, size);
        	 
        	return; 
        }
        
        void srtheap(void* base, size_t nelem, size_t size){
        
        	 while(nelem > 1){
        		  swap(base, base + (nelem-1) * size, size);
        		  nelem--;
        		  shiftdown(base,base,nelem,size);
        		  
        	 }
        	 
        	 
        }
        
        int main(int argc, char *argv[])
        {
        	int SIZE = 34000;
        	int a[SIZE];
        	int i;
        	for( i = 0; i < SIZE;i++){
        		 a[i] = SIZE - i;	 
        	}
        	size_t size = 4;
        	size_t nelem = sizeof(a)/sizeof(a[0]);
        	void* base = malloc(sizeof(void));
        	base = (void*)a;
        	printarray(base, nelem);
        	
        	
        	srtheap(base, nelem, size);
        	
        	printarray(base, nelem);
        	printf("Sorted\n");
        	system("PAUSE");
        	return 0;
        }
    heap.c file without main function - doesn't work, not sure how to do it but the problem is with the srtheap() function.

    Code:
         #include <stdio.h>
         #include <stdlib.h>
               
        void swap(void* ap, void* bp, size_t s){
        	 int temp;
        	 temp = *(int*)ap;
        	 *(int*)ap = *(int*)bp;
        	 *(int*)bp = temp;
        }
        
        int compar(void* ap, void* bp){
           if(*(int*)ap < *(int*)bp){
        		return -5;			 
           }
           else if(*(int*)ap > *(int*)bp){
        		return 5;	 
           }
        	return 0;
        }
        
        void printarray(void* root, size_t nelem){
        	int i;  
        	for(i = 0; i < nelem; ++i){
        			printf(" %i ", *(int*)(root + 4*i));		
        	}
        	printf("\n\n");	
        }
        
        void shiftdown(void* root, void* base, size_t nelem, size_t size){
        	 int leftIndex = ((base - root)/size * 2 + 1);
        	 int rightIndex = ((base - root)/size * 2 + 2);
        	 void* left;
        	 void* right;
        	 
        	 
        	 
        	 if(leftIndex < nelem){
        				  left = leftIndex*size + root;
        	 }
        	 if(rightIndex < nelem){
        				   right = rightIndex*size + root;
        	 }
        	if(rightIndex < nelem){
        			if(compar(right, base) > 0){
        							 if(compar(left, right) > 0){
        							 swap(base, left, size);
        							
        							 }
        							 else{
        								swap(base, right, size);
        							
        								shiftdown(root, right, nelem, size);
        							  
        							 }
        			}		   
        	 }
        	 if(leftIndex < nelem){
        			if(compar(left, base) > 0){
        							swap(base, left, size);
        							
        							shiftdown(root, left, nelem, size);	  
        			}
        	 }
        
        	 return;
        	 
        }
        
        void heapify(void* root, void* base, size_t nelem, size_t size){
        	 int heap;
        	 heap = 1;
        	 int baseIndex = (base - root)/size;
        	 int leftIndex = ((base - root)/size * 2 + 1);
        	 int rightIndex = ((base - root)/size * 2 + 2);
        	 
        	 void* left;
        	  
        	 void* right;
        	 
        	 if(leftIndex < nelem)
        	  left = leftIndex*size + root;
        	  if(rightIndex < nelem)
        	  right = rightIndex*size + root;
        	 
        
        	 
        	 if(rightIndex < nelem){
        			if(compar(right, base) > 0){
        							 swap(base, right, size);
        							 heap = 0;
        			}		   
        	 }
        	 if(leftIndex < nelem){
        			if(compar(left, base) > 0){
        							swap(base, left, size);
        							heap = 0;	   
        			}
        	 }
        	 
        	
        	 
        	 if(baseIndex + 1 < nelem)
        	 heapify(root, base + size,nelem, size);
        	 
        	 if(heap == 0)
        	 heapify(root, root, nelem, size);
        	 
        	return; 
        }
        
        void srtheap1(void* base, size_t nelem, size_t size){
        
        	 while(nelem > 1){
        		  swap(base, base + (nelem-1) * size, size);
        		  nelem--;
        		  shiftdown(base,base,nelem,size);
        		  
        	 }
        	 
        	 
        }
        
        void srtheap(void *base, size_t nelem, size_t size, int (*compar)(const void *, const void *)) {
        	int a[size];
        	size_t size1 = 4;
        	nelem = sizeof(a)/sizeof(a[0]);
        	base = malloc(sizeof(void));
        	base = (void*)a;
        	printarray(base, nelem);
        	
        	
        	srtheap1(base, nelem, size1);
        	
        	printarray(base, nelem);
        	printf("Sorted\n");
        	system("PAUSE");
        	return;
        }

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,337
    This would be your srt.h file
    Code:
    #if defined BUBB
    void srtbubb(void *base, size_t nelem, size_t size, int (*compar)(const void *, const void *));
    #elif defined HEAP
    void srtheap(void *base, size_t nelem, size_t size, int (*compar)(const void *, const void *));
    #elif defined INSR
    void srtinsr(void *base, size_t nelem, size_t size, int (*compar)(const void *, const void *));
    #elif defined MERG
    void srtmerg(void *base, size_t nelem, size_t size, int (*compar)(const void *, const void *));
    #else
    // qsort is a standard function
    #endif
    Each of your bubb.c, heap.c etc would respectively contain
    Code:
    #include "srt.h"
    
    #if defined BUBB
    
    // any other support functions
    
    void srtbubb(void *base, size_t nelem, size_t size, int (*compar)(const void *, const void *)) {
        // implementation of bubble sort
    }
    #endif
    So that when you do this
    gcc -std=c99 -DRAND -DPRNT -DTYPE=float -DHEAP *.c

    You only get the code from main.c and heap.c compiled, and the contents of bubb.c are conditionally compiled out.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 11
    Last Post: 09-25-2011, 12:22 AM
  2. linking file stream with input file?
    By flamehead144 in forum C Programming
    Replies: 8
    Last Post: 02-07-2009, 07:55 PM
  3. Replies: 4
    Last Post: 07-06-2006, 02:53 AM
  4. Problem with linking a .dll file
    By Krankenstein in forum C++ Programming
    Replies: 2
    Last Post: 04-30-2003, 07:28 AM
  5. Linking functions from .ASM file
    By knutso in forum C Programming
    Replies: 5
    Last Post: 08-22-2002, 04:19 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21