Thread: array help please

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    20

    array help please

    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 addition(int matrix[MAXROW][MAXCOL],int i,int j);
    int main(void)
    	{
    	addition(matrix[MAXROW][MAXCOL],row,col);
    	return 0
    	}
    
    int addition(int matrix[MAXROW][MAXCOL],int i,int j)
    	{
    	for (i=0;i<MAXROW;++i)
    		for (j=0;J<MAXCOL;++j);
    			sum+=matrix[i][j];
    	printf("\nsum=%d",sum);
    	return 0
    	}
    Last edited by CXHatchback; 04-16-2003 at 09:29 PM.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    20
    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. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    20
    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. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User
    Join Date
    Apr 2003
    Posts
    20
    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. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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:
    addition(matrix[MAXROW][MAXCOL],row,col);
    Should be:

    addition( matrix, row, col );

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

    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Registered User
    Join Date
    Apr 2003
    Posts
    20
    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. #9
    Registered User
    Join Date
    Mar 2003
    Posts
    102
    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. #10
    Registered User
    Join Date
    Apr 2003
    Posts
    20
    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. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.
    Hope is the first step on the road to disappointment.

  12. #12
    Registered User
    Join Date
    Apr 2003
    Posts
    20
    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. #13
    Registered User
    Join Date
    Mar 2003
    Posts
    102
    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. #14
    Registered User
    Join Date
    Apr 2003
    Posts
    20
    Damn that gets hairy after 1D

  15. #15
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    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.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  2. Replies: 6
    Last Post: 11-09-2006, 03:28 AM
  3. [question]Analyzing data in a two-dimensional array
    By burbose in forum C Programming
    Replies: 2
    Last Post: 06-13-2005, 07:31 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM