10 Replies - 2778 Views - Last Post: 19 April 2010 - 10:39 PM Rate Topic: -----

#1 Guest_Maegan*


Reputation:

Choosing the highest and lowest out of a set of numbers in vb6

Posted 14 April 2010 - 05:57 PM

Okay, so I have a homework assignment that I've been trying to figure out for three days but am getting nowhere. The assignment is to use an inputbox to enter in any amount of test scores and the program should pick out the highest and lowest from all of them. As of now all it does is if I enter in 55,66, and 99, it brings up 556699 in both highest and lowest. Any type of help would be appreciated. Here's my code:


 
        Dim intgrades As String = InputBox("Enter grades.", "What are your grades?")



        Dim highgrade As Integer
        Dim lowgrade As Integer
       



        highgrade = (Val(intgrades))
        lowgrade = Val(intGrades)


        For x = 1 To 2
            If Val(intgrades) > Val(lowgrade) Then
                lowgrade = Val(intgrades)
            End If
        Next



        For x = 1 To 2

            If Val(intgrades) > Val(highgrade) Then
                highgrade = (Val(intgrades))
            End If
        Next


        Me.lblLowest.Text = lowgrade
        Me.lblHighest.Text = highgrade





    End Sub 


Is This A Good Question/Topic? 0

Replies To: Choosing the highest and lowest out of a set of numbers in vb6

#2 vb5prgrmr  Icon User is offline

  • D.I.C Lover

Reputation: 109
  • View blog
  • Posts: 1,016
  • Joined: 21-March 09

Re: Choosing the highest and lowest out of a set of numbers in vb6

Posted 14 April 2010 - 06:16 PM

Okay, two variables MyH as Integer and MyL As Integer. Enter loop until a value of cancel is entered. While in loop, test MyH<input and if so MyH = input and do the same for MyL>input.



Good Luck
Was This Post Helpful? 0
  • +
  • -

#3 Guest_Maegan*


Reputation:

Re: Choosing the highest and lowest out of a set of numbers in vb6

Posted 15 April 2010 - 03:15 PM

Okay, i've still been working on this and have changed the code somewhat but now it only changes the highest score and displays the lowest as 0. I'm not sure what i'm doing wrong so if anyone can offer up an example or something that would be appreciated.

Dim intgrades As String = InputBox("Enter grades.", "What are your grades?")
        Dim highgrade As Integer
        Dim lowgrade As Integer
        





        For x = 1 To 20
            If Val(intgrades) < Val(lowgrade) Then
                Me.lblLowest.Text = intgrades
            ElseIf Val(intgrades) > Val(lowgrade) Then
                Me.lblLowest.Text = lowgrade
            End If


            If Val(intgrades) > Val(highgrade) Then
                Me.lblHighest.Text = intgrades
            ElseIf Val(intgrades) < Val(highgrade) Then
                Me.lblHighest.Text = highgrade
            End If




        Next


Was This Post Helpful? 0

#4 keakTheGEEK  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 107
  • View blog
  • Posts: 344
  • Joined: 23-February 10

Re: Choosing the highest and lowest out of a set of numbers in vb6

Posted 15 April 2010 - 04:30 PM

See comments in Code:
'This statement. How are you entering your grades? 55 98 75 or 55,88,99 etc
'If you're entering them separated by space, then you need to split them up
'into individual grades to check each one to see which one is highest and lowest.
'You can split on a space, comma, or any other delimiter... 
Dim intgrades As String = InputBox("Enter grades.", "What are your grades?")

'So, after the statement above is executed (lets say grades were entered like so: 68, 55, 88, 98, 77)
'Split them up. VB6 has a split function that will spit each element into an array:
        Dim arrayGrades() As String
        arrayGrades = Split(intGrades, ",") 'Also, note that your variable intGrades is actually a string

        'Now arrayGrades has each grade as individual elements of the array. You can iterate through the 
        'array and check each grade to see which one is highest and which one is lowest.

        Dim highgrade As Integer
        Dim lowgrade As Integer
        

        'Modify your for loop so that you are iterating through the arrayGrades:
        Dim x As Integer
        For x = LBound(arrayGrades) To UBound(arrayGrades)

            'Assign elements of the array to highgrade and lowgrade like so:
            highgrade = Val(arrayGrades(x))

        Next 




