Help with 2D arrays
Aug 6, 2011 at 4:12am UTC
So I'm trying to write a program that displays available seats on an airplane (7 rows with 4 seats each). The user is supposed to specify a row and column of a seat with a number(1-7) followed by a letter(A-D) like so:
7a
6c
6d
The above are just some possible examples. After each seat is selected a display of the available seats should display with an "x" appearing wherever a seat is occupied. line 154 is meant to accomplish this task but it doesn't appear to be working. Any advice?
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
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
const short COLUMNS = 4;
const short ROWS = 7;
//initializes seats array
void initialize(char seats[][COLUMNS]);
//converts digits (0-3) to chars (A-D)
//returns letter A-D corresponding to digit
char convert(short digit);
//converts char (A-D) to a digit (0-3)
//returns digit 0-3 corresponding to column_char
short convert(char column_char);
//display seats array
void display(char seats[][COLUMNS]);
//initializes vacancy array so each element is true
void initialize(bool vacant[][COLUMNS]);
//places an "x" for a specified place
//row and column specify coordinates of place
//asks user to input row/column if the position is already occupied
void register_place(bool vacant[][COLUMNS], char seats[][COLUMNS], short row, char column_char);
int main()
{
using namespace std;
char control = 'y' ;//this variable is used by user to stay in or exit out of do-while loop
do
{
char seats[ROWS][COLUMNS];
bool vacant[ROWS][COLUMNS];
initialize(seats);
initialize(vacant);
display(seats);
short row;
char column_char;
//TESTING
while (true )
{
cout << "Enter row/column: " ;
cin >> row >> column_char;
register_place(vacant, seats, row, column_char);
}
cout << "\n\nWould you like to run the program again? (Y/N): " ;
cin >> control;
while ((control != 'y' ) && (control != 'n' ))
{
cout << "Incorrect input. Would you like to run the program again? (Y/N): " ;
cin >> control;
control = tolower(control);
}
}while (control == 'y' );
cout << "Press key to exit: " ;
char exit;
cin >> exit;
return 0;
}
void initialize(char seats[][COLUMNS])
{
using namespace std;
for (short i = 0; i < ROWS; i++)
{
short count(0);
for (short j = 0; j < COLUMNS; j++)
{
seats[i][j] = convert(count);
count++;
}
}
return ;
}
void initialize(bool vacant[][COLUMNS])
{
using namespace std;
for (short i = 0; i < ROWS; i++)
{
for (short j = 0; j < COLUMNS; j++)
{
vacant[i][j] = true ;
}
}
return ;
}
char convert(short digit)
{
using namespace std;
switch (digit)
{
case 0:
return 'A' ;
case 1:
return 'B' ;
case 2:
return 'C' ;
default :
return 'D' ;
}
}
void display(char seats[][COLUMNS])
{
using namespace std;
for (short i = 0; i < ROWS; i++)
{
cout << i+1 << " " ;
for (short j = 0; j < COLUMNS; j++)
{
cout << seats[i][j];
if (j == 1)
{
cout << " " ;
}
}
cout << endl;
}
return ;
}
void register_place(bool vacant[][COLUMNS], char seats[][COLUMNS], short row, char column_char)
{
using namespace std;
column_char = toupper(column_char);
short column = convert(column_char);
while (!vacant[row][column])
{
display(seats);
cout << "Invalid position. Enter new coordinates: " ;
cin >> row >> column_char;
column_char = toupper(column_char);
column = convert(column_char);
}
vacant[row][column] = false ;
seats[row][column] = 'x' ;
return ;
}
short convert(char column_char)
{
using namespace std;
if (column_char == 'A' )
{
return 0;
}
else if (column_char == 'B' )
{
return 1;
}
else if (column_char == 'C' )
{
return 2;
}
else //column_char == 'D'
{
return 3;
}
}
Aug 6, 2011 at 4:48am UTC
Never mind. I figured it out.
Topic archived. No new replies allowed.