Thread: Sorting two dimensional arrays

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    10

    Sorting two dimensional arrays

    Hello, i want to write a programme which will sort two dimensional arrays, from a[0][] to a[2][] ascending and a[3][] to a[5][] descending. My code is below but there is a mistake on output and i couldn't figure it out, thanks in advance..

    Code:
    #include <stdio.h>
    #define row 6
    #define col 6
    void bubbleSort(int[row][col],int,int,char);
    
    int main()
    {
    	int a[row][col]={{3,4,2,1,0,-1},{5,9,4,-5,-8,7},{8,2,6,5,1,0},{1,5,6,8,0,4},{-6,-9,-4,5,8,-7},{-8,-2,6,-5,1,0}},i,y;
    	char dec='a';
    	bubbleSort(a,1,3,dec);	
    	dec='d';
    	bubbleSort(a,4,6,dec);
    	for(i=0;i<6;i++)
    	{
    		for(y=0;y<col;y++)
    		{
    			printf("a[%d][%d]=%d\n",i,y,a[i][y]);
    		}
    	}
    	system("Pause");
    	return 0;
    
    }
    
    void bubbleSort(int x[row][col],int low,int high,char asc)
    {
    	int i,y,temp,a;
    	for(i=low-1;i<high;i++)
    	{
    		for(a=0;a<6;a++)
    		{
    			for(y=0;y<col;y++)
    			{
    				if(asc=='a')
    				{
    					if(x[i][y]>x[i][y+1])
    					{
    						temp=x[i][y+1];
    						x[i][y+1]=x[i][y];
    						x[i][y]=temp;
    					}
    				}
    				if(asc=='d')
    				{
    					if(x[i][y]<x[i][y+1])
    					{
    						temp=x[i][y+1];
    						x[i][y+1]=x[i][y];
    						x[i][y]=temp;
    					}
    				}
    			}
    		}
    	}
    }

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    6
    What exactly is wrong with the output? :x

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    10
    In output, the values are wrong.. For example a[2][] has only one 1 but in output it gives two 1 and it passes 8, like it in other arrays too...

  4. #4
    Registered User
    Join Date
    Jun 2009
    Posts
    486
    EDIT: ignore me, post was in error

  5. #5
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    Transpose the matrix.
    Sort each half of the rows like you want.
    Transpose it back to the original orientation.

    I think that's what you want.

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    10
    Finally i found, thanks for your replies..
    Last edited by ejjjjj; 05-30-2010 at 12:03 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 26
    Last Post: 06-11-2009, 11:27 AM
  2. Replies: 16
    Last Post: 01-01-2008, 04:07 PM
  3. Replies: 2
    Last Post: 02-23-2004, 06:34 AM
  4. two dimensional arrays
    By ssjnamek in forum C++ Programming
    Replies: 4
    Last Post: 05-01-2002, 08:12 PM
  5. sorting arrays
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 10-13-2001, 05:39 PM