CS 46A - Lecture 15

Cover page image

Pre-class reading

Common Loop Algorithm: Computing a Total

Common Loop Algorithm: Counting Matches

Lecture 15 Clicker Question 1

What is wrong with this algorithm to find the average of a set of scores?

int total = 0; 
int count = 0;
while (in.hasNextInt()) 
{ 
   int input = in.nextDouble(); 
   total = total + input; 
   count++;
}
double average = total / count;
  1. The code won't compile
  2. An exception will occur when there is no input
  3. The average will not always be calculated correctly
  4. Both 2 and 3

Common Loop Algorithm: Finding the First Match

Lecture 15 Clicker Question 2

What are the values of found, ch, and position when the algorithm has finished processing the string FRED?

  1. found = false, ch = ?, position = 5
  2. found = false, ch = d, position = 4
  3. found = false, ch = ?, position = 4
  4. found = true, ch = d, position = 5

Common Loop Algorithm: Prompting Until a Match is Found

Common Loop Algorithm: Comparing Adjacent Values

Lecture 15 Clicker Question 3

What happens with the algorithm "Comparing Adjacent Values" when only one input is provided?

  1. The algorithm will falsely report an adjacent duplicate
  2. The algorithm will report that there is no adjacent duplicate
  3. The algorithm will report nothing at all
  4. An exception will occur

Common Loop Algorithm: Processing Input with Sentinel Values

Loop and a Half

ch06/dataset/DataAnalyzer.java

import java.util.Scanner;

/**
   This program computes the average and maximum of a set
   of input values.
*/
public class DataAnalyzer
{  
   public static void main(String[] args)
   {  
      Scanner in = new Scanner(System.in);
      DataSet data = new DataSet();

      boolean done = false;
      while (!done)
      {  
         System.out.print("Enter value, Q to quit: ");
         String input = in.next(); 
         if (input.equalsIgnoreCase("Q"))
            done = true;
         else
         {  
            double x = Double.parseDouble(input);
            data.add(x);
         }
      }

      System.out.println("Average = " + data.getAverage());
      System.out.println("Maximum = " + data.getMaximum());
   }
}

ch06/dataset/DataSet.java

/**
   Computes information about a set of data values.
*/
public class DataSet
{
   private double sum;
   private double maximum;
   private int count;

   /**
      Constructs an empty data set.
   */
   public DataSet()
   {
      sum = 0;
      count = 0;
      maximum = 0;
   }

   /**
      Adds a data value to the data set
      @param x a data value
   */
   public void add(double x)
   {
      sum = sum + x;
      if (count == 0 || maximum < x) maximum = x;
      count++;
   }

   /**
      Gets the average of the added data.
      @return the average or 0 if no data has been added
   */
   public double getAverage()
   {
      if (count == 0) return 0;
      else return sum / count;
   }

   /**
      Gets the largest of the added data.
      @return the maximum or 0 if no data has been added
   */
   public double getMaximum()
   {
      return maximum;
   }
}

Program Run:

Lecture 15 Clicker Question 4

Suppose you simplified the add method to:
   public void add(double x)
   {
      sum = sum + x;
      if (maximum < x) maximum = x;
      count++;
   }

What is the effect of this change?

  1. The maximum will still be correct for all inputs
  2. The maximum will be incorrect for all inputs
  3. The maximum will be incorrect when no inputs are provided
  4. The maximum will be incorrect if all inputs are negative