0
int checkdiag(int arr[][100], int size)
{
    int h=0,i,j,count=0;
    for(i=0;i<size;i++)
        for(j=0;j<size;j++)
            if(arr[i][j]==arr[++i][++i])
                count++;
    if(count=size)
        h=1;        
    return(h);
}

This function is supposed to check my 2D array to see if all the diagonals are the same value. The maximum array size is 100 by 100.

int 
main(void)  
{
    int r,c,**m,i,j;  
    FILE*in;
    in=fopen("matrix.txt","r");
    fscanf(in,"%d",&r);
    c=r;
    m=(int**)malloc(r*sizeof(int*));

    for(i=0;i<r;i++)
        m[i]=(int*)malloc(c*sizeof(int));

    for(i=0;i<r;i++)
    {   
        for (j=0; j<c; j++)
        {
            fscanf(in,"%d",&m[i][j]);
        }
    }
    fclose(in);
    checkdiag(m,r)
    return(0);
}
derekerdmann
  • 17,696
  • 11
  • 76
  • 110
Danny Vo
  • 1
  • 1
  • what were you trying to do? – Debosmit Ray Apr 12 '16 at 16:16
  • I have a 2D array that is from a data file and I need to write a function to determine if all the values of the diagonal are the same value. – Danny Vo Apr 12 '16 at 16:26
  • Can you show the code where you actually try to pass a 2D array to that function? – Matt C Apr 12 '16 at 16:28
  • No, I need more than that. Show the code where you define m and r as well, and include that in your question. – Matt C Apr 12 '16 at 16:32
  • BTW with "all the diagonals are the same value" do you mean all the elements in the diagonal or do you really want to check if all the (199) diagonals have only 199 distinct values? like in { {1,2,3},{4,1,2},{5,4,1}}? – Bob__ Apr 12 '16 at 16:44
  • just the large diagonal from first element on left to last element on the right – Danny Vo Apr 12 '16 at 16:52

3 Answers3

2

To pass a 2D as a function parameter in C, you need to specify the length of the array, and the length of each array element.

The code you have provided in your question, should work as long if you are using C99. Otherwise, you have to specify the length and length of each array element.

You can not simply specify the array length and element's length with variables without declaring those variables -

void array_function(int array[][n]);  //Wrong!
void array_function(int array[m][]);  //Wrong!
void array_function(int array[m][n]); //Wrong!

You can however, also accept those variables as arguments, or hardcode your array and array element's length -

void array_function(int n, array[][n])         //Correct!
void array_function(int m, int n, array[m][n]) //Correct!

void array_function(int m, array[m][100])      //Correct!
void array_function(int n, array[100][n])      //Correct!

void array_function(int array[100][100]);      //Correct!
void array_function(int array[100][100]);      //Correct!

Accepting arrays of varying length -

