?
Solved

Basic Java Question: Creating a two dimension array to output multiplication tables

Posted on 2008-11-18
7
Medium Priority
?
2,083 Views
Last Modified: 2013-11-23
Hello Everyone!

Now that I have some more free time, I am back to trying to understand JAVA. I have a basic script that needs a lot of work. I am trying to write an application using a two dimension array that will create a multiplication table. Below is an example of what I would like for it to ouput:

0     1     2     3     4     5     6
1     1     2     3     4     5     6
2     2     4     6     8     10     12
3     3     6     9     12     15     18
4     4     8     12     16     20     24
5     5     10     15     20     25     30
6     6     12     18     24     30     36

Basically, the results are obtained by multiplying the row and column values together. I have attached what I have so far but I do not really understand what I need to do to get the output above.

/**
 * @(#)test.java
 *
 * test application
 *
 * @author 
 * @version 1.00 2008/11/18
 */
 
public class test {
    
    public static void main(String[] args) {
    	
 long[][] table;           
  table = new long[x][y];
  for (int i = 0; i < x; i++) {
    for (int j = 0; j < y; j++) {
      table[i][j] = i * j;
    }
  }
 
    }
}

Open in new window

0
Comment
Question by:Joseph Moody
6 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 22985686
Looks a bit like homework ...
0
 
LVL 22

Author Comment

by:Joseph Moody
ID: 22985715
More of a personal assignment.
0
 
LVL 6

Accepted Solution

by:
wwnosal earned 2000 total points
ID: 22985731
How about something like this:
public class Dummy {
 
  public static void main(String[] args) {
    int x = 12;
    int y = 10;
    long[][] multi = new long[x][y];
    for( int i = 0;i < x;i++ )
      for( int j = 0;j < y;j++ )
        multi[i][j] = ( i + 1 ) * ( j + 1 );
 
    for( int i = 0;i < y;i++ ) {
      for( int j = 0;j < x;j++ ) {
        System.out.print(multi[j][i] + " ");
      }
      System.out.println();
    }
  }
}

Open in new window

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 22

Author Closing Comment

by:Joseph Moody
ID: 31517869
Now that is pretty cool! There are some more things that I want to do with it but that now that I have a basic piece to build off of, I should be fine! Thanks!
0
 
LVL 20

Expert Comment

by:Venabili
ID: 22985872
Well - guess I was wrong and just getting the code was what was enough... which puts the question in the "do my homework category"... So friendly reminder for the experts - it is ok to help someone and to explain what they need to do - it's not so ok to write a full program and run away with no explanation :)

Jmoody10,

Good luck with Java - if the plan is to learn it.

By the way - you can change the first cycle a bit -- because the table is mirrored you can fill it with half the moved. So something like this wouold do (as your table is square. If it was not, you need to do some checks on the i,j before reverting
for( int i = 0;i < x;i++ )
      for( int j = i;j < y;j++ ){
        multi[i][j] = ( i + 1 ) * ( j + 1 );
        multi[j][i] = multi[i][j]
}

Open in new window

0
 
LVL 22

Author Comment

by:Joseph Moody
ID: 22986601
Thanks Venabili. I have actually spent the last hour disecting the code and I think it is making more sense to me. I do have one question that pertains to more of the array itself. What is the purpose of "long"?
long[][] multi = new long[x][y];

Open in new window

0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses
Course of the Month14 days, 13 hours left to enroll

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question