7.2 Solutions

  1.  
    4
    0
    3
    This is because the array has values 3 2 1 0 (at index value 0 1 2 3)
    first println is list[1] + 2 which is 2 + 2 = 4
    second println is list[1 + 2] which is list[3] = 0
    third println is list[1] + list[2] which is 2 + 1 = 3
    1.  
      for (int i = 0; i < sample.length; i++)
         sample[i] = 1; 
    2.  
      int temp = sample[0];
      sample[0] = sample[SIZE - 1];
      sample[SIZE - 1] = temp; 
    3.  
      for (int i = 0; i < sample.length; i++)
         sample[i] = abs(sample[i]); 
    4.  
      int sampleSum = 0;
      for (int i = 0; i < sample.length; i++)
         sampleSum += sample[i]; 
    5.  
      for (int i = 0; i < sample.length; i++)
         if (i % 2 != 0)
            print(sample[i] + " ");
      println(); 
  2.  
    float max (float[] a)
    {
       float maxValue = a[0];
       for (int i = 1; i < a.length; i++)
          if(a[i] > maxValue)
             maxValue = a[i];
       return maxValue;
    } 
  3.  
    boolean equals (float[] a, float[] b)
    {
       if (a.length != b.length)
          return false;
       else
       {
          for (int i = 0; i < a.length; i++)
             if (a[i] != b[i])
                return false;
          return true;
       }
    } 
  4.  
    // Assume Input.pde is in a tab
    void setup()
    {
       final int MAX_SCORE = 10;
       int[] scoreArray = new int[MAX_SCORE+1];
       int score;
    
       // This loop isn't really necessary since already all 0
       for (int i = 0; i <= MAX_SCORE; i++)
          scoreArray[i] = 0;
    
       do
       {
          score = getInt("Please enter a score out of " + MAX_SCORE);
          if (score >= 0 && score <= MAX_SCORE)
             scoreArray[score]++;
       } while (score >= 0);
    
       println("Score        # of Occurrences");
       for (int j = 0; j <= MAX_SCORE; j++)
          println(j + "              " + scoreArray[j]);
    
       int sum = 0, count = 0;
       for (int j = 0; j<= MAX_SCORE; j++)
       {
          sum += scoreArray[j] * j;
          count += scoreArray[j];
       }
       print("The mean score is ");
       println(round(1.0 * sum / count * 10) / 10.0);
    }