Re: Problem: printing a matrix
- From: SARA <dev.sara87@xxxxxxxxx>
- Date: Fri, 3 Oct 2008 12:05:00 -0700 (PDT)
On Oct 3, 8:25 pm, John W Kennedy <jwke...@xxxxxxxxxxxxx> wrote:
SARA wrote:
hi all
please i wan to print a matrix like this one
http://mathdemos.gcsu.edu/mathdemos/vigenere/ventableau.gif
I write this code but did not give me the desired results
what is the wrong with it
public static void main(String[] args) {
args should be final.
// TODO code application logic here
Eclipse puts that line there to tell you that you have to put your
actual main method there. You've done that, so this line should go.
int ROWS=26;
ROWS should be final.
int COLS=26;
COLS should be final.
int k=0;
You don't need k here, and you don't need to initialize it.
char []ALPH=
{'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
ALPH should be final.
char[][] VIG = new char[26][26];
VIG should be final, and should not have an all-caps name, and [26][26]
should be [ROWS][COLS].
for(int i=0; i<ROWS; i++)
{
for(int j=0; j<COLS; j++)
{
k=Math.abs(j-i);
This is where k should be declared, as final int.
VIG[i][j]=ALPH[k];
However, since this is the only place you use k, you should just have
the Math.abs(j-i) there instead of k, and not declare k at all.
}
}
for(int i=0; i<ROWS; i++)
{
for(int j=0; j<COLS; j++)
{
System.out.print(VIG[i][j]);
}
System.out.println(" ");
Do you actually need to print that space? System.out.println(); will
finish the line just as well.
}
}
However, with all those corrections made, the question remains: what's
wrong with the program? It works, and it does exactly what you wrote.
Perhaps, instead of "Math.abs(j-i)" you mean "(i + j) % COLS"?
There are a number of other stylistic questions. This is how I would do
it (except that I would add a good many comments):
import static java.lang.System.out;
public final class Matrix {
static final int ROWS = 26;
static final int COLS = 26;
static final char[][] VIG = new char[ROWS][COLS];
static {
for (int i = 0; i < ROWS; ++i)
for (int j = 0; j < COLS; ++j)
VIG[i][j] = (char) ('A' + (i + j) % COLS);
}
public static void main(final String[] args) {
for (int i = 0; i < ROWS; ++i) {
for (int j = 0; j < COLS; ++j)
out.print(VIG[i][j]);
out.println();
}
}
}
--
John W. Kennedy
Read the remains of Shakespeare's lost play, now annotated!http://pws..prserv.net/jwkennedy/Double%20Falshood/index.html
thank u
that is what i intend to do ,
Regards
.
- Follow-Ups:
- Re: Problem: printing a matrix
- From: Lew
- Re: Problem: printing a matrix
- References:
- Problem: printing a matrix
- From: SARA
- Re: Problem: printing a matrix
- From: John W Kennedy
- Problem: printing a matrix
- Prev by Date: Re: Problem: printing a matrix
- Next by Date: Re: Problem: printing a matrix
- Previous by thread: Re: Problem: printing a matrix
- Next by thread: Re: Problem: printing a matrix
- Index(es):
Relevant Pages
|