• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

2 dimensional array without hard coding values

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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







 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
J Felder
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for all your help, I got it.
 
J Felder
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!)
 
lowercase baba
Posts: 13091
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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...

 
Sheriff
Posts: 22849
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
J Felder
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 13091
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic