aspose file tools*
The moose likes Beginning Java and the fly likes 2 dimensional array without hard coding values Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "2 dimensional array without hard coding values" Watch "2 dimensional array without hard coding values" New topic
Author

2 dimensional array without hard coding values

J Felder
Greenhorn

Joined: Nov 09, 2007
Posts: 4
I have to write a program that would generate a matrix that should display the following output. They are asking that we dont hard code our values in this program. I am lost on how I will get these values to display without hard coding. My question is how should my expression look. Any information that you all can give me I would greatly appreciate it.

Output:

2 4 6 8 10
3 6 9 12 15
4 8 12 16 20
5 10 15 20 25
6 12 18 24 30







Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Hi,

Welcome to JavaRanch!

Instead of storing the values in an array, consider how you could compute each value from the loop index variables at the position you need to print it. For example, 2, 4, 6... could be (column + 1)*2, right? If you think a little about it, there's a pretty obvious equation that uses both "row" and "column" and computes the value of every cell in your matrix.

I suspect it's OK to hardcode the "5"'s, by the way -- the loop limits.


[Jess in Action][AskingGoodQuestions]
J Felder
Greenhorn

Joined: Nov 09, 2007
Posts: 4
Thanks for all your help, I got it.
J Felder
Greenhorn

Joined: Nov 09, 2007
Posts: 4
Okay I tried what you told me and it works. but my output looks like this:

init:
deps-jar:
compile:
run:
2468102468102468102468102468103691215
3691215
3691215
3691215
3691215
BUILD SUCCESSFUL (total time: 0 seconds)


This is my code:

Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Well, three things:

1) "" and " " aren't the same; the first is an empty String, the second has one space in it. You want to print spaces between your numbers, not empty strings.

2) The first loop doesn't have that extra "System.out.println()" after each iteration of the inner loop, so everything is on one line.

3) You need to come up with a single equation which uses both variables to compute each number, and then just use a single set of nested loops (since you only want to print one matrix!)
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11476
    
  16

I made some minor modifications to get it to compile, and stuck in some print statements. try this code, and see if you can figure out what it's doing...



There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19760
    
  20

You're now adding the same numbers for each row: (column + 1) * 2.

You still don't see the pattern, do you?

Let's look at the first number in each row. See a pattern there? How does this relate to the row number?

Ok, now you have the first number. How do the rest of these numbers relate to that first number? See another pattern? And how does this relate to the column number?


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
J Felder
Greenhorn

Joined: Nov 09, 2007
Posts: 4
Yes I see the pattern, for every number in the first row you add 2 then the next row you add 3 to each number then so on and so on. I know am missing something on trying to put this all into one equation. Am going to take a break and then come back and look at it again. To try to find what am missing.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11476
    
  16

so, you add 2, then 4, then 5, then 6.

hmmm... what can i use to easily get me those numbers, one after the other...

HEY!!! that kind of sounds like a for-loop might work. I can start my loop with any value i want, so starting it at 2 shouldn't be a problem. then, I just have to make sure I stop it before my counter gets to be 7.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: 2 dimensional array without hard coding values