Prelim 1 Solutions
Question 1 (30 points).
Trace the sequence of assignments to variables made by the program
segment given below when it is run with input data 5. Repeated assignments of the same value to a variable should be
repeated in the table. You may not need
all of the table columns that have been provided.
Assume the variable in has been declared such that expression in.readInt() inputs one integer value.
int p = in.readInt();
int d = 0;
int n = 0;
while ( p > 1 ) {
d = 2;
while
( ( p % d) != 0 ) d = d + 1;
if ( d
== p ) n = n + 1;
p = p - 1;
}
System.out.println();
Solution
p |
? |
5 |
|
|
|
|
|
|
|
4 |
|
3 |
|
|
|
2 |
|
|
1 |
|
|
|
|
d |
? |
|
0 |
|
2 |
3 |
4 |
5 |
|
|
2 |
|
2 |
3 |
|
|
2 |
|
|
|
|
|
|
n |
? |
|
|
0 |
|
|
|
|
1 |
|
|
|
|
|
2 |
|
|
3 |
|
|
|
|
|
Question 2 (35 points). Complete the program segment according
to the following input/output specification.
Input. The input of your
program consists of two or more positive integers terminated by -1 -1. The positive integers specify one or more runs, where a run is a sequence that
begins and ends with the same value. In
particular, the first integer of the first run (if there is at least one run)
is given by the first integer of the run is repeated. The first integer of the second run (if there is a second run) is
given by the next integer, and so on.
Here is some
sample input consisting of three runs of length 5, 4, and 2, respectively.
10
50 50 20 10
50
60 75 50
50
50
-1 -1
Notice that the
runs are as short as possible, e.g., we do NOT say that the above input
consists of two runs of lengths 5 and 6, respectively. Assume the input is well-formed, i.e., no
run is left open (e.g., 10 -1).
Output.
Output the number
of runs and the average length run ( rounded to the nearest whole number
). For the sample input given above,
the output would be:
Number of runs: 3
Average run length: 4
Solution
TokenReader in = new TokenReader( System.in );
int first = in.readInt(); // first integer of the current run.
int next = in.readInt(); // next integer of the current run.
int length = 2; // length of the current run so far.
int sum = 0; // total length of completed runs so far.
int nRuns = 0; // number of completed runs so far.
while ( first != -1 && next !=
-1 ){
if ( next != first )
length++;
else {
nRuns++;
sum += length;
first = in.readInt();
length =
2;
}
next = in.readInt();
}
System.out.println("Number
of runs: " + nRuns);
System.out.println("Av.
run length: " +
+ Math.round( (double)sum/nRuns));
Question 3 (35 points).
Complete the program segment according to the following input/output
specification.
Input. The input consists
of a non-negative integer n followed by n additional non-negative
integers. For example
4 5 3 0 2
Output. Output n lines of
stars (*'s), where the number of *'s in a line is given by the corresponding
integer in the input. For example, for
the input given, the output would be:
*****
***
<a blank line>
*
Solution 1
TokenReader in = new TokenReader( System.in );
int n = in.readInt(); // number of lines of output requested.
// other declarations
int stars;
// statements
while ( n > 0 ) {
stars =
in.readInt();
while
( stars > 0 ) {
System.out.print("*");
stars--;
}
System.out.println();
n--;
}
Solution 2
TokenReader in = new
TokenReader( System.in );
int n = in.readInt(); // number of lines of output requested.
// other declarations
int line = 1;
int j, stars;
// statements
while ( line <=n ) {
stars = in.readInt();
j = 1;
while
( j <= stars ) {
System.out.print("*");
j++;
}
System.out.println();
line++;
}