Assessment 4_7_sample

Instructions

This assessment is designed to be completed in 10 minutes or less.

Copy the template below and get all the test cases to pass. Hard-coding answers will not receieve credit; your solution must work for valid arrays.

The problem

Imagine that the user provides a two dimensional list. Return a one dimensional list that is the version of that original list where each row is the average of the original row. For example, if the user enters [[3, 1], [2, 4, 3], [], [1]], your code would return the array [2, 3, 0, 1]. Both the inputs and the returned average values will always be integers (don’t use double).

Please do not use any Java code or libraries that we have not covered in class; this defeats the purpose of the assessment and will not receive credit.

Code Template and Test cases

import java.util.Arrays;

public class Assess4_7_Sample{

  public static int[] averageRow(int[][] arr){
    int[] result = new int[2];
    return result;
  }

  public static void main(String[] args){

    int [][] arr1 = {{}};
    int [] result1 = {0};
    System.out.println("test1: " + Arrays.equals(averageRow(arr1), result1));
    int [][] arr2 = {{1}};
    int [] result2 = {1};
    System.out.println("test2: " + Arrays.equals(averageRow(arr2), result2));
    int [][] arr3 = {{1, 3}};
    int [] result3 = {2};
    System.out.println("test3: " + Arrays.equals(averageRow(arr3), result3));
    int [][] arr4 = {{4, 6}, {1, 3}};
    int [] result4 = {5, 2};
    System.out.println("test4: " + Arrays.equals(averageRow(arr4), result4));
    int [][] arr5 = {{4, 6}, {1, 2, 3}};
    int [] result5 = {5, 2};
    System.out.println("test5: " + Arrays.equals(averageRow(arr5), result5));
    int [][] arr6 = {{4, 6}, {1, 2, 3}, {4}};
    int [] result6 = {5, 2, 4};
    System.out.println("test6: " + Arrays.equals(averageRow(arr6), result6));
    int [][] arr7 = {{4, 6}, {}, {1, 2, 3}, {4}};
    int [] result7 = {5, 0, 2, 4};
    System.out.println("test7: " + Arrays.equals(averageRow(arr7), result7));
    int [][] arr8 = {{4, 6}, {}, {1, 2, 3}, {4, 2}};
    int [] result8 = {5, 0, 2, 3};
    System.out.println("test8: " + Arrays.equals(averageRow(arr8), result8));
  }

}