Answer to Question #8310 in Visual Basic for mona

Question #8310
Write VB6 program that takes n numbers as input.
It outputs the frequency of positive and negative numbers.
A sample input and output are given as follows:
Input n=9 the 9 numbers are as follows: 40 -1 59 -37 283 -91 -103 341 523
Output: Frequency of positive numbers:5
Frequency of negative numbers:4
1
Expert's answer
2012-04-12T09:52:16-0400
Should it take numbers one by one (i.e. 1 => buttonclick => -4 => buttonclick)?

Or should it parse string of numbers (i.e 1,-4,77,5 => buttonclick)?

VERSION 5.00
Begin VB.Form Form1
Caption = "Form1"
ClientHeight = 2970
ClientLeft = 60
ClientTop = 450
ClientWidth = 6630
LinkTopic = "Form1"
ScaleHeight = 2970
ScaleWidth = 6630
StartUpPosition = 3 'Windows Default
Begin VB.CommandButton Button_calculate
Caption = "Calculate"
Height = 975
Left = 120
TabIndex = 1
Top = 1800
Width = 6375
End
Begin VB.TextBox num_input
Height = 615
Left = 120
TabIndex = 0
Top = 840
Width = 6375
End
Begin VB.Label Label1
Caption = "Please enter your numbers. Use space as delimeter."
BeginProperty Font
Name = "MS Sans Serif"
Size = 12
Charset = 204
Weight = 700
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
Height = 735
Left = 120
TabIndex = 2
Top = 120
Width = 6375
End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Dim counter_positive As Integer 'Gathers frequency of positive numbers
Dim counter_negative As Integer 'Gathers frequency of negative numbers
Dim operation_string As String 'Holds input string. Will be used for string operations.
Dim string_part As String 'Holds part of the string cut from previous variable
Dim cut_at As Integer 'Holds the position of the next space in the the string. It is the place where operation_string would be cut.

Private Sub Button_calculate_Click() 'Here we go on button click

'resetting frequency counters
counter_negative = 0
counter_positive = 0

'checking if there is any text in textbox, and ending calcualtion if there is no text.
If Len(Me.num_input.Text) = 0 Then
MsgBox ("Please input correct values")
Exit Sub
End If

'main part of the program
operation_string = Me.num_input.Text 'Taking the text from the textbox to the variable
Do While Len(operation_string) > 0 'Starting the loop. It ends when lenghts of variable reaches zero.
cut_at = InStr(operation_string, " ") 'Searching the position of next space in string
If cut_at > 0 Then 'Checking for situation when there are no spaces left but we have some text left
string_part = Mid(operation_string, 1, cut_at) 'getting part of string, from the beginning to the first space
operation_string = Right(operation_string, Len(operation_string) - cut_at) 'cutting string from right to left, up to position of first space
Else 'that's that situation mentioned before. No spaces left but we have some symbols left.
string_part = operation_string 'so we jut taking the rest of the string
operation_string = "" 'setting length of string to zero, so loop would end
End If
If IsNumeric(string_part) Then 'checking if obtained part of the string is number
If string_part < 0 Then counter_negative = counter_negative + 1 'if number is negative, increasing respective frequency counter
If string_part > 0 Then counter_positive = counter_positive + 1 'same as above
End If
Loop 'repeating loop
MsgBox ("Frequency of positive numbers:" & counter_positive & Chr(13) quot;Frequency of negative numbers:" & counter_negative) 'chr(13) calls the line break

End Sub

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS