|
Post by carlos45 on Apr 17, 2024 0:09:58 GMT
Attachments:![](//storage.proboards.com/7292714/thumbnailer/0bEThDVoKBswFzWGBxXa.png)
|
|
|
Post by carlos45 on Apr 17, 2024 0:14:29 GMT
Here without function _Title "Qbasic64" Screen _NewImage(700, 740, 256) _Delay .25 _ScreenMove 20, 30
Dim terno(60) For i = 1 To 60 terno(i) = 0 Next i dezenas$ = "071940074042153542154042151942154042071935193542193542154042151935071542073540153542071535071940071940071519071542071940071540151935151935354042193540193540" For i = 1 To Len(dezenas$) Step 2 dezena$ = Mid$(dezenas$, i, 2) dezena = Val(dezena$) terno(dezena) = terno(dezena) + 1 Next i For i = 1 To 50 If terno(i) > 0 Then Print "Dez "; TS2$(i); " h "; TS2$(terno(i)); " repetidas" End If Next i Function TS2$ (nn As Long) TS2$ = Right$("00" + LTrim$(Str$(nn)), 2) End Function
Attachments:![](//storage.proboards.com/7292714/thumbnailer/DnU0LUtccjMDqhziyDMS.png)
|
|
Aaditya Parashar
Full Member
![*](//storage.proboards.com/forum/images/stars/star.png) ![*](//storage.proboards.com/forum/images/stars/star.png)
Just somebody with an abnormal coding routine.
Posts: 124
|
Post by Aaditya Parashar on Apr 17, 2024 4:26:57 GMT
Error on line 9: You need to specify the second argument for the function:
Print i; ContarRepeticoes%(i, dezenas$) And the function should be:
Function ContarRepeticoes%(dez, dezenas$) For I = 1 To Len(dezenas$) Step 2 If Val(Mid$(dezenas$, I, 2)) = dez Then Contar = Contar + 1 Next I ContarRepeticoes = Contar End Function You don't need an array to store the counts.
|
|
|
Post by bplus on Apr 17, 2024 7:56:56 GMT
AP is correct, you don't need array
_Title "carlos code fix" Screen _NewImage(700, 600, 32) _ScreenMove 20, 30
d$ = "071940074042153542154042151942154042071935193542193542154042151935071542073540153542071535071940071940071519071542071940071540151935151935354042193540193540" For i% = 1 To 50 count = ContarRepeticoes%(i%, d$) If count <> 0 Then Print i%, count Next i% Function ContarRepeticoes% (dez, dezenas$) For I% = 1 To Len(dezenas$) Step 2 If Val(Mid$(dezenas$, I%, 2)) = dez Then Contar% = Contar% + 1 Next ContarRepeticoes% = Contar% End Function
also it might good idea NOT to call the function with the same variable names you used to define the function (you only need that in GOSUBs) that is the beauty of functions and subs, once they are working you can use them in any program without worry of matching variable names in main code.
also we are using QB64 and would be good idea not to call anything here Qbasic with or without the 64, QBasic was DOS stuff.
|
|
|
Post by carlos45 on Apr 17, 2024 22:04:17 GMT
Error on line 9: You need to specify the second argument for the function: Print i; ContarRepeticoes%(i, dezenas$) And the function should be: Function ContarRepeticoes%(dez, dezenas$) For I = 1 To Len(dezenas$) Step 2 If Val(Mid$(dezenas$, I, 2)) = dez Then Contar = Contar + 1 Next I ContarRepeticoes = Contar End Function You don't need an array to store the counts. Attachments:![](//storage.proboards.com/7292714/thumbnailer/jvqw0OnQGpkSwgrJfHWE.png)
|
|
|
Post by carlos45 on Apr 17, 2024 22:09:52 GMT
Once again, thank you immensely, Bplus. Your example was perfect, just the way I wanted. I will be careful not to use the title 'QBasic' anymore. Thanks for the contribution!" Carlos Attachments:![](//storage.proboards.com/7292714/thumbnailer/nEDfYEJkcVLzEzTwKAlL.png)
|
|
|
Post by bplus on Apr 17, 2024 22:52:25 GMT
hi Carlos, as usual my pleasure to be of help when i can. AP thanks also you gave me a head start this morning ![:)](//storage.proboards.com/forum/images/smiley/smiley.png)
|
|
|
Post by carlos45 on Apr 17, 2024 22:56:57 GMT
|
|
|
Post by bplus on Apr 17, 2024 23:59:47 GMT
Declaring variables before using them is good programming practice especially in huge programs where typo can waste tons of time ie use Option _Explicit and force yourself to declare variables first. Besides saving you time from typos it helps keep you mindful of Types.
but no you are not required to declare variables before using them in QB64, either in Main module nor in subroutines.
|
|
|
Post by carlos45 on May 29, 2024 0:22:02 GMT
Good evening, Bplus.
This function example is perfect. I have a lot of difficulty creating functions. I'm trying to create the same function, but using an array. In this example it uses string, I'm trying with array
For i% = 1 To 50 count = ContarRepeticoes%(i%, d$) If count <> 0 Then Print i%, count Next i% Function ContarRepeticoes% (dez, dezenas$) For I% = 1 To Len(dezenas$) Step 2 If Val(Mid$(dezenas$, I%, 2)) = dez Then Contar% = Contar% + 1 Next ContarRepeticoes% = Contar% End Function
Try implementing this, I know it's far from working:
Function ContarRepeticoes% (nt() As String, dez As Integer) For i% = 1 To UBound(nt()) For i% = 1 To 44 Step 3 If Val(Mid$(nt(i%), j%, 2)) = dez Then Contar% = Contar% + 1 Next ContarRepeticoes% = Contar% End Function Next i%
For i = 1 To 3 For j = 1 To 44 Step 3 num = Val(Mid$(nt$(i), j, 2)) count(num) = count(num) + 1 Next j Next i
For i = 1 To 25 Locate 4 + i, 6: Color 46: Print TS2$(i); " = "; Color 30: Print TS4$(count(i))
Next i Therefore, I ask again for your help.
Carlos
|
|
|
Post by bplus on May 29, 2024 3:12:47 GMT
$Console:Only a$(1) = "02 03 05 06 09 10 11 13 14 16 18 20 23 24 25" a$(2) = "01 04 05 06 07 09 11 12 13 15 16 19 20 23 24" a$(3) = "01 04 06 07 08 09 10 11 12 14 16 17 20 23 24" Dim i As Long For i = 1 To 3 Print "a$("; i; ")" printcounts a$(i) Print Next
Sub printcounts (digit$) Dim j As Long Dim count(1 To 25) For j = 1 To 44 Step 3 num = Val(Mid$(digit$, j, 3)) count(num) = count(num) + 1 Next For j = 1 To 25 Print j; " = "; Locate , 5: Print count(j) Next End Sub
|
|
|
Post by carlos45 on May 30, 2024 0:19:51 GMT
|
|
|
Post by bplus on May 30, 2024 1:52:38 GMT
Width 80, 30 ' <<< 30 so can fit 25 rows on screen 0 Dim a$(1 To 3) a$(1) = "02 03 05 06 09 10 11 13 14 16 18 20 23 24 25" a$(2) = "01 04 05 06 07 09 11 12 13 15 16 19 20 23 24" a$(3) = "01 04 06 07 08 09 10 11 12 14 16 17 20 23 24" Locate 4 Print CountDigits$(a$())
Function CountDigits$ (digits$()) Dim As Long i, j, num, count(1 To 25) ' variables to process digits strings
Dim rtn$ ' <<< prepare to set function to return this string when done
For i = LBound(digits$) To UBound(digits$) ' count the whole array of digit strings For j = 1 To 44 Step 3 ' one digit string break down to 3 digit parts num = Val(Mid$(digits$(i), j, 3)) ' convert string to number count(num) = count(num) + 1 ' add to count of that number Next Next For i = 1 To 25 ' 25 lines of counts put into one l o n g string rtn$ = rtn$ + Right$(" " + _Trim$(Str$(i)), 2) rtn$ = rtn$ + " = " + Right$(" " + _Trim$(Str$(count(i))), 2) rtn$ = rtn$ + Chr$(10) ' next line Next CountDigits$ = rtn$ ' <<< returns 25 rows of print in one string End Function ![](//storage.proboards.com/7292714/thumbnailer/cxwnGulmfGmBYNtTglmX.png)
|
|
|
Post by carlos45 on May 30, 2024 20:06:58 GMT
Bplus, the function is perfect. I really liked it, but, unlike the post that is as a string, I can use it in any part of the code or in other codes. Is this example limited to this code only? Carlos Attachments:![](//storage.proboards.com/7292714/thumbnailer/P0IltGMBDmbNbEwirscI.png)
|
|
|
Post by carlos45 on May 30, 2024 22:08:36 GMT
Bplus, each example is a learning experience. I noticed that, in this function, the string grows, and as a result, I cannot manipulate it or use it for other purposes. I tried to fix it by deleting the line "rtn$ = rtn$ +... " but I couldn't. It would be interesting if each number was individual. See the image: the string cannot be manipulated unless I create more lines to extract each number, which makes the code very redundant. Width 80, 30 ' <<< 30 so can fit 25 rows on screen 0 Dim a$(1 To 3) a$(1) = "02 03 05 06 09 10 11 13 14 16 18 20 23 24 25" a$(2) = "01 04 05 06 07 09 11 12 13 15 16 19 20 23 24" a$(3) = "01 04 06 07 08 09 10 11 12 14 16 17 20 23 24" Locate 4
Dim result As String result = CountDigits$(a$()) For i = LBound(result) To UBound(result) Print RESULT(i) Next
Function CountDigits$ (digits$()) Dim As Long i, j, num, count(1 To 25) ' variables to process digits strings Dim lines$(1 To 25) For i = LBound(digits$) To UBound(digits$) ' count the whole array of digit strings For j = 1 To Len(digits$(i)) Step 3 ' one digit string break down to 3 digit parts num = Val(Mid$(digits$(i), j, 2)) ' convert string to number (2 characters) count(num) = count(num) + 1 ' add to count of that number Next Next For i = 1 To 25 ' 25 lines of counts put into one l o n g string lines$(i) = Right$(" " + _Trim$(Str$(i)), 2) + " = " + Right$(" " + _Trim$(Str$(count(i))), 2) Next CountDigits$ = lines$ End Function
Carlos Attachments:![](//storage.proboards.com/7292714/thumbnailer/tTIUrpBhplBs0aODUdfC.png)
|
|