.:`=-~rANdOm~`-=:. Game Servers

Technology (Read Only) => Programming => Topic started by: Infinimint on August 31, 2012, 04:50:34 PM

Title: Decode This Puzzle
Post by: Infinimint on August 31, 2012, 04:50:34 PM
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++;
               }
            }
         }
      }
Title: Re: Decode This Puzzle
Post by: 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)
Title: Re: Decode This Puzzle
Post by: Infinimint on August 31, 2012, 06:31:19 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)

Yep, you got it for the most part :P

Spoiler (click to show/hide)
Title: Re: Decode This Puzzle
Post by: Sabb on August 31, 2012, 07:54:21 PM
Yep, you got it for the most part :P

Spoiler (click to show/hide)
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.
Title: Re: Decode This Puzzle
Post by: Infinimint on August 31, 2012, 08:23:40 PM
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. (http://upload.wikimedia.org/wikipedia/commons/3/3a/Apple_logo_Think_Different.png)