丰富与非丰富矩阵
Abundant vs Non-Abundant Matrices
我被告知要编写一个程序来读取包含三个数组的文件,每个数组的大小为 5 x 6 并包含许多 0 和非零数字。然后我要创建一个数组,其中行数未定义,但有 3 列,它指示非零数字所在的位置。第一列是行索引,第二列是列索引。第三列包含实际的非零数字本身。
这是一个非常迂回的程序。但我认为我遇到的主要问题是这个--
- 当两个非零数字在同一行时,我不可能在新矩阵中为两个数字打印出相同的行索引。我尝试将 row-index 设置为 a,然后将 row-index 设置为单独的行计数器,但它仍然搞砸了并逐行递增。此刻,我脑子里一片空白。
我的代码打印的唯一方法是将我的 rind 设置为 1 而不是 0。但这会使整个程序关闭;我已将其更改为 1 以更清楚地说明我的问题
也许这对学校作业的要求有点过分,我深表歉意。如果这个 post 以某种方式违反了规则,我会立即将其删除。
感谢任何愿意看一眼的人。
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class Prog465h {
static Scanner inFile = null;
public static void main(String[]args) {
try {
// create scanner to read file
inFile = new Scanner(new File ("prog465h.dat"));
} catch (FileNotFoundException e) {
System.out.println("File not found!");
System.exit(0);
}
while(inFile.hasNext()) {
int rows = inFile.nextInt();
int columns = inFile.nextInt();
int rind = 1;
int cr = 0; // count rows
int cc = 0; // count zeroes
int[][] first = new int[rows][columns];
for (int a = 0; a < first.length; a++) {
// catch the next
for (int b = 0; b < first[a].length; b++) {
first[a][b] = inFile.nextInt();
}
}
for (int a = 0; a < first.length; a++) {
for (int b = 0; b < first[a].length; b++) {
System.out.print(first[a][b] + " ");
if (first[a][b] != 0) {
rind++;
}
}
System.out.println(" ");
}
System.out.println("COUNTS ARE BELOW:");
int[][] mod = new int [rind][3]; // new array based on non-zeroes
for (int a = 0; a < first.length; a++) {
cc = 0;
for (int b = 0; b < first[a].length; b++) {
if (first[a][b] == 0) { // if there is a 0 increase number of columns counted
cc++;
} else { // if not--
mod[cr][2] = first[a][b]; // then make this nonzero number the last column of x row of mod.
// x row depends on...?
// the number of counted rows?
mod[cr][0] = (a+1); // put the number of rows counted for this number
mod[cr][1] = (cc+1); // put the number of 0's (aka columns) counted for this number
cc = 0;
}
}
cr++;
}
for (int a = 0; a < mod.length; a++) {
for (int b = 0; b < mod[a].length; b++) {
System.out.print(mod[a][b] + " ");
}
System.out.println(" ");
}
System.out.println("\n **** ALL DONE **** \n");
}
}
}
我的输出:(注意第一个矩阵如何打印三个 0。这不应该发生,它应该完全跳过该行。)
0 0 7 0 0 0
0 0 0 0 -8 0
0 0 0 0 0 0
2 0 0 0 0 0
0 0 0 0 0 0
COUNTS ARE BELOW:
1 3 7
2 5 -8
0 0 0
4 1 2
**** ALL DONE ****
0 2 0 3 0 1
8 0 4 0 1 0
0 3 0 1 0 -7
5 0 9 0 6 0
0 2 0 -1 0 7
COUNTS ARE BELOW:
1 2 1
2 2 1
3 2 -7
4 2 6
5 2 7
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
**** ALL DONE ****
0 0 1 0 0 2
3 0 0 4 0 0
0 0 5 0 0 6
7 0 0 8 0 0
0 0 9 0 0 1
COUNTS ARE BELOW:
1 3 2
2 3 4
3 3 6
4 3 8
5 3 1
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
**** ALL DONE ****
示例输出(输出应该是什么):
Original Matrix
0 0 7 0 0 0
0 0 0 0 -8 0
0 0 0 0 0 0
2 0 0 0 0 0
0 0 0 0 0 0
1 3 7
2 5 -8
4 1 2
The Original Matrix is Sparse
Original Matrix
0 2 0 3 0 1
8 0 4 0 1 0
0 3 0 1 0 -7
5 0 9 0 6 0
0 2 0 -1 0 7
The Original Matrix is Abundant
Original Matrix
0 0 1 0 0 2
3 0 0 4 0 0
0 0 5 0 0 6
7 0 0 8 0 0
0 0 9 0 0 1
1 3 1
1 6 2
2 1 3
2 4 4
3 3 5
0 0 9
4 1 7
4 4 8
5 3 9
5 6 1
The Original Matrix and the Sparse Matrix
are Equally Efficient
文件:
5
6
0 0 7 0 0 0
0 0 0 0 -8 0
0 0 0 0 0 0
2 0 0 0 0 0
0 0 0 0 0 0
5
6
0 2 0 3 0 1
8 0 4 0 1 0
0 3 0 1 0 -7
5 0 9 0 6 0
0 2 0 -1 0 7
5
6
0 0 1 0 0 2
3 0 0 4 0 0
0 0 5 0 0 6
7 0 0 8 0 0
0 0 9 0 0 1
感谢您对这是家庭作业的坦诚态度。一般来说,如果您 post 提出这样的问题,我认为您获得帮助不会有问题。您显然已经尝试过这个问题并且非常接近解决问题。
我看过了,看来您只需要将 cr++;
行向上移动一点即可。将它向上移动几行,使其位于内部 for 循环中(因此它会在 cc = 0;
行之后立即执行。此外,请确保初始化 int rind = 0;
(而不是 1)。你会还需要将 cc = 0;
更改为 cc++;
.
当我 运行 它时,它产生了您在问题中 post 编辑的示例输出。
只是一个观察,但您也可以通过将 2 个 for 循环压缩为一个来稍微整理一下代码:
for (int a = 0; a < first.length; a++) {
// catch the next
for (int b = 0; b < first[a].length; b++) {
first[a][b] = inFile.nextInt();
}
}
for (int a = 0; a < first.length; a++) {
for (int b = 0; b < first[a].length; b++) {
System.out.print(first[a][b] + " ");
if (first[a][b] != 0) {
rind++;
}
}
System.out.println(" ");
}
可能会变成:
for (int a = 0; a < first.length; a++) {
// catch the next
for (int b = 0; b < first[a].length; b++) {
first[a][b] = inFile.nextInt();
System.out.print(first[a][b] + " ");
if (first[a][b] != 0) {
rind++;
}
}
System.out.println(" ");
}
我被告知要编写一个程序来读取包含三个数组的文件,每个数组的大小为 5 x 6 并包含许多 0 和非零数字。然后我要创建一个数组,其中行数未定义,但有 3 列,它指示非零数字所在的位置。第一列是行索引,第二列是列索引。第三列包含实际的非零数字本身。
这是一个非常迂回的程序。但我认为我遇到的主要问题是这个--
- 当两个非零数字在同一行时,我不可能在新矩阵中为两个数字打印出相同的行索引。我尝试将 row-index 设置为 a,然后将 row-index 设置为单独的行计数器,但它仍然搞砸了并逐行递增。此刻,我脑子里一片空白。
我的代码打印的唯一方法是将我的 rind 设置为 1 而不是 0。但这会使整个程序关闭;我已将其更改为 1 以更清楚地说明我的问题
也许这对学校作业的要求有点过分,我深表歉意。如果这个 post 以某种方式违反了规则,我会立即将其删除。
感谢任何愿意看一眼的人。
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class Prog465h {
static Scanner inFile = null;
public static void main(String[]args) {
try {
// create scanner to read file
inFile = new Scanner(new File ("prog465h.dat"));
} catch (FileNotFoundException e) {
System.out.println("File not found!");
System.exit(0);
}
while(inFile.hasNext()) {
int rows = inFile.nextInt();
int columns = inFile.nextInt();
int rind = 1;
int cr = 0; // count rows
int cc = 0; // count zeroes
int[][] first = new int[rows][columns];
for (int a = 0; a < first.length; a++) {
// catch the next
for (int b = 0; b < first[a].length; b++) {
first[a][b] = inFile.nextInt();
}
}
for (int a = 0; a < first.length; a++) {
for (int b = 0; b < first[a].length; b++) {
System.out.print(first[a][b] + " ");
if (first[a][b] != 0) {
rind++;
}
}
System.out.println(" ");
}
System.out.println("COUNTS ARE BELOW:");
int[][] mod = new int [rind][3]; // new array based on non-zeroes
for (int a = 0; a < first.length; a++) {
cc = 0;
for (int b = 0; b < first[a].length; b++) {
if (first[a][b] == 0) { // if there is a 0 increase number of columns counted
cc++;
} else { // if not--
mod[cr][2] = first[a][b]; // then make this nonzero number the last column of x row of mod.
// x row depends on...?
// the number of counted rows?
mod[cr][0] = (a+1); // put the number of rows counted for this number
mod[cr][1] = (cc+1); // put the number of 0's (aka columns) counted for this number
cc = 0;
}
}
cr++;
}
for (int a = 0; a < mod.length; a++) {
for (int b = 0; b < mod[a].length; b++) {
System.out.print(mod[a][b] + " ");
}
System.out.println(" ");
}
System.out.println("\n **** ALL DONE **** \n");
}
}
}
我的输出:(注意第一个矩阵如何打印三个 0。这不应该发生,它应该完全跳过该行。)
0 0 7 0 0 0
0 0 0 0 -8 0
0 0 0 0 0 0
2 0 0 0 0 0
0 0 0 0 0 0
COUNTS ARE BELOW:
1 3 7
2 5 -8
0 0 0
4 1 2
**** ALL DONE ****
0 2 0 3 0 1
8 0 4 0 1 0
0 3 0 1 0 -7
5 0 9 0 6 0
0 2 0 -1 0 7
COUNTS ARE BELOW:
1 2 1
2 2 1
3 2 -7
4 2 6
5 2 7
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
**** ALL DONE ****
0 0 1 0 0 2
3 0 0 4 0 0
0 0 5 0 0 6
7 0 0 8 0 0
0 0 9 0 0 1
COUNTS ARE BELOW:
1 3 2
2 3 4
3 3 6
4 3 8
5 3 1
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
**** ALL DONE ****
示例输出(输出应该是什么):
Original Matrix
0 0 7 0 0 0
0 0 0 0 -8 0
0 0 0 0 0 0
2 0 0 0 0 0
0 0 0 0 0 0
1 3 7
2 5 -8
4 1 2
The Original Matrix is Sparse
Original Matrix
0 2 0 3 0 1
8 0 4 0 1 0
0 3 0 1 0 -7
5 0 9 0 6 0
0 2 0 -1 0 7
The Original Matrix is Abundant
Original Matrix
0 0 1 0 0 2
3 0 0 4 0 0
0 0 5 0 0 6
7 0 0 8 0 0
0 0 9 0 0 1
1 3 1
1 6 2
2 1 3
2 4 4
3 3 5
0 0 9
4 1 7
4 4 8
5 3 9
5 6 1
The Original Matrix and the Sparse Matrix
are Equally Efficient
文件:
5
6
0 0 7 0 0 0
0 0 0 0 -8 0
0 0 0 0 0 0
2 0 0 0 0 0
0 0 0 0 0 0
5
6
0 2 0 3 0 1
8 0 4 0 1 0
0 3 0 1 0 -7
5 0 9 0 6 0
0 2 0 -1 0 7
5
6
0 0 1 0 0 2
3 0 0 4 0 0
0 0 5 0 0 6
7 0 0 8 0 0
0 0 9 0 0 1
感谢您对这是家庭作业的坦诚态度。一般来说,如果您 post 提出这样的问题,我认为您获得帮助不会有问题。您显然已经尝试过这个问题并且非常接近解决问题。
我看过了,看来您只需要将 cr++;
行向上移动一点即可。将它向上移动几行,使其位于内部 for 循环中(因此它会在 cc = 0;
行之后立即执行。此外,请确保初始化 int rind = 0;
(而不是 1)。你会还需要将 cc = 0;
更改为 cc++;
.
当我 运行 它时,它产生了您在问题中 post 编辑的示例输出。
只是一个观察,但您也可以通过将 2 个 for 循环压缩为一个来稍微整理一下代码:
for (int a = 0; a < first.length; a++) {
// catch the next
for (int b = 0; b < first[a].length; b++) {
first[a][b] = inFile.nextInt();
}
}
for (int a = 0; a < first.length; a++) {
for (int b = 0; b < first[a].length; b++) {
System.out.print(first[a][b] + " ");
if (first[a][b] != 0) {
rind++;
}
}
System.out.println(" ");
}
可能会变成:
for (int a = 0; a < first.length; a++) {
// catch the next
for (int b = 0; b < first[a].length; b++) {
first[a][b] = inFile.nextInt();
System.out.print(first[a][b] + " ");
if (first[a][b] != 0) {
rind++;
}
}
System.out.println(" ");
}