CS 1073 Introductory Programming
for Scientific Applications
Mid-term Exam, Fall 2004


Directions: Fill in answers on the pages below. Don't spend too much time on any one problem. Even if you have forgotten some things, fake it as well as you can.
Questions about basics.
  1. Consider the following program that starts with three vertices (x1, y1), (x2, y2), and (x3, y3) of a triangle. First fill in the calculation for one side a below (any of the three sides). Do not include the calculations for the other two sides. Then use the formula s = (1/2)(a + b + c) to give the value s, called the semi-perimeter. Finally use the formula Area = sqrt(s(s-a)(s-b)(s-c)) to give the area. You must write all these formulas in correct Java.


Questions about loops.
  1. The following loop adds the integers from 1 to 10 and prints the sum. Modify the loop so that it will add up the squares of odd integers from 1 to 9 (inclusive), and will then print the resulting sum.

  2. Say exactly what number the following loop will print, and indicate how you got the answer (that is, show your work).


Questions about functions.
  1. Say exactly what the following Java program will print.


Questions about arrays.
  1. Draw a diagram (as we have in class) to show what the locations of the array will look like. Label each location with the name Java uses to access it, and show the number inside each location.

  2. With the above declaration, what will be the value of a.length?

  3. Consider the following Java program:

    1. What will the program print?

    2. Add code above that will print the values of the array b, using the printArray function.

    3. Write a new function multiplyElements that will multiply all the elements of an array which is its parameter, and will then return the product, as an int. (Write this function in above.)

    4. Show how to use the function in part c. above to multiply the elements of the array b and to print the resulting number (which is 120 in this case). (Add this also to the above.)

  4. (Extra credit) Using the same program above, write code below for a function addElements to add the elements of an array, returning the sum, and for a function multiplyArrays to multiply the corresponding elements in two arrays, returning the array of the products. Then use these two functions to define a function innerProduct which calculates the inner product that we worked on in class.


Points for each problem: 1-15, 2-15, 3-15, 4-20, 5-10, 6-5, 7-20, 8-10.