Like Tree3Likes
  • 2 Post By laserlight
  • 1 Post By Micki-Zee

Transpose Matrix

This is a discussion on Transpose Matrix within the C Programming forums, part of the General Programming Boards category; Im busy trying to transpose my array but the code doesnt seem to be working...Can someone help me out please? ...

  1. #1
    Registered User
    Join Date
    Jun 2011
    Location
    Haifa, Israel, Israel
    Posts
    7

    Transpose Matrix

    Im busy trying to transpose my array but the code doesnt seem to be working...Can someone help me out please?

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <math.h>
    
    
    #define N 3
    
    
    
    int find_inner_product (int *a,int *b,int size)
    	
    {int inner_product=0,i=0;
    	for (i=0;i<size;i++)
    		
    		{inner_product=inner_product+ *(a+i)*(*(b+i));}
    		
    	return inner_product;} /*function to do the inner product*/
    
    int matrix_transpose(int a[][N],int size)
    	
    {int i=0,j=0,temp[N][N],transpose_matrix[N][N];
    	for	( i = 0; i<N; i++ ) 
    	{for( j = 0; j<N; j++ )
    		
    	{		a[i][j]=temp[i][j];
    		temp[i][j]=transpose_matrix[j][i];
    		}
    	}
    }
    
    int main(void)
    {
    	int matrix1[N][N],matrix2[N][N],i=0,j=0,inner_product=0,transpose_matrix[N][N];
    	
    	
    	printf("Please enter the values of the 3x3 Matrix: ");
    	
    	for	( i = 0; i < N; i++ ) 
    		{for( j = 0; j< N; j++ )
    		
    		{scanf("%d", &matrix1[i][j]);}
    			
    		} /*this for loop goes through every number and stores it in the first matrix*/
    		printf("\nPlease enter the values of the second 3x3 Matrix: ");
    	
    	for	( i = 0; i < N; i++ ) 
    	{for( j = 0; j< N; j++ )
    		
    		{scanf("%d", &matrix2[i][j]);}
    		} /*this for loop goes through every number and stores it in the second matrix*/
    
    	
    	inner_product=find_inner_product(matrix1,matrix2,N); /*calling the function to do the first row dot product*/
    	printf("\nThe inner product between the top two rows is %d",inner_product);
    	
    	for	( i = 0; i < N; i++ ) 
    	{for( j = 0; j< N; j++ )
    	transpose_matrix[i][j]=matrix_transpose( matrix1[N][N],N);
    	}
    	
    	for	( i = 0; i < N; i++ ) 
    	{for( j = 0; j< N; j++ )
    		printf("\n%d", transpose_matrix[i][j]);
    }
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,395
    I'll help with some cosmetic changes that make it easier to read:
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <math.h>
    
    #define N 3
    
    int find_inner_product (int *a, int *b, int size)
    {
        int inner_product = 0;
        int i = 0;
        for (i = 0; i < size; i++)
        {
            inner_product += a[i] * b[i];
        }
    
        return inner_product;
    }
    
    int matrix_transpose(int a[][N], int size)
    {
        int temp[N][N], transpose_matrix[N][N];
        int i = 0, j = 0;
        for (i = 0; i < N; i++)
        {
            for (j = 0; j < N; j++)
            {
                a[i][j] = temp[i][j];
                temp[i][j] = transpose_matrix[j][i];
            }
        }
    }
    
    int main(void)
    {
        int matrix1[N][N], matrix2[N][N], transpose_matrix[N][N];
        int inner_product = 0;
        int i = 0, j = 0;
    
        printf("Please enter the values of the 3x3 Matrix: ");
        for (i = 0; i < N; i++)
        {
            for (j = 0; j < N; j++)
            {
                scanf("%d", &matrix1[i][j]);
            }
        }
    
        printf("\nPlease enter the values of the second 3x3 Matrix: ");
        for (i = 0; i < N; i++)
        {
            for(j = 0; j < N; j++)
            {
                scanf("%d", &matrix2[i][j]);
            }
        }
    
        inner_product = find_inner_product(matrix1, matrix2, N); /* first row dot product */
        printf("\nThe inner product between the top two rows is %d", inner_product);
    
        for (i = 0; i < N; i++)
        {
            for(j = 0; j < N; j++)
                transpose_matrix[i][j] = matrix_transpose(matrix1[N][N], N);
        }
    
        for (i = 0; i < N; i++)
        {
            for (j = 0; j < N; j++)
                printf("\n%d", transpose_matrix[i][j]);
        }
    }
    ankit1703 and iMalc like this.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jun 2011
    Location
    Haifa, Israel, Israel
    Posts
    7
    The problem is with the transpose function,
    Code:
    int matrix_transpose(int a[N][N],int size)
    	
    {int i=0,j=0,temp[N][N],transpose_matrix[N][N];
    	for	( i = 0; i<N; i++ ) 
    	{for( j = 0; j<N; j++ )
    		
    	{		a[i][j]=temp[i][j];
    		temp[i][j]=transpose_matrix[j][i];
    		}
    	}
    Whats the problem?
    ankit1703 likes this.

  4. #4
    Registered User
    Join Date
    Jun 2011
    Location
    Haifa, Israel, Israel
    Posts
    7
    Ive refined it a tiny bit but i still cant seem to find the problem. Again, the transpose doesnt work!


    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <math.h>
    
    
    #define N 3
    
    
    
    int find_inner_product (int *a,int *b,int size)
    	
    {int inner_product=0,i=0;
    	for (i=0;i<size;i++)
    		
    		{inner_product=inner_product+ *(a+i)*(*(b+i));}
    		
    	return inner_product;} /*function to do the inner product*/
    
    int matrix_transpose(int a[N][N],int size)
    	
    {int i=0,j=0,temp[N][N],transpose_matrix[N][N];
    	for	( i = 0; i<N; i++ ) 
    	{for( j = 0; j<N; j++ )
    		
    	{		a[i][j]=temp[i][j];
    		temp[i][j]=transpose_matrix[j][i];
    		}
    	}
    }
    
    int main(void)
    {
    	int matrix1[N][N],matrix2[N][N],i=0,j=0,inner_product=0,transpose_matrix[N][N];
    	
    	
    	printf("Please enter the values of the 3x3 Matrix: ");
    	
    	for	( i = 0; i < N; i++ ) 
    		{for( j = 0; j< N; j++ )
    		
    		{scanf("%d", &matrix1[i][j]);}
    			
    		} /*this for loop goes through every number and stores it in the first matrix*/
    		printf("\nPlease enter the values of the second 3x3 Matrix: ");
    	
    	for	( i = 0; i < N; i++ ) 
    	{for( j = 0; j< N; j++ )
    		
    		{scanf("%d", &matrix2[i][j]);}
    		} /*this for loop goes through every number and stores it in the second matrix*/
    
    	
    	inner_product=find_inner_product(matrix1,matrix2,N); /*calling the function to do the first row dot product*/
    	printf("\nThe inner product between the top two rows is %d",inner_product);
    	
    	matrix_transpose(matrix2,N);

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Code:
    int matrix_transpose(int a[N][N],int size)
    	
    {int i=0,j=0,temp[N][N],transpose_matrix[N][N];
    	for	( i = 0; i<N; i++ ) 
    	{for( j = 0; j<N; j++ )
    		
    	{		a[i][j]=temp[i][j];
    		temp[i][j]=transpose_matrix[j][i];
    		}
    	}
    }
    1) You need to pass in the array as a pointer, not as an array definition.
    2) using a[N] does not give you access to N inside the function.
    3) transposing (swapping) values requrires 3 steps not 2... save first value, move second value to first, copy saved value to second.
    4) When calling the function you correctly passed in the size, but totally ignored it within the function.
    5) your temp variable does not need to be an array
    6) the function should not call itself... let the loops do their work.

  6. #6
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by CommonTater View Post
    1) You need to pass in the array as a pointer, not as an array definition.
    What the OP has in this respect is fine.

    6) the function should not call itself... let the loops do their work.
    It doesn't. You've confused the name of the function with one of its variables.

    2) using a[N] does not give you access to N inside the function.
    N is a #define. Again, what the OP has there is fine.

    3) transposing (swapping) values requrires 3 steps not 2... save first value, move second value to first, copy saved value to second.
    5) your temp variable does not need to be an array
    This is the real problem, or at least hints at it (the swap is done incorrectly). In fact, everything is being set to the random uninitialized values in temp[][] and transpose_matrix.[][]:

    Code:
    	int i=0,j=0, temp[N][N], transpose_matrix[N][N];
    	for	( i = 0; i<N; i++ ) {
    		for( j = 0; j<N; j++ ) {
    			a[i][j]=temp[i][j];
    			temp[i][j]=transpose_matrix[j][i];
    		}
    	}
    After that, all arrays will be garbage. If Mickie-Zee can tell us what the output is compared to the input, that will help confirm the nature of the problem.
    Last edited by MK27; 07-01-2011 at 06:13 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  7. #7
    Registered User
    Join Date
    Jun 2011
    Location
    Haifa, Israel, Israel
    Posts
    7
    What should my three lines of code look like to save tranpose it? The three steps?
    All i have at the moment is:
    Code:
    int matrix_transpose(int a[N][N],int size)
    	
    {int i=0,j=0,temp=0,transpose_matrix[N][N];
    	for	( i = 0; i<N; i++ ) 
    	{for( j = 0; j<N; j++ )
    		
    	{	temp=a[i][j];
    		temp=transpose_matrix[i][j];
    		}
    	}
    }

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    So you've got a red cup full of water, a blue cup full of tea, and an empty green cup. How would you get the red cup full of tea and the blue cup full of water?

    Also: you are currently transposing your matrix twice (once for (i,j) and once for (j,i)), hence when you do get your swap working you won't get the answer you want, since if you swap everything twice it ends up exactly where it was at the beginning. Once you have put the first row into the first column, then you shouldn't mess with the first column when you do the next row.

  9. #9
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Micki-Zee View Post
    What should my three lines of code look like to save tranpose it? The three steps?
    Code:
    temp = a;        // save first value
    a = b;          // copy transposed value
    b = temp;       // copy saved value

  10. #10
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,292
    Quote Originally Posted by Micki-Zee View Post
    Again, the transpose doesnt work!
    You're so far very poor at describing the problem. If you were booking your car in at a garage over the phone and they asked what you wanted them to look at would you simply say that it "doesn't work"?!

    You should always tell us what actually happens. "doesn't work" prety much covers ever possible thing that could be wrong. E.g. won't compile, wont run, crashes, does not call the function, does not change the data, turns the data into garbage, produces the wrong answer etc etc, the list goes on... I think I've made my point.

    I hope to never near those two words together from you again.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Micki-Zee View Post
    The problem is with the transpose function,
    ...
    Whats the problem?
    You tell me. You're the one trying to get it to work. What's wrong with it? Ask Questions The Smart Way!


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Matrix Transpose
    By onako in forum C++ Programming
    Replies: 2
    Last Post: 07-30-2010, 05:47 AM
  2. Transpose matrix
    By ineedmunchies in forum C Programming
    Replies: 4
    Last Post: 12-08-2009, 08:09 AM
  3. transpose matrix?
    By tmoney$ in forum C Programming
    Replies: 4
    Last Post: 05-03-2003, 03:51 PM
  4. transpose matrix
    By tmoney$ in forum C Programming
    Replies: 1
    Last Post: 05-03-2003, 02:31 PM
  5. transpose of matrix
    By Kohatian 3279 in forum C++ Programming
    Replies: 1
    Last Post: 03-19-2002, 03:59 PM

Tags for this Thread


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