Re: Problem: printing a matrix



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
.



Relevant Pages

  • Re: A speakable (kind of) SUO-KIF ?
    ... parens, but this is a simple example. ... function before declaring or defining it). ... Just define g or at least declare it before using it for ... For 3 args: ...
    (comp.lang.lisp)
  • Default arguments, etc ..
    ... Is there a way to declare a variadiac function in a Base class? ... Default args to a function are the last entries within the function ... class Derived1: public Base ...
    (alt.comp.lang.learn.c-cpp)
  • Re: A cute reader hack
    ... (declare (dynamic-extent args)) ... You seem to have suggested to declare as dynamic-extent the &rest ... It is always unsafe to modify &rest arguments. ...
    (comp.lang.lisp)
  • Re: A cute reader hack
    ... (lambda (&rest args) ... (declare (dynamic-extent args)) ... You seem to have suggested to declare as dynamic-extent the &rest ...
    (comp.lang.lisp)