• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2087
  • Last Modified:

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

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
Joseph Moody
Asked:
Joseph Moody
1 Solution
 
CEHJCommented:
Looks a bit like homework ...
0
 
Joseph MoodyBlogger and wearer of all hats.Author Commented:
More of a personal assignment.
0
 
wwnosalCommented:
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
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

 
Joseph MoodyBlogger and wearer of all hats.Author Commented:
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
 
VenabiliCommented:
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
 
Joseph MoodyBlogger and wearer of all hats.Author Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now