[英]How would i find the position of this array index and also the minimum?
How would I complete this program by adding the minimum value of the array, and where the minimum value is located? 如何通过添加数组的最小值以及最小值所在的位置来完成此程序?
public static void main(String[] args) {
Scanner input;
/* A file for the program to open, the absolute location is based on
* the location of the project. /src/array2d/ locates the file in
* the current source folder
*/
File fileIn = new File("src/array2d/array2dtest1.txt");
// You can fetch the full absolute path with the method below
// System.out.println(fileIn.getAbsolutePath());
/* try...catch is necessary for reading files, as it is possible that
* the file does not exist.
*/
try {
//creating a scanner from the file, rather than using console.
input = new Scanner(fileIn);
}
catch (FileNotFoundException e) {
// if file is not found, terminate the program
System.out.println(fileIn.getName() + " is not found.");
return;
}
int row = input.nextInt();
int column = input.nextInt();
int [][] arr = new int[row][column];
int [] min = arr[0];
for (int i = 0; i < row; i++) {
for (int j = 0; j < column; j++) {
arr[i][j] = input.nextInt();
}
}
for (int i = 0; i < row; i++) {
for (int j = 0; j < column; j++) {
int k;
k = arr[i][j];
System.out.printf(" %3d", k );
}
System.out.println();
}
input.close();
//min
int i;
for(i = 1; i < arr.length; i++) {
if(i == 1)
min = arr[i];
}
System.out.printf(" min: " + min);
}
The output should be: 输出应为:
39 95 99 56 41
88 8 1 48 75
3 58 13 54 80
92 72 74 25 86
30 38 3 21 2
The minimum is 1, and its position is (whatever the position is) 最小值为1,其位置为(无论位置如何)
Here is a new min
loop for you, stuffed into the loop that does both the rows and columns, with some better style of formatting for your strings as well :) 这是一个适合您的新的min
循环,该循环将填充行和列的循环,并为您的字符串提供更好的格式化样式:)
int min = 0; /* set initial minimum */
int minRowPos = 0; /* set minimum row and column positions */
int minColPos = 0;
for(int i = 0; i < row; i++)
{
for(int j = 0; j < column; j++)
{
int k;
k = arr[i][j];
System.out.printf(" %3d", k );
if(min < arr[i][j]){ /* test and set new min across arr */
min = arr[i][j];
minRowPos = i; /* set row position of new minimum */
minColPos = j; /* set col position of new minimum */
}
}
System.out.println();
System.out.printf("Array min: %d at row, column: %d,%d ", min, minRowPos, minColPos);
}
input.close();
also, delete the declaration of min
you have at the top 另外,删除顶部的min
声明
int [] min = arr[0];
you could be cleaner and move all the declarations to the top of the class if you like, but I didn't want to clutter things up any more and keep it a small chunk of change. 您可以更干净一些,如果愿意的话,可以将所有声明移到类的顶部,但是我不想再把事情弄得一团糟,只保留一小部分更改。
//min
int i;
int pos;
for(i = 1; i < arr.length; i++) {
if(i == 1)
min = arr[i];
pos = i;
}
System.out.printf(" min: " + min + " position " + pos );
Firstly your code int [] min = arr[0];
首先,您的代码int [] min = arr[0];
should give you an error because you cannot have a reference variable of type int[ ] reference to int. 应该会给您一个错误,因为您不能拥有类型为int的int []引用变量。
Now to the answer : 现在来回答:
<java>
int minimum = arr[0];
int minimum_index = 0
for(int i = 1;i<arr.length;i++){
if(arr[i]<minimum){ // Found an array element lesser than previously recorded minimum
minimum = arr[i]; //update the recorded minimum
minimum_index = i; // update the recorded minimum index
}
}
System.out.printf("Array minimum %d found at %d",minimum,minimum_index);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.