There's a lot more you will need to code to get the highest and lowest grades. But first you need to understand how to store each grade individually for computation. Also, just some advice, I would get out of the habit of using the Val() function. By default it will return zero if the parameter passed to it cannot be converted to a numerical type w/o any kind of warning. Hope that points you in the right direction. Good Luck!

Also, are you sure that this is VB6?
Dim intgrades As String = InputBox("Enter grades.", "What are your grades?")



That looks like VB .NET to me...
Was This Post Helpful? 0
  • +
  • -

#5 Raju2047  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 6
  • View blog
  • Posts: 55
  • Joined: 16-February 09

Re: Choosing the highest and lowest out of a set of numbers in vb6

Posted 15 April 2010 - 04:51 PM

Hope this may help u lil bit.
Dim a, b, c, d, e As Integer
a = InputBox("How many scores to be entered.")
For b = 1 To a
c = InputBox("enter the score.")
If b = 1 Then
e = c
d = c
End If
If c > d Then
d = c
End If
If c < e Then
e = c
End If
Next
lblHighest.Text = d
lblLowest.Text = e

This post has been edited by Raju2047: 15 April 2010 - 04:53 PM

Was This Post Helpful? 0
  • +
  • -

#6 vb5prgrmr  Icon User is offline

  • D.I.C Lover

Reputation: 109
  • View blog
  • Posts: 1,016
  • Joined: 21-March 09

Re: Choosing the highest and lowest out of a set of numbers in vb6

Posted 15 April 2010 - 11:02 PM

You will need to initially set the lowest grade to some high value like 1000 before you start your comparisons...



Good Luck
Was This Post Helpful? 0
  • +
  • -

#7 vb5prgrmr  Icon User is offline

  • D.I.C Lover

Reputation: 109
  • View blog
  • Posts: 1,016
  • Joined: 21-March 09

Re: Choosing the highest and lowest out of a set of numbers in vb6

Posted 15 April 2010 - 11:02 PM

You will need to initially set the lowest grade to some high value like 1000 before you start your comparisons...



Good Luck
Was This Post Helpful? 0
  • +
  • -

#8 Raju2047  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 6
  • View blog
  • Posts: 55
  • Joined: 16-February 09

Re: Choosing the highest and lowest out of a set of numbers in vb6

Posted 16 April 2010 - 05:38 AM

is there any mistakes in my coding... i haven't set lowest grade to some high value..bt i couldn't find any...
Was This Post Helpful? 0
  • +
  • -

#9 vb5prgrmr  Icon User is offline

  • D.I.C Lover

Reputation: 109
  • View blog
  • Posts: 1,016
  • Joined: 21-March 09

Re: Choosing the highest and lowest out of a set of numbers in vb6

Posted 16 April 2010 - 06:45 AM

You set the variable that is to hold the lowest value to some high number prior to entering the loop or comparing it other input...



Good Luck
Was This Post Helpful? 0
  • +
  • -

#10 thava  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 180
  • View blog
  • Posts: 1,606
  • Joined: 17-April 07

Re: Choosing the highest and lowest out of a set of numbers in vb6

Posted 16 April 2010 - 04:37 PM

seems there is no prob in Raju2047's algorithm
Was This Post Helpful? 0
  • +
  • -

#11 bytelogik  Icon User is offline

  • D.I.C Head

Reputation: 22
  • View blog
  • Posts: 212
  • Joined: 06-October 09

Re: Choosing the highest and lowest out of a set of numbers in vb6

Posted 19 April 2010 - 10:39 PM

hi Maegan,
working with comparisons for highest and lowest is a little bit of confusing to most of the beginner coders. try this. (VB6)

        Dim intgrades As String
        Dim highgrade As Integer
        Dim lowgrade As Integer
        dim grades() as string   
        dim x as integer,Grade as integer
        
        'setting the initial values is the logic for comparison   
        highgrade=0
        lowgrade= 100   'replace this 100 to your_maximum_grade + 1 

        'use some error checking if intgrades is NULL
        intgrages = InputBox("Enter grades.", "What are your grades?")  'use only "," to separate grades
        grades=split(intgrades,",")    'this will split your string into individual grades for comparision
             
        For x = 0 To ubound(grades)
            Grade=grades(x)
            if Grade>highgrade then highgrade=Grade
            If lowgrade > Grade Then lowgrade = Grade
        Next

        Me.lblLowest.Text = lowgrade
        Me.lblHighest.Text = highgrade



all the best.

This post has been edited by bytelogik: 19 April 2010 - 10:42 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1