Reputation: 11
I am trying to display the lowest and the highest value. The highest value is correct but the smallest is always giving me 0 can you help me.
package openlink.domain;
import java.io.*;
public class table2 {
public static void main(String[] args) {
int table1[][] = new int[5][5];
int table2[][] = new int[5][5];
int table3[][] = new int[5][5];
int i, j, k, l, b, c, g, p, num = 0, num1 = 0, small = 0, largest = 0;
String input = " ";
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
for (i = 0; i < 5; i++) {
for (j = 0; j < 5; j++) {
table1[i][j] = 0;
}
}
for (k = 0; k < 5; k++) {
for (l = 0; l < 5; l++) {
table2[k][l] = 0;
}
}
try {
for (i = 0; i < 5; i++) {
for (j = 0; j < 5; j++) {
System.out.print("Input table1[" + i + "][" + j + "]= ");
input = in.readLine();
num = Integer.parseInt(input);
table1[i][j] = num;
}
}
for (k = 0; k < 5; k++) {
for (l = 0; l < 5; l++) {
System.out.print("Input table2[" + k + "][" + l + "]= ");
input = in.readLine();
num1 = Integer.parseInt(input);
table2[k][l] = num1;
}
}
for (b = 0; b < table3.length; b++) {
for (c = 0; c < table3.length; c++) {
table3[b][c] = table2[b][c] + table1[b][c];
}
}
System.out.println("table1");
for (i = 0; i < 5; i++) {
for (j = 0; j < 5; j++) {
System.out.print(table1[i][j] + " ");
}
System.out.println();
}
System.out.println();
System.out.println("table2");
for (k = 0; k < 5; k++) {
for (l = 0; l < 5; l++) {
System.out.print(table2[k][l] + " ");
}
System.out.println();
}
System.out.println();
System.out.println("table3");
/* this is the code in how to locate the lowest and the highest */
for (b = 0; b < 5; b++) {
for (c = 0; c < 5; c++) {
System.out.print(table3[b][c] + " ");
}
System.out.println();
}
System.out.println();
for (b = 0; b < table1.length; b++) {
for (c = 0; c < table2.length; c++) {
if (small > table3[b][c]) {
small = table3[b][c];
} else if (table3[b][c] > largest) {
largest = table3[b][c];
}
}
}
System.out.println("the highest value in table3: " + largest);
System.out.println("the lowest value in table3: " + small);
} catch (IOException e) {
}
}
}
please help me here
Upvotes: 0
Views: 81
Reputation: 1
The problem lies within
if (small > table3[b][c]) {
small = table3[b][c];
} else if (table3[b][c] > largest) {
largest = table3[b][c];
}
Since small is initialized as 0, it will never be larger than table3[b][c] Try initalizing small with a large number and it should work just fine.
On a side note: Your code needs some cleanup. Your variables need more telling names and you do not have to declare every variable for each for loop at the top.
Try to replace:
for (k = 0; k < 5; k++) {
for (l = 0; l < 5; l++) {
System.out.print("Input table2[" + k + "][" + l + "]= ");
input = in.readLine();
num1 = Integer.parseInt(input);
table2[k][l] = num1;
}
}
with
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
System.out.print("Input table2[" + i + "][" + j + "]= ");
input = in.readLine();
num1 = Integer.parseInt(input);
table2[i][j] = num1;
}
}
You can also extract this to a method just by doing the following:
private int[][] getFilledArray(int size, String arrayName) {
int[][] myArray = new int[size][size];
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String input = new String();
try {
for (int i = 0; i < myArray.length; i++) {
for (int j = 0; j < myArray[i].length; j++) {
System.out.print("Input " + arrayName + "[" + i + "][" + j + "]= ");
input = in.readLine();
myArray[i][j] = Integer.parseInt(input);
}
}
} catch (IOException e) {
e.printStackTrace();
}
return myArray;
}
Upvotes: 0
Reputation: 1499880
Well, you've initialized small
as 0 to start with... so you're only going to update it if there are negative numbers. Likewise you've initialized large
as 0, so you'll only update that if there are positive numbers. Enter all-negative numbers and you'll find that small
is right and large
is wrong.
It would be better to initialize small
and large as table3[0][0]
to start with, so you know they're each a valid value from the array.
Upvotes: 6