matix

Discussion in 'C' started by harim, Apr 30, 2011.

  1. harim

    harim New Member

    Joined:
    Apr 18, 2011
    Messages:
    15
    Likes Received:
    0
    Trophy Points:
    0
    how can i copy the elements of two two-dimensional arrays in one dimensional array in descending order please reply me soon plz plz
     
  2. eriyer

    eriyer New Member

    Joined:
    Jan 22, 2011
    Messages:
    32
    Likes Received:
    0
    Trophy Points:
    0
    Code:
    #include <stdio.h>
    
    /* 2 methods are shown - copysort() is easier */
    void copysort();
    void searchcopy();
    
    int matrix_a[ 3 ][ 3 ] = { { 101, 47, 198 }, { 1024, 479, 1978 }, { 1801, 4437, 18 } }, 
    	matrix_b[ 3 ][ 3 ] = { { 1069, 427, 198 }, { 15249, 31479, 19078 }, { 18001, 37, 47 } },
    	array_sort[ 18 ];
    
    	
    void main()
    {
    	
    	searchcopy();
    	copysort();
    }
    
    void copysort()
    {
    	/* simply move all elements to single dim array and sort single dim array */
    	int i, j, maxindex;
    	
    	for( i = 0; i < 3; i++ )
    		for( j = 0; j < 3; j++ )
    			array_sort[ i * 3 + j ] = matrix_a[ i ][ j ];
    		
    	for( i = 0; i < 3; i++ )
    		for( j = 0; j < 3; j++ )
    			array_sort[ 9 + i * 3 + j ] = matrix_b[ i ][ j ];
    		
    	for( i = 0; i < 17; i++ )
    	{
    		for( j = maxindex = i; j < 18; j++ )
    			if( array_sort[ j ] > array_sort[ maxindex ] )
    				maxindex = j;
    		
    		if( maxindex != i )
    		{
    			j = array_sort[ i ];
    			array_sort[ i ] = array_sort[ maxindex ];
    			array_sort[ maxindex ] = j;
    		}
    	}
    	
    	for( i = 0; i < 18; i++ )
    	{
    		if( i % 9 == 0 )
    			printf("\n\t\t");
    		
    		printf("%5d ", array_sort[ i ]);
    	}
    	printf("\n\n");
    	
    }
    
    void searchcopy()
    {
    	/* while copying each element to single dim array search for largest element in both matrices combined
    	   and mark the copied element by setting a bit corr to the index of the element so that it is skipped
    	   in subsequent searches 
    	*/
    	
    	int destindex, srcindex, copied, srcrow, srccol, maxindex, maxval, srcval;
    	
    	for( destindex = 0, copied = 0; destindex < 18; destindex++ )
    	{
    		for( srcindex = 0, maxval = maxindex = -1; srcindex < 18; srcindex++ )
    		{
    			if( copied & (1 << srcindex) ) /* skip this element if already moved - see comment below */
    				continue;
    				
    			srcrow = (srcindex % 9) / 3;
    			srccol = srcindex % 3;
    
    			if( srcindex < 9 )
    				srcval = matrix_a[ srcrow ][ srccol ];
    			else
    				srcval = matrix_b[ srcrow ][ srccol ];
    			
    			
    			if( srcval > maxval || maxindex < 0 )
    			{
    				maxindex = srcindex;
    				maxval = srcval;
    			}
    		}
    		
    		array_sort[ destindex ] = maxval;
    		copied |= (1 << maxindex);  /* set bit corr to index to mark this matrix element for skipping in subsequent search */
    	}
    	
    	for( srcindex = 0; srcindex < 18; srcindex++ )
    	{
    		if( srcindex % 9 == 0 )
    			printf("\n\t\t");
    		
    		printf("%5d ", array_sort[ srcindex ]);
    	}
    	printf("\n\n");
    	
    	
    }
    
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice