/* * Fall09 Midterm 1 Programming Exam * * draw a histogram of student midterm grades * * Dependencies: StdIn, StdDraw * */ public class Histogram { public static void main(String[] args) { int BARS = 21; int[] hist = new int[BARS]; int RANGE = 100/(BARS - 1); // read in data - We don't know how many students int studNum = 0; while (!StdIn.isEmpty()) { String name = StdIn.readString(); studNum++; double midterm = StdIn.readDouble(); // save info for bar graph (histogram) int index = (int) (midterm / RANGE); hist[index] = hist[index] + 1; } // draw histogram StdDraw.setPenRadius(.025); StdDraw.setXscale(0, BARS); StdDraw.setYscale(0, studNum); for (int i = 0; i < BARS; i++) { StdDraw.line(i, 0, i, hist[i]); } } }