Thanks for reading my question:
public class Gugudan_array {
public static void main(String[] args) {
for(int i = 2; i<10; i ) {
for(int j = 1; j<10; j ) {
System.out.println(i * j);
}
System.out.println();
}
}
}
In the multiplication table above, result comes correctly from 2-9, however:
public class Gugudan_array {
public static void main(String[] args) {
int[] result = new int[10];
for(int h = 0; h < result.length; h ) {
for(int i = 2; i < 10; i ) {
for(int j = 1; j<10; j ) {
result[h] = (i * j);
}
}
}
for(int a = 0; a < result.length; a ) {
System.out.println(result[a]);
}
}
}
In this code with array, the result comes out only 81. What have I done wrong? Thank you!
CodePudding user response:
I'll first explain why your code doesn't work, and then go over a correct solution:
public class Gugudan_array {
public static void main(String[] args) {
int[] result = new int[10];
for(int h = 0; h < result.length; h ) {
for(int i = 2; i < 10; i ) {
for(int j = 1; j<10; j ) {
result[h] = (i * j);
}
}
}
for(int a = 0; a < result.length; a ) {
System.out.println(result[a]);
}
}
}
The main problem lies in this chunk of code:
for(int i = 2; i < 10; i ) {
for(int j = 1; j<10; j ) {
result[h] = (i * j);
}
}
Here, you are constantly overwriting the value of result[h]
, so that once the loop ends and both i = 9
, and j = 9
, the code will execute result[h] = 9 * 9
and then continue on to the next h
.
My solution:
public class Gugudan_array {
public static void main(String[] args) {
int[] result = new int[10];
for(int i = 2; i < 10; i ) {
for(int j = 1; j<10; j ) {
result[i - 2] = (i * j);
}
}
for(int a = 0; a < result.length; a ) {
System.out.println(result[a]);
}
}
}
Output:
18
27
36
45
54
63
72
81
0
0
First, notice how I completely got rid of the h
loop. That is because we can make the index in terms of i
. When we determine the first number, when i = 2
, we want to store that number in the 0
th index of our array. Similarly, when we get our second number, when i = 3
, we want to store the result in the 1
st index of our array.
To summarize, whenever we calculate a result, we will want to store it in the i - 2
th index of our array.
Better Solution using 2D arrays:
int[][] result = new int[8][9];
for(int i = 2; i < 10; i ) {
for(int j = 1; j<10; j ) {
result[i - 2][j - 1] = (i * j);
}
}
for(int a = 0; a < result.length; a ) {
for(int b = 0; b < result[a].length; b ){
System.out.print(result[a][b] " ");
}
System.out.println();
}
Output:
2 4 6 8 10 12 14 16 18
3 6 9 12 15 18 21 24 27
4 8 12 16 20 24 28 32 36
5 10 15 20 25 30 35 40 45
6 12 18 24 30 36 42 48 54
7 14 21 28 35 42 49 56 63
8 16 24 32 40 48 56 64 72
9 18 27 36 45 54 63 72 81
Note: If you want the output to match your original code, change System.out.print(result[a][b] " ");
to System.out.println(result[a][b])
It would make the most sense to store a multiplication table in a 2D array.
This code works by mapping i * j
to the [i - 2][j - 1]
'th element of the 2D array, so that 2 * 1
, will end up in result[0][0]
I hope this made sense! Please let me know if you need any further help or clarification!
CodePudding user response:
You have to increase h
variable after all iteration. Here is whole code:
public static void main(String[] args) {
int[] result = new int[10];
for (int h = 0; h < result.length; h ) {
for (int i = 2; i < 10; i ) {
for (int j = 1; j < 10; j ) {
result[h] = (i * j);
if (h < 9) {
h ;
}
}
}
}
for (int a = 0; a < result.length; a ) {
System.out.println(result[a]);
}
}
CodePudding user response:
The issue is that your current setup with 3 nested loops, the outer loop runs 10 times, the middle loop runs 8 times for each other loop, and the inner loop runs 9 times for each middle loop, which gives a total of 10x8x9=720 times... but I suspect you just want 72 results like you show in your first example, so we need to remove the outer loop first for(int h = 0; h < result.length; h ) {
.
Now the issue is that we need to store all 72 results but your current array only fits 10 results int[] result = new int[10];
, we can solve this with a bigger array:
int[] result = new int[8 * 9]; //72 spaces
Here is a working solution with a long array and a counter that keeps track of where to store the value, note the code comments for extra details:
//Your loops calculate 8 * 9 results, so you need an array with 72 spaces
int[] result = new int[72];
//Use a variable to track the array location
int counter = 0;
//Youn only need two loops
for(int i = 2; i < 10; i ) {
for(int j = 1; j< 10; j ) {
//Save the result in the correct location
result[counter] = (i * j);
//Incriment the counter
counter ;
}
}
//Print the stored results
for(int a = 0; a < result.length; a ) {
System.out.println(result[a]);
}
Some further important reading from the official Java guide on Arrays: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html
A better solution would be to use a 2D array.