Help passing nxn array to 2d array.

This is a discussion on Help passing nxn array to 2d array. within the C Programming forums, part of the General Programming Boards category; So I am new at C, and I am having trouble passing an NxN array to my determinant function. Can ...

  1. #1
    Registered User
    Join Date
    Jun 2009
    Posts
    2

    Help passing nxn array to 2d array.

    So I am new at C, and I am having trouble passing an NxN array to my determinant function. Can someone have a look at my code and tell me where i went wrong. trying to pass nxn array to 2d pointer.
    Code:
    /*Hrach Beglaryan
     */
    #include <stdio.h>
    #include <math.h>
    #include <stdlib.h>
    
    double Determinant(double **a,int n)
    {
       double **b= NULL;
       double det = 0;
       int i,j,k,p;
       
       
    
       if (n < 1) { //doesnt make sense
    
       } else if (n == 1) { //determinant is itself
          det = a[0][0];
       } else if (n == 2) {
          det = a[0][0]*a[1][1]-a[1][0]*a[0][1];
       } else {
          det = 0;
          for (k=0;k<n;k++) {
             b = malloc((n-1)*sizeof(double *));
             for (i=0;i<n-1;i++)
                b[i] = malloc((n-1)*sizeof(double));
             for (i=1;i<n;i++) {
                p = 0;
                for (j=0;j<n;j++) {
                   if (j == k)
                      continue;
                   b[i-1][p] = a[i][j];
                   p++;
                }
             }
             det += pow(-1.0,1.0+k+1.0) * a[0][k] * Determinant(b,n-1);
             for (i=0;i<n-1;i++)
                free(b[i]);
             free(b);
          }
       }
       return(det);
    }
    void Transpose(double **a,int n)
    {
       int i,j;
       int x,y;
       double z; //temporary allocation
    
       for (i=1;i<n;i++) {
          for (j=0;j<i;j++) {
             z = a[i][j];
             a[i][j] = a[j][i];
             a[j][i] = z;
          }
       }
    	for(x=0;x<n;x++)
    	for(y=0;y<n;y++)
    	printf("%d",a[x][y]);
    }
    
    int main()
    {
    	
    	int n=0;
    	int i=0,j=0;
    	double **c;
    	double a[n][n];
    	int x=0,y=0;
    	double determinant;	
    	printf( "\n///////////////////////////////////////////////////////////\n");
    	printf( "  		FUN WITH LINEAR ALGEBRA\n");
    	printf("/////////////////////////////////////////////////////////////");
    	printf("\nPlease Enter the dimension of the square matrix");
    	scanf("%d",&n);
    	
    	printf( "Please enter elements of the matrix");
    	{
    	for(x=0;x<n;++x)
    	for(y=0;y<n;++y)
    	{
    	printf("\nPlease enter the [%d][%d] element",x,y);
    	scanf("%d",&a[x][y]);
    	}
    	}
    	
    	for(i=0;i<n;i++)
    	for(j=0;j<n;j++)
    	for(x=0;x<n;x++)
    	for(y=0;y<n;y++)
    	c[i][j]=a[x][y]; //I get a segmentation fault here. I understand that it only lets me put my first element [0][0]. 
    
    	determinant=Determinant(c,n);
    	
    	printf("The Determinant of the matix is ", determinant);
    	Transpose(c,n);
    	
    	
    	
    }
    Last edited by beglaryanh; 06-06-2009 at 05:18 PM. Reason: left out details

  2. #2
    Registered User
    Join Date
    Jun 2009
    Posts
    2
    I'm sorry. I meant to say iim tryin to pass an nxn array to a 2d pointer.

  3. #3
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Quote Originally Posted by beglaryanh View Post
    I'm sorry. I meant to say iim tryin to pass an nxn array to a 2d pointer.
    That isn't going to work, because arrays and pointers are different.

    If I'm not mistaken, this is a possibility: Pass a pointer to the array (which is different than passing a pointer to the first element!). If you're using VLAs, you're hopefully using C99 as your standard. That will allow you to dynamically specify the size of the array that your pointer points to.

    I wouldn't really recommend this, though:

    Code:
    int n=0;
    ...
    double a[n][n];
    What is the purpose in this? Bleh.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. from 2D array to 1D array
    By cfdprogrammer in forum C Programming
    Replies: 17
    Last Post: 03-24-2009, 10:33 AM
  2. Replies: 1
    Last Post: 10-21-2007, 07:44 AM
  3. 2D array becoming "deallocaded"
    By Aaron M in forum C Programming
    Replies: 2
    Last Post: 09-23-2006, 07:53 AM
  4. question about multidimensional arrays
    By richdb in forum C Programming
    Replies: 22
    Last Post: 02-26-2006, 08:51 AM
  5. Passing a 2d array by Reference
    By loko in forum C Programming
    Replies: 8
    Last Post: 07-23-2005, 06:19 AM

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