Created
December 12, 2015 10:02
-
-
Save chanwit/78ad5b086d082756d229 to your computer and use it in GitHub Desktop.
เฉลย Lab 4 (OOT 58)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package oot.lab4.group1.y58; | |
import java.util.Scanner; | |
public class No1 { | |
public static int[] genArrayA(int size) { | |
int[] a = new int[size]; | |
int n = 20; | |
int i = 0; | |
while (i < size) { | |
if (n > 20 && n % 2 != 0) { | |
a[i] = n; | |
System.out.printf("%d ", a[i]); | |
i++; | |
} | |
n++; | |
} | |
System.out.println(); | |
return a; | |
} | |
private static int[] genArrayB(int size) { | |
int[] b = new int[size]; | |
int n = 10; | |
int i = 0; | |
while (i < size) { | |
if (n > 10 && (n % 3 == 0 || n % 5 == 0)) { | |
b[i] = n; | |
System.out.printf("%d ", b[i]); | |
i++; | |
} | |
n++; | |
} | |
System.out.println(); | |
return b; | |
} | |
public static void main(String[] args) { | |
Scanner scanner = new Scanner(System.in); | |
System.out.print("Enter size: "); | |
int size = scanner.nextInt(); | |
int[] a = genArrayA(size); | |
int[] b = genArrayB(size); | |
System.out.println(); | |
System.out.print("Enter index: "); | |
int index = scanner.nextInt(); | |
sub(a, b, index); | |
} | |
private static void sub(int[] a, int[] b, int index) { | |
for (int i = 0; i < a.length; i++) { | |
System.out.printf("%d ", a[i] - b[i]); | |
} | |
System.out.println(); | |
System.out.printf("a[%d] = %d\n", index, a[index]); | |
System.out.printf("b[%d] = %d\n", index, b[index]); | |
System.out.printf("a[%d] - b[%d] = %d\n", index, index, a[index] - b[index]); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package oot.lab4.group1.y58; | |
public class No2 { | |
public static void main(String[] args) { | |
int[][] a = {{9, 8, 7}, {6, 5, 4}, {3, 2, 1}}; | |
int[][] b = {{0, 1, 1}, {3, 2, 1}, {1, 3, 2}}; | |
System.out.println("Matrix A:"); | |
prtArr(a); | |
System.out.println("Matrix B:"); | |
prtArr(b); | |
int[][] c = plus(a, b); | |
System.out.println("A + B:"); | |
prtArr(c); | |
} | |
private static void prtArr(int[][] a) { | |
for (int i = 0; i < a.length; i++) { | |
for (int j = 0; j < a.length; j++) { | |
System.out.printf("%3d ", a[i][j]); | |
} | |
System.out.println(); | |
} | |
System.out.println(); | |
} | |
private static int[][] plus(int[][] a, int[][] b) { | |
int[][] c = new int[a.length][a.length]; | |
for (int i = 0; i < a.length; i++) { | |
for (int j = 0; j < a.length; j++) { | |
c[i][j] = a[i][j] + b[i][j]; | |
} | |
} | |
return c; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package oot.lab4.group2.y58; | |
import java.util.Scanner; | |
public class No1 { | |
public static int[] createA(int size) { | |
int[] a = new int[size]; | |
int n = 19; | |
int i = 0; | |
while (i < size) { | |
if (n > 20) { | |
a[i] = n; | |
System.out.printf("%2d ", a[i]); | |
i++; | |
} | |
n++; | |
} | |
System.out.println(); | |
return a; | |
} | |
public static int[] createB(int size) { | |
int[] b = new int[size]; | |
int n = 9; | |
int i = 0; | |
while (i < size) { | |
if (n > 10 && n % 2 == 1) { | |
b[i] = n; | |
System.out.printf("%2d ", b[i]); | |
i++; | |
} | |
n++; | |
} | |
System.out.println(); | |
return b; | |
} | |
public static void main(String[] args) { | |
Scanner scanner = new Scanner(System.in); | |
System.out.print("Enter size: "); | |
int size = scanner.nextInt(); | |
int[] a = createA(size); | |
int[] b = createB(size); | |
System.out.println(); | |
System.out.print("Enter index: "); | |
int idx = scanner.nextInt(); | |
minus(a, b, idx); | |
} | |
private static void minus(int[] a, int[] b, int idx) { | |
System.out.printf("a[%d] = %d\n", idx, a[idx]); | |
System.out.printf("b[%d] = %d\n", idx, b[idx]); | |
System.out.printf("a[%d] - b[%d] = %d\n", idx, idx, a[idx] - b[idx]); | |
System.out.println(); | |
System.out.println("a-b:"); | |
for (int i = 0; i < a.length; i++) { | |
System.out.printf("%d ", a[i] - b[i]); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package oot.lab4.group2.y58; | |
import java.util.Scanner; | |
public class No2 { | |
public static void main(String[] args) { | |
int[][] a = {{1, 2, 3}, {4, 5, 4}, {3, 2, 1}}; | |
int[][] b = {{9, 8, 7}, {6, 5, 4}, {3, 2, 1}}; | |
System.out.println("Matrix A:"); | |
show(a); | |
System.out.println("Matrix B:"); | |
show(b); | |
Scanner scanner = new Scanner(System.in); | |
System.out.print("Enter x: "); | |
int x = scanner.nextInt(); | |
for (int i = 0; i < a.length; i++) { | |
for (int j = 0; j < a.length; j++) { | |
a[i][j] += x; | |
} | |
} | |
System.out.println("Matrix A:"); | |
show(a); | |
int[][] c = sub(a, b); | |
System.out.println("A - B:"); | |
show(c); | |
} | |
private static void show(int[][] a) { | |
for (int i = 0; i < a.length; i++) { | |
for (int j = 0; j < a.length; j++) { | |
System.out.printf("%2d ", a[i][j]); | |
} | |
System.out.println(); | |
} | |
System.out.println(); | |
} | |
private static int[][] sub(int[][] a, int[][] b) { | |
int[][] c = new int[a.length][a.length]; | |
for (int i = 0; i < a.length; i++) { | |
for (int j = 0; j < a.length; j++) { | |
c[i][j] = a[i][j] - b[i][j]; | |
} | |
} | |
return c; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package oot.lab4.group3.y58; | |
import java.util.Scanner; | |
public class No1 { | |
public static int[] arrayA(int size) { | |
int[] a = new int[size]; | |
int n = 10; | |
int i = 0; | |
while (i < size) { | |
if (n > 10 && n % 7 == 0) { | |
a[i] = n; | |
System.out.printf("%3d ", a[i]); | |
i++; | |
} | |
n++; | |
} | |
System.out.println(); | |
return a; | |
} | |
public static int[] arrayB(int size) { | |
int[] b = new int[size]; | |
int n = 1; | |
int i = 0; | |
while (i < size) { | |
b[i] = n; | |
System.out.printf("%3d ", b[i]); | |
i++; | |
n++; | |
if (n > 3) { | |
n = 1; | |
} | |
} | |
System.out.println(); | |
return b; | |
} | |
public static void main(String[] args) { | |
Scanner scanner = new Scanner(System.in); | |
System.out.print("Enter size: "); | |
int size = scanner.nextInt(); | |
int[] a = arrayA(size); | |
int[] b = arrayB(size); | |
System.out.println(); | |
System.out.print("Enter index i: "); | |
int i = scanner.nextInt(); | |
System.out.print("Enter index j: "); | |
int j = scanner.nextInt(); | |
plus(a, b, i, j); | |
} | |
private static void plus(int[] a, int[] b, int i, int j) { | |
for (int k = 0; k < a.length; k++) { | |
System.out.printf("%d ", a[k] + b[k]); | |
} | |
System.out.println(); | |
System.out.printf("a[%d] = %d\n", i, a[i]); | |
System.out.printf("b[%d] = %d\n", j, b[j]); | |
System.out.printf("%d + %d = %d\n", a[i], b[j], a[i] + b[j]); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package oot.lab4.group3.y58; | |
import java.util.Scanner; | |
public class No2 { | |
public static void main(String[] args) { | |
int[][] a = {{19, 18, 17}, {16, 15, 14}, {17, 18, 19}}; | |
int[][] b = {{1, 2, 3}, {4, 5, 3}, {7, 8, 9}}; | |
System.out.println("Matrix A:"); | |
print(a); | |
System.out.println("Matrix B:"); | |
print(b); | |
System.out.println("== Set A[i][j] =="); | |
Scanner scanner = new Scanner(System.in); | |
System.out.print("Enter i: "); | |
int i = scanner.nextInt(); | |
System.out.print("Enter j: "); | |
int j = scanner.nextInt(); | |
System.out.print("Enter value: "); | |
int value = scanner.nextInt(); | |
a[i][j] = value; | |
System.out.println("Matrix A:"); | |
print(a); | |
int[][] c = compute(a, b, value); | |
System.out.println("(A - B)*value :"); | |
print(c); | |
} | |
private static void print(int[][] a) { | |
for (int i = 0; i < a.length; i++) { | |
for (int j = 0; j < a.length; j++) { | |
System.out.printf("%3d ", a[i][j]); | |
} | |
System.out.println(); | |
} | |
System.out.println(); | |
} | |
private static int[][] compute(int[][] a, int[][] b, int value) { | |
int[][] c = new int[a.length][a.length]; | |
for (int i = 0; i < a.length; i++) { | |
for (int j = 0; j < a.length; j++) { | |
c[i][j] = (a[i][j] - b[i][j]) * value; | |
} | |
} | |
return c; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment