Why am I not getting the minimum value in an array JAVA? - 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

Related

Printing the Bigger to smaller with Array on Java

I have this program:
int i;
double array[] = new double[10] ;
Scanner input = new Scanner (System.in) ;
for (i = 0; i < 10; i++) {
System.out.println("Give " + (i+1) + " number") ;
array[i] = input.nextDouble();
}
double max = array[0], min =array[0];
int negative = 0 ;
for (i = 0; i < 10; i++){
if(array[i] > max){
max = array[i] ;
}
if (array[i] < min){
min = array[i];
}
}
//Difference between Biggest to Smaller number
for (i = 0; i < 10; i++){
// ...
}
for (i = 0; i < 10; i++){
if(array[i] < 0){
negative = negative + 1;
}
for (i = 0; i < 10; i++){
System.out.println("The double of " + (i+1) + " is: " + (array[i] * 2) );
}
System.out.println("\n" + "The Maximum is: " + max);
System.out.println("The Minimum : " + min);
System.out.println("The Difference between Biggest to Smaller is : ");
System.out.println("Τhe count of negative objects are : " + negative);
I want to print the bigger to smaller number difference with values I have now.
I forgot what I need to add. I searched online, but the most of answers that I found, conflict with the whole program.
Any suggestion here?

How to understand why this does not return the minimum value?

Please help to understand why, in the following, I didn't get output of minimum value in array. Using scanner for input value, the program should output also the minimum number.
** without using .sort.
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] list;
list = new int[10000];
int sum = 0;
double avr = 0;
int min = list [0];
int max = list [0];
int x = 0;
int value;
Scanner input = new Scanner(System.in);
System.out.print("Add number " + (x + 1) + ": ");
value = input.nextInt();
while (x <= list.length && (value != -1 || x == 0)) {
list[x] = value;
x++;
System.out.print("Add number " + (x + 1) + ": ");
value = input.nextInt();
}
for (int i = 0; i < list.length; i++) {
sum += list[i];
avr = sum / x;
if(list[i] > max)
max = list[i];
if( list[i] < min )
min = list[i];
}
System.out.println("The sum of all values are: " + sum);
System.out.println("The average value of the numbers are: " + avr);
System.out.println("The maximum value of the numbers are: " + max);
System.out.println("The minimum value of the numbers are: " + min);
}
}
I will suggest two improvements at the bare minimum.
Initialize your min to the first value. Right now you have min initialized to 0 and user never enters any number smaller than 0. That's why you keep getting 0 for min.
Don't run your loops 1000 times. Run it as many times as there are non zero elements.
See the following working snippet:
int[] list;
list = new int[10000];
int sum = 0;
double avr = 0;
int min = list [0];
int max = list [0];
int x = 0;
int value;
Scanner input = new Scanner(System.in);
System.out.print("Add number " + (x + 1) + ": ");
value = input.nextInt();
min = value;
while (x <= list.length && (value != -1 || x == 0)) {
list[x] = value;
x++;
System.out.print("Add number " + (x + 1) + ": ");
value = input.nextInt();
}
for (int i = 0; i < list.length && list[i] !=0; i++) {
sum += list[i];
avr = sum / x;
if(list[i] > max)
max = list[i];
if( list[i] < min )
min = list[i];
}
System.out.println("The sum of all values are: " + sum);
System.out.println("The average value of the numbers are: " + avr);
System.out.println("The maximum value of the numbers are: " + max);
System.out.println("The minimum value of the numbers are: " + min);

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

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;
}
}
}

JAVA - How to store max and minimum values in an array that's inside a loop?

I need to store the min and max values in an array given, then print them out with specific characters (+ for the maximum values, "-" for the minimum value, and "*" for all the rest).
I think I have most of it completed except for the storing values appropriately so that all the values are not "++++++++++...." like they currently are when printed out.
Any ideas? Help is greatly appreciated.
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
int numbers[] = new int[24];
int min = Integer.MAX_VALUE;
int max=Integer.MIN_VALUE;
int maxhour = 0;
int minhour = 0;
int total = 0;
char MAX = '+', MIN = '-', MIDDLE = '*';
char currentchar = 0;
for(int i=0; i< numbers.length; i++){
numbers[i] = keyboard.nextInt();
total = total + numbers[i];
if(numbers[i]<min){
min = numbers[i];
minhour = i;
currentchar = MIN;
}else if (numbers[i]>max){
max = numbers[i];
maxhour = i;
currentchar = MAX;
}
}
for(int i=0; i< numbers.length; i++){
System.out.print("Hour " + i + ":");
printTimes(currentchar, numbers[i]);
System.out.println("");
}
System.out.println("Largest Number of hits is : " + max + " at hour " + maxhour);
System.out.println("Average Number of hits is : " + (total/24) + " per hour");
System.out.println("Smallest Number of hits is : " + min + " at hour " + minhour);
}
public static void printTimes(char c, int times) {
if (times >= 70) {
for(int i=0; i< 69; i++){
System.out.print(c);
} System.out.print(">");
} else if (times < 70) {
for(int i=0; i< times; i++)
System.out.print(c);
}
}
}
Example of current output:
42 29 36 7 5 3 10 13 33 40 51 49
22 58 63 102 65 58 48 24 36 48 52 42
Hour 0:++++++++++++++++++++++++++++++++++++++++++
Hour 1:+++++++++++++++++++++++++++++
Hour 2:++++++++++++++++++++++++++++++++++++
Hour 3:+++++++
Hour 4:+++++
Hour 5:+++
Hour 6:++++++++++
Hour 7:+++++++++++++
Hour 8:+++++++++++++++++++++++++++++++++
Hour 9:++++++++++++++++++++++++++++++++++++++++
....
Largest Number of hits is : 102 at hour 15
Average Number of hits is : 39 per hour
Smallest Number of hits is : 3 at hour 5
Just change your last for:
for (int i = 0; i < numbers.length; i++) {
System.out.print("Hour " + i + ":");
if (numbers[i] == min)
currentchar = MIN;
else if (numbers[i] == max)
currentchar = MAX;
else
currentchar = MIDDLE;
printTimes(currentchar, numbers[i]);
System.out.println("");
}
I would update your code as follows -
public static void main(String[] args) {
int numbers[] = new int[24];
int total = 0;
System.out.println("Enter 24 integers please");
Scanner keyboard = null;
try {
keyboard = new Scanner(System.in);
for (int i = 0; i < numbers.length; i++) {
numbers[i] = keyboard.nextInt();
total += numbers[i];
}
} finally {
keyboard.close();
}
Integer min = null;
Integer max = null;
int maxhour = 0;
int minhour = 0;
char MAX = '+', MIN = '-', MIDDLE = '*';
for (int i = 0; i < numbers.length; i++) {
if (min == null || numbers[i] < min) {
min = numbers[i];
minhour = i;
} else if (max == null || numbers[i] > max) {
max = numbers[i];
maxhour = i;
}
}
for (int i = 0; i < numbers.length; i++) {
char currentchar = MIDDLE;
if (i == minhour) {
currentchar = MIN;
} else if (i == maxhour) {
currentchar = MAX;
}
System.out.print("Hour " + i + ":");
printTimes(currentchar, numbers[i]);
System.out.println("");
}
System.out.println("Largest Number of hits is : "
+ max + " at hour " + maxhour);
System.out.println("Average Number of hits is : "
+ (total / 24) + " per hour");
System.out.println("Smallest Number of hits is : "
+ min + " at hour " + minhour);
}

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;

Categories