2 dimensional arrays

An airline uses jets with 15 rows of 6 seats, labeled A-F. Seats A and F are window seats. Seats B and E are middle seats. Seats C and D are aisle seats.

window |A| |B| |C| aisle |D| |E| |F| window

You will store data about the status of each seat on the plane in a two-dimensional array. Use all defined indexes (i.e. do not skip array index 0). Each seat can have a status of EMPTY, RESERVED, or PURCHASED. Store the seat status in the array using an enumerated type.
NOTE: You may want to create other addditional enumerated types for use in this program

:: This is my first time working with 2 dimensional arrays. I am unfamiliar with how to do the above steps. Here is my code so far, and my intent on how to complete the rest of the program.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158


#include "stdafx.h"
#include <iostream>
#include <iomanip>
using namespace std;

// global constants
const int numOfRows = 14;
const int numOfSeats = 6;
char ArrayOfSeats [numOfRows] [numOfSeats];

// function prototypes
// takes Seat array and initializes to empty seats
void InitializeSeatingArray ();
// reads row index, seat index, and seat status from binary file, the stores row and seat index values to enumerated seat status in array.
void ReadAndStoreBinaryFile ();
// counts and displays total number of empty, reserved, and purchased seats to console
void DisplaySeatStatus ();
// displays a seating preference menu for user and prompts for preference
void DisplaySeatPrefMenu ();
// determines validity of user's seat choice
void GetAndValidateUserSeatPref ();
// saves data from Seat array to Binary file
void SaveBinaryFile ();
// displays all remaining empty seats to console
void DisplayEmptySeats ();

int main()
{
	return 0;

	// h)  Exit the program
}

void InitializeSeatingArray ()
{
	// a) Initialize a two-dimensional array, representing the seats in the plane, to all empty seats	
	for (numOfRows = 0; numOfRows < 15; numOfRows++ )
		for (numOfSeats = 0; numOfSeats < 6; numOfSeats++ )
			ArrayOfSeats [numOfRows][numOfSeats] = '0';
}

void ReadAndStoreBinaryFile ()
{
	/* b)   If the PLANE.BIN file exists, read the data about purchased and reserved seats from the file.   
First read three items (the row index, the seat index, and the seat status value) from the file.  
Then use the row and seat index values to store the enumerated seat status in the correct location 
in the 2D array.  Continue to read three items from the file, and use them to store the seat status 
in the array, until you reach the end of the file. 
Be sure to read the row index, the seat index and seat status in the same order that they were 
written  to the file.  A program's correct interpretation of the binary data is dependent upon 
reading the data in the same order that it was originally written. 
NOTE: After you read the data from the file, you will not be using the file again while the 
user chooses seats.  All modifications will be made in the 2D array.  You will not access the
file again until the user chooses to exit the program.*/

}
																			
void DisplaySeatStatus ()
{
	/*c) Using the data in the array, count and display the total number of Empty, Reserved, and 
Purchased seats in the entire plane to the screen. 
   
Sample Output: 
Current Status: 
      22 purchased seats 
       2 reserved seats 
      66 empty seats 
     --- 
      90 Total 
If the plane is full (i.e. there are 0 empty seats), do not display the status.  Instead, the program 
should just issue a "all seats on plane are full" type message and exit the program. */

}

void DisplaySeatPrefMenu ()
{
	/*  d) If the plane is not full, loop,  letting the user reserve or purchase more seats: 
  i) Display a seating preference menu, prompting the user for "Window", "Middle", or "Aisle" 
preference, or the user can choose "Done choosing seats" from this menu.  Use mnemonic 
letters as menu options (not numbers). 
  ii) Display all empty seats (1A, 1F, etc) of the specified type only (window, middle, or aisle). 
NOTE:  You may format the output however you wish, as long as it does not run off the 
screen before the user can read it. 
If there are zero empty seats of the specified type, tell the user, and return to the seating 
preference menu. */

}

