Function Error

This is a discussion on Function Error within the C++ Programming forums, part of the General Programming Boards category; I wrote this function for displaying a matrix, but it won't compile.. What am I doing wrong? Code: void display(int ...

  1. #1
    Student otchster's Avatar
    Join Date
    Oct 2005
    Posts
    30

    Function Error

    I wrote this function for displaying a matrix, but it won't compile..

    What am I doing wrong?

    Code:
    void display(int A[] [], int m, int n)
    {
       for (i =0; i< m; i++)
        { for (j=0; j < n; j++)
           cout << " " << A[i][j];
         cout << "\n";}
    }

  2. #2
    Mad OnionKnight's Avatar
    Join Date
    Jan 2005
    Location
    Umeň, Sweden
    Posts
    555
    You are not reading your error messages, which tell you that i and j are undeclared.

  3. #3
    Student otchster's Avatar
    Join Date
    Oct 2005
    Posts
    30
    Quote Originally Posted by OnionKnight View Post
    You are not reading your error messages, which tell you that i and j are undeclared.
    They are declared. I think there may be a syntax problem that I can't seem to find after staring at that block of code for 15mins.

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    There's absolutely no good reason to use global variables i and j for that piece of code. You may have made other things in that code global too, which makes it impossible to diagnose what's wrong just from that snippet. What are the compiler's error messages?

    Edit: From gcc:

    error: declaration of ‘A’ as multidimensional array must have bounds for all dimensions except the first

    I didn't notice this as I normally use pointer syntax instead of array syntax for function arguments. Is this basically your error message?
    Last edited by robatino; 07-07-2007 at 11:00 AM.

  5. #5
    Tropical Coder Darryl's Avatar
    Join Date
    Mar 2005
    Location
    Cayman Islands
    Posts
    503
    Quote Originally Posted by otchster View Post
    They are declared. I think there may be a syntax problem that I can't seem to find after staring at that block of code for 15mins.
    Based on the snippet you provided, they are not declared!

    If you are saying that they are, then the only way is with a global variable which would be a pretty poor design indeed and should need rethinking.

    Why don't you post your compiler's error message?

  6. #6
    Captain - Lover of the C
    Join Date
    May 2005
    Posts
    341
    Story time:
    Once there was a young coder. He was writing some code to display a cute little matrix. But he had a problem. He forgot to put this in his code:
    Code:
    int i, j;
    Oh Dear!!! The compiler got very angry with the young coder. It spit out error messages like crazy but the young coder didn't see them... :-( So he went to a forum where a wise old man told him his problem. Still the young coder didn't listen. He stared at his code for several more minutes... Everyone begged him to look at the error messages and tell him what they were because they couldn't believe that even a young coder would use global variables for i and j.
    To be continued...
    Don't quote me on that... ...seriously

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,833
    > int A[] []
    All the minor dimensions of a multi-dimensional array need a size.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  8. #8
    Captain - Lover of the C
    Join Date
    May 2005
    Posts
    341
    Story Time Continued:
    The young coder thought all hope was lost. But then, Salem, the wisest of them all, reminded the young coder that when passing multi-dimensional arrays to functions, all minor dimensions need a size. The young coder had a new problem. He told, "I don't know the dimensions!". How can I pass the array to my function without knowing the dimensions?
    To be continued...
    Don't quote me on that... ...seriously

  9. #9
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    You could "flatten" it to a 1D thing -

    Code:
    void print ( int *pnum, int x, int y )
    {
       for ( int j=0; j<y; j++, printf("\n") )
       {
          for ( int i=0; i<x; i++ )
          {
             printf( "&#37;d ", *pnum );
    
             *pnum++;
          }
       }
    }
    
    int main( void )
    {
       int test22[2][2] = {{9, 8}, {7, 6}};
       int test23[2][3] = {{9, 8, 7}, {6, 5, 4}};
       int test33[3][3] = {{9, 8, 7}, {6, 5, 4}, {3, 2, 1}};
       int test24[2][4] = {{9, 8, 7, 6}, {5, 4, 3, 2}};
    
    
       printf( "Printing 2x2 matrix\n" );
       print( &test22[0][0], 2, 2 );
       
       printf( "\nPrinting 2x3 matrix\n" );
       print( &test23[0][0], 3, 2 );
    
       printf( "\nPrinting 3x3 matrix\n" );
       print( &test33[0][0], 3, 3 );
       
       printf( "\nPrinting 2x4 matrix\n" );
       print( &test24[0][0], 4, 2 );
       
       return 0;
    }
    And you're laughing.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. Testing some code, lots of errors...
    By Sparrowhawk in forum C Programming
    Replies: 48
    Last Post: 12-15-2008, 04:09 AM
  3. Making C DLL using MSVC++ 2005
    By chico1st in forum C Programming
    Replies: 26
    Last Post: 05-28-2008, 02:17 PM
  4. Connecting to a mysql server and querying problem
    By Diod in forum C++ Programming
    Replies: 8
    Last Post: 02-13-2006, 10:33 AM
  5. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 11:03 AM

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