Powered by Invision Power Board

 
    Reply to this topicStart new topicStart Poll

> Java help (Update. Read first post), for CS101
United States
BubbleMan
Posted: Sep 28 2009, 06:33 PM
Quote Post


aka Aku
[*][*][*]

Group Icon
Group: Members
Posts: 18507
Member No.: 260
Joined: 27-January 04

Status: (0d) [--]


I need to draw dots using nested for loops. The actual function that creates the dot is not important. The point of this assignment is to figure out how to draw the image shown using a maximum of 3 nested for loops and a maximum of two calls to the bigDot function, which places a dot and connects it to the last dot drawn. For example:

user posted image

This is one of the images we must recreate. This one is done with the following loops:

CODE

for (row = 0; row < ROWS; row++)
   for (col = 0, col < COLS; col++)
       board.bigDot(row,col);
   break;


This one is simple enough. ROWS = 8, which is the max number of rows.
COLS = 8, which is the max number of columns. row is the current row number and col is the current column number. This one goes through each row, and places a dot in each column.

The one I'm having trouble with is this:

user posted image

First we needed to do just one diagonal line. I did this with the following code:
CODE

for (row = 0; row <= ROWS; row++)
   for (col = row; col == row; col++)
       board.bigDot(row,col);
   break;


I want to know how I can change this code so that it somehow resets itself and starts over, or a new way of doing this. You can see the path of the line which shows the order that the dots are being made.

The rules are that I'm only allowed to use nested for loops and a call to the bigDot function. No more than 3 nested loops and no more than two calls for the method. Nothing else is allowed.

EDIT: Actually I figured it out finally using this code:
CODE
for (k = 0; k <= ROWS; k = k + ROWS)
                   for (row = 0; row <= ROWS; row++)
                       for (col = row; col == row; col++)
                           board.bigDot(row,Math.abs(col - k));
                   break;


Now I need help with another picture:
user posted image

Again, I did the first half (left side) with this code:
CODE
for (col = 0; col < COLS; col++)
                   for (row = col; row < (ROWS - col); row++)
                       board.bigDot(row,col);
                   break;

too lazy to explain what the code does as I'm assuming I'm going to get help from someone who can read it. Again, I want to know how I can modify this code to sort of repeat the image, or some brand new code that can get it done.
Also, I know the paragraphs are all screwy in the code block, it's not like that in the actual code.

This post has been edited by BubbleMan on Sep 29 2009, 04:47 PM


--------------------
user posted image
PMEmail PosterUsers WebsiteAOLMSN
Top
United States
Ianprime0509
Posted: Sep 28 2009, 07:00 PM
Quote Post


My favorite E3 game so far
[*][*]

Group Icon
Group: Members
Posts: 488
Member No.: 3316
Joined: 17-February 07

Status: (0d) [--]


Instead of just having a plain "break;" you could have it check to see whether it's done the first line and then just change row and col, maybe replace the break with this:
CODE
if (row == ROWS && col == COLS)
{
row = 0;
*reverse the increment of col, I'm too lazy to figure out how to do this*
}
else
{
break;
}


It should work, but I'm not a Java expert or anything.


--------------------

user posted imageuser posted imageuser posted image
Signature version 3.0
PMEmail Poster
Top
Unspecified
Lightning
Posted: Sep 28 2009, 07:33 PM
Quote Post


Ignorance isn't stupidity but choosing to remain ignorant is
[*][*]

Group Icon
Group: IRC Operators
Posts: 6381
Member No.: 583
Joined: 31-August 04

Status: (0d) [--]


first of all, use braces. that "break" isn't doing what you think it is. (it has the wrong indentation for where it is, anyway)

second, this:
for (col = row; col == row; col++)
is completely unnecessary. a loop that executes exactly one time?

CODE
for (row = 0; row <= ROWS; row++) {
      board.bigDot(row,row);
}
for (row = 0; row <= ROWS; row++) {
      board.bigDot(ROWS-row,row);
}


This post has been edited by Lightning on Sep 28 2009, 07:33 PM


--------------------
click here to change my avatar. / gosh why are you even here lightning
hacker, n.an individual who enjoys learning computer system details and how to capitalize on his or her capabilities...not a criminal.
(from webster's new world hacker dictionary)
fedora linux 10
Fedora 10 Final!

Download today!
quality web comics (stories):
  1. girl genius: adventure! romance! mad science!
  2. punch an' pie: try a slice of life, then swallow.
  3. dresden codak: most interesting comic ever
quality web comics (one-shots):
  1. a softer world: truth and beauty bombs
  2. smbc: saturday morning breakfast cereal
  3. buttersafe: pictures and probably some words
"Religion is comparable to a childhood neurosis." - Sigmund Freud
“It is not by delusion, however exalted, that mankind can prosper, but only by unswerving courage in the pursuit of truth.” - Bertrand Russell
“To kill an error is as good a service as, and sometimes better than, the establishing of a new truth or fact.” - Charles Darwin
PMUsers WebsiteMSN
Top
United States
BubbleMan
Posted: Sep 28 2009, 07:41 PM
Quote Post


aka Aku
[*][*][*]

Group Icon
Group: Members
Posts: 18507
Member No.: 260
Joined: 27-January 04

Status: (0d) [--]


QUOTE (Lightning @ Sep 28 2009, 05:33 PM)
first of all, use braces. that "break" isn't doing what you think it is. (it has the wrong indentation for where it is, anyway)

second, this:
for (col = row; col == row; col++)
is completely unnecessary. a loop that executes exactly one time?

CODE
for (row = 0; row <= ROWS; row++) {
      board.bigDot(row,row);
}
for (row = 0; row <= ROWS; row++) {
      board.bigDot(ROWS-row,row);
}

The point of this assignment isn't to be efficient, it's to get used to the idea of nested loops. Therefor, we're required to write it in that exact format. We haven't learned breaks or if statements or anything. The only thing we're allowed to use is a nested for loop and a call to the bigDot method. I guess I didn't make that clear. As far as I know, what you wrote would lose me points on the assignment.


--------------------
user posted image
PMEmail PosterUsers WebsiteAOLMSN
Top
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

  Topic Options Topic Options Reply to this topicStart new topicStart Poll

 




[ Script Execution time: 0.0746 ]   [ 14 queries used ]   [ GZIP Enabled ]   [ Server Load: 0.76 ]