harold_finch
harold_finch

Reputation: 80

Multiplying table by using two dimensional arrays

I am trying to make a multiplication table from 1 to 10 by using two dimensional arrays. When I run the program I get each value under each other, not a table. I get the first value as 1 and all others 0. I also get the error Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 11 at summing.main(summing.java:46) Could you give some tips about the algorithm?

public static void main( String[] args) 
{
    Scanner scan = new Scanner( System.in);
    int x,y,z;
    int[][] table = new int[11][11];
    table[0][0] = 0;
    table[0][1] = 1;
    table[0][2] = 2;
    table[0][3] = 3;
    table[0][4] = 4;
    table[0][5] = 5;
    table[0][6] = 6;
    table[0][7] = 7;
    table[0][8] = 8;
    table[0][9] = 9;;
    table[0][10] = 10;  
    table[1][0] = 1;
    table[2][0] = 2;
    table[3][0] = 3;
    table[4][0] = 4;
    table[5][0] = 5;
    table[6][0] = 6;
    table[7][0] = 7;
    table[8][0] = 8;    
    table[9][0] = 9;
    table[10][0] = 10;

    System.out.println( "Start of multiplication\n");

    for (x = 0; x <= 10; x++)
    {
        for (y = 0; y <= 10; y++)
        {
            table[x][y] = table[x][1] * table[y][1];
            System.out.println(table[x][y]);//This line has the error.
        }
        table[x][y] = table[x][1] * table[y][1];
        System.out.print(table[x][y]);  
    }

    System.out.println( "\nEnd of multiplication\n" );
}

Upvotes: 0

Views: 4968

Answers (4)

xGrubbx
xGrubbx

Reputation: 1

for(int i = 0;i<11;i++){
table[0][i] = i+1;
    table [i] [0] = i+1;
}

is much more efficient than having that massive list of declarations

Upvotes: 0

varun
varun

Reputation: 1523

First if all there is no need to start the loop from x=0 and y=0. Second print all the values after calculating the values in the table Third ArrayIndexOutOfBound was Because when the y loop completed the value of y is 11 and you were using the that value of y after the loop has finished.

public static void main( String[] args) 
{
Scanner scan = new Scanner( System.in);
int x,y,z;
int[][] table = new int[11][11];
table[0][0] = 1;
table[0][1] = 1;
table[0][2] = 2;
table[0][3] = 3;
table[0][4] = 4;
table[0][5] = 5;
table[0][6] = 6;
table[0][7] = 7;
table[0][8] = 8;
table[0][9] = 9;;
table[0][10] = 10;  
table[1][0] = 1;
table[2][0] = 2;
table[3][0] = 3;
table[4][0] = 4;
table[5][0] = 5;
table[6][0] = 6;
table[7][0] = 7;
table[8][0] = 8;    
table[9][0] = 9;
table[10][0] = 10;

System.out.println( "Start of multiplication\n");

for (x = 1; x <= 10; x++)
{
    for (y = 1; y <= 10; y++)
    {
        table[x][y] = table[x][0] * table[y][0];

    }
}

System.out.println();

for(x=1;x<=10;x++){
    for(y=1;y<=10;y++){
        System.out.print(table[x][y] + "\t");
    }
    System.out.println("\n");
}

System.out.println( "\nEnd of multiplication\n" );
}    

Upvotes: 1

Prerak Sola
Prerak Sola

Reputation: 10019

You can try this, if you cannot get the error in your code. But first try to find the solution using your code as @T.J. Crowder said:

public static void main( String[] args) 
{
   // Scanner scan = new Scanner( System.in);
    int x,y;
    int[][] table = new int[11][11];
    table[0][0] = 0;
    table[0][1] = 1;
    table[0][2] = 2;
    table[0][3] = 3;
    table[0][4] = 4;
    table[0][5] = 5;
    table[0][6] = 6;
    table[0][7] = 7;
    table[0][8] = 8;
    table[0][9] = 9;;
    table[0][10] = 10;  
    table[1][0] = 1;
    table[2][0] = 2;
    table[3][0] = 3;
    table[4][0] = 4;
    table[5][0] = 5;
    table[6][0] = 6;
    table[7][0] = 7;
    table[8][0] = 8;    
    table[9][0] = 9;
    table[10][0] = 10;

    int[][] output_table = new int[11][11];

    System.out.println( "Start of multiplication\n");

    for (x = 0; x < 10; x++)
    {
        System.out.println("Table of: "+table[x][0]);
        for (y = 0; y < 10; y++)
        {
            output_table[x][y] = table[0][x] * table[y][0];
            System.out.print(output_table[x][y]+"\t");//This line has the error.
        }

        output_table[x][y] = table[0][x] * table[y][0];
        System.out.println(output_table[x][y]);  
    }

    System.out.println( "\nEnd of multiplication\n" );
}

`

Upvotes: 0

T.J. Crowder
T.J. Crowder

Reputation: 1075875

The reason you see a series of lines rather than values across is that your inner for loop uses println, not print. The inner loop would use print, then the outer loop would use println to break lines.

The reason you get an out-of-bounds exception is because of this:

for (y = 0; y <= 10; y++)
{
    table[x][y] = table[x][1] * table[y][1];
    System.out.println(table[x][y]);
}
table[x][y] = table[x][1] * table[y][1]; // <======= Here

After the loop, y has the value 11, so indexing into the table with it will cause the out-of-bounds exception. You don't want that line at all, or the one after it. After the inner loop, all you want is:

System.out.println();

...to start a new line.

Re algorithm: You don't actually need a two-dimensional array (or any array at all) to do this. But if you use one, you don't need to pre-initialize it with anything, because you have x and y. Just assign the product of x and y to the relevant cell in the inner loop:

table[x][y] = x * y;

I am trying to make a multiplication table from 1 to 10...

Then the bounds of your loops are wrong, as you're going from 0 to 10 (inclusive). Instead, you'd want 0 (inclusive) to 10 (exclusive) and to add one to your x and y when doing the multiplication:

int[][] table = new int[10][10];
// ...
for (x = 0; x < 10; ++x) {
    for (y = 0; y < 10; ++y) {
         // ...
        table[x][y] = (x + 1) * (y + 1);
    }
}

Hopefully that's enough to go on, I didn't want to actually write the code for you, as you'll learn more by working it out.

Upvotes: 1

Related Questions