introToArrays 1
introToArrays 1
One solution:
Lets declare 100 boolean variables
(one for each room).
//false will indicate that the room is not
// occupied
boolean room1 = false;
boolean room2 = false;
} else if (which==100) {
if (room100)
System.out.println(
else
System.out.println(
}
occupied );
not occupied );
occupied );
not occupied );
occupied );
not occupied );
case 100:
if (room100)
else
break;
}
System.out.println( occupied );
System.out.println( not occupied );
System.out.println( occupied );
System.out.println( not occupied );
System.out.println( occupied );
System.out.println( not occupied );
if (room100)
++count;
System.out.println( count
+ rooms are occupied. );
Arrays
List of the same type of things.
Subscript (numbering) starts with 0.
How do we declare an array?
int x[] = new int [100 ];
Hint:
Remember that an array of 100
elements is subscripted by 0..99.
What if I would like to subscript by
1..100 (or in general, 1..N)?
Hint:
Remember that an array of 100
elements is subscripted by 0..99.
What if I would like to subscript by
1..100 (or in general, 1..N)?
final int N = 100;
int a[] = new int [N+1];