How to get minimum and maximum individually and not as a whole? - java

Program does this:
Check if the max and min changes over the whole course of runs, and then prints them to the respective variables after each instance is complete based on user input.
But what I want it to do is:
Check the max and min for each individual run, then print them to the respective variables.
How can I change the code below so that it gets the max and min of each run instead of the overall max and min?
Here is the code:
package test;
import java.util.*;
public class Test {
public static void main(String[] args) {
Random generator = new Random();
float smallest, largest;
int years;
float[] array = new float[12];
smallest = Integer.MAX_VALUE;
largest = Integer.MIN_VALUE;
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter desired years: ");
years = keyboard.nextInt();
for (int i = 1; i <= years; i++){
System.out.println("Year " + i);
for (int month = 1; month <= array.length; month++){
array[i] = generator.nextFloat() * 100;
System.out.println("Month " + month + ": " + array[i]);
if(array[i] < smallest)
smallest = array[i];
if(array[i] > largest)
largest = array[i];
}
System.out.println("Max = " + largest);
System.out.println("Min = " + smallest);
}

You aren't resetting largest and smallest at the end of the loop.
package test;
import java.util.*;
public class Test {
public static void main(String[] args) {
Random generator = new Random();
float smallest, largest;
int years;
float[] array = new float[12];
smallest = Integer.MAX_VALUE;
largest = Integer.MIN_VALUE;
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter desired years: ");
years = keyboard.nextInt();
for (int i = 1; i <= years; i++) {
System.out.println("Year " + i);
for (int month = 1; month <= array.length; month++) {
array[i] = generator.nextFloat() * 100;
System.out.println("Month " + month + ": " + array[i]);
if (array[i] < smallest)
smallest = array[i];
if (array[i] > largest)
largest = array[i];
}
System.out.println("Max = " + largest);
System.out.println("Min = " + smallest);
// HERE
// Reset largest and smallest
largest = Integer.MIN_VALUE;
smallest = Integer.MAX_VALUE;
}
}
}

Related

Why am I not getting the minimum value in an array JAVA?

import java.util.*;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the size ??");
int n = sc.nextInt();
int[] marks = new int[n + 1];
for (int i = 0; i < n; i++) {
System.out.println("Enter " + i + " number ??");
marks[i] = sc.nextInt();
}
System.out.println("The following numbers are : ");
for (int j = 0; j < marks.length - 1; j++) {
System.out.println(marks[j] + " ");
}
int max = marks[0];
int min = marks[0];
int s = marks.length;
for (int i = 0; i < s; i++) {
if (marks[i] > max) {
max = marks[i];
}
if (marks[i] < min) {
min = marks[i];
}
}
System.out.println("Max is " + max + " Min is " + min);
}
Output:
Enter the size ??2
Enter 0 number ??
56
Enter 1 number ??
56
The following numbers are :
56
56
Max is 56 Min is 0
Hello and welcome to StackOverflow. Next time, febore you jump into the internet for help, please try this approach. It will solve your problem much quicker.
for (int i = 0; i < s; i++) {
System.out.println("DEBUG: number in index " + i + " is " + marks[i]);
if (marks[i] > max) {
System.out.println("DEBUG: number is greater than current max " + max);
max = marks[i];
}
if (marks[i] < min) {
System.out.println("DEBUG: number is smaller than current min " + min);
min = marks[i];
}
}
This process is called "debugging". It can be done by adding spurious amount of logging like above or by stepping through the code with a debugger.
The size of mark array is 3 in the above example
int[] marks = new int[n + 1];
Let total number of marks is (n) : 2
We are creating the array with size of 3.
By default value for int primitives in java is 0
marks array is created with size 3
{56, 56, 0}
As per the logic the minimum value among these three elements are 0

min max in array issue

So I have a program I wrote that finds the max and min value of a five number set. It works for the most part but when I enter a set of numbers like {5,6,7,8,9} then it outputs 9 as the max, but outputs 0 for the min. Any thoughts or suggestions.
import java.util.Scanner;
public class MinMax {
public static void main (String [] args) {
#SuppressWarnings("resource")
Scanner in = new Scanner (System.in);
final int NUM_ELEMENTS = 5;
double[] userVals = new double[NUM_ELEMENTS];
int i = 0;
double max = 0.0;
double min = 0.0;
System.out.println("Enter five numbers.");
System.out.println();
while (i < NUM_ELEMENTS) {
System.out.println("Enter next number: ");
userVals[i] = in.nextDouble();
i++;
System.out.println();
}
for (i = 0; i < userVals.length; i++) {
if (userVals[i] > max) {
max = userVals[i];
}
else if (userVals[i] < min) {
min = userVals[i];
}
}
System.out.println("Max number: " + max);
System.out.println("Min number: " + min);
}
}
Default your min to a number out of range (like Double.MAX_VALUE), and max to Double.MIN_VALUE. You might also simplify your code by removing the second loop; you can perform the logic in one loop and you might use Math.max(double, double) and Math.min(double, double). Something like,
Scanner in = new Scanner(System.in);
final int NUM_ELEMENTS = 5;
double[] userVals = new double[NUM_ELEMENTS];
System.out.println("Enter five numbers.");
System.out.println();
double min = Double.POSITIVE_INFINITY;
double max = Double.NEGATIVE_INFINITY;
for (int i = 0; i < NUM_ELEMENTS; i++) {
System.out.println("Enter next number: ");
userVals[i] = in.nextDouble();
min = Math.min(min, userVals[i]);
max = Math.max(max, userVals[i]);
}
System.out.println("Max number: " + max);
System.out.println("Min number: " + min);
Intialize your min variable to non-zero max value. Means max value that you can have in your input from console.

Find largest and smallest numbers number using Arrays

Trying to let users enter number of integers so I can set array length then find the max and min value. However, I can't find max and min. Please help.
import java.util.Scanner;
import java.util.Arrays;
public class ExerciseC{
public static void main(String[] args){
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter the number of integers you would like to enter:");
int numberEnter = keyboard.nextInt();
System.out.println("Enter some integers:");
int integers = keyboard.nextInt();
int numbers [] = new int [numberEnter];
int maxValue = numbers[0];
int minValue = numbers[0];
int max = 0;
int min = 0;
for (int index = 1; index < numbers.length; index ++) {
if (numbers[index] > maxValue) {
maxValue = numbers [index];
}
}
System.out.println("Print: " + maxValue);
System.out.println("The difference between the largest and the smallest is: ");
}
}
You don't seem to be entering more then one value (and you never store integers in your array). Also, you aren't setting the min. I think you wanted
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
System.out.println("Number of integers to enter:");
int numberEnter = keyboard.nextInt();
int numbers[] = new int[numberEnter];
int pos = 0;
do {
System.out.printf("Please enter integer #%d/%d:%n", pos, numberEnter);
numbers[pos++] = keyboard.nextInt();
} while (pos < numberEnter && keyboard.hasNextInt());
int min = numbers[0];
int max = numbers[0];
for (pos = 1; pos < numbers.length; pos++) {
if (numbers[pos] < min) { // <-- test min.
min = numbers[pos];
}
if (numbers[pos] > max) { // <-- test max.
max = numbers[pos];
}
}
// Display everything.
System.out.printf("%s Min: %d Max: %d%n", Arrays.toString(numbers),
min, max);
}
Your numbers[] is empty. The user's input is not stored into the array.
Here is your fixed code:
package com.company;
import java.util.Scanner;
public class ExerciseC{
public static void main(String[] args){
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter the number of integers you would like to enter:");
int numberEnter = keyboard.nextInt();
int numbers [] = new int [numberEnter];
for (int i = 0; i < numberEnter; i++) {
System.out.println("Enter integer:");
numbers[i] = keyboard.nextInt();
}
int maxValue = numbers[0];
int minValue = numbers[0];
for (int index = 1; index < numbers.length; index ++) {
if (numbers[index] > maxValue) {
maxValue = numbers [index];
}
}
System.out.println("Print: " + maxValue);
System.out.println("The difference between the largest and the smallest is: ");
}
}
import java.util.Scanner;
class StdR {
public static void main(String[] args) {
// TODO Auto-generated method stub
StdR st = new StdR();
st.stdR();
//System.out.println(st.stdR();
}
void stdR()
{
char[] grade = {'A','B','C','D','E','F'};
Scanner input = new Scanner(System.in);
byte[] st = new byte[3];
double[] percentage = new double[st.length];
for(byte s = 0; s < st.length; s++){
System.out.println("\nStudent " + s);
short noOfMarks = 0;
short totalMarks = 450;
percentage[s] = 0.0;
byte[] marks = new byte[5];
for (byte i = 0; i < marks.length; i++){
System.out.println("Enter marks of Chapter "+ i + ": ");
marks[i] = input.nextByte();
noOfMarks += marks[i];
percentage[s] += (marks[i] * 100) / totalMarks;
}
System.out.print("No of marks: " + noOfMarks + "\t");
System.out.print("Percentage: " + percentage[s] + "\t");
if (percentage[s] > 79.0 && percentage[s] < 100.1)
System.out.print("Grade: " + grade[0]);
else if (percentage[s] > 69.0 && percentage[s] < 80.0)
System.out.print("Grade: " + grade[1]);
else if (percentage[s] > 59.0 && percentage[s] < 70.0)
System.out.print("Grade: " + grade[2]);
else if (percentage[s] > 49.0 && percentage[s] < 60.0)
System.out.print("Grade: " + grade[3]);
else if (percentage[s] > 39.0 && percentage[s] < 50.0)
System.out.print("Grade: " + grade[4]);
else if (percentage[s] < 40.0)
System.out.print("Grade: " + grade[5]);
}
double smallest = percentage[0] , largest= percentage[0];
for (int i=0 ;i< percentage.length; i++) {
if (percentage[i] < smallest) {
smallest = percentage[i];
} // end finding smallest
if (percentage[i] > largest) {
largest = percentage[i];
}
}
System.out.println("\n1st Position and Top percentage is " + largest);
System.out.println("\nLast Position and Least percentage is "+smallest);
}
}

