-
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
-
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
-
Re: Finding Max / Min
-
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)
-
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!
-
Re: Finding Max / Min
what would the code be to find minimum value???
-
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).
-
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
-
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
-
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
-
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
-
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!
-
Re: Finding Max / Min
how do u recall the first value??
-
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
-
Re: Finding Max / Min
Thanks that works cheers:):)