I'm trying to sum the elements in a 2D array but I keep getting errors. and I can't figure out what it is. Help would be appreciated, Thanks.

Code:
```#include <stdio.h>
#define MAXROW 10
#define MAXCOL 10

int matrix[MAXROW][MAXCOL]={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20},row,col;

int main(void)
{
return 0
}

{
for (i=0;i<MAXROW;++i)
for (j=0;J<MAXCOL;++j);
sum+=matrix[i][j];
printf("\nsum=%d",sum);
return 0
}```

2. int matrix[MAXROW][MAXCOL]={1,2,3,4,5,6,7,8,9,10,11,12
,13,14,15,16,17,18,19,20}
That is not the correct way to initialize a two dimensional array. You want:

Code:
```signed int array[3][3] =
{
{ 1, 2, 3 },
{ 4, 5, 6 },
{ 7, 8, 9 }
};```
Consider a multiple deminsion array as an array of arrays (of arrays of ... ). Thus, you have an array of arrays of integers. As such, you should be initializing each array in turn, as per above. It's the RightThing(TM) to do.

Here is a problem:
for (j=0;J<MAXCOL;++j);
Your loop in effect does nothing at all because you're telling it not to by having that semi-colon there.

It's always the little things.

On a side note, your array has a total of 100 cells, not 20 as it appears you think it does.

The size of the array is: dimension1_size * dimension2_size * ...

Quzah.

3. Thanks I'll delete that semicolon.

I know that the array has 100 cells, that's how I'm supposed to be doing it for my work. But my teacher didn't specify that she wanted the program to have code to fill the array, so I just added some values so that there would be something to test with. And according to my C book, with the values i entered, it should be a 2x10 array

4. I keep getting an error that says illegal implicit conversion from 'int' to 'int[*][10]' and I have no idea what that means

The program stops where i try to call the addition function from the main

5. Originally posted by CXHatchback
And according to my C book, with the values i entered, it should be a 2x10 array
The array size is as you declared it. It's 10 by 10. The values you've entered will fill the first twenty elemets.

Either way is supported by the standard. It's usually best to use the method I described for readability. You'll definately want to use this method if you start using more dimensions than 2.

Quzah.

6. Originally posted by quzah
The values you've entered will fill the first twenty elemets.
That's what I meant. Any idea on the task at hand?

7. Originally posted by CXHatchback
I keep getting an error that says illegal implicit conversion from 'int' to 'int[*][10]' and I have no idea what that means

The program stops where i try to call the addition function from the main
You are calling the function incorrectly:
Should be:

Otherwise what you're doing is passing it matrix[10][10], which is outside of the bounds of the array itself.

Quzah.

8. Wow thanks that really helped a lot. The major problem I'm having is that my teacher gave us the function prototypes but I don't understand them. There's too many variables listed in the parameter type list.

9. Originally posted by CXHatchback
Wow thanks that really helped a lot. The major problem I'm having is that my teacher gave us the function prototypes but I don't understand them. There's too many variables listed in the parameter type list.
I can understand the feeling but then most times they are trying to cram something in to your brain. I though the same way with most of my teachers stuff... oh course I got sick and tired of being forced to use there buggy functions to read input. so I replaced it with my own. (worked better too).

Generaly, I would say trust your teachers. Unless they totaly get the defenations of printf() wrong.. then run

10. haha yeah that would be scary. I always thought it was wierd that a computer class was not taught in a computer lab, but this semester wasn't that bad. My teacher also has a very thick accent. It is obvious that she knows what she's doing, although she does make some careless mistakes, but she has a hard time explaining them to the class because english wasn't her first language. For the most part I'm doing pretty good since I enjoy programming and catch on pretty quick (the basic stuff anyway).

11. There is a difference between a function prototype / definition and the actual call of it. This is especially true when using pointers and arrays.
Code:
`void function( int );   /* A prototype. */`
Here we've prototyped a function that will take an integer, and will return void.

When we create the function itself, we actually have to name the variable so we can use it in the fuction. In the prototype, you can omit names, or include names if you wish for the variables.

Prototypes are optional. Definitions are not.

The function definition:
Code:
```void function( int myint )
{
printf("This is myint: %d.\n", myint );
}```
Now with arrays, it gets trickier.

void myfun( int array[] ); /* valid */
void myfun( int array[SIZE] ); /* valid */
void myfun( int array[][SIZE] ); /* valid */
void myfun( int array[SIZE][SIZE] ); /* valid */
void myfun( int array[SIZE][] ); /* invalid */

The final one is invalid, because you must specify the last dimension size in a multi-dimension array.

In any case, when you're passing an entire array to a function that accepts an array, you only ever pass it the name. (Or a spot in the array for trickier stuff, but basicly ignore that for now.)

There's more to it, but I'll call it good for now.

Quzah.

12. Originally posted by quzah
void myfun( int array[SIZE][] ); /* invalid */

The final one is invalid, because you must specify the last dimension size in a multi-dimension array.
Nice write-up, thanks. Now I know why she gave us:

int addition (int matrix[][MAXCOL], int row, int col);

13. Originally posted by quzah
void myfun( int array[] ); /* valid */
void myfun( int array[SIZE] ); /* valid */
void myfun( int array[][SIZE] ); /* valid */
void myfun( int array[SIZE][SIZE] ); /* valid */
void myfun( int array[SIZE][] ); /* invalid */
[/B]
If you have covered pointers then you can see that a an array is really nothing more then a pointer and an offset.

One thing I like to do is take an array pass it as a pointer then acces it with pointer math.

IE
void myfun(int *array)

that way you can access the array by pointer math. It can get complex especially if array is a multidimenional

say you want to access array[9]
with a pointer you would do this
*(array + 8). C will move the pointer 8 memory blocks from array. The exact movement depends on what type array is and what the size of different variable types are.

Now a mulit dimention array would be like this

*(array + (rows*array) + ofset _set_in_the_colum)

But usualy for multidimentional arrays I just do it the lazy way of array[i][j].

14. Damn that gets hairy after 1D

15. If you have covered pointers then you can see that a an array is really nothing more then a pointer and an offset.
Whenever I see something like "an array is a pointer", I recommend reading Q6.9, Q6.8, Q6.3, and Q6.2 as follow-up material.
say you want to access array[9]
with a pointer you would do this
*(array + 8).
No, array[9] and *(array + 9) are equivalent.