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.

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

Any help would appreciated.

Thanks!

Aryan_15 commented: thx mate +0

Recommended Answers

All 16 Replies

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?

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

            }

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:

max=arr[0];
for (i=1;i<arr.lenth;i++) {

}

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:

for (int i=0;)

Could be:

for (i=0;)

Try something like this. It should do the trick.

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

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

max=array[0];
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.

No as Trogan said the values were all positive then this bit of code works. And it will pick out the max number.

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 :

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.

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 :

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 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?

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.

Well I thought that you were insisting about the max=0 thing, that's why I replied the way I did.

This version

int max;

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

will not compile. You will have to initialize max to something - either the first value of the array or Integer.MIN_VALUE.

import java.io.*;
class ArrayElementMax
{
public static void main(String []args) throws IOException
{
int i=0;
int n;
System.out.println("Enter the size of the array");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
n=Integer.parseInt(br.readLine());
int a[]=new int [n];
for(i=0;i<a.length;i++)
{
System.out.println("Enter the array element");
a=Integer.parseInt(br.readLIne());
}
int max=0;
for(int i=0;i<a.length;i++)
{
if(a>max)
{
max=a;
}
}
System.out.println("The maximum number in array is:'+max"+"at location"+i);
}
}

Haven't you been reading the previous posts?
Max must not be initialized with 0:

You will have to initialize max to something - either the first value of the array or Integer.MIN_VALUE.

as Ezzaral said. (Personally I prefer the first value of the array)

Hey y don't u try the simplest way out:

Arrays.sort(testarray);

for(int i =0; i<length; i++)
System.out.println("Sorted Array values: " + testarray);

System.out.println("Minimum Value: " + testarray[0]);
System.out.println("Maximum value: " + testarray[length-1]);

what do u think using "Arrays.sort()" to sort ur array in accsending order and fill ur requirement...

Hey y don't u try the simplest way out:

Arrays.sort(testarray);

for(int i =0; i<length; i++)
System.out.println("Sorted Array values: " + testarray);

System.out.println("Minimum Value: " + testarray[0]);
System.out.println("Maximum value: " + testarray[length-1]);

what do u think using "Arrays.sort()" to sort ur array in accsending order and fill ur requirement...

This is a 17 month old thread and the idea is to learn how to find the max number of an array! To implement the algorithm for homework!

you can used template to usability

#include<iostream>
using namespace std;
template <class T>
# define max 5
T GetMin(T arr[], T n){
	T k;
	for(int i=0;i<n;i++){
		cout<<"input "<<i<<":";
		cin>>arr[i];
		cout<<endl;
	}
	k=arr[0];
	for(int i=0;i<n;i++){
	
		if(k>arr[i])k=arr[i];
	}
	return k;
}
void main(){
	//int arr[max],k;
	//k=GetMin<int>(arr,5);
	//cout<<k<<endl<<"________________"<<endl;
	float m,arr[max];
	m=GetMin<float>(arr,5);
	cout<<m;
}
commented: Yeah sure Java forum lets write some c++ -2

Hmm, wrong language and 'void main()'... batting zero so far.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.