MCX1/2
Mr.Stanley
Teitel, Principal
Mr. Gary Jaye
Danny Jaye, Math Chair
2000 APCS A Exam Question 1 (Adapted for Java)
A mode is a value in an array that is larger than both the value immediately before it in the array and the value immediately after it. In other words, a mode occurs at index k in the array A if A[k] > A[k - 1] and A[k] > A[k + 1]. The array is unimodal if the values increase until they reach a mode, then decrease, so that there is only one mode. For example, the array A shown below is unimodal with its mode occurring at index 4. Assume that the mode does not occur at the fIrst or last entry in the array.
Index k A[k]
0 3
1 5
2 9
3 10
4 12 ß mode
5 11
6 9
7 4
(a) Write method IsMode, as started below. IsMode returns true if data[k] is larger than data[k-1]
and larger than data[k-1]; otherwise, it returns false. In the example above, the call IsMode(A, 4) returns
true and the call IsMode(A, 5) returns false.
Complete method IsMode below.
static bool IsMode(final double[] data, int k) // precondition: 0 < k < data.length - 1
(b) Write method Modelndex, as started below. Modelndex returns the index of the mode of data. You may assume
that data is unimodal and the mode occurs at an index k, where 0 < k < data.length - 1. In the example above,
the call Modelndex(A) returns 4. In writing Modelndex, you may call method IsMode specified in part (a). Assume
that IsMode works as specified, regardless of what you wrote in part (a).
Complete method Modelndex below. static int ModeIndex(final double[] data) // precondition: data is unimodal and data.length >= 3
(c) Write method PrintHistogram, as started below. PrintHistogram prints a character histogram of a unimodal
array of nonnegative values, data, such that the longest bar of the histogram (the mode) has longestBar characters barChar,
and all other bars have a number of barChar characters proportional to the corresponding value in the array data (rounding
down).
For example, assume that array data contains the values shown below.
The call PrintHistogram(data, 20, 'x')will print the histogram shown in the Output column below.
Index k data[k] Length of bar Output of call
PrintHistogram (data, 20, 'x')
0 3 5 xxxxx
1 5 8 xxxxxxxx
2 9 15 xxxxxxxxxxxxxxx
3 10 16 xxxxxxxxxxxxxxxx
4 12 20 xxxxxxxxxxxxxxxxxxxx
5 11 18 xxxxxxxxxxxxxxxxxx
6 4 6 xxxxxx
In writing PrintHistogram, you may call methods IsMode and Modelndex specified in parts (a) and (b). Assume that
IsMode and Modelndex work as specified, regardless of what you wrote in parts (a) and (b).
Complete method PrintHistogram below.
static void PrintHistogram(final double[] data, int longestBar, char barChar)
// precondition: data is unimodal and data.length >= 3;
// data[k] >= 0 for 0 <= k < data.length
//====================================================================================
// File: A00_1.java
// Additional file needed: MyTerminal.java (can be downloaded at web site)
public class A00_1 {
//==============================================
// Part a. Complete isMode(), as started below.
//==============================================
public static boolean isMode(final double[] data, int k) {
return false;
}
//=================================================
// Part b. Complete modeIndex(), as started below.
//=================================================
public static int modeIndex(final double[] data) {
return -77;
}
//============================================================
// Part c. Complete printHistogram(), as started below.
//============================================================
public static void printHistogram(final double[] data, int longestBar, char barChar) {
}
static String TAB = "\t";
public static void main(String[] args) {
double[] v;
System.out.println("\n\n\n\n\n\n\t\t2000 A Question 1\n");
v = initialize();
testIsMode(v);
testModeIndex(v);
testPrintHistogram(v);
}//=== main
static void testIsMode(final double[] data) {
System.out.println("Part a. Testing isMode():");
System.out.println(" k" + TAB + "data[k]");
for (int k = 0; k < data.length; k++) {
System.out.println(" " + k + TAB + data[k] + TAB + "isMode(data, " + k + ") = " +
( isMode(data, k) ? " true" : " false" ) );
}
System.out.println("\nNew data for modeIndex():\n");
double[] data2 = new double[5];
for (int k = 0; k < data2.length; k++) data2[k] = data[k];
System.out.println(" k" + TAB + "data[k]");
for (int k = 0; k < data2.length; k++) {
System.out.println(" " + k + TAB + data2[k] + TAB + "isMode(data2, " + k + ") = " +
( isMode(data2, k) ? " true" : " false" ) );
}
MyTerminal.readString("\nFinished testing isMode -- Press <ENTER>....");
MyTerminal.cls();
}
static void testModeIndex(final double[] data) {
System.out.println("Part b. Testing modeIndex():\n");
System.out.println(" k" + TAB + "data[k]");
for (int k = 0; k < data.length; k++) {
System.out.println(" " + k + TAB + data[k]);
}
System.out.println("modeIndex(data) = " + modeIndex(data));
System.out.println("\nNew data for testing modeIndex():");
double[] data2 = new double[data.length-3];
for (int k = 0; k < data2.length; k++)
data2[k] = data[k+3];
System.out.println(" k" + TAB + "data[k]");
for (int k = 0; k < data2.length; k++) {
System.out.println(" " + k + TAB + data2[k]);
}
System.out.println("modeIndex(data) = " + modeIndex(data2));
MyTerminal.readString("\nFinished testing modeIndex -- Press <ENTER>....");
MyTerminal.cls();
}
static void testPrintHistogram(final double[] data) {
final String LINE = "123456789012345678901234567890";
System.out.println("\nPart c. Testing printHistogram():");
System.out.println(" k" + TAB + "data[k]");
for (int k = 0; k < data.length; k++) {
System.out.println(" " + k + TAB + data[k]);
}
System.out.println("Output of call printHistogram(data, 20, 'X'):" + "\n" + LINE);
printHistogram(data, 20, 'X');
MyTerminal.readString("\nPress <ENTER>....");
MyTerminal.cls();
System.out.println("New data for testing printHistogram():");
double[] data2 = new double[data.length];
for (int k = 0; k < data2.length; k++) data2[k] = data[k];
System.out.println(" k" + TAB + "data[k]");
for (int k = 0; k < data2.length; k++) {
System.out.println(" " + k + TAB + data2[k]);
}
System.out.println("Output of call printHistogram(data, 30, '#'):" + "\n" + LINE);
printHistogram(data2, 30, '#');
System.out.println("\nProgram over\n");
}
static double[] initialize() {
return new double[] {3, 5, 9, 10, 12, 11, 9, 4};
}
}//=== end of A00_1 class