[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

MSFlexgrid countingsort

Posted on 2012-12-28
9
Medium Priority
?
381 Views
Last Modified: 2013-01-08
Dear Experts, I have a MSFlexGrid1 With 5 colums of integer data, max integer is 1 to 50.
I would like to countsort these colums, so that I end up with a list in a second MSFlexgrid2, of the counted and sorted data like the list below. In the second MSFlexgrid2 there should be only two colums as shown below. The code will need to identify the individual numbers from 1 to 50, and show how many times they are repeated in the 5 columns. I am using vb6. Your help would be appreciated, thank you.

Number    Frequency
1              80
2              65
3              45
4              90
etc           etc
50            120
0
Comment
Question by:AWaterfalls
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
9 Comments
 
LVL 15

Accepted Solution

by:
eemit earned 2000 total points
ID: 38729281
Try this:

Private Sub Command1_Click()

  On Error GoTo Err_Handler

  Dim c As Long
  Dim r As Long
  
  Dim nValue As Long
  Dim nCountColumns As Long
  Dim aNumers(1 To 50) As Long
  
  c = 0
  nCountColumns = 5
  
  With MSFlexGrid1
      For c = 0 To nCountColumns - 1
    
          ' Sort by Column
          .Col = c
          .ColSel = 0
          .Sort = flexSortGenericAscending
      
          nValue = 0
          
          For r = 1 To .Rows - 1
              If Len(.TextMatrix(r, .Col)) > 0 Then
                  If IsNumeric(.TextMatrix(r, .Col)) And ((.TextMatrix(r, .Col) > 0) And (.TextMatrix(r, .Col) <= 50)) Then
                      If r = 1 Then
                          nValue = Val(.TextMatrix(r, .Col))
                          
                          aNumers(nValue) = aNumers(nValue) + 1
                      Else
                          If CheckRow(r, .Col, nValue) = False Then
                              nValue = Val(.TextMatrix(r, .Col))
                          End If
                              
                          aNumers(nValue) = aNumers(nValue) + 1
                      End If
                  End If
              End If
          Next r
      Next c

  End With
  
  Call FillCountGrid(aNumers(), MSFlexGrid2)

  Exit Sub

Err_Handler:
  Debug.Print "ERROR (Command1_Click): " & CStr(Err.Number) & ", " & Err.Description

End Sub

Private Function CheckRow( _
                    ByVal nRow As Long, _
                    ByVal nCol As Long, _
                    ByVal nValue As Long _
                    ) As Boolean
  
  With MSFlexGrid1
      If nRow <= .Rows - 1 Then
          If IsNumeric(.TextMatrix(nRow, nCol)) And ((.TextMatrix(nRow, nCol) > 0) And (.TextMatrix(nRow, nCol) <= 50)) Then
              If Val(.TextMatrix(nRow, nCol) = nValue) Then
                  CheckRow = True
              End If
          End If
      End If
  End With
  
  Exit Function

End Function

Private Sub FillCountGrid( _
                ByRef aNumers() As Long, _
                ByRef oFlexGrid As MSFlexGrid _
                )
                
  On Error GoTo Err_Handler

  Dim Entry As Variant
  Dim i As Long
  Dim j As Long
                                 
  With oFlexGrid
      .Cols = 2
      .FixedRows = 1
      .Rows = .FixedRows
      
      .TextMatrix(0, 0) = "Number"
      .TextMatrix(0, 1) = "Frequency"
      
      For i = 1 To 50
          Entry = i & Chr(9) & aNumers(i)
          
          .AddItem Entry
      Next i
      
  End With

  Exit Sub
  
Err_Handler:
  Debug.Print "ERROR (FillCountGrid): " & CStr(Err.Number) & ", " & Err.Description

End Sub

Open in new window

0
 
LVL 15

Expert Comment

by:eemit
ID: 38729340
Version 2:

Private Sub Command1_Click()

  On Error GoTo Err_Handler

  Dim c As Long
  Dim r As Long
  
  Dim nValue As Long
  Dim nCountColumns As Long
  Dim aNumers(1 To 50) As Long
  
  nCountColumns = 5
  
  With MSFlexGrid1
      nValue = 0
      
      For r = 1 To .Rows - 1
          For c = 0 To nCountColumns - 1
              If Len(.TextMatrix(r, c)) > 0 Then
                  If IsNumeric(.TextMatrix(r, c)) Then
                      nValue = Val(.TextMatrix(r, c))
                      If (nValue > 0) And (nValue <= 50) Then
                          aNumers(nValue) = aNumers(nValue) + 1
                      End If
                  End If
              End If
          Next c
      Next r
  End With
  
  Call FillCountGrid(aNumers(), MSFlexGrid2)
  
  Exit Sub

Err_Handler:
  Debug.Print "ERROR (Command1_Click): " & CStr(Err.Number) & ", " & Err.Description

End Sub

Open in new window

0
 

Author Comment

by:AWaterfalls
ID: 38731535
Version 2, Worked very well. Thank you, you get maximum points for that answer.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:AWaterfalls
ID: 38731585
I've requested that this question be closed as follows:

Accepted answer: 0 points for AWaterfalls's comment #a38731535

for the following reason:

It Just Worked First Tme.
0
 
LVL 15

Expert Comment

by:eemit
ID: 38731586
Hi AWaterfalls,
Thank you,
you should accept my answer as solution.
Please read here how to close the question:
http://www.experts-exchange.com/help/viewHelpPage.jsp?helpPageID=24
0
 
LVL 15

Expert Comment

by:eemit
ID: 38739735
Accept: eemit {http:#a38729340}

In her post {http:#a38731535}
Asker said:
"Version 2, Worked very well. Thank you, you get maximum points for that answer."

In her post {http:#a38731585}
Asker accidentally accept his own Answer:
(Version 2, Worked very well. Thank you, you get maximum points for that answer.)
Asker said as Grading Comment:
"It Just Worked First Tme."

eemit
0
 

Author Comment

by:AWaterfalls
ID: 38742527
I am sorry but I am New to all this, however the first solution gave only zeros for the frequency on the second flexgrid. While solution 2 gave the frequencies of the balls as per requested by me. Thank you for your help, and I look forward to using experts again in the future.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
Make the most of your online learning experience.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
Suggested Courses

649 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question