void GetAndValidateUserSeatPref ()
{
	/*  iii) Ask for the user's seat choice from the list of empty seats displayed. 
Read and error check a row Number and a seat Letter from the user. 
 - Verify the row choice is in the correct range (1-15). 
   - Verify the seat choice matches his/her original preference (window, middle or aisle). 
   - Verify the row/seat choice is an empty seat 
NOTES ABOUT USER INPUT:   
1)  The user should be able to enter  row numbers as 1-15 and the seat letters A-F.  And 
the program should always display the rows as 1-15 and the seats as A-F.   
Since the 2D array indexes begin at 0, the program should transparently (without the 
user's knowledge) take care of converting the user's choices to the correct indexes in the 
array, and visa versa. 
2)  You can make the following assumption about data entered by the user (i.e. you do 
not have to error check these situations): 
If you request a number, the user will enter a number.  
If you request one character, the user will enter one character
iv) If seat is in range, and of the correct type and empty, ask whether to Purchase or Reserve
the requested seat.  Then store RESERVED or PURCHASED in the array at the correct location. 
  Otherwise give an error message, telling the user why s/he cannot choose that seat  (wrong type, 
not empty, etc). 
  Continue to let the user select seats until s/he chooses "Done choosing seats" from the menu. 
  
  e) Again count and display the total number of Empty, Reserved, and Purchased seats on the whole 
plane to the screen (same as (c) above).  Pause until the user is done viewing the data. */

}

void SaveBinaryFile ()
{
	/* f)   Save the data in the array back to the PLANE.BIN file in binary format. 
Store the data on the reserved and purchased seats only.  This means that you will not be able 
to just write the entire array out to the file at once (because that would also save data on empty
seats). 
The array holds one enumerated value in each cell (EMPTY, PURCHASED, or 
RESERVED).  But your program will need to write out three data values for each 
purchased/reserved seat (a row index, a seat index, and a seat status).   
Use nested loops to examine each cell in the array. If the seat is not empty, write the data 
(row index, seat index, and seat status) for that seat out to the file.  Continue until all cells 
have been examined. 
The data should overwrite the original PLANE.BIN, if it previously existed. */

}

void DisplayEmptySeats ()
{
	/*g)   Finally, display all remaining empty seats to the screen (nicely formatted in 15 rows and  
6 columns).  Pause until the user is done viewing the data
Sample Output: 
All remaining EMPTY seats: 
 []   1A   1B   1C  [    ]   1D   1E   1F  [] 
 []   2A   2B   2C  [    ]   2D   2E   2F  [] 
 []                 [    ]                 [] 
 []        4B   4C  [    ]   4D   4E   4F  [] 
 []        5B   5C  [    ]   5D   5E   5F  [] 
 []                 [    ]                 [] 
 []                 [    ]                 [] 
 []                 [    ]                 [] 
 []                 [    ]                 [] 
 []                 [    ]                 [] 
 []  11A       11C  [    ]  11D  11E  11F  [] 
 []  12A  12B  12C  [    ]  12D       12F  [] 
 []  13A  13B  13C  [    ]  13D       13F  [] 
 []                 [    ]                 [] 
 []       15B       [    ]       15E       [] */

}


I am unsure if my ArrayOfSeats should be a variable within main rather than a global constant. Also, my array includes the columns and rows of seats, but do i create an enumerated type for (EMPTY, PURCHASED, RESERVED) and the seat letters and positions?
My last question, if there are 15 rows and 6 columns, and above they state that i shouldn't skip index 0, would I initialize them at 14 and 5, or 15 and 6?
After looking at it some more, i decided to try initializing like this...will this still work?
1
2
3
4
5
6
7
8
9
10
11
void InitializeSeatingArray ()
{
	// a) Initialize a two-dimensional array, representing the seats in the plane, to all empty seats	
	for (int i = 0; i < numOfRows; i++ )
	{
		for (int j = 0; j < numOfSeats; j++ )
		{
			ArrayOfSeats [i][j] = 'E';
		}
	}
}
@scu1casper

Yep, that works. I worked on your program a bit, and came up with this. It's just a small start, mind you.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
// Airline Seating.cpp : main project file.

#include "stdafx.h"
#include <iostream>
#include <iomanip>

using namespace std;

// global constants
const int numOfRows = 15;
const int numOfSeats = 6;
char ArrayOfSeats [numOfRows] [numOfSeats];

