Histogram - Grade Distribution - java

A grade text file was provide that has one integer number per line representing a
student’s grade in the class. These numbers are not sorted but they are bound between 0 and 100
(inclusive). Using an array, you must count the frequency of each grade value and print it to the
standard output as a horizontal histogram. You must also label the range of each histogram bar and
allow the user to indicate what size interval they would like the histogram to be made with.
This is my assignment and I'm stuck on how to label the range of each histogram bar and
allow the user to indicate what size interval they would like the histogram to be made with?
How can I fix my code?
My Code:
import java.util.Scanner;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
public class GradeHistogram{
public static void main(String[] args) throws Exception {
Scanner gradeFile = new Scanner(new FileInputStream("grades.txt"));
int counter = 0;
while (gradeFile.hasNextLine()) {
gradeFile.nextLine();
counter++;
}
int[] grades = new int[counter];
System.out.println("Grades loaded!");
System.out.println("What bucket size would you like?");
Scanner output = new Scanner(System.in);
for (int i = 0; i < grades.length; i++) {
grades[i] = Integer.parseInt(output.nextLine());
for ( i = 0; i < grades.length; i++) {
if (grades[i] > 0){
System.out.print(i + " | ");
for (int j = 0; j < grades[i]; j++) {
System.out.print("[]");
}
System.out.println();
}
}
}
}

Ops! Pay attention to the fact that you are currently reusing your variable i inside the loop. Your code may (and probably will) behave unexpectedly due to that. I also believe you are not parsing the file correctly.
You can use an auxiliary array to store the frequency of each grade. Initialize an empty array (fill it up with zeros) and then for each grade you just update the corresponding slot.
The code would be something like this:
// 1. Generate your counting array
// (give it 101 slots to include all grades from 0 to 100)
int gradeFrequency = new int[101];
for (int j = 0; j < 101; j++) {
gradeFrequency[j] = 0;
}
// 2. Parse the file and store the frequency of each grade
while (gradeFile.hasNextLine()) {
// get the grade and add 1 to its counter
int grade = Integer.parseInt(gradeFile.nextLine());
gradeFrequency[grade]++;
}
After that, your gradeFrequency array will contain the frequency of each grade in the range 0 to 100.
When printing the histogram, you can ask the user to enter the interval they wish to see the histogram to, and then just print the corresponding frequencies from the array. The code for this would be something like:
// Retrieve the interval the user wants to see
Scanner s = new Scanner(System.in);
System.out.println("Enter the interval for the histogram (e.g. '0 100'):");
int lowerBound = s.nextInt();
int upperBound = s.nextInt();
// Print the histogram title and 'column headers'
System.out.println();
System.out.println("HISTOGRAM");
System.out.println("Grade\tFrequency");
// For each grade present in the interval, print out its 'label'
// (the assigned grade)
for (int i = lowerBound; i < upperBound; i++) {
System.out.print(i + "\t");
// For each time this grade was assigned to a student,
// print an asterisk
for (int j = 0; j < gradeFrequency[i]; j++) {
System.out.print("*");
}
System.out.println();
}
(Note that I didn't include any error handling for unexpected entries, like negative boundaries, start > end or an upper bound bigger than the array size; I leave it up to you!)
The console after an example execution of this code would be something like this:
Enter the interval for the histogram (e.g. '0 100'):
5 11
HISTOGRAM
Grade Frequency
5 ***
6 *
7 ******
8 *
9 **
10 ******
11 ****
Could I make it clear or do you have any remaining questions? :)

Related

How to count inputs in an array in java

I have n inputs.
these inputs are numbers from 1 to 100.
I want to output the number that appears less than the other ones; also if there are two numbers with the same amount of appearance, I want to output the number that is less than the other one.
I wrote this code but it doesn't work!
Scanner scanner = new Scanner(System.in);
int n=scanner.nextInt(), max=0 , ans=-1;
int[] counter = new int[n];
for(int i=0; i<n; i++)
counter[scanner.nextInt()]+=1;
for(int j=1; j<=100; j++){
if(counter[j]>max)
max=counter[j];
}
for (int i=1; i<=max; i++){
if(counter[i]>0)
if(ans==-1 || counter[ans]>counter[i] || (counter[ans] == counter[i] && i<ans))
ans=i;
}
System.out.print(ans);
There’s a couple of problems with your code, but the main one is the last for loop: You are trying to find the first (ie lowest) number whose counter is equal to max, so your loop should be from 1 to n, not 1 to max.
Another problem is if you are using the number, which is in the range 1-n, as your array index, you need an array of size n+1, not n.
I pinched this from another question regarding the title of yours:
i = input.nextInt (); while (i != 0) { counts [i]++; i = input.nextInt (); } That method increments the number at the position of the user input in the counts array, that way the array holds the number of times a number occurs in a specific index, e.g. counts holds how often 3 occurs.
counter array should contain frequency values for the numbers from 1 to 100 inclusive.
That is, either a shift by 1 should be used when counting the frequency:
int[] counter = new int[100];
for (int i = 0; i < n; i++) {
counter[scanner.nextInt() - 1]++;
}
or 101 may be used as the length of counter array thus representing values in the range [0..100], without shifting by 1.
int[] counter = new int[101];
for (int i = 0; i < n; i++) {
counter[scanner.nextInt()]++;
}
The minimal least frequent number can be found in a single loop (assuming that the counter length is 101).
int minFreq = 101, answer = -1;
for(int j = 1; j <= 100; j++) {
if (counter[j] > 0 && counter[j] < minFreq) { // check valid frequency > 0
minFreq = counter[j];
answer = j;
}
}
System.out.println(answer);
For a wider range of input values (e.g. including negative values) of a relatively small count it is better to use a hashmap instead of a large sparse array.

What do I need to do to my current code to make the program run a multiplication table (using a two dimensional array)?

I'm trying to make a Java program that uses the input value from a user to calculate and list the products of two numbers up to the entered number. Like if a user enters 2, the program should calculate the products between the two numbers (1 *1, 1*2, 2*1, 2*2) stores the products in a two-dimensional array, and list the products. I'm not sure that I totally understand arrays and so I feel as though my code is problem not right in many instances, can someone please tell me what I should do to my current code to make it work properly. Thanks in advance! :)
import java.util.Scanner;
public class ProductTable {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String inputString;
char letter = 'y';
// Prompt the user to enter an integer
while(letter != 'q') {
System.out.print("Enter a positive integer: ");
int integer = input.nextInt();
// Create an two-dimensional array to store products
int[][] m = new int[integer][3];
for (int j = 1; j <= m.length; j++) {
m[j][0] = input.nextInt();
m[j][1] = input.nextInt();
m[j][2] = input.nextInt();
}
// Display the number title
System.out.print(" ");
for (int j = 1; j <= m.length; j++)
System.out.print(" " + j);
System.out.println("\n--- ");
// Display table body
for (int i = 1; i <= m.length + 1; i++) {
System.out.print(i);
for (int j = 1; j <= m.length + 1; i++) {
System.out.printf("%4d", i * j);
}
System.out.println();
}
// Prompt the user to either continue or quit
System.out.print("Enter q to quit or any other key to continue: ");
String character = input.nextLine();
inputString = input.nextLine();
letter = inputString.charAt(0);
}
}
}
You can achieve your multiplication table by iterating over 2 counter variables as you already did in your output
// Display table body
// loops running out of bounds (until m.length + 1 instead of m.length-1)
for (int i = 1; i <= m.length + 1; i++) {
System.out.print(i);
for (int j = 1; j <= m.length + 1; i++) { // missed to increment j here
System.out.printf("%4d", i * j);
}
System.out.println();
}
Arrays should be based on 0, that means an array with 3 fields has the indexes 0, 1, 2. So the last index is length-1. Your condition <=m.length+1 runs out of bounds. index<length will work since 2 is less than 3, but not 3 less than 3.
You have also a typo: In the inner loop you are doing an increment of i instead of j, so the loop will run infinite.
Try to create an outer and inner loop as you did, but with start index 0 and end condition index<inputValue. Calculate multiTable[index1][index2] = (1+index1)*(1+index2).
Then do a similar loop and print the array fields. You don't need to use an array, you could output directly as you did. But you wanted to practice handling arrays.
Since you want to learn and understand, I don't like just to write the code. imagine an array with 3 fields having the indexes 0, 1, 2:
index 0 | 1 | 2
value 1 | 2 | 3
Now you would check the length at first, that's 3. You start with 0 and count while the counter does not reach the length since the zero based index is 1 below our natural order (0,1,2 vs. 1,2,3). That is the case as long the index is below the length, meaning index<length.
Try this logic (pseudo code). To simplify the problem we include the 0 in our product table. So we get 0*0, 0*1, 0*2... Doing so, we do not have to ignore index 0 or to calculate between index 0 should represent value 1.
maxNumber = userInput()
outer loop idx1 from 0 to maxNumber
inner loop idx2 from 0 to maxNumber
array[idx1][idx2] = (idx1) * (idx2)
end loop
end loop
Do the same to dump your generated array to screen.
First get it running. Afterwards you could try to alter the logic, so that it shows you only numbers from 1 to max.

