Arrays: Jeffrey Miller, PH.D
Arrays: Jeffrey Miller, PH.D
Arrays: Jeffrey Miller, PH.D
CSCI 201
Principles of Software Development
2/10
USC CSCI 201L
Arrays
▪ An array is a group of memory locations related by the
fact that they all have the same name and the same type
▪ Arrays can be addressed using brackets with the index (or
position number) inside the brackets
› The first element of an array is in the 0th position
› So arrays are addressed from 0 to one less than the length of
the array
▪ Each element in an array can be used just as the other
scalar variables we have discussed
▪ To define an array, you have to use the “new” operator
int nums[] = new int[10];
char names[] = new char[20];
9/10
USC CSCI 201L
Program
▪ Write a program that randomly generates dice rolls. The number
of rolls will be provided by the user. Output the number of times
each number occurred followed by the percentage. Here is a
sample execution with user input bolded.
c:\>java csci201.Dice
How many rolls? 5000
The number 1 occurred 800 times (16%).
The number 2 occurred 750 times (15%).
The number 3 occurred 850 times (17%).
The number 4 occurred 825 times (16.5%).
The number 5 occurred 775 times (15.5%).
The number 6 occurred 800 times (16%).
c:\>