Guess what the output of this code is?

Post your FreeBASIC source, examples, tips and tricks here. Please don’t post code without including an explanation.
Post Reply
ganache
Posts: 47
Joined: Aug 04, 2016 9:25

Guess what the output of this code is?

Post by ganache »

Here is some code from a golf code challenge, that I translated to FB.

Code: Select all


  Dim As Integer i, j, k
  for i=-8 to 8
  for j=-8 to 9
  k=abs(i)+abs(j)
  if k > 8 then
  print " ";
  else
  print chr(57-k); 
  end if 
  next j
  print
  next i
  Sleep
  End
  
[code]

Run it!
fxm
Moderator
Posts: 12081
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Guess what the output of this code is?

Post by fxm »

Formatting only to be more readable:

Code: Select all

Dim As Integer i, j, k

for i=-8 to 8
    for j=-8 to 9
        k=abs(i)+abs(j)
        if k > 8 then
            print " ";
        else
            print chr(57-k);
        end if
    next j
    print
next i

Sleep

'End
sero
Posts: 59
Joined: Mar 06, 2018 13:26
Location: USA

Re: Guess what the output of this code is?

Post by sero »

I got this output: Image
Lothar Schirm
Posts: 436
Joined: Sep 28, 2013 15:08
Location: Germany

Re: Guess what the output of this code is?

Post by Lothar Schirm »

Nice!
UEZ
Posts: 972
Joined: May 05, 2017 19:59
Location: Germany

Re: Guess what the output of this code is?

Post by UEZ »

What do you think what this output could be?

Compile it as console!

Code: Select all

#Include "fbgfx.bi"
Using FB

'#Include "fbgfx.bi" must be added!
Declare Function LZW_Decode Alias "fb_hDecode" (Byval in_buffer As Any Ptr, Byval in_size As Integer, Byval out_buffer As Any Ptr, Byref out_size As Integer) As Integer
Declare Function Base64Decode(sString As String, Byref iBase64Len as Uinteger) As Ubyte Ptr

Dim As UInteger iLines, iCompression, iFileSize, iCompressedSize
Dim As String sBaseType, sBase64, aB64(1)

Restore __Label0:
Read iLines
Read iCompression
Read iFileSize
Read iCompressedSize
Read sBaseType

For i As Ushort = 0 To iLines - 1
   Read aB64(0)
   sBase64 &= aB64(0)
Next
Dim As UInteger l, i, c
Dim As Ubyte Ptr aBinary = Base64Decode(sBase64, iCompressedSize)

If iCompression Then
	Dim As Ubyte Ptr aBinaryC = Allocate(iFileSize) 
	LZW_Decode(aBinary, iCompressedSize, aBinaryC, iFileSize)
	For i = 0 To iFileSize
		Select Case aBinaryC[i]
			Case 10
				?
			Case Else
				? Chr(aBinaryC[i]);
		End Select
	Next
	Deallocate(aBinaryC)
Endif

aBinary = 0

? "Done."

Sleep

'original code by D.J.Peters
Function Base64Decode(sString As String, Byref iBase64Len as Uinteger) As Ubyte Ptr
	#Define P0(p) InStr(B64, Chr(sString[n + p])) - 1
	Dim As String*64 B64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
	Dim As String sDecoded
	Dim As Long nChars = Len(sString) - 1
	If nChars < 0 Then Return 0
	For n As Long = 0 To nChars Step 4
		Var b = P0(1), c = P0(2), d = P0(3)
		If b >-1 Then
			Var a = P0(0)
			sDecoded += Chr((a Shl 2 + b Shr 4))
		End If
		If c > -1 Then sDecoded += Chr((b Shl 4 + c Shr 2))
		If d > -1 Then sDecoded += Chr((c Shl 6 + d      ))
	Next
	iBase64Len = Len(sDecoded)
    
    'workaround For multiple embedded file other crash will occure
    Static As Ubyte aReturn(0 To iBase64Len - 1)
    Redim aReturn(0 To iBase64Len - 1) As Ubyte
	
	For i As ULong = 0 to Len(sDecoded) - 1 'convert result string to ascii code values
		aReturn(i) = Asc(sDecoded, i + 1)
	Next
	Return @aReturn(0) 'return pointer to the array
End Function

'Code below was generated by: FB File2Bas Code Generator v1.05 build 2020-09-23 beta

__Label0:
Data 2,8,1242,533,"Base64"
Data "IAAQASEQAqEDPdACO+ACA6EQC7EQDaAADDEQOzACEzECJaADEIERA+EQGQEQPUAREwEELMAQLtACIpERG8EReDACbwAEFIEHAVEQJeEHLQEEF4ERKJERQFACILACH1ECWFACH7ECPTECfnASD8EROnARKgEELfERFNECIPAGK3EUHBEQEjECTaEVTjERWAACfjACK1AVVgECKgESW8EVLzEVCXETSMERLGAWT+ETLMACI9ADHIETEAEXQFECTPETKxAUI4AFCQEYVpEXDLEDXgEQOxASMDERJZAQWLADhuEWGHEQAIEHUSEQePAGePASK2ASY5ERLoAHK5AYA+ECoIEHF0EXlEEWINADeDAZAtEDLPAGNvEah5ERK8AWIJAaQ2EUrwEQujEQOrADx/EGJXAcx4EFnHEbywEdvdEQlbEQSqEUO2Ad2cEC2cEd0qEQxCEc4SEQh1ESLOAC5xEa4iEaAHEQRiAHZVAGvKEeeEEd5gEC5rEd80Ef"
Data "CuECfuAHmgETPYAb7LECAOICPTAbAMECK9AD0qEbLSAYI5AgIIAFVBEQHoEFwQECEqEhOtASkDEe1PEVggEi7AEEQCEQPbEaAQEhCcIS3gECurECQDAVuIEHfVECoeEHUyEWHTECoaES/6ERIsISfcEWLQAaCzIbLNAUWBAaydEbOhAUdxEi/HEeAuECJmEfAJElqOEdXTIk/w8="
:-)
Post Reply