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


Answers: They are in red.
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.

    SourceRuns
    
    public class Triangle {
    
       public static void main(String[] args) {
          double x1 = 2, y1 = 1, x2 = 6, y2 = 1, x3 = 6, y3 = 4;
          double a, b, c;
          
          a = Math.sqrt((x1 - x2)*(x1 - x2) +(y1 - y2)*(y1 - y2));
          b = Math.sqrt((x1 - x3)*(x1 - x3) +(y1 - y3)*(y1 - y3));
          c = Math.sqrt((x3 - x2)*(x3 - x2) +(y3 - y2)*(y3 - y2));
          
          double s = 0.5*(a + b + c);
          double area = Math.sqrt(s*(s-a)*(s-b)*(s-c));
          System.out.println("Area: " + area);
          // just to check area formula with another method
          double A = Math.acos((b*b + c*c - a*a) / (2.0*b*c));
          double area2 = (0.5)*b*c*Math.sin(A);
          System.out.println("Area2: " + area2);
       }
    }
    
    
    Result of run:
    Area: 6.0
    Area2: 6.0


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.

    OriginalAnswer
    int sum = 0;
    for (int i = 1; i <= 10; i++) {
       sum = sum + i;
    }
    System.out.println("Sum: " + sum);
    // note: exam had i in place of sum
    int sum = 0;
    for (int i = 1; i <= 9; i = i + 2) { // 10 works also
       sum = sum + i*i;
    }
    System.out.println("Sum odd squares: " + sum);
    
    Output: Sum: 55Output: Sum odd squares: 165

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

    SourceRuns
    
    double sum = 0;
    double term = 1.0;
    for(int i = 1; i <= 4; i++) {
       sum = sum + term;
       System.out.println("i: " + i +
                          ", sum: " + sum + 
                          ", term: " + term);
       // calculate term for next loop
       term = 0.5*term;
    }
    System.out.println(sum);
    
    Output: 1.875
    
    Output with extra black line:
    i: 1, sum: 1.0, term: 1.0
    i: 2, sum: 1.5, term: 0.5
    i: 3, sum: 1.75, term: 0.25
    i: 4, sum: 1.875, term: 0.125
    1.875


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

    SourceRun
    
    public class Triangle {
    
       public static void chars(char ch, int n) {
          for (int i = 1; i <= n; i++)
             System.out.print(ch);
       }
    
       public static void main(String[] args) {
          for (int i = 0; i <= 4; i++) {
             chars(' ', i); chars('*', 5-i);
             System.out.println();
          }
       }
    }
                    i        5-i
    
    line 0:*****    0 blanks, 5 stars
    line 1: ****    1 blanks, 4 stars
    line 2:  ***    2 blanks, 3 stars
    line 3:   **    3 blanks, 2 stars
    line 4:    *    4 blanks, 1 stars
    


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? 4.
    This is potentially very confusing: The array elements are: a[0], a[1], a[2], and a[3], but the length of the array is 4, that is, there are 4 elements in the array.

    Another very confusing part of Java is that soon we will be talking about Strings, which are more-or-less just arrays of char. If you want the length of String a, it is a.length(), with parens.

  3. Consider the following Java program:

    SourceRuns
    
    public class Array {
    
       public static int multiplyElements(int[] x) { // Part b.
          int prod = 1;
          for (int i = 0; i < x.length; i++)
             prod = prod*x[i];
          return prod;
       }
    
       public static void printArray(int[] x) {
          for (int i = 0; i < x.length; i++)  // loop to print
             System.out.print(x[i] + " ");
          System.out.println();
       }
    
       public static void main(String[] args) {
          int[] a = {12, 23, 34, 45, 56};
          int[] b = {4, 1, 2, 5, 3};
          printArray(a);
          printArray(b); // Part c.
          System.out.println(multiplyElements(b)); // Part d.
       }
    }
    
    Original output:
      (Part a)
    12 23 34 45 56
    
    Final output:
    12 23 34 45 56
    4 1 2 5 3
    120
    
    

    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.

SourceRun

public class Array {
   public static int addElements(int[] x) { // Part b.
      int sum = 0;
      for (int i = 0; i < x.length; i++)
         sum = sum + x[i];
      return sum;
   }

   public static int[] multiplyArrays(int[] x, int[] y) {
      int[] z = new int[x.length];
      for (int i = 0; i < x.length; i++)
         z[i] = x[i]*y[i];
      return z;
   }
   
   public static int innerProduct(int[] x, int[] y) {
      return addElements(multiplyArrays(x, y));
   }

   public static void printArray(int[] x) {
      for (int i = 0; i < x.length; i++)
         System.out.print(x[i] + " ");
      System.out.println();
   }

   public static void main(String[] args) {
      int[] a = {12, 23, 34, 45, 56};
      int[] b = {4, 1, 2, 5, 3};
      printArray(a);
      printArray(b);
      System.out.println(innerProduct(a, b));
   }
}

12 23 34 45 56
4 1 2 5 3
532


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