Finding Max Number in Array

Reply

Join Date: Mar 2008
Posts: 17
Reputation: Trogan is an unknown quantity at this point 
Solved Threads: 0
Trogan Trogan is offline Offline
Newbie Poster

Finding Max Number in Array

 
0
  #1
Mar 11th, 2008
Hi,

I am trying to get the max number from an array, but I'm not sure how to tackle it. This is what I have so far.

  1. import javax.swing.*;
  2.  
  3. public class InitArray
  4. {
  5. public static void main( String args[] )
  6. {
  7. int array[] = { 69, 23, 47, 81, 92, 88, 52, 48, 56, 66, 65, 76, 71, 85, 49, 53, 56, 61, 65, 84 };
  8.  
  9. double sum, average;
  10.  
  11. sum = 0;
  12.  
  13. for (int i = 0; i < array.length; i++) {
  14. sum += array[i];
  15. }
  16.  
  17. average = sum / array.length;
  18.  
  19. System.out.println("Average mark = " + average);
  20.  
  21. }
  22. }
Any help would appreciated.

Thanks!
Reply With Quote Quick reply to this message  
Join Date: Jul 2004
Posts: 12
Reputation: gkbush is an unknown quantity at this point 
Solved Threads: 0
gkbush gkbush is offline Offline
Newbie Poster

Re: Finding Max Number in Array

 
0
  #2
Mar 11th, 2008
You have code for average and sum, but nothing on finding max.
You are already stepping through the array element by element, what do you think you should be doing?
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 9
Reputation: pradeep.singh28 is an unknown quantity at this point 
Solved Threads: 0
pradeep.singh28 pradeep.singh28 is offline Offline
Newbie Poster

Re: Finding Max Number in Array

 
0
  #3
Mar 12th, 2008
u have written the code to find out the average marks, to find maximum number ur code shuld be like this.......

public class InitArray
{
public static void main( String args[] )
{
int arr[] = { 69, 23, 47, 81, 92, 88, 52, 48, 56, 66, 65, 76, 71, 85, 49, 53, 56, 61, 65, 84 };

int max=0,i;

for (int i = 0; i < array.length; i++)
{
while(arr[i]>max)
{
max=arr[i];
}
}


System.out.println("maximum number is= " + max);

}
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 1,658
Reputation: javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all 
Solved Threads: 224
Featured Poster
javaAddict's Avatar
javaAddict javaAddict is offline Offline
Posting Virtuoso

Re: Finding Max Number in Array

 
0
  #4
Mar 12th, 2008
You already have a for-loop, you don't need another while-loop.
Max should be initialized with one element from the array, preferably the first so you can start your loop from i=1:
  1. max=arr[0];
  2. for (i=1;i<arr.lenth;i++) {
  3.  
  4. }
When you write: int max=0,i; you declare i to be an int, so you don't need to declare it again in the for loop:
  1. for (int i=0;)
Could be:
  1. for (i=0;)
Last edited by javaAddict; Mar 12th, 2008 at 7:44 am.
Check out my New Bike at my Public Profile at the "About Me" tab
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 29
Reputation: toomuchfreetime is an unknown quantity at this point 
Solved Threads: 1
toomuchfreetime's Avatar
toomuchfreetime toomuchfreetime is offline Offline
Light Poster

Re: Finding Max Number in Array

 
0
  #5
Mar 12th, 2008
Try something like this. It should do the trick.

This is one of the basic control statements for assessing values in arrays.
Good luck.


