# [SOLVED] Help with arrays, printing highest and lowest value of the array.

Show 40 post(s) from this thread on one page
Page 1 of 2 12 Last
• 11-02-2008, 03:20 AM
Sophiie
[SOLVED] Help with arrays, printing highest and lowest value of the array.
Thanks for the help. :)
• 11-02-2008, 03:30 AM
Fubarable
is this line called:
Code:

`min = sales[0];`
before the sales data has been entered into the sales array? If so, please understand that sales[0] and in fact sales[i] for any valid i will be zero. You will want to likely initialize your min and max variables just before your for loop and certainly after your sales array has been filled with the appropriate data.
• 11-02-2008, 03:35 AM
Sophiie
Can you explain a little further?

Do I set min to 0 when I declare it a double?

Would I have to change min = sales[0]?

• 11-02-2008, 03:42 AM
Fubarable
Here, look at and run this example. It should be self-explanatory:
Code:

```import java.util.Arrays; class Fubar {   private static final int ARRAY_SIZE = 5;   public static void main(String[] args)   {     int[] intArray = new int[ARRAY_SIZE];     int min1 = intArray[0]; // initialize min1 *before* filling the array     int min2;     // fill the array     for (int i = 0; i < intArray.length; i++)     {       intArray[i] = 5 + i;     }         min2 = intArray[0]; // initialize min2 *after* filling the array     for (int i = 0; i < intArray.length; i++)     {       // check for mins here       if (intArray[i] < min1)       {         min1 = intArray[i];       }       if (intArray[i] < min2)       {         min2 = intArray[i];       }     }         System.out.println("min1 = " + min1);     System.out.println("min2 = " + min2);     System.out.println("intArray = " + Arrays.toString(intArray));   } }```
• 11-02-2008, 06:35 PM
Sophiie
Thanks for ur help.
• 11-02-2008, 07:52 PM
Norm
To debug your code, add: System.out.println("min=" +min); inside your loop to show the value of min during every loop. Then you will see why min is always 0!

Hint:
When searching for min/max values, set the initial values of those variables to the end most value in the other direction. max = smallest possible value, min = largest possible value
• 11-02-2008, 10:35 PM
gamilah
am sorry sophiie i need the whole program ,I just started learning about arrays too and realy want to know whats wrong with ur program..
• 11-03-2008, 01:36 AM
Sophiie
Thank you Norm!!! The hint was exactly what I needed to figure this thing out!! Thanks again.

Thanks for everyone that tried to help! :)

Still, the second part of my help is not addressed. The min and max values are in reference to an array also. How do I make the min and max values in reference to that certain #?

1 - 23 min --- In the print out I want to say that 1 has the lowest value
but cant get it to work, also with max.
2 - 32
3 - 55 max
4 - 31

The array was initialized very early on and Im not sure how to make reference to it now with the min and max values.

Thanks for the help!! :)
• 11-03-2008, 03:29 AM
Norm
Quote:

make the min and max values in reference to that certain #?
Not sure what that means? Are you asking how to have min and max be indexes into the array with the values vs containing the values?
One way: put the initial min(big number) and max(small number) values into the array before putting in the other values to be searched and set the min and max indexes accordingly. Then start any searchs at index = 2 vs 0.
• 11-03-2008, 03:43 AM
Sophiie
My program is about having a sales amount entered for several employees. When running the program, it prompts for the user to enter the sales amount for each employee. Then I needed to find the max and min sales and print that with the employee that it is in reference to. If employee 1 made the highest sales, I want it to print that that employee made the highest. If employee 4 made the lowest sales, I want it to print that employee made the lowest sales also while printing the sales amount they made. I've tried but I still dont understand what to do.

Thanks for the help!
• 11-03-2008, 04:04 AM
Norm
You need a way of keeping each employee's data together. Create a small class to hold the data for each employee. As you read in the data, create an instance of that class with the data and store the instance in the array.
• 11-03-2008, 05:26 AM
Sophiie
Would this class be the one that goes without the public static void main(String[] args) statement?

I am recently starting to learn about programs with more than 1 class.

Can you explain to me how programs work with more than 1 class?

I hope I am not asking to much. I really appreciate all the help given!! It's a life safer!

Thanks again, I will get to my project.
• 11-03-2008, 01:47 PM
gamilah
Quote:

Would this class be the one that goes without the public static void main(String[] args) statement?
no ur program runs either way with or without [public static void main(String[] args) statement]

Quote:

Can you explain to me how programs work with more than 1 class?
u can put ur program inside a method in one class and call it by the main class 4 example
Code:

``` class Account {         String name; double balance; void display() { System.out.print(name); System.out.print(" has \$"); System.out.print(balance); } double getInterest(double percentageRate) { return balance * percentageRate / 100.00; } }```
this is the main class which will use the fields & methods in class Account
Code:

```class UseAccount { public static void main(String args[]) { Account myAccount = new Account(); Account yourAccount = new Account(); myAccount.name = "gamilah"; //here we used the name field in Account class myAccount.balance = 24.02; yourAccount.name = "sophiie"; yourAccount.balance = 55.63; myAccount.display(); //we used display method in Account class System.out.print(" plus \$"); System.out.print(myAccount.getInterest(5.00)); System.out.println(" interest "); yourAccount.display(); double yourInterestRate = 7.00; System.out.print(" plus \$"); double yourInterestAmount = yourAccount.getInterest(yourInterestRate); System.out.print(yourInterestAmount); System.out.println(" interest "); } }```
i hope it helped u:)
• 11-03-2008, 11:37 PM
Sophiie
Im still figuring it all out but I really do appreciate you helping me out!! Thanks for your time and effort put into helping me!
• 11-04-2008, 01:01 AM
Sophiie
Aaah, I cant get it to work. I have been following different examples in the effort to do it myself but I dont know what Im doing wrong. Where would I start? Which names and methods would I have to use for the second class to hold the data?

Thanks for the help!
• 11-04-2008, 01:29 AM
gamilah
don't mention it ,just tell me if u need a simpler example
for now i can tell u that any class or a methos is like a car if u don't call it it still standing once u call it (drive it) it moves
so always always put ur program in a separate class and in the main class the object that calls that class
Code:

```public class Print{//this class alone is like a stopping car         public  void toPrint(){         System.out.println("I used Print class"); } } [CODE] public class UsePrint{         public static void main(String[]args){                 Print x= new Print();//here the car starts moving                 x.toPrint();         }         }```
[/CODE]

i hope its clear now:)
• 11-04-2008, 08:55 AM
I know the min max thing was an exercise for school, but in "real life" there is an easier way.

Code:

```  int[] bogus = { 4, 6, 1, 5, 3 };   Arrays.sort(bogus);   System.out.println("min: " + bogus[0]);   System.out.println("max: " + bogus[bogus.length - 1]);```
• 11-05-2008, 03:03 AM
Sophiie
Thank you very much for your help.

Back to making references...um, I dont think Im suppose to create another class to hold the data...Is there another way to do it?

Im refering to the part where I have to print the sales amount and the employee it is in reference to.

THANKS!!
• 11-05-2008, 03:26 AM
Norm
If you have more than one value to save for each employee, you could have parallel arrays to hold the data. The data for each employee would be found at the same index in each array.
• 11-05-2008, 03:35 AM
Sophiie
Thanks again for the help!
Show 40 post(s) from this thread on one page
Page 1 of 2 12 Last