Solved

# Multiplication

Posted on 2005-03-30
Medium Priority
196 Views
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
0
Question by:ildave1
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 11
• 6
• 3
• +1

LVL 3

Author Comment

ID: 13660924
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

LVL 86

Expert Comment

ID: 13660946
The first thing to do is to initialize a table of the correct size. At the moment you initialize to zero size
0

LVL 21

Assisted Solution

MogalManic earned 200 total points
ID: 13660952
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

LVL 86

Accepted Solution

CEHJ earned 600 total points
ID: 13660997
Should actually be

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

as row and col never get used otherwise
0

LVL 37

Expert Comment

ID: 13661209
>>        return max[][];

I guess you meant

return ans;
0

LVL 37

Expert Comment

ID: 13661216
... returning the 2-dimensional array "ans" you just created
0

LVL 37

Assisted Solution

zzynx earned 200 total points
ID: 13661238
>> Any insight into this would be helpfull

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

LVL 86

Expert Comment

ID: 13661265
Homework done then!
0

LVL 37

Expert Comment

ID: 13661326
Ooops. Ask for a deletion if I offended someone.
0

LVL 3

Author Comment

ID: 13661773
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

LVL 37

Expert Comment

ID: 13661849
>>   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

LVL 3

Author Comment

ID: 13661916
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

LVL 37

Expert Comment

ID: 13661955
>> 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

LVL 37

Expert Comment

ID: 13662017
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

LVL 3

Author Comment

ID: 13662051
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

LVL 3

Author Comment

ID: 13662107
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

LVL 37

Expert Comment

ID: 13662135
>> 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

LVL 37

Expert Comment

ID: 13662169
>> 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

LVL 3

Author Comment

ID: 13662211
>> 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

LVL 37

Expert Comment

ID: 13662330
>> So now I can't ask a question and reward the correct answer?

>> Its over.
Meaning?
0

LVL 37

Expert Comment

ID: 13681378
Thanks
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing instaâ€¦
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used byâ€¦
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define â€¦
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arrâ€¦
###### Suggested Courses
Course of the Month13 days, 5 hours left to enroll