Thread: Parallel arrays, dynamic memory allocation, and sorting arrays

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #8
    Registered User
    Join Date
    Dec 2007
    Posts
    5
    Code:
    #include <stdio.h>
    #include <string.h>
    
    #define MAXOPS 200
    #define MAXNAME 15
    
    void swap(int *index1, int *index2);
    void namesort(int n, int usenum, char list[], int sort[]);
    
    int main(void)
    {
    	int numop, i, j, k;
    	int length1, length2, minlen;
    	int sort[MAXOPS];
    	int score[MAXOPS];
    	char lastname[MAXOPS][MAXNAME];
    	char firstname[MAXOPS][MAXNAME];
    	char date[MAXOPS][4];
    	
    
    	FILE *ifile;
    
    	ifile = fopen("operatives.txt", "r");
    
    	numop = 0;
    	while (fscanf(ifile, "&#37;s %s %s %d", lastname[numop], firstname[numop], 
    			date[numop], &score[numop]) != EOF) {
    		numop++;
    	}
    	
    	for (i = 0; i < numop; i++){
    		sort[i] = i;
    	}
    	
    		
    	/* I'M TRYING TO MAKE A FUNCTION OUT OF THIS... */
    
    	/* for (i = 0; i < numop; i++){
    		for (j = numop - 1; i < j; --j){
    			length1 = strlen(lastname[j-1]);
    			length2 = strlen(lastname[j]);
    			if (length1 <= length2){
    				minlen = length1;
    			}else{
    				minlen = length2;
    			}
    			k = 0;
    			while (k < minlen){
    				if (lastname[sort[j-1]][k] > lastname[sort[j]][k]){
    					swap(&sort[j-1], &sort[j]);
    					k = minlen;
    				}else if (lastname[sort[j-1]][k] == lastname[sort[j]][k]){
    					k++;
    					if (k == minlen && length1 > length2){
    						swap(&sort[j-1], &sort[j]);
    					}
    				}else{
    					k = minlen;
    				}
    			}
    		}
    	} */
    	
    	namesort(numop, numop, lastname[0], &sort[0]);
    			
    	printf("|   FIRST NAME       |   LAST NAME        |  DATE  |   SCORE   |\n");
    	printf("================================================================\n");
    	
    	for (i = 0; i < numop; i++){
    		printf("|   %-17s", firstname[sort[i]]);
    		printf("|   %-17s", lastname[sort[i]]);
    		printf("|   %s  ", date[sort[i]]);
    		printf("|   %4d    |\n", score[sort[i]]);
    	}
    	
    	fclose(ifile);
    	
    	return(0);
    }
    
    
    void namesort(int n, int usenum, char list[], int sort[])
    {
    	int i, j, k;
    	int length1, length2, minlen;
    	for (i = 0; i < usenum; i++){
    		for (j = usenum - 1; i < j; --j){
    			length1 = strlen(list[j-1]);
    			length2 = strlen(list[j]);
    			if (length1 <= length2){
    				minlen = length1;
    			}else{
    				minlen = length2;
    			}
    			k = 0;
    			while (k < minlen){
    				if (list[sort[j-1]][k] > list[sort[j]][k]){
    					swap(&sort[j-1], &sort[j]);
    					k = minlen;
    				}else if (list[sort[j-1]][k] == list[sort[j]][k]){
    					k++;
    					if (k == minlen && length1 > length2){
    						swap(&sort[j-1], &sort[j]);
    					}
    				}else{
    					k = minlen;
    				}
    			}
    		}
    	}
    }
    
    void swap(int *index1, int *index2)
    {
    	int temp = *index2;
    	*index2 = *index1;
    	*index1 = temp;
    }
    Ok, so this is what I've got so far. I'm trying to make a function that will sort the arrays by name. But I'm getting several error messages at compile time--one at the length1/2 =... saying "passing arg of strlen makes pointer from integer without a cast"; then I get more error messages at
    Code:
    if (list[sort[j-1]][k] > list[sort[j]][k]){
    saying "subscript value is neither array nor pointer".

    Can anyone see what I'm doing wrong? My goal is to be able to have multiple sorting techniques. For instance, sort first by name, then by score; or by last name then first name. The idea being that if there's ever a "tie" between two values in the primary sorting method, I could call another function to resolve the tie.

    As always, your help is greatly appreciated!
    Last edited by sbaker1313; 01-01-2008 at 03:10 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 26
    Last Post: 06-11-2009, 11:27 AM