Posts Fun with 2D arrays
Post
Cancel

Fun with 2D arrays

Introduction

View Instructions.

Alternatively, see comments above each method for details about functionality.

I completely stole one method from someone (ily nate c)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import java.lang.*;
import java.util.*;
import java.lang.Math;

 class FunWith2DArrays
{
/*
 *    preCondition : isArrow[i].length == isArrow[k].length for all i, k, 0 < i,j < isArrow.length
 *                   isArrow.length > 0  && isArrow[0].length > 0
 *
 *    postCondition: returns true if the 2D array is a square array (same number of rows and columns)
 *                           and the 2d array contains zeros in all entries except the first row,
 *                           first column and the main diagonal
 */
   public boolean isArrowHeadArray( int[] [] isArrow)
   {

      if(isArrow.length!=isArrow[0].length)
      {
        return false;
      }
      for(int r = 0; r<isArrow.length; r++)
      {
        for(int c = 0; c<isArrow.length; c++)
        {
          if(r!=c && r!=0 && c!=0)
          {
            if(isArrow[r][c]!=0)
            {
              return false;
            }
          }
          if(isArrow[0][c]==0 || isArrow[r][0]==0 || isArrow[r][r]==0 )
          {
            return false;
          }
          
        }
      
    }
    return true;
   }

/*
 *    preCondition  : gpa[j].length == gpa[k].length for 0 <= j, k < gpa.length
 *                   gpa.length > 0  && gpa[0].length > 0
 *
 *
 *    postcondition : returns true if mgp is as Generalized Permutation Matrix with integer entries
 *                    that is, returns true iff the following conditions are true
 *                      1)  All entries in the Matrix are integers
 *                             since you are being passed an int[][], you do not need to test this condition
 *                      2)  there is exactly one nonzero entry in each row and each column.
 *                          The nonzero entry can be any nonzero value (e.g., a positive or negative int)
 *                      3)  The array is a square array  (number of rows == number of columns)
 */
    public boolean isIntegerGeneralizedPermutationArray(int[][] gpa){
        int i,j;
        for(int r=0;r<gpa.length;r++){
            i=0;
            for(int c=0;c<gpa[0].length;c++){
                if(gpa[r][c]!=0)
                    i++;
                if(r==0){
                    j=0;
                    for(int rr=0;rr<gpa.length;rr++)
                        if(gpa[rr][c]!=0)
                            j++;
                    if(j!=1)
                        return false;
                }
            }
            if(i!=1)
                return false;
        }
        return true;
    }

/*
 *    preCondition : ma[i].length == ma[k].length for all i, k, 0 <= i,j < ma.length
 *                   ma.length > 0  && ma[0].length > 0
 *
 *                   Do NOT assume the 2d array is a square array
 *                   That is, ma.length may not be equal to ma[0].length 
 *
 *    postcondition : returns true if ma is a Monge Matrix
 *                    A m-by-n matrix is said to be a Monge array if for all i, j, k, p
 *                      with 0 <= i K k < m    and 0 <= j < p < n
 *                      and ma[i][j] + ma[k][p] <= ma[i][p] + ma[k][j]
 */
public boolean isMongeArray(int[][] ma){
  
   for(int i=0; i<ma.length; i++){
      for(int j=0; j<ma[i].length; j++){
         try{
            if( ma[i][j] + ma[i+1][j+1] > ma[i][j+1] + ma[i+1][j] ){
               return false;
            }
         }catch(Exception e){}
      }
   }
   return true;
   
}
}

This post is licensed under CC BY 4.0