To pass in a function with varying size (both length and element's length), you need to specify what the length and element's length are when you pass the array to the function. So your function prototype would look like this:

void array_function(int m, int n, array[m][n]) //Correct!

With the C99 standard, you can leave out the first size variable, and allow for arrays of unknown length, but each array in that array has the same number of elements -

void array_function(int m, array[][m]) //Correct, with C99!  

But with your code, I'm not sure why you've hardcoded the value 100 in your function. If you plan on taking in arrays and doing something with their diagonals, I am assuming you will only be given square matrices. You should write your function like so:

void array_function(int size, array[size][size])

OR

void array_function(int size, array[][size])

I recommend the first option, as it makes it clear that you are accepting square matrices.

Community
  • 1
  • 1
Matt C
  • 4,470
  • 5
  • 26
  • 44
  • 1
    It turns out OP [didn't declare a 2D array](http://stackoverflow.com/questions/7586702/is-2d-array-a-double-pointer)... – Bob__ Apr 12 '16 at 17:12
1

Multiple issues:

First, the type of m in main does not match the type of arr in checkDiag; int ** is not compatible with int [][100] (which is shorthand for int (*)[100]).

If you want the types to match up, declare m as follows:

int (*m)[100]; // m is a *pointer* to a 100-element array of int

Then, when you allocate m, do the following:

m = malloc( r * sizeof *m ); // allocates space for an r x 100 array of int

The advantage of this method is that you allocate the entire array in one operation, and the rows are guaranteed to be contiguous.

Note that if r isn't 100, you won't be allocating a "square" matrix. This shouldn't be that much of an issue for what you're doing, though, because you're only reading and checking r by r elements. If you want to be a stickler, though, you could do this:

 int r;
 // get value of r
 int (*m)[r] = malloc( r * sizeof *m );

This dynamically allocates an r x r array of int. We're using variable-length array syntax (that is, using the variable r to define the array size). Note that variable-length arrays were introduced with C99 and made optional in C2011; you'll want to make sure your implementation supports them1.

In this second case, you'll want to change the prototype of checkDiag from

int checkDiag( int arr[][100], int size )

to

int checkDiag( int size, int arr[][size] )

size must be declared before it can be used in a VLA declaration.

In either case, you'd simply call free( m ); to deallocate the array when you're done.

The second major issue is this line in checkDiag:

 if(arr[i][j]==arr[++i][++i])

This line invokes undefined behavior; you're updating the value of i multiple times between sequence points, and you're trying to both use the old and incremented values in the same expression (even then, it doesn't make sense logically). Try something more like the following:

for ( i = 0; i < size - 1; i++ )
{
  if ( arr[i][i] == arr[i+1][i+1] )
    count++;
}

The final issue is the line

if(count=size)

You want to use == instead of = here; you're just assigning size to count, and since the result is non-zero, setting h to 1. Change that to

if ( count == size )
  h = 1;

Alternately, you could ditch the h variable altogether and just write

return count == size;


1. To do this programmatically, look for the feature macro __STDC_NO_VLA__, like so:
#if defined( __STDC_NO_VLA__ )
#error "VLAs are not available for this implementation!"
#endif

John Bode
  • 119,563
  • 19
  • 122
  • 198
0

Not sure what you want, but I assume you want to check if all elements in the main diagonal are the same. In this case the 2D array has to be the same size in both dimensions. I dont know what language you are using but this code should do approximately what you want:

int checkdiag(int** arr, int size)
{
    int d=arr[0][0];
    for(int i=1;i<size;i++)
       if(arr[i][i]!=d)
           return 0;     
    return 1;
}

If all diagonal elements are the same the return value is 1 otherwise it is 0.

To check if all diagonales are the same, you have to do the procedure with each diagonal.

Edit: Then you can call the function this way:

int** arr;
int size = 100;
arr=(int**)malloc(size*sizeof(int*));
for(int i=0;i<size;i++)
    arr[i]=(int*)malloc(size*sizeof(int));

//fill array with values
int ret = checkdiag(arr,size);

Edit2: Sice OP shared more information, I updated checkdiag to work with pointers.

RomCoo
  • 1,868
  • 2
  • 23
  • 36
  • I get this error: passing argument 1 of 'checkdiag' from incompatible pointer type. – Danny Vo Apr 12 '16 at 16:21
  • 1
    Maybe it's better: `int d = arr[0][0]; for( int i = 1; i < size; i++ ) { if ( arr[i][i] != d ) return 0; } return 1;`... – Bob__ Apr 12 '16 at 16:57
  • The problem isn't necessarily with logic or the algorithm, it's specific to the C programming language. – Matt C Apr 12 '16 at 16:58
  • @Bob__ thats true. I will change it. – RomCoo Apr 12 '16 at 17:01
  • Considering that OP declared arr as a pointer to pointer to int (second snippet posted) [not as a 2D array](http://stackoverflow.com/questions/7586702/is-2d-array-a-double-pointer), the best signature is `int checkdiag(int **arr, int size)`. Note, I'm not blaming you. – Bob__ Apr 12 '16 at 17:06
  • @MatthewCliatt the problem is BOTH the logic (wrong) and the signature (mismatch) OP used for the function. Having a program that compiles but doesn't do what expected isn't much a success. – Bob__ Apr 12 '16 at 17:21
  • @Bob__ You are right, I just wanted to point out that this answer didn't address OP's problem of not being able to pass his array to the function. – Matt C Apr 12 '16 at 17:23
  • @Bob__ Thanks for info – RomCoo Apr 12 '16 at 17:47