CS 101 - Introduction to Computing, Spring 2007

Lab 13

Reminder

The program Jython Environment for Students (or JES) is the development environment that we will use for this lab.

In the ACCC Labs that have JES, it can be found by:

  1. Clicking on Start
  2. Then click on All Programs
  3. Then click on Class Applications
  4. Then click on Engineering
  5. Finally click on Jython Environment for Students

Nested For Loops

For this assignment, you will want to put one loop inside of another. This is often used when we have two values that need to change to process some date. An example of this was when we were modifying images and we need to access every pixel through the X,Y coordinates. We would set up one for loop to iterate throught the X values and one for loop to iterate throught the Y values. Look at the following section of code take from a function to crop an image.
def cropImage():
  
  #Allow the user to pick up an image file
  file = pickAFile()
  pic = makePicture(file)

  # decide the size of new image
  newWidth = 100
  newHeight = 150
  new_pic = makeEmptyPicture(newWidth, newHeight)

  # now decide starting with which part of image
  # we should crop the image
  start_x = 50
  start_y = 100

  # so we will crop the image starting from pixel
  # at row 100 column 50 and size of the image would be
  # 100 columns and 150 rows.
  for x in range(1, newWidth+1):
    for y in range(1, newHeight+1):

      # get pixel from original image
      orig_pixel = getPixel(pic, x+start_x, y+start_y);
      new_pixel = getPixel(new_pic, x, y);

      # get the color from original pixel
      c = getColor(orig_pixel)
      setColor(new_pixel, c)

  # Now return the new image
  return(new_pic)
Program to crop an image

In the above code the first loop uses the variable x as its index variable. This loop is often called the "outer loop". The second loop uses the variable y as its index variable. This loop is ofter called the "inner loop". The inner loop will be completely executed during each iteration of the outer loop. So in the above code the value of x will be 1 while the value of y changes from 1 to 150. Then while the value of x is 2, the value of y again changes from 1 to 150. This continues until the value of x finally becomes 100.

Lab Assignment 13

Due: Tuesday 4/24/2007 by 11:59 pm

For this assignment, you are to write a JES function that will take two parameters and print out the family of multiplication sentences based on those parameters.

The multiplication sentences are to be of the form:

    multiplier * multiplicant = product
Where the multiplier is to range from 0 to the first parameter

and the multiplicant is to range from 0 to the second parameter.

For example, if the parameters given to the function are 2 and 3, the function should write out the following:

   0 X 0 = 0
   0 X 1 = 0
   0 X 2 = 0
   0 X 3 = 0

   1 X 0 = 0
   1 X 1 = 1
   1 X 2 = 2
   1 X 3 = 3

   2 X 0 = 0
   2 X 1 = 2
   2 X 2 = 4
   2 X 3 = 6

Note, don't worry if you fill up the JES command window with all of these statements. That is to be expected for just about any parameter value. However, the space between the sentence families is something that you are to create.

The primary approach to be used for this assignment is to use 2 for loops where one loop is nested inside the other. The above example of output assumes the outer loop interates on the multiplier (the first operand in the sentences) while the inner loop iterates on the muliplicant (the second operand in the sentences).

Create a file using JES that will:

  1. Contain a comment indicating

  2. Contain a JES function called lab13 () that will
These functions may call other functions that are built-in to JES or that you have written.

Submittal of the Lab Assignment

Comments on the ACCC Labs

On the computers in the ACCC Labs there should an H: drive. This drive is actually a networked connection to your own file space maintained by the ACCC. No matter what machine you use or what lab you are in, the H: drive will access the same file space. This means that you can save a file on the H: drive on a computer in one lab and access that same file on a computer in another lab. This can be very helpful. It is suggested that you store your python program files on your H: drive.