Printing the Bigger to smaller with Array on Java - 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?

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

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

Second smallest number in array - incorrect output

So this has been bugging me for a hour and a half and it's getting really annoying. I'm not sure what's the issue because I've rechecked several times my code, I've googled and searched for other ways or if I did something wrong and I still don't see anything.
This is an excerpt from my code:
// MAX AND MIN NUMBER
int maxnum = array[0];
int minnum = array[0];
for (int i = 0; i < array.length; i++) {
if (array[i] > maxnum) {
maxnum = array[i];
} else if (array[i] < minnum) {
minnum = array[i];
}
}
System.out.println("Maximum number is " + maxnum + ", minimum is " + minnum + ".");
// --------------------------------------------------------------------------------
// SECOND SMALLEST NUMBER
int secondnum = array[0];
for (int i = 0; i < array.length; i++) {
if (array[i] == minnum) {
secondnum = minnum;
} else if (array[i] < minnum) {
secondnum = minnum;
minnum = array[i];
} else if (array[i] < secondnum) {
secondnum = array[i];
}
}
System.out.println("Second smallest number is " + secondnum + ".");
array[i] is an input from the user. At the beginning, the program asks you to enter how many numbers you wish to enter and then you proceed to enter them.
I put in five and I enter five numbers but my second smallest number is the same as my smallest number. Sometimes I get a correct answer.
Any ideas?
EDIT:
Works with user3591111's help. Thanks!
In the first for you can start in i =1 in the second for try.
int secondnum = maxnum;
for (int i = 0; i < array.length; i++) {
if (array[i] != minnum && array[i] <secondnum )
secondnum = array[i];
}
This wont work if you have repeated numbers because your small can be equal to your second small for that you will need to store the location of your small number and in the if change to if ((array[i] != minnum || smallPos != i) && array[i] <secondnum )

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

Getting min and max values from an array - Java

import java.util.Scanner;
public class Sales {
public static void main(String[] args) {
int[] sales;
sales = getSales();
printSales(sales);
printSummary(sales);
}
private static int[] getSales() {
Scanner input = new Scanner(System.in);
int[] temp;
System.out.print("Enter the number of salespeople: ");
temp = new int[input.nextInt()];
for (int i = 0; i < temp.length; i++) {
System.out.print("Enter sales for salesperson " +
(i + 1) + ": ");
temp[i] = input.nextInt();
}
return temp;
}
private static void printSales(int[] s) {
System.out.println();
System.out.println("Salesperson Sales");
System.out.println("----------- -----");
for (int i = 0; i < s.length; i++) {
System.out.printf("%6d%12d\n", i + 1, s[i]);
}
}
private static void printSummary(int[] s) {
int sum = 0;
int max_sale = 0; // Salesperson with the most sales
int min_sale = 0; // Salesperson with the least sales
for (int i = 0; i < s.length; i++) {
sum = (s[i] + sum);
if (s[i] > max_sale)
max_sale = s[1];
else if (s[i] > min_sale)
s[i] = min_sale;
}
System.out.println();
System.out.println("Total sales: " + sum);
System.out.println("Average sales: " + (double)sum / s.length);
System.out.println("Salesperson " + (max_sale + 1) +
" had the maximum sale with " +
s[max_sale]);
System.out.println("Salesperson " + (min_sale + 1) +
" had the minimum sale with " +
s[min_sale]);
}
}
The purpose of the application is to take the number of salespeople as input, along with their sales and then display individual sales, total sales, and average. That is working fine, but it's also supposed to display which salesperson had the max and minimum sales and what they were (lines 51 - 54). At the moment, any time the max is greater than the number of salespeople I get an ArrayIndexOutOfBoundsException and for whatever reason can't figure it out.
1 - Modify your for loop to get the max and min without modifying the array
2 - Try to print max and min instead of printing sum[max] and some[min] (which can throws IndexOutOfBoundsException)
3 - min_sale should be greater than 0, actually a value enough large (because you can have only positive values in your array)
To summarize :
int sum = 0;
int max_sale = Integer.MIN_VALUE; // Salesperson with the most sales
int min_sale = Integer.MAX_VALUE; // Salesperson with the least sales
for (int i = 0; i < s.length; i++){
sum = (s[i] + sum);
if (s[i] > max_sale)
max_sale = s[i];
else if (s[i] < min_sale)
min_sale = s[i];
}
System.out.println("Salesperson " +
" had the maximum sale with " +
max_sale);
System.out.println("Salesperson " +
" had the minimum sale with " +
min_sale);
if (s[i] > max_sale)
max_sale = s[1];
else if (s[i] > min_sale)
s[i] = min_sale;
Here you are trying to assign the value in s[1] to max_sales. whereas you should be assigning max_sale = i. and the if condition should be
if(s[i] > s[max_sale] )
Updated code:
for (int i = 0; i < s.length; i++)
{
sum = (s[i] + sum);
// Finds the index of the sales person with best sales
if (s[i] >= s[max_sale])
max_sale = i;
// If this sales person is not the best, then check if he is last
else if (s[min_sale] > s[i])
min_sale = i;
}
the specific problem that's causing your error is here,
System.out.println("Salesperson " + (max_sale + 1) +
" had the maximum sale with " +
s[max_sale]);
System.out.println("Salesperson " + (min_sale + 1) +
" had the minimum sale with " +
s[min_sale]);
you're using your result as though it were an index
change it to the following
System.out.println("Salesperson " + (max_sale + 1) +
" had the maximum sale with " +
max_sale);
System.out.println("Salesperson " + (min_sale + 1) +
" had the minimum sale with " +
min_sale);

Categories