Skip to content

Commit

Permalink
make
Browse files Browse the repository at this point in the history
  • Loading branch information
JishnuS420 committed Apr 25, 2024
1 parent 10770bb commit 5264a01
Showing 1 changed file with 0 additions and 168 deletions.
168 changes: 0 additions & 168 deletions _notebooks/2024-04-19-ArrayLesson.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -338,39 +338,6 @@
"Complete method arraySum below. "
]
},
{
"cell_type": "code",
"execution_count": 32,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"16\n"
]
}
],
"source": [
"public class Main {\n",
" public static void main(String[] args) {\n",
" int[] arr = {1, 3, 2, 7, 3};\n",
" int sum = arraySum(arr);\n",
" System.out.println(sum);\n",
" }\n",
"\n",
" public static int arraySum(int[] arr) {\n",
" int sum = 0;\n",
" for (int elem : arr) {\n",
" sum += elem;\n",
" }\n",
" return sum;\n",
" }\n",
"}\n",
"\n",
"Main.main(null)"
]
},
{
"cell_type": "markdown",
"metadata": {},
Expand All @@ -380,57 +347,6 @@
"(b) Write a static method rowSums that calculates the sums of each of the rows in a given twodimensional array and returns these sums in a one-dimensional array. The method has one parameter, a twodimensional array arr2D of int values. The array is in row-major order: arr2D[r][c] is the entry at row r and column c. The method returns a one-dimensional array with one entry for each row of arr2D such that each entry is the sum of the corresponding row in arr2D. As a reminder, each row of a two-dimensional array is a one-dimensional array. For example, if mat1 is the array represented by the following table, the call rowSums(mat1) returns the array {16, 32, 28, 20}. Assume that arraySum works as specified, regardless of what you wrote in part (a). You must use arraySum appropriately to receive full credit. Complete method rowSums below."
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"16\n",
"32\n",
"28\n",
"20\n"
]
}
],
"source": [
"public class arr2Dsums {\n",
" public static void main(String[] args) {\n",
" int[][] arr2D = {{1, 3, 2, 7, 3}, {10, 10, 4, 6, 2}, {5, 3, 5, 9, 6}, {7, 6, 4, 2, 1}};\n",
" int[] result = rowSums(arr2D);\n",
" for (int num : result) {\n",
" System.out.println(num);\n",
" }\n",
" }\n",
"\n",
" public static int[] rowSums(int[][] arr2D) {\n",
" if (arr2D == null) {\n",
" return new int[0];\n",
" }\n",
" int[] sums = new int[arr2D.length];\n",
" int rowNum = 0;\n",
" for (int[] row : arr2D) {\n",
" sums[rowNum] = arraySum(row);\n",
" rowNum++;\n",
" }\n",
" return sums;\n",
" }\n",
"\n",
" public static int arraySum(int[] arr) {\n",
" int sum = 0;\n",
" for (int num : arr) {\n",
" sum += num;\n",
" }\n",
" return sum;\n",
" }\n",
"}\n",
"\n",
"arr2Dsums.main(null)"
]
},
{
"cell_type": "markdown",
"metadata": {},
Expand All @@ -439,90 +355,6 @@
"\n",
"(c) A two-dimensional array is diverse if no two of its rows have entries that sum to the same value. In the following examples, the array mat1 is diverse because each row sum is different, but the array mat2 is not diverse because the first and last rows have the same sum.Assume that arraySum and rowSums work as specified, regardless of what you wrote in parts (a) and (b). You must use rowSums appropriately to receive full credit. Complete method isDiverse below."
]
},
{
"cell_type": "code",
"execution_count": 29,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Array 1: \n",
"16\n",
"32\n",
"28\n",
"20\n",
"\n",
"Array 2: \n",
"14\n",
"35\n",
"36\n",
"14\n",
"\n",
"Diverse: true\n"
]
}
],
"source": [
"public class arr2Dsums {\n",
" public static void main(String[] args) {\n",
" int[][] arr2D = {{1, 3, 2, 7, 3}, {10, 10, 4, 6, 2}, {5, 3, 5, 9, 6}, {7, 6, 4, 2, 1}};\n",
" int[][] arr12D = {{1, 1, 5, 3, 4}, {12, 7, 6, 1, 9}, {8, 11, 10, 2, 5}, {3, 2, 3, 0, 6}};\n",
" \n",
" System.out.println(\"Array 1: \");\n",
" int[] result = rowSums(arr2D);\n",
" for (int num : result) {\n",
" System.out.println(num);\n",
" }\n",
"\n",
" System.out.println(\"\\nArray 2: \");\n",
" int[] result1 = rowSums(arr12D);\n",
" for (int num : result1) {\n",
" System.out.println(num);\n",
" }\n",
" \n",
" boolean diverse = isDiverse(arr2D);\n",
" System.out.println(\"\\nDiverse: \" + diverse);\n",
" }\n",
"\n",
" public static int[] rowSums(int[][] arr2D) {\n",
" if (arr2D == null) {\n",
" return new int[0];\n",
" }\n",
" int[] sums = new int[arr2D.length];\n",
" int rowNum = 0;\n",
" for (int[] row : arr2D) {\n",
" sums[rowNum] = arraySum(row);\n",
" rowNum++;\n",
" }\n",
" return sums;\n",
" }\n",
"\n",
" public static int arraySum(int[] arr) {\n",
" int sum = 0;\n",
" for (int num : arr) {\n",
" sum += num;\n",
" }\n",
" return sum;\n",
" }\n",
"\n",
" public static boolean isDiverse(int[][] arr2D) {\n",
" int[] sums = rowSums(arr2D);\n",
" for (int i = 0; i < sums.length; i++) {\n",
" for (int j = i + 1; j < sums.length; j++) {\n",
" if (sums[i] == sums[j]) {\n",
" return false;\n",
" }\n",
" }\n",
" }\n",
" return true;\n",
" }\n",
"}\n",
"\n",
"arr2Dsums.main(null)"
]
}
],
"metadata": {
Expand Down

0 comments on commit 5264a01

Please sign in to comment.