/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package myarray;
/**
*
* @author Evan
*/
public class MyArray {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// this makes an easy to read table for the index(subscript) and Value
//Each one holds
System.out.println("Index\tValue");
int myArray[]={2,4,5,7,9,100,2,3,4,5,6,7,2,3,2,1,1,2};
//Displays what Index and What Values are in it, can go to any size
//using the myArray.length
for(int counter=0;counter<myArray.length;counter++){
System.out.println(counter + "\t" + myArray[counter]);
}
// this is the loop to add up all the numbers in the array then divide
// how many numbers there are and give an average of all the numbers
int avg = 0;
for(int i=0; i < myArray.length ; i++)
avg = avg + myArray[i];
int average = avg / myArray.length;
//prints the average of all the numbers
System.out.println("The average of all the numbers is: " + average );
//prints the highest value in the list
System.out.println("The highest number in the list is: " + getMaxValue(myArray));
//prints the lowest value in the list
System.out.println("The lowest number in the list is: " + getMinValue(myArray));
//prints what number occurs the most
System.out.println("The number that occurs most in this list is: " + (occ(myArray, 1)));
}
//This finds the highest number in myArray
public static int getMaxValue(int[] myArray){
int maxValue = myArray[0];
for(int i=1;i<myArray.length;i++){
if(myArray[i] > maxValue){
maxValue = myArray[i];
}
}
return maxValue;
}
// this finds the lowest value in myArray
public static int getMinValue(int[] myArray){
int minValue = myArray[0];
for(int i=1;i<myArray.length;i++){
if(myArray[i] < minValue){
minValue = myArray[i];
}
}
return minValue;
}
// this finds what number occurs most in myArray
public static int occ(int[] myArray, int numToFind){
int occur=0;
for (int i = 0; i < myArray.length; i++){
if(myArray[i] == numToFind)
occur++;
}
return occur;
}
}