// function prototypes
// takes Seat array and initializes to empty seats
void InitializeSeatingArray ();
// reads row index, seat index, and seat status from binary file, the stores row and seat index values to enumerated seat status in array.
void ReadAndStoreBinaryFile ();
// counts and displays total number of empty, reserved, and purchased seats to console
void DisplaySeatStatus ();
// displays a seating preference menu for user and prompts for preference
void DisplaySeatPrefMenu ();
// determines validity of user's seat choice
void GetAndValidateUserSeatPref ();
// saves data from Seat array to Binary file
void SaveBinaryFile ();
// displays all remaining empty seats to console
void DisplayEmptySeats ();

int main()
{
	InitializeSeatingArray();
	ArrayOfSeats[5][3]=1;
	ArrayOfSeats[11][5]=1;
	
	DisplayEmptySeats();
	
	cout << endl << endl << endl;
		return 0;

	// h)  Exit the program
}

void InitializeSeatingArray ()
{
	// a) Initialize a two-dimensional array, representing the seats in the plane, to all empty seats	
	for (int i = 0; i < numOfRows; i++ )
	{
		for (int j = 0; j < numOfSeats; j++ )
		{
			ArrayOfSeats [i][j] = 'E';
		}
	}
}

void ReadAndStoreBinaryFile ()
{
	/* b)   If the PLANE.BIN file exists, read the data about purchased and reserved seats from the file.   
First read three items (the row index, the seat index, and the seat status value) from the file.  
Then use the row and seat index values to store the enumerated seat status in the correct location 
in the 2D array.  Continue to read three items from the file, and use them to store the seat status 
in the array, until you reach the end of the file. 
Be sure to read the row index, the seat index and seat status in the same order that they were 
written  to the file.  A program's correct interpretation of the binary data is dependent upon 
reading the data in the same order that it was originally written. 
NOTE: After you read the data from the file, you will not be using the file again while the 
user chooses seats.  All modifications will be made in the 2D array.  You will not access the
file again until the user chooses to exit the program.*/

}
																			
void DisplaySeatStatus ()
{
	/*c) Using the data in the array, count and display the total number of Empty, Reserved, and 
Purchased seats in the entire plane to the screen. 
   
Sample Output: 
Current Status: 
      22 purchased seats 
       2 reserved seats 
      66 empty seats 
     --- 
      90 Total 
If the plane is full (i.e. there are 0 empty seats), do not display the status.  Instead, the program 
should just issue a "all seats on plane are full" type message and exit the program. */

}

void DisplaySeatPrefMenu ()
{
	/*  d) If the plane is not full, loop,  letting the user reserve or purchase more seats: 
  i) Display a seating preference menu, prompting the user for "Window", "Middle", or "Aisle" 
preference, or the user can choose "Done choosing seats" from this menu.  Use mnemonic 
letters as menu options (not numbers). 
  ii) Display all empty seats (1A, 1F, etc) of the specified type only (window, middle, or aisle). 
NOTE:  You may format the output however you wish, as long as it does not run off the 
screen before the user can read it. 
If there are zero empty seats of the specified type, tell the user, and return to the seating 
preference menu. */

}

void GetAndValidateUserSeatPref ()
{
	/*  iii) Ask for the user's seat choice from the list of empty seats displayed. 
Read and error check a row Number and a seat Letter from the user. 
 - Verify the row choice is in the correct range (1-15). 
   - Verify the seat choice matches his/her original preference (window, middle or aisle). 
   - Verify the row/seat choice is an empty seat 
NOTES ABOUT USER INPUT:   
1)  The user should be able to enter  row numbers as 1-15 and the seat letters A-F.  And 
the program should always display the rows as 1-15 and the seats as A-F.   
Since the 2D array indexes begin at 0, the program should transparently (without the 
user's knowledge) take care of converting the user's choices to the correct indexes in the 
array, and visa versa. 
2)  You can make the following assumption about data entered by the user (i.e. you do 
not have to error check these situations): 
If you request a number, the user will enter a number.  
If you request one character, the user will enter one character
iv) If seat is in range, and of the correct type and empty, ask whether to Purchase or Reserve
the requested seat.  Then store RESERVED or PURCHASED in the array at the correct location. 
  Otherwise give an error message, telling the user why s/he cannot choose that seat  (wrong type, 
not empty, etc). 
  Continue to let the user select seats until s/he chooses "Done choosing seats" from the menu. 
  
  e) Again count and display the total number of Empty, Reserved, and Purchased seats on the whole 
plane to the screen (same as (c) above).  Pause until the user is done viewing the data. */

}

