Practice Problems in LOLGraphics/Solution to Problem I

Code edit

HAI 3.4 0 1
IM IN UR CODE EXECUTIN UR KOMANDZ

PLZ RUN SUBPROGRAM INPUT
PLZ RUN SUBPROGRAM OPERATION
PLZ RUN SUBPROGRAM COUNT

IM OUTTA UR CODE

IM IN UR SUBPROGRAM DAT IZ KALLED INPUT
PLZ TYPE TEXT X= 
I HAS A FOUR BYTE DAT IZ CALLED X
PLZ ASK TEH USR 2 GIMME A FOUR BYTE X
PLZ PRINT FOUR BYTE X

PLZ TYPE TEXT Y= 
I HAS A FOUR BYTE DAT IZ CALLED Y
PLZ ASK TEH USR 2 GIMME A FOUR BYTE Y
PLZ PRINT FOUR BYTE Y
IM OUTTA UR SUBPROGRAM

IM IN UR SUBPROGRAM DAT IZ KALLED OPERATION
PLZ PRINT TEXT 1 +
PLZ PRINT TEXT 2 -
PLZ PRINT TEXT 3 *
PLZ PRINT TEXT 4 /
I HAS A ONE BYTE DAT IZ CALLED OP
PLZ ASK TEH USR 2 GIMME A ONE BYTE OP
IM OUTTA UR SUBPROGRAM

IM IN UR SUBPROGRAM DAT IZ KALLED COUNT
SWITCH [OP]
CASE 1 PLUS
CASE 2 MINUS
CASE 3 MULTIPLICATION
CASE 4 DIVISION
IM OUTTA UR SUBPROGRAM

IM IN UR SUBPROGRAM DAT IZ KALLED PLUS
I HAS A FOUR BYTE DAT IZ CALLED Z
PLZ SET FOUR BYTE Z X+Y
PLZ PRINT FOUR BYTE Z
IM OUTTA UR SUBPROGRAM

IM IN UR SUBPROGRAM DAT IZ KALLED MINUS
I HAS A FOUR BYTE DAT IZ CALLED Z
PLZ SET FOUR BYTE Z X-Y
PLZ PRINT FOUR BYTE Z
IM OUTTA UR SUBPROGRAM

IM IN UR SUBPROGRAM DAT IZ KALLED MULTIPLICATION
I HAS A FOUR BYTE DAT IZ CALLED Z
PLZ SET FOUR BYTE Z X*Y
PLZ PRINT FOUR BYTE Z
IM OUTTA UR SUBPROGRAM

IM IN UR SUBPROGRAM DAT IZ KALLED DIVISION
I HAS A FOUR BYTE DAT IZ CALLED Z
PLZ SET FOUR BYTE Z 0
PLZ RUN SUBPROGRAM SUBTRACT
PLZ ASK CEILIN KAT 2 CHEK IZ [[[X]]]<0
IF CEILIN KAT IZ NODDING PLZ RUN CORRECT
PLZ PRINT FOUR BYTE Z
PLZ PRINT FOUR BYTE X
IM OUTTA UR SUBPROGRAM

IM IN UR SUBPROGRAM DAT IZ KALLED SUBTRACT
PLZ SET FOUR BYTE X X-Y
PLZ SET FOUR BYTE Z Z+1
PLZ ASK CEILIN KAT 2 CHEK IZ [[[X]]]>0
IF CEILIN KAT IZ NODDING PLZ RUN SUBTRACT
IM OUTTA UR SUBPROGRAM
 
IM IN UR SUBPROGRAM DAT IZ KALLED CORRECT
PLZ SET FOUR BYTE Z Z-1
PLZ SET FOUR BYTE X X+Y
IM OUTTA UR SUBPROGRAM

 IMPORTANT: The subprograms minus and subtract are not the same! Minus is for subtraction operation, and subtract is used for division because division is broken in LOLGraphics.

Explanation edit

This code can be easily divided into 3 parts:

  1. The first part of the code asks the user to enter 2 numbers and stores them in 2 4-byte variables called x and y. Note that LOLGraphics doesn’t have scopes - once you define a variable it exists forever until the program ends. That’s why there’s no problem in defining variables in the subprograms where they are first used.
  2. The second part of the code asks the user to enter the mathematical operation that he wishes to do. Because LOLGraphics doesn’t let you ask for string inputs - only number inputs, each one of the 4 basic arithmetic operations is given a number from 1-4.
  3. The third part of the code checks what operation the user entered using a switch/case structure and calculates the result of the arithmetic operation. Addition, subtraction, multiplication, and division are straight forward so I will focus on the division routine.
    Because division seems to be broken in LOLGraphics, I had to implement it myself. Ideally, the code would just keep subtracting Y from X in a loop while X>=Y but this simply isn’t possible. The code does it while X>0, but that means there’s a chance it will subtract to below 0, that’s why it after that has to check that X is not negative, and if it is correct the answer by subtracting 1 from Z, and Y to Z. After that, Z will store the integer part of the solution, and X the reminder, and both will be printed.

Alternative ways edit

There are multiple things that can be done differently. For one, if you run this code you will see X=5. You can theoretically add spaces that it will be X = 5, by remember that since the LOLGraphics interpreter wastes spaces from the beginning and end of every line, so to print the second space you will need to use the command PLZ ADD A SPACE.

Also, it’s possible to print to the user what operation is calculating. Since the code is in any case running with conditions, and each operation has a subprogram, the best way would be to add in each such subprogram a print command. PLZ PRINT TEXT + for the addition subprogram, etc.

Also it’s possible to implement the division routine differently. This code will subtract Y from X while X>0. This means that it’s possible that it will stop at X=-2 for example and that the result will be of so there’s a subprogram that checks and corrects if necessary. You can also subtract Y from X while X>Y but take into account that if X can be evenly divided by Y, then your answer will be lower by 1 then the correct answer, so you will still need to run a correction subprogram.