Array initialization has me dumbfounded.

This is a discussion on Array initialization has me dumbfounded. within the C++ Programming forums, part of the General Programming Boards category; So let me preface this by saying that I am a college student taking c++. My professor has given us ...

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    3

    Array initialization has me dumbfounded.

    So let me preface this by saying that I am a college student taking c++. My professor has given us the task of designing an "airline reservation" program, (don't get excited, nothing amazing here) using a three dimensional array as the method of tracking plane, row, and seat that is reserved.

    My problem comes in the array. I have intialized it by saying

    Code:
    int planebooker [PLANE] [ROWS] [SEATS] = {0};
    PLANE ROW and SEATS are all const ints, with the # of planes in the fleet, and the # of rows and seats in the planes. (we are going fairly casic 5 seats and 40 rows.)

    When someone books the seat, I am trying to use

    Code:
    planebooker [x][y][z] = 1;
    to set a seat to reserved (x y and z are just an example this was taken from a set of for loops).

    My problem is, not matter what I do, whether i run a trio of for loops to set every seat on the plane to reserved (1), or if I let the customer pick what seat they want I can't get the spot in the array to display a 1.

    Anyone have any obvious things that I am missing? I have double checked to make sure that any and all ='s are set to == when comparing, past that, I am not sure what's up. If you want, I can post the entire code, but it's rather long.

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by chad_crider
    If you want, I can post the entire code, but it's rather long.
    Perhaps first post a minimal "stripped-down" version (which would compile) that exactly demonstrates the problem you are having difficulty with.
    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.*

  3. #3
    Registered User
    Join Date
    Feb 2006
    Posts
    3
    Well, this is a mishmash, but it should compile, and show what I need it to show.

    Code:
    #include <iostream.h>
    #include <conio.h>
    #include <stdlib.h>
    #include <time.h>
    #include <math.h>
    #include <iomanip.h>
    #include <stdio.h>
    
    const int PLANE = 4, ROWS = 41, SEATS = 6;
    
    void display (int [][ROWS][SEATS], int);
    void forloop (int [][ROWS][SEATS], int);
    
    int main ()
    {
    	int planebooker [PLANE] [ROWS] [SEATS] = {0};
    
            forloop(planebooker, PLANE);
            getch();
            display(planebooker, PLANE);
            return 0;
    }
    
    void display (int planebooker[][ROWS][SEATS], int x)
    {
    	int counter, seatnumber;
    	// x is the plane, y is the row, z is the seats
    		seatnumber = 1;
    		cout << setw(26) << "1111111111" << "2222222222";
                    cout << "33333333334" << endl;
    		cout << setw(11) << "12345" << " " << "67890";
                    cout << "1234567890" << "1234567890" ;
                    cout << "1234567890" << endl;
    		for (int z = 1 ; z < SEATS ; z++)
    		{
    			counter = 0;
    			for (int y = 1 ; y < ROWS ; y++)
    			{
    				if (counter == 0)
    					cout << setw(5) << seatnumber << " ";				
    				if ((counter - 5) == 0)
    					cout << " ";
    				cout << planebooker [PLANE][ROWS][SEATS];
    				counter++;
    			}
    			seatnumber++;
    			cout <<	endl;
    		}
    
    		cout << setw(12) << " FIRST " << setw(20);
                    cout  << "COACH" << endl;
    		cout << setw(12) << " CLASS " << endl;
    		getch();
    }
    
    
    void forloop (int planebooker[][ROWS][SEATS], int x)
    {
    		for (int x = 1 ; x < 4 ; x++)
    		{
    			for (int y = 1; y < 41 ; y++)
    			{
    				for (int z = 1 ; z < 6 ; z++)
    				{
    					planebooker [x][y][z] = 1;
    				}
    			}
    		}
    }
    Sorry If I messed something up jammin it in there.

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    What are you trying to use x for?

    Arrays start at 0.

    Consider something like this.
    Code:
    #include <iostream>
    using namespace std;
    
    #define ROWS  4
    #define SEATS 3
    #define PLANE 2
    
    void reserve(int planebooker [PLANE] [ROWS] [SEATS], int x, int y, int z)
    {
       planebooker [x][y][z] = 1;
    }
    
    void display(int planebooker [PLANE] [ROWS] [SEATS])
    {
       for ( int i = 0 ; i < PLANE; ++i )
       {
          for ( int j = 0 ; j < ROWS; ++j )
          {
             for ( int k = 0 ; k < SEATS; ++k )
             {
                cout << planebooker [i][j][k];
             }
             cout << ' ';
          }
          cout << '\n';
       }
    }
    
    int main()
    {
       int planebooker [PLANE] [ROWS] [SEATS] = {0};
       display(planebooker);
       cout << "---\n";
       reserve(planebooker, 0, 0, 0);
       display(planebooker);
       return 0;
    }
    
    /* my output
    000 000 000 000 
    000 000 000 000 
    ---
    100 000 000 000 
    000 000 000 000 
    */
    Last edited by Dave_Sinkula; 02-23-2006 at 10:02 PM. Reason: Modified display routine, showed output.
    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.*

  5. #5
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,069
    Look at this:

    Code:
    cout << planebooker [PLANE][ROWS][SEATS];
    That doesn't print out the seat. It prints out an array index that's out of bounds of your array. You need the same x, y, z when you print out that you had in your forloop function.
    Sent from my iPadŽ

  6. #6
    Registered User
    Join Date
    Feb 2006
    Posts
    3
    Thanks for the replys. I would like to use your solution Dave, but alas, I don't even know what a lot of that is yet. Thanks Sly for pointing me at my obvious (yet blinding) error.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Array initialization with int variable
    By tsantana in forum C++ Programming
    Replies: 5
    Last Post: 03-31-2009, 02:48 PM
  2. question about multidimensional arrays
    By richdb in forum C Programming
    Replies: 22
    Last Post: 02-26-2006, 08:51 AM
  3. Difference between array initialization
    By 3saul in forum C Programming
    Replies: 2
    Last Post: 02-05-2006, 10:51 PM
  4. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM
  5. Basic Array Initialization in C
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 02-18-2002, 05:41 PM

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