How to return person with max sales in salesperson program- JAVA [PT. 2]

Sorry if I'm being bothersome. But I am stuck on another part of my HW problem. I am now tasked at instead of having an salesperson 0, salespersons start with 1. I have tried to solve this by parsing the strings i to an integer and adding 1. Seemed like it would work. However, for some reason the calculating of min is not correct. It stays at 0. I've tried stepping through the code and seeing why, but I cannot see it.
import java.util.Scanner;
public class Cray {
public static void main(String[] args){
final int SALESPEOPLE = 5;
int[] sales = new int[SALESPEOPLE];
int sum, randomValue, numGreater = 0;
int max = sales[0];
int min = sales[0];
int maxperson = 0;
int minperson = 0;
Scanner scan = new Scanner(System.in);
for (int i=0; i<sales.length; i++)
{
//To attempt print out the information of salesperson 0 and salesperson 1, I returned the integer value of i and added one.
System.out.print("Enter sales for salesperson " + Integer.valueOf(i+1) + ": ");
sales[i] = scan.nextInt();
//max if statemnent works fine and is correct
if(sales[i] > max){
max= sales[i];
maxperson = i;
}
//For some reason max is calculating but not min.
if(sales[i] < min){
min = sales [i];
minperson= i;
}
}
System.out.println("\nSalesperson Sales");
System.out.println("--------------------");
sum = 0;
for (int i=0; i<sales.length; i++)
{
System.out.println(" " + Integer.valueOf(i+1) + " " + sales[i]);
sum += sales[i];
}
System.out.println("\nTotal sales: " + sum);
System.out.println("Average sales: " + sum/5);
System.out.println();
System.out.println("Salesperson " + Integer.valueOf(maxperson+1) + " had the most sales with " + max );
System.out.println("Salesperson " + Integer.valueOf(minperson+1) + " had the least sales with " + min);
System.out.println();
System.out.println("Enter any value to compare to the sales team: ");
randomValue = scan.nextInt();
System.out.println();
for(int r=0; r < sales.length; r++){
if(sales[r] > randomValue){
numGreater++;
System.out.println("Salesperson " + Integer.valueOf(r+1) + " exceeded that amount with " + sales[r]);
}
}
System.out.println();
System.out.println("In total, " + numGreater + " people exceeded that value");
}
}
The problem is that the min is never set as all positive sales will be > 0
You should initialise min as a large value so that a positive sales figure can be less then the minimum value specified in your if statement:
if (sales[i] < min) {
You could use:
int min = Integer.MAX_VALUE;

How to return person with max sales in salesperson program- JAVA

Kind of confused by one part of my HW problem. It seems like it would be easy, but I can't put my finger on it. I am trying to return the salesperson with the max and min sales. I am confused on how to do that. Help would be much appreciated.
import java.util.Scanner;
public class Cray {
public static void main(String[] args){
final int SALESPEOPLE = 5;
int[] sales = new int[SALESPEOPLE];
int sum, maxperson, minperson;
int max = sales[0];
int min = sales[0];
Scanner scan = new Scanner(System.in);
//Fill the 5 element array of sales with the user's input
for (int i=0; i<sales.length; i++)
{
System.out.print("Enter sales for salesperson " + i + ": ");
sales[i] = scan.nextInt();
//Calculate the max and min sales
//How do I return the salesperson i with the the max and min sales?
if(sales[i] > max){
max= sales[i];
}
if(sales[i] < min){
min = sales [i];
}
}
System.out.println("\nSalesperson Sales");
System.out.println("--------------------");
sum = 0;
for (int i=0; i<sales.length; i++)
{
System.out.println(" " + i + " " + sales[i]);
sum += sales[i];
}
System.out.println("\nTotal sales: " + sum);
System.out.println("Average sales: " + sum/5);
//WHere I want to print the max salesperson.
System.out.println("Salesperson" + );
}
}
you can store the index count
declare and initialize two int variables before you start scanning the input:
int maxindex=Integer.MIN_VALUE,minindex=Integer.MIN_VALUE;
And then assign your array index to them like this:
if(sales[i] > max){
max= sales[i];
maxindex = i;
}
if(sales[i] < min){
min = sales [i];
minindex = i;
}
Now you can directly get the salesperson with max and min sale
System.out.println("Max sales person "+sales[maxindex]);
System.out.println("Min sales person "+sales[minindex]);
I used a very simple trick here by storing the array indexes.
This is very basic approach though there may be more optimized approach for it.
You can adjust your check for maximum
if(sales[i] > max){
max = sales[i];
maxPerson = i;
}
Define two more variables as:
int maxSalePersonIndex = 0;
int minSalePersonIndex = 0;
Update your if as:
if(sales[i] > max){
max= sales[i];
maxSalePersonIndex = i;
}
if(sales[i] < min){
min = sales [i];
minSalePersonIndex = i;
}
In the end, just print the values of maxSalePersonIndex and minSalePersonIndex
System.out.println("Salesperson index with max sale = " + maxSalePersonIndex);
System.out.println("Salesperson index with min sale = " + minSalePersonIndex);
Currently in your for-loop, you're storing the maximum and minimum sales, not the salespeople who attained those sales. Therefore, you should be storing i in max / min, as opposed to sales[i].
int max = 0, int min = 0
...
// in the for-loop
if(sales[i] > sales[max]){
max = i; // i.e. salesperson "i"
}
if(sales[i] < sales[min]){
min = i;
}
...
System.out.println("Max salesperson: " + max);
System.out.println("Min salesperson: " + min);

Categories