Post subject: VB Help! ;-;
Player (34)
Joined: 12/18/2005
Posts: 250
Hi everyone, I'm really having some trouble with vb.net homework I want to have the program display how many letters in a string it has. (i.e. How are you has 1 h, 2 o, 1 y, etc) I also want to have the most frequently occurring letter to be 150 pixels high and the others proportional to their occurrence count relative to the maximum. It should also display in a Label the most frequent letter(s), and the least frequent letter(s) that appear in the message (a letter that doesn't appear should not be listed among the least frequent letters). Here's an example of a typical analysis. I've got some work done as it is though. Public Class MessageAnalyzer Inherits System.Windows.Forms.Form 'Frequencies of the letters of the alphabet in the textbox Private mintFrequency(25) As Integer Private Sub pnlDisplay_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles pnlDisplay.Paint 'Display the alphabet at the bottom of the panel Dim objRnd As Random = New Random Dim intX As Integer = 5 Dim intY As Integer = 210 Dim intWidth As Integer = 20 Dim intGap As Integer = 5 For intCount As Integer = Asc("A") To Asc("Z") Dim intHeight As Integer = objRnd.Next(10, 200) e.Graphics.FillRectangle(Brushes.Red, intX, intY - intHeight, intWidth, intHeight) e.Graphics.DrawString(Chr(intCount), Me.Font, Brushes.Red, intX + intGap, 220) intX += intWidth + intGap Next End Sub Private Sub txtInput_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtInput.KeyPress If e.KeyChar = vbCr Then 'Return key 'Find the frequencies 'Iterate through the text in the textbox Dim strInput As String = txtInput.Text.ToUpper For intIndex As Integer = 0 To strInput.Length - 1 Dim strLetter As String = strInput.Chars(intIndex) Console.WriteLine("character at {0} = {1}", intIndex, strLetter) Next 'draw the bar graph pnlDisplay.Invalidate() 'triggers a repaint End If End Sub End Class Sorry for the long post, but I'm really lost now. Any help would be nice. :)
我々を待ち受けなさい。
Sir_VG
He/Him
Player (39)
Joined: 10/9/2004
Posts: 1911
Location: Floating Tower
It's been a while since I programmed in VB, but probably the way to do it is roughly this (I can't give an exact program, but I'll try to explain): First, determine the string length. You'll use this to loop check the string. In the loop, use the Left$ to look at the (loop#) character. Then determine what letter it is and store that in a variable. LOOP Then draw your graph based upon each variable. So it's kinda like... *DEFINE VARIABLES Determine String length, store as integer. For I = 1 to INTEGER Determine what Character is for Left$(I,1) Next Draw Graph. Hope that gets what I'm trying to say across.
Taking over the world, one game at a time. Currently TASing: Nothing
JXQ
Experienced player (750)
Joined: 5/6/2005
Posts: 3132
I'm not all for setting crap up in classes, or drawing graphs, but here would be a sample portion of the meat of the program.
'Whenever enter is pressed, run this routine
'assuming that you get your message from txtMessage.text

dim Count as integer(65 to 90)
dim Max as integer
dim MaxPointer as integer
dim Min as integer
dim MinPointer as integer
dim MaxMsg as string
dim MinMsg as string
dim Message as string
dim CurChar as string
dim Index as integer

Message = txtMessage.text

For Index = 1 to len(Message)

  CurChar = ucase$(mid$(Message,Index,1))  ' grabs current character
  if curChar >= "A" and CurChar <= "Z" then  'checks for valid letter
    Count(asc(CurChar)) = Count(asc(CurChar)) + 1  'if valid, add to count
  end if

next Index

Max = Count(65)  'Set max and min to A's count
Min = Count(65)
MaxPointer = 65
MinPointer = 65

for Index = 66 to 90
  if Count(Index) > Max then
    Max = Count(Index)
    MaxPointer = Index
  end if
  if Count(Index) < Min then
    Min = Count(Index)
    MinPointer = Index
  end if
next Index

'Now find other letters that are tied for highest or lowest

for Index = 65 to 90
  if Count(Index) = Max then
    MaxMsg = MaxMsg & chr$(Index) & ", "
  end if
  if Count(Index) = Min then
    MinMsg = MinMsg & chr$(Index) & ", "
  end if
next Index

'Get rid of any extra commas

MaxMsg = left$(MaxMsg,len(MaxMsg)-2)
MinMsg = left$(MinMsg,len(MinMsg)-2)

'Add appropriate label

if len(MaxMsg) > 1 then
  MaxMsg = "Most Frequent letters: " & MaxMsg
else
  MaxMsg = "Most Frequent letter: " & MaxMsg
end if

if len(MinMsg) > 1 then
  MinMsg = "Least Frequent letters: " & MaxMsg
else
  MinMsg = "Least Frequent letter: " & MaxMsg
end if
Subject to errors as I didn't actually run this, but following it should give you an idea of the algorithm.
<Swordless> Go hug a tree, you vegetarian (I bet you really are one)