  1.  
  2. max = 0;
  3.  
  4. for (int i = 0; i < array.length; i++)
  5. {
  6. if(max < array[i])
  7. {
  8. max = array[i];
  9. }
  10. }

P.S. try and get min from the array now.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 1,658
Reputation: javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all 
Solved Threads: 224
Featured Poster
javaAddict's Avatar
javaAddict javaAddict is offline Offline
Posting Virtuoso

Re: Finding Max Number in Array

 
0
  #6
Mar 13th, 2008
The following algorithm is wrong. If the array has all negative numbers then when you give as initial value zero: (max=0), zero will always be greater than any number in the array. So the result max number will be zero even if the array has not this number.
Max, or min should be initialized with one of the values of the array (preferably the first):
  1. max=array[0];
  2. min=array[0];
and then you should use the for-loop described in the code.
But the bottom line is that initializing the max or the min with zero is WRONG, because you don't know the values of the array you want to search.
Last edited by javaAddict; Mar 13th, 2008 at 4:53 am.
Check out my New Bike at my Public Profile at the "About Me" tab
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 29
Reputation: toomuchfreetime is an unknown quantity at this point 
Solved Threads: 1
toomuchfreetime's Avatar
toomuchfreetime toomuchfreetime is offline Offline
Light Poster

Re: Finding Max Number in Array

 
0
  #7
Mar 13th, 2008
No as Trogan said the values were all positive then this bit of code works. And it will pick out the max number.

  1.  
  2. int max = 0;
  3.  
  4. for (int i = 0; i < array.length; i++)
  5. {
  6.  
  7. if(max < array[i])
  8. {
  9. max = array[i];
  10. }
  11.  
  12. }

Where if the array was full of negative numbers then you would initialize it like :

  1. int max;
  2.  
  3. for (int i = 0; i < array.length; i++)
  4. {
  5.  
  6. if(max < array[i])
  7. {
  8. max = array[i];
  9. }
  10.  
  11. }

But you are right that it would be of a better industry standard not to initialize max to any value. That is if you are unsure with what values you would be dealing with.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 1,658
Reputation: javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all 
Solved Threads: 224
Featured Poster
javaAddict's Avatar
javaAddict javaAddict is offline Offline
Posting Virtuoso

Re: Finding Max Number in Array

 
0
  #8
Mar 13th, 2008
Originally Posted by toomuchfreetime View Post
No as Trogan said the values were all positive then this bit of code works. And it will pick out the max number.

  1.  
  2. int max = 0;
  3.  
  4. for (int i = 0; i < array.length; i++)
  5. {
  6.  
  7. if(max < array[i])
  8. {
  9. max = array[i];
  10. }
  11.  
  12. }

Where if the array was full of negative numbers then you would initialize it like :

  1. int max;
  2.  
  3. for (int i = 0; i < array.length; i++)
  4. {
  5.  
  6. if(max < array[i])
  7. {
  8. max = array[i];
  9. }
  10.  
  11. }

But you are right that it would be of a better industry standard not to initialize max to any value. That is if you are unsure with what values you would be dealing with.
At the first part of your code where you set max=0, you DON'T know that the array has positive numbers.
At the second part of the code when you say int max; is like saying int max=0; which is the same thing.

And I never said that:
But you are right that it would be of a better industry standard not to initialize max to any value.
I said exactly the opposite, to initialize max or min with one of values from the array.
You cannot have two different codes that do exactly the same thing and assume that you know the values in the array to call one or the other:
initialize max with one of the values of the array, and then use the for-loop which will work no matter what the array has.

Can someone else who knows programming back me up here?
Last edited by javaAddict; Mar 13th, 2008 at 8:26 am.
Check out my New Bike at my Public Profile at the "About Me" tab
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 29
Reputation: toomuchfreetime is an unknown quantity at this point 
Solved Threads: 1
toomuchfreetime's Avatar
toomuchfreetime toomuchfreetime is offline Offline
Light Poster

Re: Finding Max Number in Array

 
0
  #9
Mar 13th, 2008
Look your right.

I was just saying when Trogan first posted the problem he supplied all the values for the array which led me too see that they were all positive. which led to my answer to that particular question.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 1,658
Reputation: javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all 
Solved Threads: 224
Featured Poster
javaAddict's Avatar
javaAddict javaAddict is offline Offline
Posting Virtuoso

Re: Finding Max Number in Array

 
0
  #10
Mar 13th, 2008
Well I thought that you were insisting about the max=0 thing, that's why I replied the way I did.
Check out my New Bike at my Public Profile at the "About Me" tab
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC