Thread: Multidimensional Arrays!

  1. #1
    Registered User JM1082's Avatar
    Join Date
    Mar 2011
    Posts
    51

    Thumbs down Multidimensional Arrays!

    Hi all,

    I'm struggling to understand the secret behind passing a multidimensional array to a function in my program, please see my code below:
    Code:
    #include <iostream>
    using namespace std;
    
    const int employees = 4;
    const int items = 5;
    
    int validateEmployeeNumber(); // enure only numbers within the acceptable range are passed
    void zeroArray( int[][items], int ); // wipe all data from the array
    
    int main()
    {
        int sales[employees][items];
        int employeeNumber = 0;
    
        zeroArray( sales[][items], employees);
    
        while ( employeeNumber != -1 )
        {
            employeeNumber = validateEmployeeNumber();
    
    
        }
    
    } // end main
    
    // wipe all data from the array
    void zeroArray( int sales[][items], int employees )
    {
        for ( int i = 0; i < employees; i++ )
        {
            for ( int j = 0; j < items; j++ )
            {
                sales[i][j] = 0;
            }
        }
    } // end function zeroArray
    
    // enure only numbers within the acceptable range are passed
    int validateEmployeeNumber()
    {
        int empNo;
    
        cout << "Enter employee number ( or 0 to show summary ): ";
        cin >> empNo;
    
        while ( empNo < 0 || empNo > 4 )
        {
            cout << "Your input was not recognised!\nEnter employee number ( or 0 to show summary ): ";
            cin >> empNo;
        }
    
        return empNo - 1;
    } // end function validateEmployeeNumber
    My program refuses to work even though I've followed the textbook instructions and explicitly specified the second dimension's array boundaries!

    My IDE keeps telling me "Line 15 - error: expected primary-expression before ‘]’ token"

    Any help on this issue would be most appreciated!

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > zeroArray( sales[][items], employees);
    Calling it, use just the array name (like you would if it were a 1D array), or indeed just a scalar.

    zeroArray( sales, employees);
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Multidimensional arrays
    By Dontgiveup in forum C++ Programming
    Replies: 4
    Last Post: 03-25-2011, 07:09 PM
  2. C multidimensional arrays
    By sin2win in forum C Programming
    Replies: 6
    Last Post: 02-19-2010, 09:15 PM
  3. Multidimensional arrays
    By Niels_M in forum C Programming
    Replies: 51
    Last Post: 09-12-2009, 03:16 PM
  4. Multidimensional Arrays
    By jordanguyoflove in forum C Programming
    Replies: 4
    Last Post: 10-16-2008, 06:16 PM
  5. Multidimensional Arrays?
    By CreatedByShadow in forum C++ Programming
    Replies: 7
    Last Post: 01-13-2006, 10:35 PM