Edited by An Alien, 02 March 2011 - 05:35 PM.
9 replies to this topic
#1
Posted 25 February 2011 - 08:28 AM
Can somebody explain how multidimensional work? How to store and access info in them with examples would be nice too.
|
|
|
#2
Posted 25 February 2011 - 08:51 AM
like so?
for(int i=0 ; i<=10 ; i++){
for(int j=0 ; j<=10 ; j++){
System.out.print(i + " * " + j + " = " + i*j);
}
System.out.println("");
}
#3
Posted 25 February 2011 - 11:37 AM
Yeah, but with a multidimesional array.
#4
Posted 25 February 2011 - 03:44 PM
what win DC gave you is 2D array, which sounds like what you wanted.
you can use the first for loop for controlling the row and second for loop for controlling column or vice versa. so your array looks something like this: product[i][j] with i indicating the row and j the column.
What is it that you want to do with your program? I'm confused. It seems like you want to print out what's in products, but you never put anything in product, so it would print out 0 for each cell. But I don't think the way you're doing it is correct. You can't print out an array by saying System.out.println(product[w]). That's saying print out the whole "w" row. Is that what you wanted? Or did you wanted to access one...cell at a time. If yes, use win DC loop.
you can use the first for loop for controlling the row and second for loop for controlling column or vice versa. so your array looks something like this: product[i][j] with i indicating the row and j the column.
What is it that you want to do with your program? I'm confused. It seems like you want to print out what's in products, but you never put anything in product, so it would print out 0 for each cell. But I don't think the way you're doing it is correct. You can't print out an array by saying System.out.println(product[w]). That's saying print out the whole "w" row. Is that what you wanted? Or did you wanted to access one...cell at a time. If yes, use win DC loop.
#5
Posted 25 February 2011 - 04:08 PM
I really don't even know how to use multidimensional arrays, I just came up with some random code that'll I was hoping would print out a multiplication table that is 10 by 10.
I understand what you're saying and what WinDC is. I'll try to add something the array first and then try printing it out. But WinDC's example is not a mulit-dimesional array which is what I'm looking for.
I understand what you're saying and what WinDC is. I'll try to add something the array first and then try printing it out. But WinDC's example is not a mulit-dimesional array which is what I'm looking for.
#6
Posted 25 February 2011 - 04:16 PM
@sourlemon: No, wim DC gave him an answer to print a multiplication table, not create one with a 2D array of ints. However, I'll also admit that the modification is trivial:
@An Alien: Sometimes it's helpful to try and describe what you're trying to do more thoroughly in description rather than in code. Here's the entirety of the information you gave us other than your code:
This could be interpreted to mean a wide variety of different possible questions, and your code wasn't particularly helpful since you were attempting to render it how you thought it worked, and it actually worked dramatically differently. This board has some very helpful people on it (it's honestly kind of an addiction), but all we can offer here is shots in the dark to try and help. Try giving more description of what you're doing and how you tried to do it. If you don't know anything about multidimensional arrays, ask something like "How do I create and use a multidimensional array?" Give some details and yes, even code that you tried, but you have to clarify what you're asking! :)
int[][] products = new int[10][10];
for(int i=0 ; i<10 ; i++){
for(int j=0 ; j<10 ; j++){
//System.out.print(i + " * " + j + " = " + i*j);
products[i][j] = (i+1) * (j+1);
}
//System.out.println("");
}
@An Alien: Sometimes it's helpful to try and describe what you're trying to do more thoroughly in description rather than in code. Here's the entirety of the information you gave us other than your code:
An Alien said:
I know I'm doing it totally wrong cause my output is totally wrong. So if you guys can slowly guide me in making the multiplication table 10*10
Yeah, but with a multidimesional array.
Yeah, but with a multidimesional array.
Wow I changed my sig!
#7
Posted 25 February 2011 - 04:29 PM
Sorry, I usually give a lot more detail to my problems. I know this forum is awesome and have gotten help many times from here. Basically, I want a multiplication table using multidimensional tables and for loops.
Output should be something like this:
1 2 3 4 5 6 7 8 9 10
2 4 6 8 10 12 14 16 18 20
3 6 9 12 15 18 21 24 27 30
4 8 12 ........................40
5 10 15 20...................50
6 12............................60
7 14.............................70
8.....................................
9.....................................
10................................100
Output should be something like this:
1 2 3 4 5 6 7 8 9 10
2 4 6 8 10 12 14 16 18 20
3 6 9 12 15 18 21 24 27 30
4 8 12 ........................40
5 10 15 20...................50
6 12............................60
7 14.............................70
8.....................................
9.....................................
10................................100
#8
Posted 26 February 2011 - 12:23 AM
They provided the code to create a multidimensional array filled with the products, now you do the same thing to display it (not real code.)
print [1][0] .. print [1][1] .. print [1][2].......... print [1][9].. println();
print [2][0] .. print [2][1] ........
for(i=0;i<10;i++) {
for(j=0;j<10;j++) {
print product[i][j];
print "\t"; //tab or space formatting if wanted
}
println();
}Try to visualise what the loop will do to the array, so you understand how they work.print [1][0] .. print [1][1] .. print [1][2].......... print [1][9].. println();
print [2][0] .. print [2][1] ........
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.
#9
Posted 02 March 2011 - 05:36 PM
Okay, I updated my first post, please read it.
#10
Posted 02 March 2011 - 07:37 PM
Her's a good website to bookmark. He has provided tons of tutorials and code.
Java: Arrays -- 2-dimensional
Java: Arrays -- 2-dimensional
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users


Sign In
Create Account


Back to top









