Results 1 to 15 of 15

Thread: Finding Max / Min

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Mar 2009
    Posts
    76

    Finding Max / Min

    The program will allow the teacher to:

    Store the names and marks of the pupils
    Calculate Percentages
    Produce a list of pupils showing their marks and percentages.
    Show the highest achieving pupil in the class.
    Show the lowest achieving pupil in the class.
    Display whether a pupil has sat their NAB.

    Main Algorithm

    1. Start Loop.
    2. Enter Names and Marks.
    3. Calculate Percentage.
    4. End loop
    5. Display pupils name and percentages.
    6. Display the highest mark in the class.
    7. Display the lowest mark in the class.
    8. Show any resits required.


    Code:
    
    Private Sub cmdLow_Click()
    Dim High_Score As Integer
    
    High_Score = score(0)
    For Counter = 1 To (no_scores - 1)
    If score(Counter) > High_Score Then
    High_Score = score(Counter)
    End If
    Next Counter
    Form1.Print High_Score
    
    
    End Sub
    
    Private Sub cmdLow_Click()
    Dim Low_Score As Integer
    
    Low_Score = score(0)
    For Counter = 1 To (no_scores - 1)
    If score(Counter) > Low_Score Then
    Low_Score = score(Counter)
    End If
    Next Counter
    Form1.Print Low_Score
    
    
    
    End Sub
    
    Private Sub cmdQuit_Click()
    Unload Me
    End Sub
    
    Private Sub cmdStart_Click()
    Dim Pupil_Name As String
    Dim Mark As Integer
    Dim Percentage As Integer
    
    
    Pupil_Name = InputBox("Enter name of pupil")
    Mark = InputBox("Enter their mark")
    
    While Mark < 0 Or Mark > 20
    MsgBox ("Invalid Mark. Try Again")
    Mark = InputBox("Enter their mark")
    Wend
    
    Percentage = Mark / 20 * 100
    
    lstNames.AddItem Pupil_Name
    lstMarks.AddItem Mark
    lstPercentage.AddItem Percentage & "%"
    
    
    If Percentage < 60 Then
    lstResit.AddItem Pupil_Name
    End If
    
    End Sub
    
    Private Sub Label1_Click()
    
    End Sub
    I am struggling with steps 6 and 7 the others i think i have done fully. Also the maximum entries allowed is 10, how can i limit this???

    Thanks 4 Help

  2. #2
    PowerPoster Spoo's Avatar
    Join Date
    Nov 2008
    Location
    Right Coast
    Posts
    2,656

    Re: Finding Max / Min

    Cheese

    Seems like it would be helpful to have an array
    that contains all of the scores. You could then
    sort the array, descending. The top 10 and bottom 10
    would then be available for you to display.

    Spoo

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Mar 2009
    Posts
    76

    Re: Finding Max / Min

    Code???

  4. #4
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Finding Max / Min

    No, tutorials - so that you can actually learn what is going on in the code for your assignment.

    See the Array articles in the "Data types/variables" section of our Classic VB FAQs (in the FAQ forum)

  5. #5
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863

    Re: Finding Max / Min

    Some comments, no code!
    As Spoo stated, you need to have a look into Arrays. You store to vlaue for name etc onky in the Listboxes, I'd suggest you use a globally declared Array for those!
    If you allow only 10 elements for such an array your last problem would be solved (However that doesn't sound like the use of a Do-Loop)
    Your sub for Low-Score is basically doing the same as your High-Score code, to find a Low score you need to check if the next entry is lower instead of higher.
    You are using the Variable "No_scores" this one isn't declared nor is it set to a value!

    Another approach for Hihest, lowest and Resist checking would be to check for that during the Input of a new entry. That way your display could be instant!
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Mar 2009
    Posts
    76

    Re: Finding Max / Min

    what would the code be to find minimum value???

  7. #7
    PowerPoster
    Join Date
    Oct 2008
    Location
    Midwest Region, United States
    Posts
    3,574

    Re: Finding Max / Min

    For min value:

    Work your way through the array.

    Store the value in the first element in a variable (something like "tempMin").

    Look at the value in the 2nd element and compare it to tempMin. If lower, it becomes the new tempMin, etc. all the way through element 10 (or whatever).

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Mar 2009
    Posts
    76

    Re: Finding Max / Min

    there is no array im inputting the values and they go to a listbox
    so i cant search through an array that i dont have.
    the code i require is how to find minimum value in a listbox

  9. #9
    PowerPoster Spoo's Avatar
    Join Date
    Nov 2008
    Location
    Right Coast
    Posts
    2,656

    Re: Finding Max / Min

    Cheese

    I'm not a ListBox power user, but I did notice that
    there is a Sorted property. Could that help?

    Spoo

  10. #10
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: Finding Max / Min

    The listbox does have a Sorted property that can only be set during design time. However, the sorting is string, not numeric. So 10 comes before 2 (string sort). If using the sorted listbox, you will want to format your numbers with some leading zeros; VB's Format() function.

    Another option is to do what others have suggested. Whether an array or a listing, the logic is the same.
    1. Set the Min & Max values to the same value; the 1st list item
    2. Loop thru each list item starting with the 2nd list item
    3. Compare to Min and if smaller, set Min to list item value
    4. Compare to Max and if larger, set Max to list item value
    5. When your loop finishes all the list items, you will have the Min & Max
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  11. #11

    Thread Starter
    Lively Member
    Join Date
    Mar 2009
    Posts
    76

    Re: Finding Max / Min

    How do u st the start point to he first value though since it will be random every time as I will be entering it through an inputbox

  12. #12
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863

    Re: Finding Max / Min

    You are storing each value somewhere ( in your case in a listbox.list), each element of that list or array can be recalled seperatley. Start with the first-one, the value that it is holding might be random, however it will remain the first one!
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

  13. #13

    Thread Starter
    Lively Member
    Join Date
    Mar 2009
    Posts
    76

    Re: Finding Max / Min

    how do u recall the first value??

  14. #14
    PowerPoster
    Join Date
    Oct 2008
    Location
    Midwest Region, United States
    Posts
    3,574

    Re: Finding Max / Min

    Code:
    Private Sub Command1_Click()
        Dim tempMin As Integer
        Dim listCount As Integer
        Dim i As Integer
        listCount = List1.listCount
        tempMin = val(List1.List(0))
        For i = 1 To listCount - 1
            If val(List1.List(i)) < tempMin Then
                tempMin = val(List1.List(i))
            Else
                'tempMin is lower than this item
            End If
        Next i
        MsgBox tempMin
    End Sub
    
    Private Sub Form_Load()
        List1.AddItem ("19")
        List1.AddItem ("4")
        List1.AddItem ("9")
        List1.AddItem ("2")
    End Sub

  15. #15

    Thread Starter
    Lively Member
    Join Date
    Mar 2009
    Posts
    76

    Re: Finding Max / Min

    Thanks that works cheers

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