Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please add Commenting (commented each line of the code and used proper variable naming conventions which are self explanatory.)on this MakeHistogram, takes an integer n,

Please add Commenting (commented each line of the code and used proper variable naming conventions which are self explanatory.)on this MakeHistogram, 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

Code:

import stdlib.StdDraw;

import stdlib.StdIn;

public class MakeHistogram {

public static void main(String[] args) {

int N = StdIn.readInt();

int left = StdIn.readInt();

int right = StdIn.readInt();

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

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

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

allstr = allstr + str + " ";

}

}

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

int len = str_array.length;

double[] double_array = new double[len];

for (int i = 0; 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

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

if (sample >= avg * i && sample

k = i;

break;

}

}

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

x = i * interval_width + halfWidth;

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

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

}

}

}

image text in transcribedimage text in transcribedimage text in transcribed

Lookup Multiple Numberjava X Line Numjava X Hamming Distance Driverjava x SubGraphjava x Make Histogramjava x MakeHistogra import stdlib.StdDraw; import stdlib.StdIn; public class MakeHistogram { public static void main(String[] args) { int N = StdIn.readInt (); int left = StdIn.readInt(); int right = stdin.readInt(); co Jo cn bo Poin co Jo n bo Polo co Jo po J E double [] samples = make_samples (left, right); int[] intervals = fillSamples In Intervals (samples, N, left, right); HHHHHHHNNNNNNNNNMMMMMMMMM draw_histogram(intervals, samples) ; public static double[] make_samples (int left, int right) { String allstr = ""; 34 while (!StdIn.isEmpty()) { String str = stdin.readString(); double n = Double.parseDouble (str); if (n >= left && n = avg * i && sample = avg * i && sample = left && n = avg * i && sample = avg * i && sample

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Beginning ASP.NET 4.5 Databases

Authors: Sandeep Chanda, Damien Foggon

3rd Edition

1430243805, 978-1430243809

More Books

Students also viewed these Databases questions