# Thread: [RESOLVED] Logic problem

1. ## [RESOLVED] Logic problem

Hello

I am working on a small project in VB6.
The program should populate a ListView table.
It is about assigning door-phone numbers for a residential building.
The building can have from 1 to 10 floors, and each floor can have from 0 to 10 apartments.
Door-phone system has one calling unit and handset in each apartment. There are also decoders connected between the calling unit and the handset. From 1 to 4 handset units can be connected to each decoder.

In the first picture there is an example of 4 floors and 4 decoders. On the first floor there are 3 apartments marked with numbers 0001, 0002, 0003. Address 003 from decoder number 0 is unused. Apartment numbers continue with decoder number 1, which is located on floor number 2 and has 2 apartments marked with numbers 0004 and 0005. Addresses 006 and 007 are unused. Can you see a pattern?

The second picture shows an example with 5 apartments on the first floor and 2 decoders. On the second floor there is 1 decoder for 2 apartments and on the third floor there are 3 decoders for 9 apartments.

I need the apartment numbers to be in order starting from 0001, with unused addresses from each decoder to be 0000.

Any idea how this could be written in VB6?

ps I use Google Translate

1.zip

2. ## Re: Logic problem

I'm going with the premise, that the apartments are "there", just not rented out.
Meaning:
Decoder 0 - Address 003 should be Apartment 0004 with a Flag "unused", and so on.

IMO, it makes no sense, to "jump" over unused apartments

3. ## Re: Logic problem

Decoder 0 - Address 003 is unused because Apartment 0004 is on the 2nd floor and Decoder 0 is on the 1st floor.

1st floor
apartment 0001
apartment 0002
apartment 0003

2nd floor
apartment 0004
apartment 0005

etc.

4. ## Re: Logic problem

OK, got you.
So the "Master" is not the Decoder, but the Apartment, since an Apartment should know on which floor it resides on.
The count of apartments per floor divided by 4 round up to next integer gives you the number of decoders per floor.
Then it's a simple "run down" assigning the addresses. Any "left over" of apartments is set to 0000

5. ## Re: Logic problem

I would not call it "Master" , but each decoder has a unique ID number shown in the images in the first column "Decoder No."

6. ## Re: Logic problem

Personally I'd try to organize it so that the address and the apt number match... leaving address 0000 unused (since there is no apartment 0000)....
That would then make things match up a bit better...

But then again, I'm not 100% sure I'm following correctly.

-tg

7. ## Re: Logic problem

Edit: Ok, just read OP again and didn't catch the part where different floors have different numbers of apartments.

Something like this - or could be done recursively if you want to be clever about it.

Code:
```Dim nFloor As Long
CONST FLOOR_COUNT As Long = 1000 ' Moderately tall building.
Dim nApartmentCount(FLOOR_COUNT) As long
Dim nFloor As long
dim nApartment As Long

' Fill in nApartmentCount  from a text file or whatever.  Do that here or call another sub.

For  nFloor = 1 To FLOOR_COUNT

for nApartment = 1 To nApartmentCount(nFloor)

GenerateBuzzerNumber(nFloor, nApartment)

Next nApartment

Next nFloor```

8. ## Re: Logic problem

This will need a reference to the "Microsoft Hierarchiel FlexGrid Control 6.0" component. Place one on a form named "grid"
The following should get you pretty close to what you want. Sorry its a mess and lacking comments.

Basically set apartment count and decoder count for each floor in SetupFloors() and the FLOOR_CT const at the top.

Code:
```Option Explicit

'Set floor count here
Private Const FLOOR_CT& = 3

Private Type apt_floor
apartment_ct As Long
decoder_ct As Long
End Type

Private apt_floors() As apt_floor
Private row_ct&

Call SetupFloors    'Setup info for floors
Call SetupGrid      'Setup the grid. Row count, col headers
Call DisplayGrid    'Fill the grid
End Sub

Private Sub SetupFloors()
'Setup your decoder and apartment counts here for each floor

ReDim apt_floors(FLOOR_CT) As apt_floor

apt_floors(1).apartment_ct = 5
apt_floors(1).decoder_ct = 2

apt_floors(2).apartment_ct = 2
apt_floors(2).decoder_ct = 1

apt_floors(3).apartment_ct = 9
apt_floors(3).decoder_ct = 3

End Sub

Private Sub SetupGrid()
Dim i&

For i& = 1 To FLOOR_CT
row_ct& = row_ct& + (apt_floors(i&).decoder_ct * 4)
Next i&
grid.Rows = row_ct& + 1

grid.Cols = 3

grid.TextMatrix(0, 0) = "Decoder No."
grid.TextMatrix(0, 1) = "Address"
grid.TextMatrix(0, 2) = "Apartment No."

grid.ColWidth(0) = 1500
grid.ColWidth(1) = 2000
grid.ColWidth(2) = 1500

grid.Top = 0
grid.Left = 0
grid.Height = Me.Height - 900
grid.Width = Me.Width - 500

grid.Font.Size = 10

End Sub

Private Sub DisplayGrid()
Dim row_idx&
Dim apt_no_idx&
Dim decoder_no_idx&
Dim i&, j&, k&
Dim floor_start&

decoder_no_idx& = 0
apt_no_idx& = 1

For i& = 1 To FLOOR_CT&
floor_start& = address_idx& + 1
For j& = 1 To (apt_floors(i&).decoder_ct * 4)

grid.TextMatrix(address_idx& + 1, 0) = decoder_no_idx&

If j& <= apt_floors(i&).apartment_ct Then
grid.TextMatrix(address_idx& + 1, 2) = apt_no_idx&
apt_no_idx& = apt_no_idx& + 1
Else
grid.TextMatrix(address_idx& + 1, 2) = 0
End If

For k& = 0 To 2
grid.Row = address_idx& + 1
grid.Col = k&
If i& Mod 2 Then
grid.CellBackColor = &H8000000A
Else
grid.CellBackColor = vbWhite
End If
Next k&

If j& Mod 4 = 0 Then decoder_no_idx& = decoder_no_idx& + 1

Next j&

Next i&

End Sub

Private Sub Form_Resize()
grid.Height = Me.Height - 900
grid.Width = Me.Width - 500
End Sub```

9. ## Re: Logic problem

DrBobby thank you very much, it is working as expected.

10. ## Re: [RESOLVED] Logic problem

Any idea how this could be written in VB6?
Before coding in any language, I'd suggest you first design the program. How would you do this using pen & paper? What is the algorithm for doing this? Then produce instructions in English (pseudo code) that would enable someone else to complete the task just using the instructions (without knowing what is the task). Once you have these correct then code the program from the instructions. IMO what you shouldn't do is to jump straight in and start coding. Design first, code second.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•

Click Here to Expand Forum to Full Width