Multiplication

Question.  I have this tutorial regarding Arrays.

    /**
     * Builds an integer multiplication table.  No particular
     * behavior is guarenteed for a negative input value.  For
     * example, if max was 3, the table would look like this:
     * +-+-+-+-+
     * |0|0|0|0|
     * +-+-+-+-+
     * |0|1|2|3|
     * +-+-+-+-+
     * |0|2|4|6|
     * +-+-+-+-+
     * |0|3|6|9|
     * +-+-+-+-+
     * Notice that the table has max+1 rows and columns.
     *
     * @param max The number the table goes up to
     */

   public int[][] getMultTab(int max)
    {
        int row = 0;
        int col = 0;
        int[][] ans = new int[row][col];
       
        for (int i = 0; i < ans.length; i++)
           for (int j = 0; j < ans[i].length; j++)
           ans[i][j] = i*j;
       
           return max[][];
    }
}

Am I going abou this the right way?  I dont think i'm thinking about it correctly.  Any insight into this would be helpfull.  I'm trying to understand this array stuff.  If anybody has a simplistic way to view how you would look at this, it would help greatly.

Thanks You,
David McGraw
LVL 3
ildave1Asked:
Who is Participating?
 
CEHJConnect With a Mentor Commented:
Should actually be

int[][] ans = new int[max+1][max+1];

as row and col never get used otherwise
0
 
ildave1Author Commented:
I should actually post what method is this referencing. Note that nothing needs to be changed here.

      /**
       * Test the "getMultTab" method.  Print a message about success
       * or failure.
       *
       * @param tObj The Lab9 object to be tested
       */
      public static void test_getMultTab(Lab9 tObj)
      {
            System.out.print("Testing method getMultTab(int):  ");
            try
            {
                  int[][] tab = tObj.getMultTab(50);
                  if (tab == null) throw new Exception("Result is null");
                  if (tab.length != 51) throw new Exception("Incorrect size");
                  if (tab[0].length != 51) throw new Exception("Incorrect size");

                  for (int i=0; i<tab.length; ++i)
                  {
                        for (int j=0; j<tab[i].length; ++j)
                        {
                              if (tab[i][j] != i*j) throw new Exception("Bad value at index: "+i+", "+j);
                        }
                  }

                  System.out.println("[PASS]");
            }
            catch (Throwable t)
            {
                  System.out.println("[FAIL] "+t.getMessage());
            }

      }
}
0
 
CEHJCommented:
The first thing to do is to initialize a table of the correct size. At the moment you initialize to zero size
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
MogalManicConnect With a Mentor Commented:
The initialization of row,col is incorrect.  It currently creates arrays of 0 length and your for loops never get entered.  If you want to create a 4x4 matrix when max is 3 then the initialization should be:
        int row = max+1;
        int col = max+1;
0
 
zzynxSoftware engineerCommented:
>>        return max[][];

I guess you meant

            return ans;
0
 
zzynxSoftware engineerCommented:
... returning the 2-dimensional array "ans" you just created
0
 
zzynxConnect With a Mentor Software engineerCommented:
>> Any insight into this would be helpfull
See the comments


   public int[][] getMultTab(int max) {
        int[][] ans = new int[max+1][max+1];  // Create a 2-dim array of size (max+1) x (max+1)
       
        for (int i = 0; i < ans.length; i++)
           for (int j = 0; j < ans[i].length; j++)
           ans[i][j] = i*j;  // assign to each value in the array the result of row * column
       
           return ans;  // return the created 2-dim array
    }
0
 
CEHJCommented:
Homework done then!
0
 
zzynxSoftware engineerCommented:
Ooops. Ask for a deletion if I offended someone.
0
 
ildave1Author Commented:
Great!  Thanks a lot for the help everybody.  

// int[][] ans = new int[max+1][max+1];
   I wondered about that.  I dont know why I thought I could attempt to create row and col by simply putting them in.

// return ans;
   Bingo. max was giving me an error, I forgot to change it back to ans when I was tinkering around.

Just a side note, this wasnt homework.  I know its easy to asume that, expecially with a hub like this.  However, arrays are the subject of what we are learning this week, so I'm trying to understand them to the best that I can.  What I was attempting an assignment that was actually found on a different universities webpage from googling arrays.

Pretty good way to get a feel for the different types of learning styles that goes on at various universities.

Again, I appreciate the assistance!  Welp, Back to trying to figure out this Calc! I love all nighters!

Regards,
David McGraw
0
 
zzynxSoftware engineerCommented:
>>   Bingo. max was giving me an error, I forgot to change it back to ans when I was tinkering around.
Then why am I not in the point split?
0
 
ildave1Author Commented:
zzynx,

Well, I didnt think that alone merited points. After reviewing the subject again, I do like that you explained how this application worked on your prior post.

I apologize zzynx, I pulled an all nighter crunching numbers in my calc book and tinkering around with some Java.

I tell you what, Im going to create another question marked as, 'Java Award'.  That should do without the hassle of going through admin.

Regards,
David McGraw
0
 
zzynxSoftware engineerCommented:
>> Im going to create another question marked as, 'Java Award'.  
Not allowed

>> That should do without the hassle of going through admin.
That's absolutely no hassle. I can ask to reopen if you want.
0
 
zzynxSoftware engineerCommented:
Posting another question like you did will cause more hassle then just reopening this.
As I told you, this is not allowed. I asked a reopen of this question.
0
 
ildave1Author Commented:
Not Allowed?.... There is a question there.. Either you will answer it, or somebody else will... Be the first...

Go answer my question!!! Its under "Java Award"...

Hint: google.com or heck you may just know.
0
 
ildave1Author Commented:
Okay, I deleted the question.  This question wont be re-opened. CEHJ reported this as a HW question so it's schedualed for deletion.

Thanks anyways

Regards,
David McGraw
0
 
zzynxSoftware engineerCommented:
>> This question wont be re-opened.
Let's see ;°)

>> CEHJ reported this as a HW question so it's schedualed for deletion
What makes you think that?
0
 
zzynxSoftware engineerCommented:
>> Not Allowed?.... There is a question there.
First, there wasn't.

>> Go answer my question!!! Its under "Java Award"...
I don't like to work this way. Moreover it's not allowed.
0
 
ildave1Author Commented:
>> This question wont be re-opened.
>>>Let's see ;°)
Okay :)

>> CEHJ reported this as a HW question so it's schedualed for deletion
>>>What makes you think that?
Check the community service board ;)

>> Go answer my question!!! Its under "Java Award"...
>>>>I don't like to work this way. Moreover it's not allowed
So now I cant ask a question and reward the correct answer? lol. Just giving you a hard time man.  Its over.

Thanks,
Dave
0
 
zzynxSoftware engineerCommented:
>> So now I can't ask a question and reward the correct answer?
Apparently you answered already yourself, since you removed it.

>> Its over.
Meaning?
0
 
zzynxSoftware engineerCommented:
Thanks
0
All Courses

From novice to tech pro — start learning today.