Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Program 1 , “ Histogram ,” takes an integer “n,” and two integers “left and righ

ID: 3750700 • Letter: P

Question

Program 1, “Histogram,” takes an integer “n,” and two integers “left and right,” and uses “StdDraw” to plot a histogram of the count of the numbers in the standard input stream that fall in each of the n intervals defined by dividing (left, right) into n equal-sized intervals.

Note: The program is to read the values from an input file whose first line would contain “n,” “left,” and “right” values. The remaining lines would contain the sequence of integer values.

A sample run would be as follows.

>more data.txt

4 40 80

52 41 72 61

71 60 50 52 61

77 41 61 70 79 41

67 60 50 61 76

>java Histogram < data.txt

It should plot a histogram where the x-axis points are 40, 50, 60, 70, 80 and the intervals are 40-50, 50-60, 60-70, 70-80. The height of the rectangles for the intervals should be 3, 4, 7, 6 respectively.

Explanation / Answer

Answer:

import stdlib.StdDraw;

import stdlib.StdIn;

public class MakeHistogram {

    public static void main(String[] args) {

       // read input values

        int N = StdIn.readInt();

        int left = StdIn.readInt();

        int right = StdIn.readInt();

        // take an array to collect samples between the ranges

        double[] samples = make_samples(left, right);

        // make a array intervals[] include N entry

        // intervals[i] is the ith interval between (l, r)

        // every time value indicate number of intervals

        // that fall in this interval

        int[] intervals = fillSamplesInIntervals(samples, N, left, right);

        // draw histogram

        draw_histogram(intervals, samples);

    }

    public static double[] make_samples(int left, int right) {

        String allstr = "";

        while(!StdIn.isEmpty()) {

            String str = StdIn.readString();

            double n = Double.parseDouble(str);

            if (n >= left && n <= right) {

                allstr = allstr + str + " ";

            }

        }

        String[] str_array = allstr.split(" ");

        int len = str_array.length;

        double[] double_array = new double[len];

        for (int i = 0; i < len; i++) {

            double_array[i] = Double.parseDouble(str_array[i]);

        }

        return double_array;

    }

    public static int[] fillSamplesInIntervals(double[] samples, int N, double left, double right) {

        int[] result = new int[N];

        int p;

        for (int i = 0; i < samples.length; i++) {

            p = findIntervalIndex(samples[i], N, left, right);

            result[p] += 1;

        }

        return result;

    }

    public static int findIntervalIndex(double sample, int N, double left, double right) {

        int k = 0;

        double avg = (right - left) / N;

        for (int i = 0; i < N; i++) {

            if (sample >= avg * i && sample < avg * (i + 1)) {

                k = i;

                break;

            }

        }

        // edge condition

        if (sample == right) {

            k = N - 1;

        }

        return k;

    }

    public static void draw_histogram(int[] intervals, double[] samples) {

        StdDraw.setPenRadius(.006);

        StdDraw.line(0, 0, 1, 0);

        StdDraw.line(0, 0, 0, 1);

        int n = intervals.length;

        int total = samples.length;

        double interval_width = 1.0 / n;

        double x, y;

        double halfWidth = interval_width / 2;

        StdDraw.setPenColor(StdDraw.GRAY);

        StdDraw.setPenRadius();

        for (int i = 0; i < n; i++) {

            x = i * interval_width + halfWidth;

            y = (double)intervals[i] / total / 2;

            StdDraw.rectangle(x, y, halfWidth, y);

        }

    }

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote