-5

Hello guys am working on a window application in vb.net Am trying to code an array that can add only 8 best subjects grades from the 10 subjects done by a student. the ten subject grades are displayed as labels and also the TOTAL of best eight subject grades is displayed as a label. Any one to help me. am new in programming. Here is my code.

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    ' subject grades
    Label1.Text = 7
    Label2.Text = 1
    Label3.Text = 3
    Label4.Text = 8
    Label5.Text = 4
    Label6.Text = 9
    Label7.Text = 2
    Label8.Text = 5
    Label9.Text = 2
    Label10.Text = 6

    Dim gradeList As New ArrayList()
    gradeList.Add(Label1.Text)
    gradeList.Add(Label2.Text)
    gradeList.Add(Label3.Text)
    gradeList.Add(Label4.Text)
    gradeList.Add(Label5.Text)
    gradeList.Add(Label6.Text)
    gradeList.Add(Label7.Text)
    gradeList.Add(Label8.Text)
    gradeList.Add(Label9.Text)
    gradeList.Add(Label10.Text)


    'sort grades in an arraylist
    gradeList.Sort()

    ' Please guys help me and modify this code to sort and  SUM UP ONLY best eight done subject grades.
    ' I want label11.text to display the answer 
    'THANKS.

End Sub`
user3666449
  • 1
  • 1
  • 1
  • 3
  • First hint - do **NOT** use `ArrayList`. Use `List` instead. – Tim May 22 '14 at 19:49
  • I would use a list then sort it. Remove the worst two. Getting the top 8 grades. – Chase Ernst May 22 '14 at 20:10
  • 1
    What is the best grade, 1 or 9, and does `gradeList.Sort()` not sort things how you want (sorting numeric strings like this is iffy, but since they are all 1 digit it should work!)? You will want to make sure you have [`Option Strict`](http://msdn.microsoft.com/en-us/library/zcd4xwzs.aspx) and [`Option Explicit`](http://msdn.microsoft.com/en-us/library/y9341s4f.aspx) On (and [`Option Infer`](http://msdn.microsoft.com/en-us/library/bb384665.aspx) if you start using LINQ, or are lazy like me), to avoid many programming errors. – Mark May 22 '14 at 20:32
  • 3
    -1 for total lack of effort. You have a sorted list. This isn't a programming problem, it's a basic logic problem that can be solved by just taking a step back and thinking about it. – helrich May 22 '14 at 20:33

2 Answers2

4

As others have suggested, use a strongly-typed collection rather than ArrayList. You probably want grades to be Integer rather than String. It probably won't matter for single-digit grades, but might sort oddly otherwise.

If you change your gradeList variable into a List(Of Integer) and turn on Option Strict, you'll notice that the label text is String. You can use CInt or Integer.Parse to convert to Integer. They aren't quite the same.

You've already sorted the list in your code, so it seems what you're really asking is "How do I sum the first 8 items in a list?"

If you think about that question, you should be able to write a For-loop that does so.

You can also do what you want to do with a one-line LINQ expression:

Dim sum As Integer = (From grade In gradeList Select grade Order By grade Descending).Take(8).Sum()
Community
  • 1
  • 1
pmcoltrane
  • 3,052
  • 1
  • 24
  • 30
1

Sorting numbers as strings leads to problems. For example if you add somewhere 10 in your grades and sort the array list, like this:

Dim gradeList As New ArrayList()
gradeList.Add("7")
gradeList.Add("1")
gradeList.Add("3")
gradeList.Add("8")
gradeList.Add("10") '! watch out
' sort string
gradeList.Sort()
' output sorted strings
for each grade in gradeList
    Console.WriteLine(grade)
next grade

the output is:

1
10
3
7
8

And it is obviously wrong, because the numbers are compared as literals and everything starting with 1 is less than 3 in this case.

The problem is easily solved:

  • convert the literals to numbers (or add them directly to the list as integers)
  • put them in a list
  • sort the list (default order is ascending)
  • reverse the sorted list (make the sorting descending)
  • take the first (biggest) and the second element (second biggest) from the list and sum them

A solution using a List and LINQ could look like this:

' list of integers to be sorted
dim grades as List(of integer) = new List(of integer)
' helper variable holding the parsed grade
dim g as Integer
' convert literals if possible to integers and put them in the list
for each grade in gradeList
    if Integer.TryParse(grade, g) then
        grades.Add(g)
    end if
next grade
' sort the grades as numbers (default is ascending order)
grades.Sort()
' invert the order = descending order
grades.Reverse()
' take the highest two and sum them
dim sum as integer = grades(0) + grades(1)
' or using LINQ ...
'dim sum as integer = grades.Take(1).First() + grades.Skip(1).Take(1).First()
Console.WriteLine(sum)

The output is in this case:

10
8
7
3
1

and the sum is:

18
keenthinker
  • 7,645
  • 2
  • 35
  • 45