Click to See Complete Forum and Search --> : Array question


Elizabeth8338
03-06-2003, 07:12 PM
I'm sure this is a really stupid question, but I've been looking for ages and I can't find it in my book. I'm working on an array for my intro to programming class, and I'm having some trouble getting it to work right...I know absolutely nothing about java. :)

Here's what I have so far (I'm using the JCreator compiler, if that makes a difference?):

import java.io.*;
import java.util.*;

class HomeworkB
{
public static void main(String[] args)
{
int a [][] = new int [11][11];

for(int row = 0; row<a.length; row ++)
{
for(int col = 0; col<a[row].length; col++)
a[row][col] = row * col;
}

for(int row = 0; row<a.length; row ++)
{
for(int col = 0; col<a[row].length; col++)
System.out.print(a[row][col] + " ");
System.out.println();
}

System.out.println();
System.exit(0);
}
}

Elizabeth8338
03-06-2003, 07:16 PM
The code above works, but I need it formatted differently. It's just a basic multiplication table. My prof wants me to get rid of the first row and column of 0's and have the multiples there. He also wants an asterisk in the first space.

Here's a basic picture of what he wants:

* | 1 2 3 4 5 6 7 8 9 10
-------------------------
1 |1 2 3 4 5 6 7 8 9 10
2 |2 4 6 8 10 12 14 16 18 20
3 |...
4 |
5 |
6 |
7 |
8 |
9 |
10|

The numbers are supposed to line up in the columns.

Any help would be greatly appreciated! :D

AdamBrill
03-06-2003, 08:06 PM
Okay, try this code instead:
import java.io.*;
import java.util.*;

class Form1
{
public static void main(String[] args)
{
int a [][] = new int [11][11];
System.out.print("* | 0 1 2 3 4 5 6 7 8 9 10\n");
System.out.print("-------------------------\n");
for(int row = 1; row<a.length; row ++)
{
for(int col = 0; col<a[row].length; col++)
a[row][col] = row * col;
}

for(int row = 1; row<a.length; row ++)
{
System.out.print(row + " | ");
for(int col = 0; col<a[row].length; col++)
System.out.print(a[row][col] + " ");
System.out.println();
}

System.out.println();
System.exit(0);
}
}Also, you must not have noticed, but this is a javascript forums, which is totally different from java, just so you know. :) I'll still do my best to answer your questions, but don't expect too much. LOL :D

Elizabeth8338
03-06-2003, 08:24 PM
Thanks very much for your help! Sorry to be off topic...I didn't realize there was that much of a difference. :confused: I'm not exactly a programmer...

AdamBrill
03-06-2003, 08:30 PM
LOL That's quite all right. If you have any other questions, ask away... Just don't expect too much. :D