Created
October 26, 2013 06:01
-
-
Save chanwit/7165804 to your computer and use it in GitHub Desktop.
เฉลย Lab 4 กลุ่ม 1
This file contains hidden or 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; | |
import java.util.Scanner; | |
public class No1 { | |
public static int[] genArrayA(int num) { | |
int[] a = new int[num]; | |
int n = 0; | |
int i = 0; | |
System.out.print("Array A: "); | |
while (i < num) { | |
if (n > 10 && n % 7 == 0) { | |
System.out.printf("%2d ", n); | |
a[i] = n; | |
i = i + 1; | |
} | |
n++; | |
} | |
System.out.println(); | |
return a; | |
} | |
public static int[] genArrayB(int num) { | |
int[] b = new int[num]; | |
int n = 0; | |
int i = 0; | |
System.out.print("Array B: "); | |
while (i < num) { | |
if (n > 50 && n % 3 == 0) { | |
System.out.printf("%2d ", n); | |
b[i] = n; | |
i = i + 1; | |
} | |
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.print("Enter index to sum: "); | |
int index = scanner.nextInt(); | |
printSum(a, b, index); | |
} | |
private static void printSum(int[] a, int[] b, int index) { | |
System.out.printf("a[%d] = %d\n", index, a[index]); | |
System.out.printf("b[%d] = %d\n", index, b[index]); | |
System.out.printf("Sum = %d\n", a[index] + b[index]); | |
} | |
} |
This file contains hidden or 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; | |
public class No2 { | |
public static void main(String[] args) { | |
int[][] a = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}; | |
int[][] b = {{19, 18, 17}, {16, 15, 14}, {13, 12, 11}}; | |
System.out.println("Matrix A:"); | |
printArray(a); | |
System.out.println("Matrix B:"); | |
printArray(b); | |
int[][] c = minus(a, b); | |
System.out.println("A - B:"); | |
printArray(c); | |
} | |
private static void printArray(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[][] minus(int[][] a, int[][] b) { | |
int[][] c = new int[a.length][a[0].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; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment