Thread: return(array), 2 dimensional arrays to functions.

  1. #1
    Registered User nyekknyakk's Avatar
    Join Date
    Aug 2010
    Posts
    35

    2 dimensional arrays to functions

    i have a problem involving arrays in functions. i don't know how to declare functions with 2-dimensional arrays as its parameter and return the values of the 2-dimensional arrays.
    here is my code:
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    int mat_A[50][50], mat_B[50][50];
    int disp_mat(int mat_A[50][50], int mat_B[50][50]);
    
    main()
    {
    	/*main function - asks for contents of mat_A and mat_B from user*/
    }
    
    int disp_mat(int mat_A[50][50], int mat_B[50][50])
    {
    	printf("Matrix A: \n");
    	printf("Number of rows: %d \n", r1);
    	printf("Number of columns: %d \n", c1);
    	    
    	for(i=0;i<r1;i++){
    		for(j=0;j<c1;j++)
    			printf("|%d|", mat_A[i][j]);
    			printf("\n"); }
    
    	printf("Matrix B:");
    	printf("Number of rows: %d \n", r2);
    	printf("Number of columns: %d \n", c2);
    			
    	for(i=0;i<r2;i++){
    		for(j=0;j<c2;j++)
    			printf("|%d|", mat_B[i][j]);
    			printf("\n"); }
    
    	return(mat_A[50][50], mat_B[50][50]);
    }
    i know its wrong. please help me figure out how to work with functions involving 2 dimensional arrays.

    thanks!
    Last edited by nyekknyakk; 08-18-2010 at 11:04 PM.

  2. #2
    Registered User
    Join Date
    Jul 2010
    Posts
    26
    You declared get_cmd as a function that is supposed to return integer
    Code:
    int get_cmd() /*Return type is int*/
    {
         ....
         ....
        return(array); /*array is not an int*/
    }
    But then you tried to return an array from it. That's what the compiler is complaining about.

  3. #3
    Registered User nyekknyakk's Avatar
    Join Date
    Aug 2010
    Posts
    35
    qwertylurker, i already fixed that problem. i seem to have a new problem now:
    please read my thread again, it has been edited!
    Last edited by nyekknyakk; 08-18-2010 at 11:07 PM.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You cannot return two variables from a function, period. You can put them in a struct if you really must and return an instance of the struct.
    However, even better is to take pointers to the buffers to store the data in. Also, main lacks a return type.
    And why are the variables global when you take them by parameters? Move them into main.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Jul 2010
    Location
    Oklahoma
    Posts
    107

    Thumbs up

    Elysia,

    I like that a lot:

    Quote Originally Posted by Elysia View Post
    You cannot return two variables from a function, period.
    It would show when potential colleagues would show up in a programming lab before having a firm grasp on Algebra....

    A function is indeed a mapping from a space X to another space Y. These spaces maybe vectors, as you suggested, wrapping the "output" in a structure. But in order for any general relation (hint hint) to be a function it must indeed map from at least one member of the domain to exactly one member of the co-domain. These are considered one-to-one functions, although one may further constrain a relation so that it is onto as well -- thus guaranteeing a one-to-one inverse function. It becomes a rather involved topic in Abstract Algebra, and it is a rather useful topic in Linear Algebra among others.

    Nyekknyakk, I think you're bouncing the problem around really well -- again, keep it up. C-functions always return at most one result, be it an aggregate type (structure of some sort) or an atomic value (an integer for example). Their side-effects however can change many variables. Actually, the pointer (or reference) parameters have been referred to as in/out parameters by some of my colleagues because a function that takes them as a non-constant parameter can change their value without notice.

    Best Regards,

    New Ink -- Henry

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Linear algebra is hardly needed to start programming.
    Usually a linear mapping takes exactly one matrix as input (since vectors are really a matrices with one column, they are matrices too) and one matrix as output. A function can take any number of input values.
    Furthermore, a function does not have to be one-on-one. It can be linearly dependent, or linearly independent as you wish. The only restriction is that it can only return one and only one value. That is, it can only output one "transformed" matrix, instead of multiple.
    But contrary to some linear algebra, you cannot simply take two vectors (arrays) and put them in a column fashion to create a matrix. C doesn't work that way. You have to create a struct, ro your matrix, if you will, and then fill up the vectors of the struct (or matrix). Then return that struct (or matrix).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    As you know, when you pass array to function, it decays to pointer and you can modify array contents. (i.e arrays are not passed by value)
    Why do you still need to return value?

  8. #8
    Registered User nyekknyakk's Avatar
    Join Date
    Aug 2010
    Posts
    35
    Hi! So you're saying that i don't need to return a value anymore?
    What if my program runs like this:
    First, it asks the user for the matrices, then goes adds the matrices, then displays them.
    All those three, aking a matrix, adding a matrix and displaying a matrix, are different funcions.

  9. #9
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    You can just do:
    Code:
    // prototypes
    void read_matrix( int matrix_a[50][50], int matrix_b[50][50]);
    void add_matrix( int matrix_a[50][50] , int matrix_b[50][50], int result_matrix[50][50]);
    void display_matrix( int matrix[50][50] );
    
    // usage..
    read_matrix( mat_A,mat_B );
    add_matrix(mat_A,mat_B, result);
    display_matrix( result );

  10. #10
    Registered User nyekknyakk's Avatar
    Join Date
    Aug 2010
    Posts
    35
    Hi, so how about the number of rows and columns? it is also defined by the user, too. is it not needed to be in the parameters anymore?
    and also what are the return values for each of the functions? or are return values still needed?
    what if my program is looping? the results may be wrong if there is no return values, right?

  11. #11
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    If row and column are defined by user, you need to do memory allocation.
    And I'd suggest to use struct to store your matrix information.
    eg
    Code:
    struct matrix {
       int **matrix;
       int row;
       int col;
    };
    Question 6.16

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Array of functions
    By frktons in forum C Programming
    Replies: 29
    Last Post: 06-30-2010, 09:51 AM
  2. entry level homework help: Passing Arrays into functions.
    By DHart07 in forum C++ Programming
    Replies: 8
    Last Post: 04-21-2010, 09:11 AM
  3. functions and arrays
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 03-14-2002, 09:57 AM
  4. 2D arrays with functions made easy
    By goran in forum C Programming
    Replies: 1
    Last Post: 09-17-2001, 12:08 PM
  5. elements of arrays; functions
    By sballew in forum C Programming
    Replies: 6
    Last Post: 09-03-2001, 01:48 AM