Thread: Passing an Array

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    7

    Passing an Array

    I'm having trouble transferring my transposed array to my print subroutine. I could really use some help.

    Code:
    #include <stdio.h>
    
    float transposeMatrix (int input_row, int input_col, float input_matrix[][20])
    {
    	int row;
    	int col;
    	float tran_matrix[20][20]={0};
    	
    	for (row=0; row<=input_col; row++)
    	{
    		for (col=0; col<=input_row; col++)
    		{
    			(tran_matrix[row][col])=(input_matrix[col][row]);
    		}
    	}
    	return tran_matrix;
    
    }
    
    
    
    void printMatrix (int input_row, int input_col, float input_matrix[][20]) 
    {
    	int row;
    	int col;
    	int upcol;
    	
    
    	for (row=0; row<input_row; row++)
    	{
    	printf("|--------|");
    	
    		for (upcol=1; upcol<input_col; upcol++)
    		{
    		printf("--------|");
    		}
    
    		printf("\n \n");
    
    		for (col=0; col<input_col; col++)
    		{
    			printf("| ");
    			printf("%+6.1f ", input_matrix[row][col]);
    		}
    		printf("| \n \n");
    	}
    	printf("|--------|");
    	
    	for (upcol=1; upcol<input_col; upcol++)
    	{
    		printf("--------|");		
    	}
    
    	printf ("\n");
    
    }
    
    
    int main (void)
    {
    	int user_row;
    	int user_col;
    	int row;
    	int col;
    	float input_array[20][20]={0};
    	float t_matrix={0};
    	int rows;
    	int columns;
    
    	// Scans for user input of rows and columns.
    	printf ("Number of Rows: ");
    	scanf ("%d", &user_row);
    	printf ("Number of Columns: ");
    	scanf ("%d", &user_col);
    
    	// Scans for user input of number entry. 
    	for (row=0; row<user_row; row++)
    	{
    		for (col=0; col<user_col; col++)
    		{
    			scanf ("%f", &input_array[row][col]);
    		}
    
    	}
    
    	printf("Original Matrix: \n");
    	printMatrix ( user_row, user_col, input_array);
    
    	t_matrix=(transposeMatrix( user_row, user_col, input_array));
    	
    	printf("Transposed Matrix: \n");
    	printMatrix (user_row, user_col, t_matrix);
    
    
    }
    The final line is where I'm having trouble.

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    t_matrix is actually a float **. You'll have to change your transposeMatrix() function definition so that it returns that type and then change your definition of t_matrix from float to float ** in main().
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User
    Join Date
    Sep 2010
    Posts
    7
    I really don't understand what your trying to tell me to do. Could you be a little more specific to help me out some more?

  4. #4
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Code:
    float transposeMatrix (int input_row, int input_col, float input_matrix[][20])
    ... becomes ...
    Code:
    float **transposeMatrix (int input_row, int input_col, float input_matrix[][20])
    And I bet you can figure out how to redefine t_matrix in main() now...
    If you understand what you're doing, you're not learning anything.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    See answers in your other thread on the same subject.

    > ... becomes ...
    > float **
    Er - no - TWICE!
    1. It's the wrong type, ** is not a pointer to a 2D array.
    2. It would be a pointer to a local variable anyway.

    I'm shocked
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Quote Originally Posted by Salem View Post
    See answers in your other thread on the same subject.

    > ... becomes ...
    > float **
    Er - no - TWICE!
    1. It's the wrong type, ** is not a pointer to a 2D array.
    2. It would be a pointer to a local variable anyway.

    I'm shocked
    Oops! Heh. That's what I get for posting drunk.
    If you understand what you're doing, you're not learning anything.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. problem passing to array
    By helg in forum C Programming
    Replies: 5
    Last Post: 09-27-2010, 02:45 PM
  2. Passing Argument from incompatible pointer type
    By AmritxD in forum C Programming
    Replies: 3
    Last Post: 08-15-2010, 03:23 PM
  3. Passing a dynamic array to a function
    By esmeco in forum C Programming
    Replies: 15
    Last Post: 06-05-2010, 04:25 PM
  4. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  5. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 06:58 PM