I need helo sorting 2 dimention arrays

This is a discussion on I need helo sorting 2 dimention arrays within the C Programming forums, part of the General Programming Boards category; Hi, I was working on sorting 2 dimension arrays in ascending order and need a little help. Here is my ...

  1. #1
    Banned
    Join Date
    Apr 2011
    Posts
    56

    I need helo sorting 2 dimention arrays

    Hi,
    I was working on sorting 2 dimension arrays in ascending order and need a little help.
    Here is my code:

    Code:
    #include <stdio.h>
    
    void bubbleSort(int [][]);
    void swap(int *,int *);
    
    int main()
    {
    int i; 
    char list[5][9] = {"tulip","marigold","petunia","rose","daisy"};
    for(i=0;i<5;i++) {
    printf("\n%10s", *(list + i) );
    }
    printf("\n\n");
    return 0;
    }
    
    void bubbleSort(int list[][]))
    {
    	int current,walker;
    		
    	for(current=0;current<MAX;current++){
    		for(walker=MAX-1;walker > current;walker--) {
    		    
    			if (list[walker] < list[walker-1]) {
    			   swap(&list[walker],&list[walker-1]); //swap
    			}
    
    		} 
    	}
    } 
    
    void swap(int *val1,int *val2)
    {
    	int temp;
    	temp = *val1;    
    	*val1 = *val2; 
    	*val2 = temp;    
    }
    can someone please help me with that? I am also suspecting the use of strcmp() and and strcpy() to copy one string to another, but now sure how.

    Thanks

  2. #2
    cas
    cas is offline
    Registered User
    Join Date
    Sep 2007
    Posts
    991
    Code:
    void bubbleSort(int [][]);
    You have to give all dimensions but the first, so:
    Code:
    void bubbleSort(int [][ENTRY_SIZE]);
    But... you've got an array of strings you're trying to sort, not an array of an array of ints. It looks like you took code to sort an array of ints, slapped in an array of strings, and expected it to work. You have to rewrite the sort function to expect such an array; the compare part to compare strings; and the swap part to swap strings.

    Plus you'll actually have to call your sort function for it to sort anything.

    Edit: I missed your comment about strcmp() and strcpy(). Do you know how these functions work? If not, consult your C book and get familiar with them before attempting a sort.
    Last edited by cas; 04-27-2011 at 07:28 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting a structure of arrays
    By Unregistered in forum C Programming
    Replies: 7
    Last Post: 03-15-2002, 10:45 AM
  2. help with sorting arrays
    By Matt in forum C Programming
    Replies: 1
    Last Post: 12-12-2001, 09:07 AM
  3. help with my bubble sorting of arrays
    By Matt in forum C Programming
    Replies: 1
    Last Post: 12-11-2001, 03:43 PM
  4. Sorting Arrays
    By Jax in forum C Programming
    Replies: 3
    Last Post: 11-11-2001, 11:35 AM
  5. sorting arrays
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 10-13-2001, 05:39 PM

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