Thread: Array problem

  1. #1
    Registered User
    Join Date
    Mar 2007
    Posts
    23

    Array problem

    Hi I am making a program that uses multi dimensional arrays to display numbers from 1 to 54 in 6 rows and 9 columns. Here is my code:

    Code:
    #include <stdio.h>
    
    #define MAXCOL 6
    #define MAXROW 9
    
    void fillArray(int array[][MAXROW]);
    void printArray(int array[][MAXCOL]);
    
    int main()
    {
       int array[MAXROW][MAXCOL];
    
       fillArray(array);
       printArray(array);
    
       return 0;
    }
    
    void fillArray(int array[][MAXROW])
    {
       /* fill the array with the numbers 1 to 54
          in ascending order going from left to right
          then top to bottom. Therefore 1 will be in 
          the top left hand corner, 6 in the top right
          hand corner, 49 in the bottom left corner
          and 54 in the bottom right hand corner.
       */
    
    }
    
    void printArray(int array[][MAXCOL])
    {
       int row, col;
       int size = 1;
    
       printf("\n");
       for (row=0; row<=MAXROW; row++)
       {
          for (col=0; col<=MAXCOL; col++)
          {
            array[row][col]=size;
    	printf("%9d",array);
          }
          printf("\n");
       }
    }
    It gives me an error in line 13: array.c(13): error #2140: Type error in argument 1 to 'fillArray'; found 'int [6] *' expected 'int [9] *'.

    I am not sure why? I have tried changing the MAXCOLS AND MAXROWS around but it's not doing anything except giving more errors.

    Please help.

  2. #2
    Registered User
    Join Date
    May 2006
    Posts
    903
    Shouldn't this:

    void fillArray(int array[][MAXROW])

    read like this:

    void fillArray(int array[][MAXCOL])
    ?

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,665
    void fillArray(int array[][MAXROW]);
    void printArray(int array[][MAXCOL]);
    Why aren't these both [][MAXCOL];
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Mar 2007
    Posts
    23
    OK, I have changed the code to look like this:

    Code:
    #include <stdio.h>
    
    #define MAXCOL 6
    #define MAXROW 9
    
    void fillArray(int array[][MAXCOL]);
    void printArray(int array[][MAXCOL]);
    
    int main()
    {
       int array[MAXROW][MAXCOL];
    
       fillArray(array);
       printArray(array);
    
       return 0;
    }
    
    void fillArray(int array[][MAXCOL])
    {
       /* fill the array with the numbers 1 to 54
          in ascending order going from left to right
          then top to bottom. Therefore 1 will be in 
          the top left hand corner, 6 in the top right
          hand corner, 49 in the bottom left corner
          and 54 in the bottom right hand corner.
    
          HINT. While there are some errors in the
          printArray function it should give you a good
          idea as to how to complete this function.
       */
    
    }
    
    void printArray(int array[][MAXCOL])
    {
       int row, col;
       int size = 1;
    
       printf("\n");
       for (row=0; row<=MAXROW; row++)
       {
          for (col=0; col<=MAXCOL; col++)
          {
            array[row][col]=size;
    	printf("%9d",array[row][col]);
          }
          printf("\n");
       }
    }
    However, it is only printing an array of 1s something like:

    1 1 1 1 1 1 1
    1 1 1 1 1 1 1
    1 1 1 1 1 1 1
    1 1 1 1 1 1 1
    1 1 1 1 1 1 1
    1 1 1 1 1 1 1
    1 1 1 1 1 1 1
    1 1 1 1 1 1 1
    1 1 1 1 1 1 1

    I need it to print an array for numbers 1 to 54 in 9 rows and 6 columns. Thanks for your inputs.

  5. #5
    Registered User
    Join Date
    Mar 2007
    Posts
    23
    Never mind, don't bother replying. I got it to work.

    Thanx anyway.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,665
    > for (row=0; row<=MAXROW; row++)
    Arrays run from 0 to N-1, so this loop should be
    for (row=0; row<=MAXROW-1; row++)
    or more usually
    for (row=0; row<MAXROW; row++)

    The same goes for the columns as well.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Lean Mean Coding Machine KONI's Avatar
    Join Date
    Mar 2007
    Location
    Luxembourg, Europe
    Posts
    444
    You're never modifying the size variable in your printArray function.

    Either you do this:
    Code:
    array[row][col]=size++;
    OR

    Code:
    array[row][col]=row+col;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Array problem
    By TomBoyRacer in forum C++ Programming
    Replies: 3
    Last Post: 04-08-2007, 11:35 AM
  2. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  3. Replies: 6
    Last Post: 02-15-2005, 11:20 PM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Need desperate help with two dimensional array problem
    By webvigator2k in forum C++ Programming
    Replies: 4
    Last Post: 05-10-2003, 02:28 PM