CSC300: Loop Mistakes

Contents [0/11]

Starter code [1/11]
Does this work? [2/11]
Does this work? [3/11]
Does this work? [4/11]
Does this work? [5/11]
Does this work? [6/11]
Does this work? [7/11]
Does this work? [8/11]
Does this work? [9/11]
Does this work? [10/11]
Does this work? [11/11]

(Click here for one slide per page)


Starter code [1/11]

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package algs11;
import java.util.Arrays;
import stdlib.*;
public class Playground {
  /* Return number of times 5.0 occurs in the list */
  public static int numFives (double[] list) {
    return StdRandom.uniform (100); //TODO: fix this
  }
  /* This is a test function */
  public static void testNumFives (int expected, double[] list) {
    int actual = numFives (list);
    if (expected != actual) {
      StdOut.format ("Failed: Expecting [%d] Actual [%d] with argument %s\n", expected, actual, Arrays.toString (list));
    }
  }
  /* A main function for testing */
  public static void main (String[] args) {
    testNumFives (3, new double[] { 11, 21, 5, 31, 5, 41, 5, 51});
    testNumFives (4, new double[] { 11, 21, 5, 31, 5, 5, 41, 5, 51});
    testNumFives (4, new double[] { 11, 21, 5, 5, 5, 31, 41, 5, 51});
    testNumFives (0, new double[] { 11, 21, 31, 41 });
    testNumFives (1, new double[] { 11, 21, 5, 31, 41 });
    testNumFives (1, new double[] { 11, 21, 31, 41, 5 });
    testNumFives (1, new double[] { 5, 11, 21, 31, 41 });
    testNumFives (0, new double[] { 11 });
    testNumFives (1, new double[] { 5 });
    testNumFives (3, new double[] { 5, 5, 5 });
    testNumFives (0, new double[] { });
    StdOut.println ("Finished tests");
  }
}

Does this work? [2/11]

01
02
03
04
05
06
07
  public static int numFives (double[] a) {
    int result = 0;
    for (int i=0; i<a.length; i++)
      if (a[i] == 5.0)
        result++;   
    return result;
  }

Does this work? [3/11]

01
02
03
04
05
06
07
08
  public static int numFives (double[] a) {
    double[] list = new double[] { 4, 5, 6, 5, 3 };
    int result = 0;
    for (int i=0; i<list.length; i++)
      if (list[i] == 5.0)
        result++;   
    return result;
  }

Does this work? [4/11]

01
02
03
04
05
06
07
08
09
10
11
  public static int numFives (double[] a) {
    int result = 0;
    for (int i=0; i<a.length; i++) {
      if (a[i] == 5.0)
        result++; 
      else
        return result;
      return result;
    }
    StdOut.print (result); 
  }

Does this work? [5/11]

01
02
03
04
05
06
07
08
09
  public static int numFives (double[] a) {
    int result = 0;
    for (int i=0; i<a.length; i++)
      if (a[i] == 5.0)
        result++;   
      else
        i++;
    return result;
  }

Does this work? [6/11]

01
02
03
04
05
06
07
  public static int numFives (double[] a) {
    int result = 0;
    for (int i=0; i<a.length; i++)
      if (i == 5.0)
        result++;   
    return result;
  }

Does this work? [7/11]

01
02
03
04
05
06
07
  public static int numFives (double[] a) {
    int result = 0;
    for (int i=0; i<a.length; i++)
      if (a == 5.0)
        result++;   
    return result;
  }

Does this work? [8/11]

01
02
03
04
05
06
07
  public static int numFives (double[] a) {
    int result = 0;
    for (int i=0; 0<a.length; i++)
      if (a[i] == 5.0)
        result++;   
    return result;
  }

Does this work? [9/11]

01
02
03
04
05
06
07
  public static int numFives (double[] a) {
    int result = 0;
    for (int i=0; i>0; i++)
      if (a[i] == 5.0)
        result++;   
    return result;
  }

Does this work? [10/11]

01
02
03
04
05
06
07
08
09
10
11
12
13
  public static int numFives (double[] a) {
    int result = 0;
    int i = 0;
    while (i < a.length) {
      if (a[i] == 5.0) {
        result++; 
        i++;
      } else {
        i++;
      }
    }
    return result;
  }

Does this work? [11/11]

01
02
03
04
05
06
07
08
09
10
11
12
13
  public static int numFives (double[] a) {
    int result = 0;
    int i = 0;
    while (i < a.length) {
      if (a[i] == 5.0) {
        result++; 
        i++;
      }
      if (a[i] != 5.0)  
        i++;
    }
    return result;
  }

Revised: 2008/03/17 13:01