Technology (Read Only) > Programming

Decode This Puzzle

(1/1)

Infinimint:
I got bored and made this in Java. If you already know Java, don't give away the answer :P

Question: a) What is the output of this? b) How does changing the value of the variables affect the result? c) Provide a visual representation of the output as well as an example with changed variables.
Rules: You cannot actually compile the code or anything similar. It's cheating. Honor code bro, honor code.


--- Quote ---
      int cMax = 5;
      int rMax = 5;
      int columns = 0;
      int rows = 0;

         while (rows < rMax) {
         if (columns == cMax) {
            System.out.println();
            columns = 0;
            rows++;
         } else {
            if (rows == 0 || rows == rMax - 1) {
               for (columns = 0; columns < cMax; columns++) {
                  System.out.print("*");
               }
            } else {
               if (columns >= 1 && columns <= cMax -2) {
                  System.out.print(" ");
                  columns++;
               } else {
                  System.out.print("*");
                  columns++;
               }
            }
         }
      }

--- End quote ---

coolzeldad:
While this looks like a book problem... I'll answer cause I think its fun LOL I'm retarded.

Anyway I think this is how it works without looking at anythingz

Spoiler: dunlewkifnotwantanswer (click to show/hide)
Here's teh code I documented.. I think with it you can change variables/methods in anyway


int cMax = 5; //max columns
int rMax = 5; //max rows
int columns = 0; //current column count
int rows = 0; //current row count

while (rows < rMax) { //runs 5 times (0, 1, 2, 3, 4) --using starting index of 0
        if (columns == cMax) { //if we hit 6 columns (if 0 == 5) 0, 1, 2, 3, 4, 5
                System.out.println(); //output newline
                columns = 0; //reset column count
                rows++; //next row
        } else {
                if (rows == 0 || rows == rMax - 1) { //if starting or at end of row
                        for (columns = 0; columns < cMax; columns++) { //output * 5 times ( if columns < 5 || 0, 1, 2, 3, 4 )
                                System.out.print("*"); //put asterisk
                        }
                } else { //if in the middle rows ( 1, 2, 3 )
                        if (columns >= 1 && columns <= cMax -2) { //if we are in the middle columns ( 1, 2, 3 )
                                System.out.print(" "); //put space
                                columns++; //next column
                        } else { //if we are in the outside columns ( 0, 4 )
                                System.out.print("*"); //put asterisk
                                columns++; //next column
                        }
                }
        }
}

Example with current cmax and rmax values of 5, replacing space with underscore for space formatting


*****

*___*

*___*

*___*

*****

Infinimint:

--- Quote from: coolzeldad on August 31, 2012, 06:20:01 PM ---While this looks like a book problem... I'll answer cause I think its fun LOL I'm retarded.

Anyway I think this is how it works without looking at anythingz

Spoiler: dunlewkifnotwantanswer (click to show/hide)
Here's teh code I documented.. I think with it you can change variables/methods in anyway


int cMax = 5; //max columns
int rMax = 5; //max rows
int columns = 0; //current column count
int rows = 0; //current row count

while (rows < rMax) { //runs 5 times (0, 1, 2, 3, 4) --using starting index of 0
        if (columns == cMax) { //if we hit 6 columns (if 0 == 5) 0, 1, 2, 3, 4, 5
                System.out.println(); //output newline
                columns = 0; //reset column count
                rows++; //next row
        } else {
                if (rows == 0 || rows == rMax - 1) { //if starting or at end of row
                        for (columns = 0; columns < cMax; columns++) { //output * 5 times ( if columns < 5 || 0, 1, 2, 3, 4 )
                                System.out.print("*"); //put asterisk
                        }
                } else { //if in the middle rows ( 1, 2, 3 )
                        if (columns >= 1 && columns <= cMax -2) { //if we are in the middle columns ( 1, 2, 3 )
                                System.out.print(" "); //put space
                                columns++; //next column
                        } else { //if we are in the outside columns ( 0, 4 )
                                System.out.print("*"); //put asterisk
                                columns++; //next column
                        }
                }
        }
}

Example with current cmax and rmax values of 5, replacing space with underscore for space formatting


*****

*___*

*___*

*___*

*****

--- End quote ---

Yep, you got it for the most part :P

Spoiler (click to show/hide)Although, since the values are already at 0, they are not accounted for when we do column++ or row++, so there is only 5.
1,2,3,4,5

The cool part about it though, is that you can replace the cMax and rMax values for the number of columns and rows, respectively, making it dynamic and resizable while keeping the same properties (an empty star square).

With a few adjustments it could be used to title any text that you input to it, and it could easily be converted to C++ (I'll post it in the OP in a few minutes.)

Well done :P

Sabb:

--- Quote from: Infinimint on August 31, 2012, 06:31:19 PM ---Yep, you got it for the most part :P

Spoiler (click to show/hide)Although, since the values are already at 0, they are not accounted for when we do column++ or row++, so there is only 5.
1,2,3,4,5

The cool part about it though, is that you can replace the cMax and rMax values for the number of columns and rows, respectively, making it dynamic and resizable while keeping the same properties (an empty star square).

With a few adjustments it could be used to title any text that you input to it, and it could easily be converted to C++ (I'll post it in the OP in a few minutes.)

Well done :P
--- End quote ---
His example does have 5 rows/columns? Just that the if statement checks when it's about to hit 6 total rows/columns, then outputs a new line and resets the value. But I'm not sure what you mean with the 0 not being accounted for. Zero is an integer so the variable isn't without value and an asterisk will be printed if the value is zero because the if statement basically says "if rows is equal to 0 or 4 (max [5] -1) then place an asterisk." So it still starts placing an asterisk at 0 technically making it 0,1,2,3,4 not 1,2,3,4,5. Unless I'm misunderstanding what you're trying to point out idk.

Infinimint:
To clarify, the visual counting on the front end with the variables does not take the zero into account, but like you guys mentioned, the backend  of how the code works itself does. So it is right to say that it counts starting from one if you were talking about the language, however the way that the variables are setup kinda just count to 5 :P

But yes, seeing how the first set must be less than 4, in order for it to cycle 5 times it must count 0,1,2,3,4.
However, in other cases, such as counting the columns, it goes all the way up to five since it makes decisions based upon the variable.

It's hard to explain from my perspective. Visually, the math doesn't appear to count from 0, but in reality the code does count from 0.


TL;DR My mind works weird and I see things differently, hence my hipster PC.

Navigation

[0] Message Index

Go to full version