Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Who here is VERY good at troubleshooting QB programs?
#51
Quote:Well, I tried it but all I get is a "subscript out of range error" I also noticed that the number that it shows for the probability is always the same.....10. One of the for loops must be messing the variables up.
Now that we're doing X2=ENTRY(X1,X)
we're processing more variables from the ENTRY array.
However, when X1 is 2 or more, we're most probably gonna pickup zeros because you only entered one set of 10 numbers.

So, when X2 is zero, then the instruction
X(X2) = X(X2) + 1
will cause the error because X has no zero location demensioned.

You'll need to put in a zero test.
*****
Reply
#52
I don't have a solution to your problem, just a few questions/comments:

1. What exactly is the algorithm you want to use when calculating the probability, ie. in plain english?

2. Do try and give meaningful names to your variables. 'X' and the like don't provide any information on the type or contents, and only work to confuse those reading the code.

3. I've reworked the code for readability. I'm still unsure about certain parts:
Code:
defint a-z

const MAXSETS = 10
const MAXENTRIES = 10

dim entries( MAXSETS, MAXENTRIES )        '' entry array
dim totalsets as integer                  '' # of sets entered by user

dim occurances( 1 to 52 ) as integer      '' # of repetitions of numbers 1..52

dim NTH(1 to 10)                          '' ???
dim nx(1 to 52)                           '' ???

'' - main program ---------------------------------------------------------- ''
   '' [ ...display welcome screen... ]
  
   '' main loop
   dim place as integer : place = 0
   do
      if place = 0 then gosub MainMenu
      if place = 1 then gosub NUMBERS
      if place = 2 then gosub CalculateProbability
      if place = 3 then gosub Save
      if place = 4 then gosub Load
      if place = 5 then exit do           '' exit the loop ...
   loop

   '' [ ...display ending text/perform any cleanup necessary... ]

   sleep : end

'' - main menu ------------------------------------------------------------- ''
MainMenu:
   '' [ ...display main menu screen... ]

   do
      input place : place = int( place )
   loop until( place > 0 ) and ( place < 6 )
return

'' - get entries from user ------------------------------------------------- ''
NUMBERS:
   '' [ ...display number entry instructions... ]

   dim setindex as integer : setindex = 0
   while( setindex < MAXSETS )
      
      '' [ ...get entries as before... ]
      '' entries( setindex, ... ) = ...

      setindex = setindex + 1
      dim done as string
      do
         input "Enter another set?( y/n )"; done : done = lcase$( done )
         if( done = "n") then
            totalsets = setcount
            place = 0 : return
         end if
      loop until done = "y"
   wend
   place = 0
return

'' - calculate probability ------------------------------------------------- ''
CalculateProbability:
   print "GETTING THE PROBABILITY OF THE NUMBERS(MOST COMMON)."
  
   dim currentset as integer
   dim currententry as integer
   for currentset = 0 to totalsets - 1    '' <- iterate through total sets ( not 10 )
      for currententry = 0 to MAXENTRIES - 1
         dim entry as integer : entry = entries( currentset, currententry )
         if( entry = 0 ) then exit for    '' <- assuming zero ends the entry list
         occurances( entry ) = occurances( entry ) + 1
      next
   next
  
   dim i as integer
   for i=1 to 10 : nth(i) = 0 : next      '' clear these arrays, whatever
   for i=1 to 52 : nx(i) = 0 : next       '' "nth" and "nx" is supposed to mean
  
   dim picked as integer : picked = 0
   for Z = 1 to 10

      for i=1 to 24
         if( picked < occurances( i ) ) and ( nx( i ) = 0 ) then
            picked = i
            nx( i ) = 1
         end if
      next

      for i=25 to 52     '' <- Why are we treating 25-52 differently ?
         if( picked < occurances( i ) ) then
            flag = 0
            for j = 1 to 10
               if( nth( j ) = occurances( i ) ) then flag = 1 : exit for
            next
            if( flag = 0 ) then picked = i
         end if
      next

      nth( z ) = picked

      print Z
      print picked
      sleep 10
  
   next Z
   '' [ ...display the information... ]

   place = 0
return

'' - saving/loading -------------------------------------------------------- ''
dim FileName as string
dim FileHandle as integer     '' let QB decide what file handle to use
dim n as integer              '' iterator for occurances array

Save:
   input "Enter file to save, w/o extension: "; FileName
   FileName = FileName + ".txt"
   FileHandle = freefile
   open FileName for output as #FileHandle

   for n = 1 to 52
      print #FileHandle, occurances( n )
   next

   close #FileHandle
   place = 0
return

Load:
   input "Enter file to load, w/o extension: "; FileName
   FileName = FileName + ".txt"
   FileHandle = freefile
   open FileName for input as #FileHandle

   for n = 1 to 52
      if( eof( FileHandle ) ) then
         '' [ ...Handle File Format Error... ]
         exit for
      end if
      input #FileHandle, occurances( n )
   next

   close #FileHandle
   place = 0
return
stylin:
Reply
#53
I can follow all the coding to a point but anyway....

To find the probability it goes through the numbers finding which numbers occur the most and remembers them. The idea with the zeros was that the program wouldn't even acknowledge them so the only numbers the program is interested in is the non-zero numbers.

For example for probability:
Code:
number=0
for X=1 to 10
if usernumber(1,X)>number then number=usernumber(1,X)
next X

Something like that.
Not yet Snake! It's not over yet!
Reply
#54
Quote:To find the probability it goes through the numbers finding which numbers occur the most and remembers them.
I thought so. It seems then that you need a sorting algorithm, not a mathematical one:
Code:
'' [ ...fill entries array with user numbers... ]

   dim setIndex as integer : dim entryIndex as integer
   '' for all user sets ...
   for setIndex = 0 to totalsets - 1
      '' for all entries ...
      for entryIndex = 0 to MAXENTRIES - 1
         dim number as integer : number = entries( setIndex, entryIndex )
         if( number = 0 ) then               '' move to next set when 1st zero is
            exit for                         '' encountered; 0 denotes end of set
         end if
         '' increment the frequency of that number
         frequency( number ) = frequency( number ) + 1
      next
   next
  
   '' { now we have an array, frequency, that tells us }
   '' { the frequency of the numbers 1..52 }

   '' we can sort this array by frequency, using any number of algorithms. The
   '' simplest would probably be the bubble-sort :

   '' - BubbleSort ------------------------------------------------------------ ''
      dim Limit as integer : Limit = ubound( frequency )
      dim LastSwap as integer                '' the last necessary swap
      Do
         LastSwap = 0                        '' reset our swap position
         For number = 1 To ( Limit - 1 )     '' sort from beginning to our limit

            '' Two adjacent numbers are out of order, so swap their values
            If( frequency(number).Length < frequency(number + 1).Length ) Then
               Swap frequency(number), frequency(number + 1)
               LastSwap = number             '' remember position of last swap
            End If
         Next number
                                             '' set limit to last necessary swap;
         Limit = LastSwap                    '' the numbers after that swap
                                             '' are already in order

      Loop While LastSwap                    '' make passes until no swaps are done
   '' - /BubbleSort ----------------------------------------------------------- ''
  
   '' { our frequency array is now sorted from highest to lowest }

   '' Now we can display the top X recurring numbers:
   print "Ten most recurring numbers entered:"
   dim luckyNumber as integer
   for luckyNumber = 1 to 10
      print frequency( luckyNumber )
   next luckyNumber
stylin:
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)