Showing posts with label Average. Show all posts
Showing posts with label Average. Show all posts

Monday, June 17, 2013

Java Program Sentinel Control Loop



Java Program Sentinel Control Loop



Use in Program Eclipse


import java.util.Scanner;
public class SentinelControlLoop {

 /**
  * 
  * @param args
  */
 public static void main(String[] args) {
  // TODO Auto-generated method stub
  
  Scanner myInput = new Scanner(System.in);
  
  
  //Declare Variables
  int num, sum = 0, max, min, count =0;
  double average = 0.0;
  final int Sentinel = -9999;
  
  
  //Priming Stage
  System.out.print("Enter an integer value, or -9999 to quit: ");
  num = myInput.nextInt();
  
  //intitalize max and min
  max = min = num;
  
  
  
  //WHILE Loop
  while (num != Sentinel) {
   //body of loop
   //Update Sum
   sum = sum + num; //sum +=
   System.out.println("Sum is now "+ sum);
   
   if (num > max)
    max = num;
   if (num < min)
    min = num;
   
   //increment count of data items read
   count++; //Same as count = count + 1;
   System.out.print("num = " + num + " Count = " + count );
   System.out.print(" Enter an integer value, or -9999 to quit: ");
   num = myInput.nextInt();
   
   
  }
  if (count != 0) {
   average = (double)sum / count;
   System.out.println("Average is " + average);
   System.out.println (" max = "+ max + ", min " + min);
  }
  else 
   System.out.println("Cant average 0 items ");
  
 }

}





-->

Java Program Methods/Arrays Exam Scores Average, Max and Min



Java Program for Arrays. Average, Random Shuffle, Max and Min Arrays for Exam Scores

For use in Eclipse.



import java.util.Scanner;
public class Put Your Class here {

 /**
  * 
  * @param args
  */
 public static void main(String[] args) {
  // TODO Auto-generated method stub
  double[] exam1 = new double[10];
  double average;
  double max, min;
  

  fillArray(exam1);
  printArray(exam1);
  average = getArrayAverage (exam1);
  System.out.printf(" Average Exam 1 Score is %6.2f \n", average);
  max = getMax(exam1);
  min = getMin(exam1);
  randomShuffle(exam1);
  printArray(exam1);
 
  selectionSort(exam1);
  printArray(exam1);
  
  
  
 }//End of main
 public static void fillArray( double[] list) {
  Scanner myIn = new Scanner(System.in);
  for (int i=0; i <list.length; i++){
   System.out.print("Enter a number: ");
   list[i] = myIn.nextDouble();
  }//end of for loop
 }//end of the fill array loop

 public static void printArray(double[] list) {
  System.out.println(" \t Index \t Value \n");//print header
  System.out.println("=======================");
  for (int i=0; i <list.length; i++){
   System.out.println("\t" + i + "\t" + list[i]);
  }//end of for loop
 }//end of print array method
 
 public static void randomShuffle(double[] list) {
  int j; 
  double temp;
  for (int i= 0; i <list.length; i++) {
   j = (int)(Math.random() * 5);
 
  j= (int) (Math.random() * list.length);
  temp = list[i];
  list[i] = list[j];
  list[j] = temp;
 }//end of for
}// end of random shuffle
 
 public static void selectionSort(double[] list) {
  double currentMin;
  int currentMinIndex;
  
  for (int i = 0; i < list.length - 1; i++) {
   currentMin = list[i];
   currentMinIndex = i;
   
   for (int j= i + 1; j < list.length; j++){
    if (list[j] < currentMin) {
     currentMin = list[j]; currentMinIndex = j;
    }//end if
   }//end inner loop
   if (currentMinIndex != i) {
    list[currentMinIndex] = list[i];
    list[i] =currentMin;
   }//end if
  }// end outer loop
 }//end selection sort
 
 public static double  getArrayAverage(double[] list) {
  double avg=0.0, total=0.0;
  
  for (int i=0; i <list.length; i++){
   total = total + list[i];
  }//end of for loop
  if (list.length > 0) avg = total / list.length;
  return avg;
 }//end of the get array average method
 
 public static double getMax(double[] list) {
  double m = Double.MAX_VALUE;
  for (int i =0; i < list.length; i++){
   if (list[i] > m) m = list[i];
  }
  return m;
 }
 public static double getMin(double[] list) {
  double n = Double.MAX_VALUE;
  for (int i =0; i < list.length; i++){
   if (list[i] < n) n = list[i];
  }
  return n;
 }

 
}//End