Simulation
Category: Simulation
Author: Stuart Reges
Book Chapter: 7.1
Problem: Simulation
You are to simulate the execution of a method that
manipulates an array of integers. Consider the following method:
public static int mystery(int[] list) {
int x = 0;
for (int i = 0; i < list.length - 1; i++)
if (list[i] > list[i + 1])
x++;
return x;
}
Notice that this method takes an integer array as a parameter and returns an
integer. In the left-hand column below are specific lists of integers. You
are to indicate in the right-hand column what value would be returned by
method mystery if the integer list in the left-hand column is passed as a
parameter to mystery.
List Value returned
--------------------------------------
(8) ______________
(14, 7) ______________
(7, 1, 3, 2, 0, 4) ______________
(10, 8, 9, 5, 6) ______________
(8, 10, 8, 6, 4, 2) ______________
1) (8)
2) (14, 7)
3) (7, 1, 3, 2, 0, 4)
4) (10, 8, 9, 5, 6)
5) (8, 10, 8, 6, 4, 2)