2D Array Scores

This is the original prompt:
Write program that declares a 2-dimensional array of doubles called scores with three rows and three columns. Use a nested while loop to get the nine (3 x 3) doubles from the user at the command line. Finally, use a nested for loop to compute the average of the doubles in each row and output these three averages to the command line.
Here is my code:
import java.util.Scanner;
public class Scorer {
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
double [][] scores = new double[3][3];
double value = 0;
int i = 0;
int j;
while (i < 3) {
j = 0;
while (j < 3) {
System.out.print("Enter a number: ");
value = scnr.nextDouble();
scores[i][j] = value;
j++;
}
i++;
}
int average = 0;
for (i = 0; i < scores.length; i++) {
for (j = 0; j < scores[i].length; j++) {
average += value;
value = value / scores[i][j];
System.out.println(value);
}
}
}
}
The part that I need help on is the nested for loop at the bottom of the code. This code is supposed to compute the average of the numbers that are entered; however, I am confused on how to do that with the nested for loop.
you're almost there!
Here are the things you need to do:
1)you've to initialize the variable 'average' after the first for loop.
because average needs to be 0 i.e., reset after second for loop ends each time.
2)you've defined "value = value / scores[i][j]" . I don't know why you did that, but "value = scores[i][j]" must solve your problem.
3) you should print the average only thrice i.e., after calculating average of each row. so, print average at the end of second for loop.
Hope this makes it clear.
here's the code for your reference:
for (i = 0; i < 3; i++) {
int average = 0;
for (j = 0; j < 3; j++) {
value = scores[i][j];
average += value;
}
System.out.println(average/3);
}
Ever i represents a row, every j represents a column.
You need the average of every row, meaning that for every same i and every different j for that i you need to store the values and calculate the average.
Looks like homework code. We can give you hints but not write it for you :(

How to change position of the elements in an array

I'm writing a program, that take 10 Integers from a keyboard and list them in an array indexed from 0-9, and reports the position of the lowest number in the array. If the lowest number has any other position than 0, then the program is supposed to switch the position of the lowest number input with the number in the first position in the array:
import java.util.Scanner;
public class q35 {
public static void main(String args[]) {
Scanner tastatur = new Scanner(System.in);
int[] helTall = new int[10];
int input;
int lowest = Integer.MAX_VALUE;
for(int i=0;i<helTall.length;i++) {
System.out.println("Integers? ");
input = tastatur.nextInt();
if (input < lowest) {
lowest = input;
}
helTall[i] = input;
}
for (int i = 0; i < helTall.length; i++) {
helTall[0] = lowest;
System.out.println(helTall[i]);
}
System.out.println("Lowest number is " + lowest);
}
}
The only problem is that instead of changing position with the lowest number with the number at helTall[0], it just completely replaces the first number in the sequence helTall[0] with the lowest Integer, that way if my input is 4 5 63 23 6 -4 7 33 23 99, then the output becomes -4 5 63 23 6 -4 7 33 23 99 (as you can see the first input number is completely erased), but it should have been -4 5 63 23 6 4 7 33 23 99 any tips/advice/solutions? Thanks in advance.
You should keep track of the index of the lowest number (each time you write lowest = input; you should add lowestIndex=i;.
Then helTall[lowestIndex] will be the lowest number.
So you swap helTall[lowestIndex] with helTall[0] instead of just overwriting the value of helTall[0].
I thought it was enough to describe the solution in words, but I guess it wasn't...
int lowest = Integer.MAX_VALUE;
int lowestIndex = 0;
for(int i=0;i<helTall.length;i++){
System.out.println("Integers? ");
input = tastatur.nextInt();
if (input < lowest){
lowest = input;
lowestIndex = i;
}
helTall[i]=input;
}
// swap the numbers
if (lowestIndex > 0) {
int temp = lowest;
helTall[lowestIndex] = helTall[0];
helTall[0] = temp;
}
// display output
for (int i = 0; i < helTall.length; i ++) {
System.out.println(helTall[i]);
}
This part is wrong:
for (int i = 0; i < helTall.length; i ++) {
helTall[0]=lowest;
System.out.println(helTall[i]);
}
First, you do not need to repeatedly (10 times) put lowest into helTall[0]. Let's move that outside first so it's only done once:
helTall[0]=lowest;
for (int i = 0; i < helTall.length; i ++) {
System.out.println(helTall[i]);
}
Next, the line we put outside the loop overwrites the helTall[0] without regard for what is already in there. We need to temporarily save that number in there elsewhere, then overwrite the spot, so that we can use it to overwrite the spot where the lowest number was:
int numberAtLocationZero = helTall[0];
helTall[0]=lowest;
// Now, on this line we need to write helTall[lowestNumberIndex] = numberAtLocationZero;
for (int i = 0; i < helTall.length; i ++) {
System.out.println(helTall[i]);
}
In the above code I wrote a comment. It relies on you either knowing where in the array the lowest number was, or to find it again. If you add a new variable to your code that takes in values called lowestNumberIndex and update that each time you detect a new lowest number, then you're pretty much done if you uncomment my comment.
You need a separate variable to store what is in the bottom of the array before you overwrite it with your new lowest input number.
helTall[0]=lowest;
should be proceeded by
int placeholder = helTall[0]
and followed by
hellTall[i] = placeholder;
This way, you'll end up swapping the two array elements, the first swapped with the lowest. Is this what you're trying to accomplish? This leaves most of the array unsorted.

Finding repeats in a 2D array

I have made a program that outputs the number of repeats in a 2D array. The problem is that it outputs the same number twice.
For example: I input the numbers in the 2D array through Scanner: 10 10 9 28 29 9 1 28.
The output I get is:
Number 10 repeats 2 times.
Number 10 repeats 2 times.
Number 9 repeats 2 times.
Number 28 repeats 2 times.
Number 29 repeats 1 times.
Number 9 repeats 2 times.
Number 1 repeats 1 times.
Number 28 repeats 2 times.
I want it so it skips the number if it has already found the number of repeats for it. The output should be:
Number 10 repeats 2 times.
Number 9 repeats 2 times.
Number 28 repeats 2 times.
Number 29 repeats 1 times.
Number 1 repeats 1 times.
Here is my code:
import java.util.Scanner;
public class Repeat
{
static Scanner leopard = new Scanner(System.in);
public static void main(String [] args)
{
final int ROW = 10; //Row size
final int COL = 10; //Column size
int [][] num = new int[ROW][COL];
int size;
//Get input
size = getData(num);
//Find repeat
findRepeats(num, size);
}
public static int getData(int [][] num)
{
int input = 0, actualSize = 0; //Hold input and actualSize of array
System.out.print("Enter positive integers (-999 to stop): ");
//Ask for input
for(int i = 0; i < num.length && input != -999; i++)
{
for(int j = 0; j < num[i].length && input != -999; j++)
{
input = leopard.nextInt();
//Check if end
if(input != -999)
{
num[i][j] = input;
actualSize++;
}
}
}
System.out.println();
return actualSize;
}
public static void findRepeats(int [][] num, int size)
{
int findNum;
int total = 0, row = 0, col = 0;
for(int x = 0; x < size; x++)
{
//Set to number
findNum = num[row][col];
//Loop through whole array to find repeats
for(int i = 0; i < num.length; i++)
{
for(int j = 0; j < num[i].length; j++)
{
if(num[i][j] == findNum)
total++;
}
}
//Cycle array to set next number
if(col < num[0].length-1)
col++;
else
{
row++; //Go to next row if no more columns
col = 0; //Reset column number
}
//Display total repeats
System.out.println("Number " + findNum + " appears " + total + " times.");
total = 0;
}
}
}
I know why it is doing it, but I cannot figure out how to check if the number has already been checked for it to skip that number and go to the next number. I cannot use any classes or code that is not used in the code.
Since you cannot use anything other than this, lets say, basic elements of Java consider this:
Make another temporary 2D array with two columns (or just two separate arrays, personally I prefer this one). On the start of the algorithm the new arrays are empty.
When you take a number (any number) from the source 2D structure, first check if it is present in the first temporary array. If it is, just increment the value (count) in the second temporary array for one (+1). If it is not present in the first tmp array, add it to it and increase the count (+1) in the second at the same index as the newly added number in the first (which should be the last item of the array, basically).
This way you are building pairs of numbers in two arrays. The first array holds all your distinct values found in the 2D array, and the second one the number of appearances of the respective number from the first.
At the and of the algorithm just iterate the both arrays in parallel and you should have your school task finished. I could (and anyone) code this out but we are not really doing you a favor since this is a very typical school assignment.
It's counting the number two times, first time it appears in the code and second time when it appears in the code.
To avoid that keep a system to check if you have already checked for that number. I see you use check int array but you haven't used it anywhere in the code.
Do this,
Put the number in the check list if you have already found the count of it.
int count = 0;
check[count] = findNum;
count++;
Note: You can prefill you array with negative numbers at first in order to avoid for having numbers that user already gave you in input.
Next time in your for loop skip checking that number which you have already found a count for
for(int x = 0; x < size; x++) {
findNum = num[row][col];
if(check.containsNumber(findNUm)) { //sorry there is no such thing as contains for array, write another function here which checks if a number exists in the array
//skip the your code till the end of the first for loop, or in other words then don't run the code inside the for loop at all.
}
}
Frankly speaking I think you have just started to learn coding. Good luck! with that but this code can be improved a lot better. A piece of advice never create a situation where you have to use 3 nested for loops.
I hope that you understood my solution and you know how to do it.
All answers gives you some insight about the problem. I try to stick to your code, and add a little trick of swap. With this code you don't need to check if the number is already outputted or not. I appreciate your comments, structured approach of coding, and ask a question as clear as possible.
public static void findRepeats(int [][] num, int size)
{
int findNum;
int total = 1, row = 0, col = 0;
int [] check = new int[size];
while(row < num.length && col < num[0].length)
{
//Set to number
findNum = num[row][col];
//Cycle array to set next number
if(col < num[0].length-1)
col++;
else
{
row++; //Go to next row if no more columns
col = 0; //Reset column number
}
//Loop through whole array to find repeats
for(int i = row; i < num.length; i++)
{
for(int j = col; j < num[i].length; j++)
{
if(num[i][j] == findNum) {
total++;
//Cycle array to set next number
if(col < num[0].length-1)
col++;
else
{
row++; //Go to next row if no more columns
col = 0; //Reset column number
}
if(row < num.length - 1 && col < num[0].length -1)
num[i][j] = num[row][col];
}
}
}
//Display total repeats
System.out.println("Number " + findNum + " appears " + total + " times.");
total = 1;
}
}
you can use a HashMap to store the result. It Goes like this:
// Create a hash map
HashMap arrayRepeat = new HashMap();
// Put elements to the map
arrayRepeat.put(Number, Repeated);

Categories