void SaveBinaryFile ()
{
	/* f)   Save the data in the array back to the PLANE.BIN file in binary format. 
Store the data on the reserved and purchased seats only.  This means that you will not be able 
to just write the entire array out to the file at once (because that would also save data on empty
seats). 
The array holds one enumerated value in each cell (EMPTY, PURCHASED, or 
RESERVED).  But your program will need to write out three data values for each 
purchased/reserved seat (a row index, a seat index, and a seat status).   
Use nested loops to examine each cell in the array. If the seat is not empty, write the data 
(row index, seat index, and seat status) for that seat out to the file.  Continue until all cells 
have been examined. 
The data should overwrite the original PLANE.BIN, if it previously existed. */

}

void DisplayEmptySeats ()
{
	/*g)   Finally, display all remaining empty seats to the screen (nicely formatted in 15 rows and  
6 columns).  Pause until the user is done viewing the data
Sample Output: 
All remaining EMPTY seats: 
 []   1A   1B   1C  [    ]   1D   1E   1F  [] 
 []   2A   2B   2C  [    ]   2D   2E   2F  [] 
 []                 [    ]                 [] 
 []        4B   4C  [    ]   4D   4E   4F  [] 
 []        5B   5C  [    ]   5D   5E   5F  [] 
 []                 [    ]                 [] 
 []                 [    ]                 [] 
 []                 [    ]                 [] 
 []                 [    ]                 [] 
 []                 [    ]                 [] 
 []  11A       11C  [    ]  11D  11E  11F  [] 
 []  12A  12B  12C  [    ]  12D       12F  [] 
 []  13A  13B  13C  [    ]  13D       13F  [] 
 []                 [    ]                 [] 
 []       15B       [    ]       15E       [] */
	cout << endl << "  The Available Seats For Purchase" << endl << endl;
for (int a=0;a<15;a++)
	{
		int z=0;
		cout << "[] ";
		for (char letter = 'A'; letter <= 'F'; letter++)
		{
			if (ArrayOfSeats[a][z] == 'E')
			{
				if (a<9)
					cout << " ";
				cout << a+1 << letter << " ";
			}
			else
			{
				cout << "    ";
			}
			z++;
			if (letter == 'C')
				cout << " |   | ";
		}
	cout << " []" << endl;
	}

}
two questions though:
#1 - if i initialize numOfRows to 15 and numOfSeats to 6, and I want to use index 0, wouldn't that be equal to 16 rows and 7 seats? So, should I make it 14 and 5?
#2 - what is this doing?
1
2
3
4
	ArrayOfSeats[5][3]=1;
	ArrayOfSeats[11][5]=1;
	
	DisplayEmptySeats();
Oh nvm I see what you did there. I didn't notice that you had added code to the displayEmptySeats function. :) Thanks!

But still wondering about the numOfRows and numOfSeats thing though.
@scu1casper

#1 - if i initialize numOfRows to 15 and numOfSeats to 6, and I want to use index 0, wouldn't that be equal to 16 rows and 7 seats? So, should I make it 14 and 5?


No. When you initialize an array, like the ArrayOfSeats[numOfRows][numOfSeats], the numbers would be 0 to numOfRows minus one. That is, 0 to 14, which makes 15. The same for numOfSeats. 0 to 5, making 6.
When you look at the print-out of the seating, you should notice that the 6th row, seat 4, is blank, as is the 12th row, 6th seat.

And this,
1
2
ArrayOfSeats[5][3]=1;
ArrayOfSeats[11][5]=1;
is actually wrong. I should have written it as
1
2
ArrayOfSeats[5][3]='R' ; // 'Reserved' or 'P' for 'Purchased'
ArrayOfSeats[11][5]='P';

Topic archived. No new replies allowed.