| | |
Finding Max Number in Array
![]() |
•
•
Join Date: Mar 2008
Posts: 17
Reputation:
Solved Threads: 0
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.
Any help would appreciated.
Thanks!
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.
Java Syntax (Toggle Plain Text)
import javax.swing.*; public class InitArray { public static void main( String args[] ) { int array[] = { 69, 23, 47, 81, 92, 88, 52, 48, 56, 66, 65, 76, 71, 85, 49, 53, 56, 61, 65, 84 }; double sum, average; sum = 0; for (int i = 0; i < array.length; i++) { sum += array[i]; } average = sum / array.length; System.out.println("Average mark = " + average); } }
Thanks!
•
•
Join Date: Jan 2008
Posts: 9
Reputation:
Solved Threads: 0
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);
}
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);
}
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:
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:
Could be:
Max should be initialized with one element from the array, preferably the first so you can start your loop from i=1:
Java Syntax (Toggle Plain Text)
max=arr[0]; for (i=1;i<arr.lenth;i++) { }
Java Syntax (Toggle Plain Text)
for (int i=0;)
Java Syntax (Toggle Plain Text)
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
Try something like this. It should do the trick.
This is one of the basic control statements for assessing values in arrays.
Good luck.
P.S. try and get min from the array now.
This is one of the basic control statements for assessing values in arrays.
Good luck.
java Syntax (Toggle Plain Text)
max = 0; for (int i = 0; i < array.length; i++) { if(max < array[i]) { max = array[i]; } }
P.S. try and get min from the array now.
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):
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.
Max, or min should be initialized with one of the values of the array (preferably the first):
Java Syntax (Toggle Plain Text)
max=array[0]; min=array[0];
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
No as Trogan said the values were all positive then this bit of code works. And it will pick out the max number.
Where if the array was full of negative numbers then you would initialize it like :
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.
java Syntax (Toggle Plain Text)
int max = 0; for (int i = 0; i < array.length; i++) { if(max < array[i]) { max = array[i]; } }
Where if the array was full of negative numbers then you would initialize it like :
java Syntax (Toggle Plain Text)
int max; for (int i = 0; i < array.length; i++) { if(max < array[i]) { max = array[i]; } }
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.
•
•
•
•
No as Trogan said the values were all positive then this bit of code works. And it will pick out the max number.
java Syntax (Toggle Plain Text)
int max = 0; for (int i = 0; i < array.length; i++) { if(max < array[i]) { max = array[i]; } }
Where if the array was full of negative numbers then you would initialize it like :
java Syntax (Toggle Plain Text)
int max; for (int i = 0; i < array.length; i++) { if(max < array[i]) { max = array[i]; } }
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 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.
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
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
![]() |
Similar Threads
- Array problem in Pascal (Pascal and Delphi)
- 'Segmentation Fault' (C++)
- Permuttations (C++)
- Creating own interrupt vector tables and..... (Assembly)
- finding max value of 10 integers (C++)
- pls i need help (C)
- beginner"problem in arrays help me in codes" (C)
Other Threads in the Java Forum
- Previous Thread: interface java.util.List<Employee>
- Next Thread: Client catching Web Service User Exceptions [Java 6.0.17]
| Thread Tools | Search this Thread |
account android api applet application array arrays automation awt bidirectional binary birt bluetooth busy_handler(null) chat class classes client code columns component database designadrawingapplicationusingjavajslider draw eclipse error errors eventlistener exception expand fractal game givemetehcodez graphics gui guidancer homework html ide image inetaddress inheritance integer intellij j2me java javamicroeditionuseofmotionsensor javaprojects jlabel jme jni jpanel jtextfield julia linux list loop map method methods midlethttpconnection mobile mobiledevelopmentcreatejar monitoring myaggfun netbeans newbie nullpointerexception open-source plazmic print problem program programming project property recursion ria scanner search server set smart sms smsspam sort sourcelabs splash sql sqlite static string subclass support swing testautomation threads tree webservices windows






