/**

 * Title:        Pr#3 Review Solutions

 * Course:      CS100J

 * @author Ivan Lysiuk

 */

 

 

//problem 1

static int[] rightShift(int[] array)

    {

 

    int[] temp = new int[array.length];

    int clk;

    for (clk= 0; clk < array.length-1; clk++)

        temp[clk+1] = array[clk];

    temp[0] = array[clk];

 

    return temp;

    }

 

 

//problem 2

 

static int[] delete(int[] a, int elem)

    {

    /*2 steps:

    -count # of "non- elem" in init array

      -create an array of appropriate size

    -copy "non-elem" into the newly created array

    */

    int numNonElem=0;

    for (int clk=0; clk < a.length; clk++)

      if (a[clk]!=elem)

        numNonElem++;

 

    int[] b = new int[numNonElem];

    //additional counter to move on the new array

    int added =0;

    for(int clk=0; clk<a.length; clk++)

      {

      if (a[clk]!=elem)

        {

        b[added] = a[clk];

        added++;

        }

      }

    return b;

    }


// problem 3

 

static boolean isPalindrome(char [] a)

      {

      int numChecks = a.length/2;

 

      for (int clk=0; clk < numChecks; clk++)

        if (a[clk]!=a[(a.length-1)-clk])

          return false;

      return true;

      }

 

//problem 4

 

    static int[][] copy(int[][] matx)

      {

 

      int[][] temp = new int[matx.length][];

     

      for (int inx1=0; inx1<matx.length; inx1++)

            {

                    temp[inx1] = new int[matx[inx1].length];

            for (int inx2=0; inx2<matx[inx1].length;inx2++)

                        temp[inx1][inx2] = matx[inx1][inx2];

        }

      return temp;

      }

 

//problem 5

 

    static int[][] rightShift(int[][] matrix)

      {

      /*

      the picture says: column major!

      column-major  <= => the first index counts columns

      */

 

      int[][] temp = new int[matrix.length][];

      for (int col=0; col <matrix.length-1; col++)

        {

        temp[col+1] = new int[matrix[col].length];

        for (int cnt=0; cnt <matrix[col].length;cnt++)

          {

          temp[col+1][cnt] = matrix[col][cnt];

          }

        }

        for (int cnt=0; cnt <matrix[0].length;cnt++)

          temp[0][cnt] = matrix[matrix.length-1][cnt];

      return temp;

      }


//problem 6

 

static double aver(int[][] matx)

      {

      int sum =0;

      int numElems=0;

     

      //visit ALL elements

      //sum them and find their number

      for (int inx1=0; inx1 < matx.length; inx1++)

        for (int inx2=0; inx2 < matx[inx2].length; inx2++)

          {

          sum += matx[inx1][inx2];

          numElems++;

          }

      return (double)sum/numElems;

      }