Loops and Arrays

Tom Kelliher, CS 116

Dec. 1, 2000

Administrivia

Announcements

Course evaluation Monday.

Will post exam solution Friday afternoon.

Will post postlab solution (MPG problem) Monday afternoon.

Final review on 12/12 from 1--3pm in HS 134.

Assignment

Read 8.3, lab handout.

From Last Time

Loops

Outline

  1. for loops.

  2. Exercises.

  3. Nested loops.

  4. Arrays. Examples: Sorted, a simple sort, linear search.

Coming Up

Lab 7.

for Loops

  1. Example 2: Compute .
    int summation(int n)
    {
       int sum = 0;
       int i;
    
       for (i = 0; i <= n; ++i)
          sum += i;
    
       return sum;
    }
    

  2. Example 3: Sum evens between 0 and n.
    int evenSummation(int n)
    {
       int sum = 0;
       int i;
    
       for (i = 0; i <= n; i += 2)
          sum += i;
    
       return sum;
    }
    

Exercises

Complete the bodies of the following methods:

  1. Compute using a for loop:
    double pow(double x, int n)
    {
       // ...
    }
    

  2. Compute . (, by definition). Use a while loop.
    int fact(int n)
    {
       // ...
    }
    

Nested Loops

Yes, loops can be nested, like ifs.

What are the values of count1 and count2 after each code segment executes?

  1. int i;
    int j;
    int count1 = 0;
    int count2 = 0;
    
    for (i = 0; i < 5; ++i)
    {
       ++count1;
       for (j = 0; j < 5; ++j)
          ++count2;
    }
    

  2. int i;
    int j;
    int count1 = 0;
    int count2 = 0;
    
    for (i = 0; i < 5; ++i)
    {
       for (j = i; j < 5; ++j)
          ++count2;
       ++count1;
    }
    

Arrays

  1. Collection of data (variables) all of the same type.

  2. Each item (array element) in the array is referred to by index.

  3. Must specify type, number of elements

  4. Examples:
    final int SIZE = 3;
    int idata[] = new int[SIZE];
    double ddata[] = { 0.0, 100.0, 123.4, 67.89 };
    int i;
    double sum;
    
    data[0] = 12;
    data[1] = -4;
    data[2] = 7;
    
    sum = 0.0;
    
    for (i = 0; i < ddata.length; ++i)
       sum += ddata[i];
    
    Note use of [], indexing starts with 0, and the use of .length.

Sorted

Write a method to determine if the data in an int array are sorted into ascending order. Should work for any size array.

boolean sorted(int d[])
{
   int i;

   for (i = 0; i < d.length - 1; ++i)
      if (d[i] > d[i + 1])
         return false;

   return true;
}

A Simple Sort

  1. Bubble sort.

  2. Based upon sorted()

  3. Idea: walk through array, comparing adjacent elements. If out of order, swap.

void sort(int d[])
{
   int i;
   boolean sorted = false;
   int temp;

   while (!sorted)
   {
      sorted = true;

      for (i = 0; i < d.length - 1; ++i)
         if (d[i] > d[i + 1])
         {
            sorted = false;
            temp = d[i];
            d[i] = d[i + 1];
            d[i + 1] = temp;
         {
   }
}

Linear Search

See class homepage for full applet.

   // Instance variables.

   private String faculty[] = { "Kelliher", "Koppelman", "Lewand",
                                "McKibben", "Morrison", "Tutinas",
                                "Zimmerman" };

   private TextField search = new TextField(20);
   private Button searchB = new Button("Search");
   private Label result = new Label("No search yet");

   // ...

   public void actionPerformed(ActionEvent e)
   {
      int i;
      String target = search.getText();

      for (i = 0; i < faculty.length; ++i)
         if (faculty[i].equals(target))
         {
            result.setText("Found at index " + i);
            return;
         }

      result.setText("Not found");
   }



Thomas P. Kelliher
Thu Nov 30 12:51:32 EST 2000
Tom Kelliher