You are here:

C++/two dimensional arrays

Advertisement


Question
I have to take everything out of the main function and put them into functions.  I copied and pasted everything from main into three functions.  I am having trouble figuring out which data types and variables I need to use when passing the two dimensional arrays.

Code so far:

#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>

using namespace std;

const int ROWS = 10;
const int COLUMNS = 15;

void initSeats(char [] []);
void availSeats();
void resSeats();

int main()
{
  int    selection,row,seat,num,i,j;
  char   seats[ROWS][COLUMNS];
  bool   avail;

  do
  {
     cout << "1. Initialize All Seats to Available
";
     cout << "2. Print the Current Status of Seat Availability
";
     cout << "3. Reserve a Seat(s)
";
     cout << "4. Quit

";
     cout << "Selection: ";
     cin >> selection;

     switch(selection)
     {
        case 1:
   
      initSeats ([] [COLUMNS]);
         break;

        case 2:
   
      availSeats();
      
         break;

        case 3:
         cout << "On which row would you like to be seated? ";
         cin >> row;
         cout << "In which seat would you like to begin your seats? ";
         cin >> seat;
         cout << "How many seats would you like? ";
         cin >> num;

         resSeats();
      
         break;

        case 4:
         cout << "Thank you ... Goodbye !!!

";
         break;

        default:
         cout << "Invalid Option ... Try Again !

";
         break;
     }
  } while (selection != 4);

  return 0;
}

void initSeats(char [] [COLUMNS])
{
 for(i=0;i<ROWS;i++)
    for(j=0;j<COLUMNS;j++)
       seats[i][j]='O';
 cout << "All Seats Have Been Initialized

";
 return;
}

void availSeats()
{
 for(i=0;i<ROWS;i++)
 {
    for(j=0;j<COLUMNS;j++)
       cout << setw(3) << seats[i][j];
    cout << endl;
 }
 cout << endl;
 return;
}

void resSeats()
{
 avail = true;
 
 for(i=seat;i<=(seat+(num-1));i++)
    if (seats[row][i] == 'X')
       avail = false;

 if (avail == true)
 {
    for(i=seat;i<=(seat+(num-1));i++)
       seats[row][i] = 'X';
    cout << "These seats are available, and are now reserved!

";
 }  
 else
    cout << "These seats are NOT available, and have NOT been reserved!

";
 return;
}

Answer
> I am having trouble figuring out which data types and variables I need to use
> when passing the two dimensional arrays.

It helps to first realize that arrays are *never* actually passed to functions. For example,

     void foo( int array[] )
     {
         // ...
     }

     int main()
     {
         int a[20] ;
         foo(a) ;
     }

The type of the argument to foo is not "array of int", it is "pointer to int". In fact, it would be exactly the same if it were written as

     void foo( int* array )
     {
         // ...
     }

The name of an array is almost always converted to a pointer to the first element of that array. For example:

     int b[20] ;
     b[3] = 100 ;

The second line is equivalent to *( b + 3 ) = 100 ; where b is a pointer to the first element, and pointer arithmetic is used to reach the 3rd element. That line could just as well have been written this way:

     3[b] = 100 ; // perfectly valid.

This is equivalent to *( 3 + b ) = 100 ; which is equivalent to *( b + 3 ) = 100 ;

Once you grasp that array names almost always decay to a pointer to the first element, take another look at the function call from main() above: foo(a) ;. You should understand what this is really doing - it's *not* passing an int array named a to a function that expects an int array. it's passing an int pointer to a function that expects an int pointer. foo(a) ; is equivalent to foo( &( a[0] ) ) ;

Now, consider the case of a function (apparently) taking a two-dimensional array:

     enum { SIZE = 10 } ;
     void bar( int array2d[][SIZE] )
     {
         // ...
     }

     int main()
     {
         int b[20][SIZE] ;
         bar(b) ;
     }

We know better - this function does not take an array at all, but a pointer. we should think of multidimensional arrays in a different way:
A 2d array is really a 1d array, each of whose elements is itself a 1d array. It could be equivalently written this way:

     enum { SIZE = 10 } ;
     void bar( int (*array2d) [SIZE] )
     {
         // ...
     }

It should begin to become clear now. the type of array2d is "pointer to array of SIZE ints". why does SIZE have to be specified? because it is required for evaluating array subscript operator which requires pointer arithmetic.

array2d actually points to the first of several arrays of ints, and we want to access the third of those arrays: array2d[3] ; this is equivalent to *( array2d + 3 ). Note that we are adding 3 to a pointer, and recall that because of pointer arithmetic the address required is 3 * sizeof( *array2d ) bytes past array2d.

In order for the compiler to generate code for this, it needs to know ahead of time what sizeof( *array2d ) is. sizeof( *array2d ) is equivalent to sizeof( int[SIZE] ), therefore the compiler needs to know SIZE at compile time.

It follows that any time you declare a function with an argument which is a multi-dimensional array, you need to specify the size of every dimension except the first; the sizes of those dimensions must be constants known at compile time.

So your three functions will take the two dimensional array as:

   void initSeats( char seats [] [COLUMNS] );
   void availSeats( char seats [] [COLUMNS] );
   void resSeats( char seats [] [COLUMNS] );

And you can call them this way from main:

int main()
{
  char seats[ROWS][COLUMNS];

  initSeats( seats ) ;
  // etc

}

There are a couple of other errors in your code. Variables in main do not become automatically available to functions. For instance, the variables row, seat and num are not available to the function resSeats, unless you pass them as arguments. For example:

void resSeats( char seats [] [COLUMNS], int row, int seat, int num );

// in main
    
    resSeats( seats, row, seat, num );

Similarly, variables i and j need to be defined in each function - the variables in main are not available to them.  

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


vijayan

Expertise

my primary areas of interest are generic and template metaprogramming, STL, algorithms, design patterns and c++11. i would not answer questions about gui and web programming.

Experience

about 15 years or so

Education/Credentials
post graduate engineer

©2016 About.com. All rights reserved.