Not Yet Answered # Find top 3 in a range of numbers

Reverend Jim 1,421 Discussion Starter naz1234 Discussion Starter naz1234 Reverend Jim 1,421

0

- Put the numbers into an array
- Sort the array in ascending order
- Take the "top" 3

Your definition of "top 3" seems to be "the first 3". My definition of "top 3" would be the three largest. If this is a homework assignment I suggest you get a clear definition.

0

Yup , it was "the first 3" . Sorry for mistake.

Actually that numbers, I extracted from DatagridView. Then I need to find the "first 3".

I manage to extract it and scan the maximum. But I still stuck to figured it out to find the "first 3"

Here my first code :

```
Dim aa3 As Decimal = 0
Dim bb3 As Decimal = 0
Dim cc3 As Decimal = 0
Dim dd3 As Decimal = 0
Dim ee3 As Decimal = 0
aa3 = Convert.ToDecimal(DataGridView1.Rows(4 - 2).Cells(4).Value)
bb3 = Convert.ToDecimal(DataGridView1.Rows(52 - 2).Cells(4).Value)
cc3 = Convert.ToDecimal(DataGridView1.Rows(100 - 2).Cells(4).Value)
dd3 = Convert.ToDecimal(DataGridView1.Rows(148 - 2).Cells(4).Value)
ee3 = Convert.ToDecimal(DataGridView1.Rows(196 - 2).Cells(4).Value)
' Create a list of Long values.
Dim longs3 As New List(Of Double)(New Double() _
{aa3, bb3, cc3, dd3, ee3})
' Get the maximum value in the list.
Dim max3 As Double = longs3.Max()
' Display the result.
TextBox7.Text = Format(max3, "0.00")
```

0

Hi Reverend Jim,

I manage to find the first 3 numbers. But, when I put them in a texbox, it become more messy . How to show in 3 textbox ?

Here my 2nd code:

```
Dim Baa3 As Decimal = 0
Dim Bbb3 As Decimal = 0
Dim Bcc3 As Decimal = 0
Dim Bdd3 As Decimal = 0
Dim Bee3 As Decimal = 0
Baa3 = Convert.ToDecimal(DataGridView1.Rows(4 - 2).Cells(4).Value)
Bbb3 = Convert.ToDecimal(DataGridView1.Rows(52 - 2).Cells(4).Value)
Bcc3 = Convert.ToDecimal(DataGridView1.Rows(100 - 2).Cells(4).Value)
Bdd3 = Convert.ToDecimal(DataGridView1.Rows(148 - 2).Cells(4).Value)
Bee3 = Convert.ToDecimal(DataGridView1.Rows(196 - 2).Cells(4).Value)
' Create a list of Long values.
Dim longs3 As New List(Of Double)(New Double() _
{Baa3, Bbb3, Bcc3, Bdd3, Bee3})
' first three values.
Dim topThreeGrades As IEnumerable(Of Double) = _
longs3 _
.OrderByDescending(Function(grade) grade) _
.Take(3)
' Display the results.
Dim output As New System.Text.StringBuilder("The first three values are:" & vbCrLf)
For Each grade As Double In topThreeGrades
output.AppendLine(grade)
Next
TextBox1.Text = output.ToString()
TextBox2.Text = output.ToString()
TextBox3.Text = output.ToString()
```

0

You can get the required numbers from the grid by

```
Dim arr(4) As Integer
Dim i As Integer = 0
For Each r As Integer In {2, 50, 98, 146, 194}
arr(i) = DataGridView1.Rows(r).Cells(4).Value
i += 1
Next
```

and you can sort the numbers by

```
Array.Sort(arr)
```

copy the numbers to the textboxes by

```
TextBox1.Text = arr(0)
.
.
```

I used integers for my example. Modify as required for your data type.

This article has been dead for over six months. Start a new discussion instead.

Recommended Articles

Help! I want to create a java program that finds the highest even integer among the values entered by the user. Stop asking values when a value less than 1 have been entered. If no even integer is entered, display "No Even Integer"

Here is the sample output that I ...

Hello All ...

Iam Getting An Error With try to excecute the stored procedure .

I have Have Sql database , the stored procedure like so :

```
USE [MPRS]
GO
/****** Object: StoredProcedure [dbo].[Search_Licenses_By_Number] Script Date: 26-Nov-16 8:06:52 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE ...
```

I don’t want at this stage work on a big separate project as I've already got plenty ...