CS100, Spring 2000, Solutions to Prelim 1 ===================================== Q1 ===================================== int n = in.readInt(); double sum = 0.0; double squares = 0.0; int k = 0; double speed; while (k != n) { speed = in.readDouble(); sum = sum + speed; squares = squares + speed*speed; k = k+1; if (n == 0) System.out.println("No molecules!"); else { double Vmean = sum/n; double Vrms = Math.sqrt(squares/n); // or Math.pow(squares/n,.5) System.out.println("Percent error:" + Math.abs(100*(Vrms-Vmean)/Vmean)); } ===================================== Q2 ===================================== from worst to best: semi-direct, wacko, "&&", "==" semi-direct: if (month == 2) days = 28; else if (month <= 7) if (month%2 == 0) days = 30; else days = 31; else if (month%2 == 0) days = 31; else days = 30; wacko: if (month == 2) days = 28; else days = (month/8+month) % 2 + 30; "&&": if (month == 2) days = 28; else if (month<=7 && month%2 == 0 || month>=8 && month%2 == 1) days = 30; else days = 31; "==": if (month == 2) days = 28; else if ((month <= 7) == (month%2 == 0)) days = 30; else days = 31; ===================================== Q3 ===================================== version 1: int max; // max so far int prev = in.readInt(); // previous value int next; // next value to process if (prev<-10 || prev>10) System.out.println("no numbers"); else { next = in.readInt(); if (prev<-10 || prev>10) System.out.println("only one number"); else { max = prev+next; while (-10 <= prev && prev <= 10) { if (prev+next > max) max = prev+next; prev = next; next = in.readInt(); } System.out.println("max: " + max); } } Deficiencies: - "magic number" 10 used instead of a named constant - has many special cases and is deeply nested _______________________________________________________________________________ version 2: int limit = 10; // valid numbers are -limit..limit int minusInfinity = -2*limit-1; // smaller than any legal pair sum int max = minusInfinity; // max so far int prev = minusInfinity; // previous value int next = in.readInt(); // next value to process // process numbers, maintaining vars above, until find number out of limits while (Math.abs(next) <= limit) { prev = next; next = in.readInt(); if (Math.abs(next) <= limit) max = Math.max(max, prev+next); } // print max pair sum (or error message, if none) if (max==minusInfinity) System.out.println("not enough numbers"); else System.out.println("max: " + max); Deficiency: - "next in bounds" is tested twice each loop iteration _______________________________________________________________________________ version 3: int limit = 10; // valid numbers are -limit..limit int minusInfinity = -2*limit-1 ; // smaller than any pair sum, int max = minusInfinity; // max so far int prev = minusInfinity - limit; // previous value -- init so that if add // 0 or 1 #, still at most minusInfinity int next = in.readInt(); // next value to process // process numbers, maintaining vars above, until find number out of limits while (Math.abs(next) <= limit) { // update max pair sum max = Math.max(max, prev+next); // read next prev = next; next = in.readInt(); } // print max pair sum (or error message, if none) if (max <= minusInfinity) System.out.println("not enough numbers"); else System.out.println("max: " + max); ==================================== Bonus ==================================== B1 a) yes, 0 is a multiple of 17 B2 d) 0 is neither positive nor negative B3 a) 2 is the median of 2, 2, 5 B4 a) yes: read news with a fixed-width(=mono-spaced=non-proportional) font B5 c) never ok to put off to the side a comment for a group of statements B6 a) Java does, but we don't allow $ as part of a variable name B7 b) only projects --not exercises-- are partners allowed B8 L) is the missing middle initial from DIS, TKY, SNA, LJB, AMH B9 g) Po Chen is not on CS100 staff this semester B10 b) Tue has the least total time for office hours and tutoring