Sunday, January 29, 2012

Finding Maximum and Minimum

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package serch;

/**
 *
 * @author KAPILAN
 */
public class MaxMin {
int [] array;
int n,max,min;
public MaxMin(int n){
    this.n=n;
    array=new int[n];
    for(int i=0;i<n;i++){
        array[i]=(int) Math.round(Math.random()*22+1000);
    }
}
public void findmaxmin(){
    max=array[0];
    min=array[0];
    for(int i=0;i<n;i++){
        if(array[i]<min)min=array[i];
        else if(array[i]>max)max=array[i];
       
    }
  }
    public void print(){
        findmaxmin();
        for(int i=0;i<n;i++){
            System.out.print(array[i]+" ");

        }
        System.out.println();
        System.out.println("max = "+max);
        System.out.print("min = "+min);
    }
    public static void main(String[] a){
        MaxMin s1=new MaxMin(10);
        s1.print();
    }
}
/*
 * 1002 1020 1012 1007 1016 1012 1001 1021 1020 1001
max = 1021
min = 1001
 */

No comments:

Post a Comment