Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Newbie needs help with array

Status
Not open for further replies.

donniea21

MIS
Feb 16, 2001
75
US
This is for a hw assignment i am having trouble with. I need to:

Use a single-subscripted array to represent a seating chart on a plane. Initialize all the elements of the array to 0 to indicate that all the seats are empty.

This is where i am having trouble:
As each seat is assigned, set the corresponding elements of the array to 1 to indicate that the seat in taken. The program should never assign a seat twice.

Do i increment the array? I know this is basic stuff but i am very new to it. Thanks
 
Why don't you use a boolean array for this?

Sorry, I did not exactly get what you meant by "increment the array". You can use the following to initialize the array (named seats here)...

boolean[] seats = new boolean[numberOfSeats]
for (int i = 0; i < seats.length; i++) {
seats = false;
}

Altough: This is not necessary - ints are automatically 0, booleans automatically false - they can't be null like &quot;real&quot; objects.

For access to a single element, simply use seats[index].

Post if you still have problems...
allow thyself to be the spark that lights the fire
haslo@haslo.ch - www.haslo.ch​
 
The user chooses 1 or 2 from an input dialog. 1 for smoking 2 for non. The part i cant figure out is if I choose 1, the program should set seat[0] to 1 for filled. If i choose 1 again, the program needs to set seat[1] to 1 and so forth untill all the seats are filled. Seats 6 - 10 are designated for non smoking so i choose 1 more than five times, the program needs to state that seats 1-5 are filled. Thanks again for any help.

 
void fill(int fillValue, int[] seats)
{
boolean overfilled = false;
int i = 0;
while (!overfilled && seats > 0) {
i++;
if ((i >= 5 && fillValue == 1) || i >= seats.length) {
overfilled = true;
}
}
if (overfilled) {
// print error message
} else {
seats = fillValue;
}
}

You can also try to start from the top of the array if you have a smoker.

Hope this helps...
allow thyself to be the spark that lights the fire
haslo@haslo.ch - www.haslo.ch​
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top