CS100A Sections
Nov 10-11
Question 1
from sample prelim3Give the output produced by the following program.
public class CUCSApplication {
public static void test (int[] b, int c) {
c = c + 10;
for (int i = 0; i < b.length; i++) {
b[i] = b[i] + 1;
}
}
public static void main(String args[]) {
int x = 20;
int[] A = {2, 4, 6, 8, 10};
test (A, x);
System.out.println("x is now: " + x);
System.out.print("A is now: " );
for (int i = 0; i < 5; i++)
System.out.print(A[i] + " ");
System.out.println();
}
}
Solution.
Notice that the value in x has not been changed.x is now: 20
A is now: 3 5 7 9 11
Question 3
from first sample prelim3Assume you have been given the following class
public class student {
int ID; // ID number for a student
int prelim; // score on prelim
}
and that the variables below have been defined in method main for storing information on students in a course. The course has no more than 100 students in it.
int num; // number of students in the course
// list[0 .. (num - 1)] contains information
// on the students in the course
student[] list = new student[100];
// order[0..(num – 1)] contains the values 0..(num-1),
// arranged to give an ordering of the elements of
// array list by increasing ID number
int [] order = new int[100];
Also assume that the variables have already been initialized so that the comments above are true. That is:
(1) Variable num contains the number of students in the course.
(2) The information on these students has been stored in elements 0 through num -1 of array list.
(3) Elements 0 through num -1 of array order contain the values 0 through num -1, arranged so that they give us an ordering of the indexes of list according to increasing ID number. That is,
list[order [0]].ID < list[order [1]].ID < . . . < list[order [num - 2]].ID < list[order [num - 1]].ID
(a) (10 points) Write a segment of Java code that prints a list of the students in the class, printing them in decreasing order according to the students' ID numbers. Your code should print the ID number followed by the prelim score for each student, with each student printed on an separate line. You do not need to write a complete method, just a segment of code. Be sure to declare any additional variables that you use.
Solution
// Print a list of the students ordered by
// decreasing ID number
for (int i = num - 1; i >= 0; i--)
System.out.println(list[order[i]].ID + " " +
list[order[i]].prelim );
or
for (int i = 1; i <=num; i++) {
System.out.print (list[order[num-i]].ID + " ");
System.out.println(list[order[num-i]].prelim );
}
NOTE: Both the ID and prelim score must appear on the same line; the solution above is ok because print, not println, is used.
Additional 2D array question (#1)
The minor diagonal of a two-dimensional array ius the diagonal that runs from the upper right corner to the lower left corner. Suppode we have an array that contains only 0’s and 1’s. Write a method NumberZero that has a 2-D integer array parameter A. The method should yield the number of elements lying above the minor diagonal of A that are 0. Elements on the minor diagonal are not included in the count.
You should assume that the array is square. A useful fact: If A is a 2-D Java array, A.length is the number of rows in the ray.
Example: If A is a 5x5 array that looks like the picture below, then the method should yield the value 4. The elements above the minor diagonal are shown in bold italics and are outlined in the picture.
Solution: Examine the elements above the diagonal in row-major order.
// yield # of zeros in square array A
// above the minor diagonal
public int NumberZero(int[][] A) {
int count; // zeros found so far
int r, c; // loop counters
count = 0;
// Inv.: count = # 0’s above
// minor diagonal in rows 0..r-1
for (r=0; r<A.length-1; r++) {
// Inv.: count = # 0’s above
// minor diag in rows 0..r-1
// + # 0’s in row r, cols 0..c-1.
for (c=0; c<A.length-1-r; c++){
if (A[r][c]==0) {
count++;
}
}
}
return count;
}