Jump to content

Can't make multi-dimesional multiplication table!

- - - - -

  • Please log in to reply
9 replies to this topic

#1
An Alien

An Alien

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 260 posts
Can somebody explain how multidimensional work? How to store and access info in them with examples would be nice too.

Edited by An Alien, 02 March 2011 - 05:35 PM.


#2
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
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
An Alien

An Alien

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 260 posts
Yeah, but with a multidimesional array.

#4
sourlemon

sourlemon

    Programmer

  • Members
  • PipPipPip
  • 99 posts
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.


#5
An Alien

An Alien

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 260 posts
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.

#6
ZekeDragon

ZekeDragon

    Writes binary right handed and hex left handed

  • Moderators
  • 2,103 posts
@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:
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.
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! :)
Wow I changed my sig!

#7
An Alien

An Alien

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 260 posts
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

#8
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,118 posts
  • Location:Vancouver, Eh! Cleverness: 200
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.)
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.

#9
An Alien

An Alien

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 260 posts
Okay, I updated my first post, please read it.

#10
lethalwire

lethalwire

    while(false){ ... }

  • Members
  • PipPipPipPipPipPipPip
  • 748 posts
  • Programming Language:Java, PHP
  • Learning:Java, PHP
Her's a good website to bookmark. He has provided tons of tutorials and code.
Java: Arrays -- 2-dimensional




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users