2D array as function parameter

This is a discussion on 2D array as function parameter within the C Programming forums, part of the General Programming Boards category; Hi I was trying to calculate the determinant of a 2X2 matrix. but the problem is passing in a 2D ...

  1. #1
    Registered User
    Join Date
    Aug 2008
    Posts
    4

    2D array as function parameter

    Hi
    I was trying to calculate the determinant of a 2X2 matrix. but the problem is passing in a 2D array. I know you need to give number of columns but in that it only declares 1D array. Follwing should make more sense.
    Code:
    double det(double M[][5],int i,int j)
    {
    	double det = 0;
    	if(i == j == 2)
    	{
    		det = M[i-2][j-2] * M[i-1][j-1] - M[i-1][j-2] * M[i-2][j-1];
    		return det;
    	}
    }
    I am calling this function by:
    Code:
    calcdet = det(Matrix,2,2);
    where Matrix is 2D array with values.

    Any help would be really appreciated.

  2. #2
    Registered User Kernel Sanders's Avatar
    Join Date
    Aug 2008
    Posts
    61
    Declare it as a 'double **' instead of a 'double [][]'

    The syntax for accessing the elements is identical

    I would also suggest returning -1 or something similar if the 'i==j==2' fails instead of letting the function terminate without a return (it will give junk back to the calling function)

  3. #3
    Registered User
    Join Date
    Aug 2008
    Posts
    4
    thanks for the reply i tried changing that but now am not getting the right answer. its always giving me -1 (else condition answer). here is my function:

    Code:
    double det(double **M,int i,int j)
    {
    	double det = 0;
    	if(i == j == 2)
    	{
    		det = M[i-2][j-2] * M[i-1][j-1] - M[i-1][j-2] * M[i-2][j-1];
    		return det;
    	}
    	else 
    		return -1;
    }
    and my main:
    Code:
    int main()
    {
    	int matsize,i=0,j=0;
    	double calcdet=0;
    	double Matrix [MAXSIZE][MAXSIZE]={{0}}; 
    	FILE* file = fopen("matrix.txt","r");
    	fscanf(file,"%d",&matsize);
    	for (i=0;i<matsize;i++)
    	{
    		for (j=0;j<matsize;j++)
    		{
    			fscanf(file,"%lf",&Matrix[i][j]);
    		}
    		j = 0;
    	}
    	fclose(file);
    	calcdet = det(&Matrix,2,2);
    	printf("%lf\n",calcdet);	
    	
    	system("PAUSE");
    	return 0;
    }
    I dont know what am doing wrong so if you can figure out my mistake i will be really thankful to you.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You very carefully avoided any mention of a problem. Fortunately I can guess that you think "i == j == 2" means something, when in fact it does not. (Well, it does; I forget whether == associates l-to-r or r-to-l; if it associates r-to-l, it means "i==1 && j==2", if it associates l-to-r, it means "false".)

    Note also that if you declare the array as double ** in your function prototype, you will not be able to use [bracket][notation] to access elements.

  5. #5
    Registered User
    Join Date
    Aug 2008
    Posts
    4
    okie i changed my if condition. Now if i cant use bracket notation then how can i access elements. initially i was trying to use M[][5] but that did not work either. any suggestions?

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Well, M[][5] does work, assuming MAXSIZE == 5. If not, it won't work, and it's not supposed to. (And even if it is, you should say M[][MAXSIZE] anyway.)

    Edit: Oh, and don't pass Matrix as &Matrix -- you had that right in the first post.

  7. #7
    Registered User
    Join Date
    Aug 2008
    Posts
    4
    thank you very much tabstop. works perfectly. i guess the whole problem was "i==j==2". good observation because it was tricky
    thank you again

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Kernel Sanders View Post
    Declare it as a 'double **' instead of a 'double [][]'
    Tabstop hinted at it: This is actually incorrect - for arrays that are not dynamically allocated [1], using double[][SIZE] is the correct way. The double ** form assumes that the argument is an array of pointers [if we keep the rest of the code in the function the same], and that the first pointer points to an array of double.

    A two dimensional array when passed to a function is not turned into an array of pointers to arrays of elements by the compiler. The compiler does the same thing for two dimensional arrays (or n-dimensional) as it does for single dimension arrays: It passes the address of the first element. In the case of n-dinensional arrays, all dimensions except the first one must be given so that the compiler can determine how to find each dimension from the index - because essentially, the array is just a flat piece of memory to the function being called.


    [1] It doesn't HAVE to be dynamically allocated, but it does have to be an array of pointers to double that is passed in, and those pointers should point to a suitable array of doubles.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. Calling a Thread with a Function Pointer.
    By ScrollMaster in forum Windows Programming
    Replies: 6
    Last Post: 06-10-2006, 09:56 AM
  3. const at the end of a sub routine?
    By Kleid-0 in forum C++ Programming
    Replies: 14
    Last Post: 10-23-2005, 07:44 PM
  4. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 11:03 AM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 06:06 PM

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