chatgpt writing a function in harbour

Forum help and suggestions to improve this forum.

Moderator: Rathinagiri

Post Reply
ROBROS
Posts: 256
Joined: Thu May 25, 2017 6:30 pm
DBs Used: DBF
Location: D 83071 Stephanskirchen

chatgpt writing a function in harbour

Post by ROBROS »

Hi all,
My grandson (he is studying IT on Technical University Munic, had a discussion about harbour and artificial intelligence. He was sure chatgpt would know harbour. I had a doubt that AI would be able to write program-code in harbour. So he required a function that would generate 6 numbers withina range fro 1 to 49 (lotto), and each number should appear only once.
It lasted 3 seconds and the code was available.

After compiling I got the error unknown function swap() and that random function was defined multiple.
Here the codeof GenerateLottoNumbers

Code: Select all

#include "hmg.ch"
FUNCTION Main()

public zahl,nI,rndnum
public lotto[6] &&array für 6 Zahlen

aSTRUCT_L  := {} &&array für db_structure lotto.dbf
AADD( aSTRUCT_L , { 'spiel '    , 'N' , 2, 0 } )  
AADD( aSTRUCT_L , { 'zahl1 '    , 'N' , 2, 0 } )
AADD( aSTRUCT_L , { 'zahl2 '    , 'N' , 2, 0 } )
AADD( aSTRUCT_L , { 'zahl3 '    , 'N' , 2, 0 } )
AADD( aSTRUCT_L , { 'zahl4 '    , 'N' , 2, 0 } )
AADD( aSTRUCT_L , { 'zahl5 '    , 'N' , 2, 0 } )
AADD( aSTRUCT_L , { 'zahl6 '    , 'N' , 2, 0 } )

DBCREATE( "lotto.dbf" , aSTRUCT_L, "dbfntx" ) &&index file ntx

aSTRUCT_V  := {}
AADD( aSTRUCT_V , { 'zahl '    , 'N' , 2, 0 } )
AADD( aSTRUCT_V , { 'J_N '     , 'L' , 1, 0 } ) &&nur Wahrheitswet .T. oder .F. möglich

DBCREATE( "valnum.dbf" , aStruct_V, "dbfntx" )

use valnum via "dbfntx"
for nI=1 to 49 &&Zalen 1-49
   dbappend()
   replace zahl with nI,J_N with .T. && Wahrheitswert .T. für alle Datensätze
next
index on zahl to zahl &&Indexdatei zahl.ntx
**********************************************************
      DEFINE WINDOW crtnum_1 ;
      WINDOWTYPE MAIN        ;
      ROW 10 ;
      COL 10  ;       
      WIDTH 800 ;         
      HEIGHT 700 ;
      FONTNAME 'ARIAL' FONTSIZE 14 ;
      On init GenerateLottoNumbers() 
      end window
*************************************************************
crtnum_1.activate
crtnum_1.center

FUNCTION generateLottoNumbers

   LOCAL aNumbers := {}
   LOCAL nCount

   // Füllt das Array mit Zahlen von 1 bis 49
   FOR nCount := 1 TO 49
      aNumbers[nCount] := nCount
   NEXT

   // Mischen Sie das Array zufällig
   FOR nCount := 1 TO 49
      nRandom := INT(RAND() * 49) + 1
      SWAP(aNumbers[nCount], aNumbers[nRandom])
   NEXT

   // Speichern Sie die ersten sechs Zahlen in der Datenbank
   USE lotto.dbf
   APPEND BLANK
   REPLACE number1 WITH aNumbers[1]
   REPLACE number2 WITH aNumbers[2]
   REPLACE number3 WITH aNumbers[3]
   REPLACE number4 WITH aNumbers[4]
   REPLACE number5 WITH aNumbers[5]
   REPLACE number6 WITH aNumbers[6]
   PACK
   CLOSE DATABASES
   return
I had written a function myself that works fine, but I think the Function swap() by AI needs a self written function to do the same.

My function uses a complete different method to do the job. Maybe its of interest, so here my function:

Code: Select all

#include "hmg.ch"

FUNCTION Main()

public zahl,nI,rndnum
public lotto[6] 

aSTRUCT_L  := {} 
AADD( aSTRUCT_L , { 'spiel '    , 'N' , 2, 0 } )  
AADD( aSTRUCT_L , { 'zahl1 '    , 'N' , 2, 0 } )
AADD( aSTRUCT_L , { 'zahl2 '    , 'N' , 2, 0 } )
AADD( aSTRUCT_L , { 'zahl3 '    , 'N' , 2, 0 } )
AADD( aSTRUCT_L , { 'zahl4 '    , 'N' , 2, 0 } )
AADD( aSTRUCT_L , { 'zahl5 '    , 'N' , 2, 0 } )
AADD( aSTRUCT_L , { 'zahl6 '    , 'N' , 2, 0 } )

DBCREATE( "lotto.dbf" , aSTRUCT_L, "dbfntx" ) &&index file ntx

aSTRUCT_V  := {}
AADD( aSTRUCT_V , { 'zahl '    , 'N' , 2, 0 } )
AADD( aSTRUCT_V , { 'J_N '     , 'L' , 1, 0 } )

DBCREATE( "valnum.dbf" , aStruct_V, "dbfntx" )

use valnum via "dbfntx"
for nI=1 to 49 &&Zalen 1-49
   dbappend()
   replace zahl with nI,J_N with .T. &&all records get flag .T.
next
index on zahl to zahl &&Indexfilei zahl.ntx
**********************************************************
      DEFINE WINDOW crtnum_1 ;
      WINDOWTYPE MAIN        ;
      ROW 10 ;
      COL 10  ;       
      WIDTH 800 ;         
      HEIGHT 700 ;
      FONTNAME 'ARIAL' FONTSIZE 14 ;
      On init crtnum() 
      end window
*************************************************************
crtnum_1.activate
crtnum_1.center

function crtnum 
select valnum &&order 

  for nI=1 to 6
    rndnum=HB_RandomInt(1,49) &&Zahlen von 1-49 möglich
    seek rndnum
    IF valnum->J_N=.T.
      lotto[nI]=rndnum
      replace J_N with .F. && now the number created is no longer available 
    ELSE 
       nI--    
   Endif
  next
    replace all J_N with .T. && for the next pass are true again
    asort(lotto)  &&sorting array lotto ascending.
** the 6 fields of the table lotto are Z AHL1,ZAHL2,ZAHL3,ZAHL4,ZAHL5,ZAHL6 
use lotto 
dbappend()  &&Datensatz anfügen
for ni=1 to 6
   cfeld='ZAHL'+ltrim(str(ni))
   replace &cfeld with lotto[ni] && Makro-Operator selects fieldnames
next
crtnum_1.release &&Programmende
return
.
I really was impressed but the drawback? Will in the future humans write programs any longer.

Hope I didn't bother you.

Regards
Robert
User avatar
AUGE_OHR
Posts: 2065
Joined: Sun Aug 25, 2019 3:12 pm
DBs Used: DBF, PostgreSQL, MySQL, SQLite
Location: Hamburg, Germany

Re: chatgpt writing a function in harbour

Post by AUGE_OHR »

hi,
can you show me CODE which take 6 Number from 1 to 49 but each Number must be Unique in harbour Programm Language

In the Harbour programming language, you can use the HB_Random() function to generate 6 unique numbers between 1 and 49. Here is an example of how to do this:

Code: Select all

// Create an array to store the numbers
array := {}

// Loop 6 times
for i := 1 to 6
   // Generate a random number
   number := HB_Random(1,49)
   
   // Check if the number is already in the array
   if !HB_ArrayIn( array, number )
      // Add the number to the array
      HB_ArrayAdd( array, number )
   end
next

// Print the array
? array
but that CODE does not "run" ... there is no Function HB_ArrayIn() or HB_ArrayAdd( and HB_Random() return "Decimal" too

but the Idea is right so here modify "working" Version for HMG

Code: Select all

#include "HMG.ch"
   REQUEST HB_GT_WIN_DEFAULT           // Console

PROCEDURE MAIN
// Create an array to store the numbers
LOCAL i, number, array := {}
LOCAL lExit := .F.

// Loop until get 6 Element
DO WHILE !lExit = .T.
   // Generate a INTEGER random number
   number := HB_RandomINT(1,49)  // only Integer
   
   // Check if the number is already in the array
*   if !HB_ArrayIn( array, number )
   IF ASCAN(array, { |x| x = number } ) = 0
      // Add the number to the array
*      HB_ArrayAdd( array, number )
       AADD( array, number )
       IF LEN(array) = 6
          lExit := .T.
       ENDIF  
   end
ENDDO

// Print the array
MsgInfo( array )
RETURN
---

Question : have you try ChatGPT yourself :?:

as i can say Answer from ChatGPT is "limited" to "1 Page" (about 1000 Sign) using "free" Account ( don´t know ABO Condition )
so it can be only a "short" Story or App like a Demo
have fun
Jimmy
ROBROS
Posts: 256
Joined: Thu May 25, 2017 6:30 pm
DBs Used: DBF
Location: D 83071 Stephanskirchen

Re: chatgpt writing a function in harbour

Post by ROBROS »

Jimmy, my function shown in my post works fine.
Chatgpt function gives errors.
I use "rndnum=HB_RandomInt(1,49)" &&Zahlen von 1-49 möglich
A number generated gets a .F. flag and can't be generated again.
Robert
User avatar
AUGE_OHR
Posts: 2065
Joined: Sun Aug 25, 2019 3:12 pm
DBs Used: DBF, PostgreSQL, MySQL, SQLite
Location: Hamburg, Germany

Re: chatgpt writing a function in harbour

Post by AUGE_OHR »

hi,

yes, your Function is ok.

i "just" want to show what ChatGPT is doing and how to use Answer as new Idea

ChatGPT does not know if a Function like HB_ArrayIn() or HB_ArrayAdd() exist
ChatGPT is not a Compiler and does not know Syntax

but ChatGPT can produce new Idea so i have modify Answer from ChatGPT

---

Fazit :
ChatGPT most fail when have "strict Rules" but can be very Creative for Image, Music or a short Story
have fun
Jimmy
User avatar
serge_girard
Posts: 3173
Joined: Sun Nov 25, 2012 2:44 pm
DBs Used: 1 MySQL - MariaDB
2 DBF
Location: Belgium
Contact:

Re: chatgpt writing a function in harbour

Post by serge_girard »

Interesting !
There's nothing you can do that can't be done...
franco
Posts: 822
Joined: Sat Nov 02, 2013 5:42 am
DBs Used: DBF
Location: Canada

Re: chatgpt writing a function in harbour

Post by franco »

Hi guys,
Just another thought,

Code: Select all

#include "hmg.ch"
FUNCTION Main()
private nI 
private aSTRUCT_L  := {}  
AADD( aSTRUCT_L , { 'z1 '    , 'N' , 2, 0 } )
DBCREATE( "lotto.dbf" , aSTRUCT_L,)
use
use  lotto via "dbfntx"
append blank
	rndnum:= HB_RandomInt(1,49)
	replace z1 with rndnum
	for nI=1 to 5
		rndnum:= HB_RandomInt(1,49)   // &&Zahlen von 1-49 möglich
		locate for z1 = rndnum 
		If ! eof()
			nI--
		Else
			append blank
	 		replace z1 with rndnum 
		Endif
 	 next
index on z1 to lotto
go top
br()
Close All
return



Function Br
	DEFINE WINDOW Form_1 ;
		AT 0,0 ;
		WIDTH 300 ;
		HEIGHT 250 ;
		TITLE 'Lotto 649 Numbers'

		@ 10,10 GRID Grid_1 ;
			WIDTH 190 ;
			HEIGHT 190;
			HEADERS {'Numbers'} ;
			WIDTHS {140}; 
			VALUE { 1 , 1 } ;
			COLUMNCONTROLS { 'TEXTBOX'	, 'NUMERIC' , '99' } ;  //{ aCtrl_1 } ;
			ROWSOURCE "LOTTO" ;
			COLUMNFIELDS { 'Z1' } 

	END WINDOW

	CENTER WINDOW Form_1

	ACTIVATE WINDOW Form_1

Return
Franco
All The Best,
Franco
Canada
ROBROS
Posts: 256
Joined: Thu May 25, 2017 6:30 pm
DBs Used: DBF
Location: D 83071 Stephanskirchen

Re: chatgpt writing a function in harbour

Post by ROBROS »

Hi all, thank you for your replies. Unfortunately I don't find the thank you button.
Franco, your code works fine, but I prefer the numbers in a row (shown in a grid). When the 6 numbers (up to 10 sets possible) are known, I would type these in and check if I find matches. If a match occurs that nummer in the grid should
change fontcolor to green.
But showing the numbers in a column like you did, is a good sample for some tasks.
Robert
Post Reply