新・明解 Java 入門 演習 6-16, 6-17, 6-18, 6-19 解答
Hello, Terminal!swaponQです!
今回は演習 6-16, 6-17, 6-18, 6-19 に取り組んでいこうと思います。
- 演習 6-16
4行3列の行列なと3行4列の行列の積を求めるプログラムを作成せよ。各要素の値はキーボードから読み込むこと。
import java.util.Scanner;
class ex06_16 {
public static void main(String[] args) {
Scanner stdIn = new Scanner(System.in);
int[][] array1 = new int[4][3];
int[][] array2 = new int[3][4];
int[][] ans = new int[4][4];
//標準入力よりarray1を作成
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 3; j++) {
System.out.printf("array1[%d][%d]:", i, j);
array1[i][j] = stdIn.nextInt();
}
}
//標準入力よりarray2を作成
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 4; j++) {
System.out.printf("array2[%d][%d]:", i, j);
array2[i][j] = stdIn.nextInt();
}
}
//ansの計算
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
for (int k = 0; k < 3; k++) {
ans[i][j] += array1[i][k] * array2[k][j];
}
}
}
//ansの出力
System.out.println("--------------");
System.out.println("ans = ");
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
System.out.printf("%3d", ans[i][j]);
}
System.out.println();
}
}
}・実行結果
array1[0][0]:1 array1[0][1]:2 array1[0][2]:3 array1[1][0]:1 array1[1][1]:2 array1[1][2]:3 array1[2][0]:1 array1[2][1]:2 array1[2][2]:3 array1[3][0]:1 array1[3][1]:2 array1[3][2]:3 array2[0][0]:1 array2[0][1]:2 array2[0][2]:3 array2[0][3]:4 array2[1][0]:1 array2[1][1]:2 array2[1][2]:3 array2[1][3]:4 array2[2][0]:1 array2[2][1]:2 array2[2][2]:3 array2[2][3]:4 -------------- ans = 6 12 18 24 6 12 18 24 6 12 18 24 6 12 18 24
- 演習 6-17
6人の2科目(国語・数学)の点数を読み込んで、科目ごとの平均点、学生ごとの平均点を求めるプログラムを作成せよ。
class ex06_17 {
public static void main(String[] args) {
int n = 6;
int[][] score = {
{84,79},{67,97},{67,92},{85,83},{87,80},{91,88}
};
for (int i = 0; i < 6; i++) {
System.out.printf("学生%d:",i + 1);
for (int j = 0; j < 2; j++) {
System.out.printf(" %3d", score[i][j]);
}
System.out.println();
}
System.out.println("\n科目ごとの平均点");
double japanese = 0;
double math = 0;
for (int i = 0; i < 6; i++) {
japanese += score[i][0];
math += score[i][1];
}
System.out.printf("国語: %.1f 数学: %.1f\n", (japanese / n), (math / n));
System.out.println();
System.out.println("学生ごとの平均点");
for (int i = 0; i < 6; i++) {
System.out.printf("学生%d: %.1f\n", (i + 1), (double) (score[i][0] + score[i][1]) / 2);
}
}
}・実行結果
学生1: 84 79 学生2: 67 97 学生3: 67 92 学生4: 85 83 学生5: 87 80 学生6: 91 88 科目ごとの平均点 国語: 80.2 数学: 86.5 学生ごとの平均点 学生1: 81.5 学生2: 82.0 学生3: 79.5 学生4: 84.0 学生5: 83.5 学生6: 89.5
- 演習 6-18
行数・各行の列数・各要素の値をキーボードから読み込むようにlist6-18を書き換えたプログラムを作成せよ。
import java.util.Scanner;
class ex06_18 {
public static void main(String[] args) {
Scanner stdIn = new Scanner(System.in);
int[][] c;
System.out.print("行数:");
int n = stdIn.nextInt();
c = new int[n][];
System.out.println("列数:");
for (int i = 0; i < c.length; i++) {
System.out.printf("c[%d] =", i);
int t = stdIn.nextInt();
c[i] = new int[t];
}
System.out.println("要素:");
for (int i = 0; i < c.length; i++) {
for (int j = 0; j < c[i].length; j++) {
System.out.printf("c[%d][%d] =", i, j);
c[i][j] = stdIn.nextInt();
}
System.out.println();
}
for (int i = 0; i < c.length; i++) {
for (int j = 0; j < c[i].length; j++) {
System.out.printf("%3d ", c[i][j]);
}
System.out.println();
}
}
}・実行結果
行数:3 列数: c[0] =4 c[1] =3 c[2] =5 要素: c[0][0] =0 c[0][1] =0 c[0][2] =0 c[0][3] =0 c[1][0] =0 c[1][1] =0 c[1][2] =0 c[2][0] =0 c[2][1] =0 c[2][2] =0 c[2][3] =0 c[2][4] =0 0 0 0 0 0 0 0 0 0 0 0 0
- 演習 6-19
クラス数・各クラスの人数・全員の点数を読み込んで、点数の合計点と平均点を求めるプログラムを作成せよ。合計点と平均点は、クラスごとのものと、全体のものとを表示すること。
import java.util.Scanner;
class ex06_19 {
public static void main(String[] args) {
Scanner stdIn = new Scanner(System.in);
System.out.print("クラス数:");
int classNum = stdIn.nextInt();
int[][] classArray = new int[classNum][];
int n = 0;
for (int i = 0; i < classArray.length; i++) {
System.out.printf("%d組の人数:", i + 1);
int studentNum = stdIn.nextInt();
n += studentNum;
classArray[i] = new int[studentNum];
for (int j = 0; j < classArray[i].length; j++) {
System.out.printf("%d組%d番の点数:", i + 1, j + 1);
classArray[i][j] = stdIn.nextInt();
}
System.out.println();
}
int[] sumArray = new int[classArray.length];
double[] aveArray = new double[classArray.length];
System.out.println(" 組 | 合計 平均 ");
System.out.println("---------------------");
for (int i = 0; i < classArray.length; i++) {
for (int j = 0; j < classArray[i].length; j++) {
sumArray[i] += classArray[i][j];
aveArray[i] = sumArray[i] / (double) classArray[i].length;
}
System.out.printf("%2d組 | %7d %6.1f \n", i + 1, sumArray[i], aveArray[i]);
}
int sum = 0;
for (Integer i : sumArray) {
sum += i;
}
double ave = (double) sum / n;
System.out.println("---------------------");
System.out.printf(" 計 | %7d %6.1f \n", sum, ave);
}
}・実行結果
クラス数:2 1組の人数:3 1組1番の点数:50 1組2番の点数:63 1組3番の点数:72 2組の人数:2 2組1番の点数:79 2組2番の点数:43 組 | 合計 平均 --------------------- 1組 | 185 61.7 2組 | 122 61.0 --------------------- 計 | 307 61.4
今回で「第6章:配列」は終了です。お疲れ様でした!
次回からは「第7章:メソッド」に入ります。
Goodbye, Terminal… swaponQでした!