Problem creating Minimum and Maximum Function

Page 1 of 1

4 Replies - 1196 Views - Last Post: 07 December 2012 - 10:03 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=303169&amp;s=61fe930f2ddc786d8d9e1a65cd2d8206&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

#1 Prino33

Reputation: 0
• Posts: 6
• Joined: 15-November 12

Problem creating Minimum and Maximum Function

Posted 07 December 2012 - 08:45 PM

I'm trying to figure a way to write a Function or If statements that picks the maximum value out of 4 numbers. I also have to do one for the minimum of 4 numbers. I found a way in which I could do it with comparing 3 numbers but cant figure out how to do it with 4.

Any help is greatly appreiciated!
Is This A Good Question/Topic? 0

Replies To: Problem creating Minimum and Maximum Function

#2 modi123_1

• Suitor #2

Reputation: 14044
• Posts: 56,199
• Joined: 12-June 08

Re: Problem creating Minimum and Maximum Function

Posted 07 December 2012 - 08:47 PM

Why not use a for loop and a two integer variables? Go through each entry in the array and compare the value if it is bigger than the current biggest or smaller than the current smallest.. if either - replace accordingly.

#3 _HAWK_

• Master(Of Foo)

Reputation: 1162
• Posts: 4,444
• Joined: 02-July 08

Re: Problem creating Minimum and Maximum Function

Posted 07 December 2012 - 08:58 PM

Use a List(Of Integer) then you have .Min and .Max on that object and your done!

#4 Prino33

Reputation: 0
• Posts: 6
• Joined: 15-November 12

Re: Problem creating Minimum and Maximum Function

Posted 07 December 2012 - 09:45 PM

Both sound great. I'm just having trouble figuring out how to implment either method into my code. Could either of you provide an example about how one could be done?

Thanks!

P.S. I addded the code I'm try to refactor below. Our teacher prdovided us with an example we just have to rewrite to work with our problem. Anyways, I dont know if you can decipher the code but im trying to refactor the code to work with a 4 value min/max equation.

```Option Strict On
Option Explicit On
Imports System.Convert
Imports Microsoft.VisualBasic.ControlChars

Public Class frmStats

Private StatsData() As frmMain.sQuarterlySales = frmMain.SalesmansQuarterlySales

Private Sub btnShowAverage_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnShowAverage.Click
Dim pdecAverageSale As Decimal
Dim pdecTotalSales As Decimal

For pintCount As Integer = 0 To StatsData.GetUpperBound(0)
pdecTotalSales += StatsData(pintCount).Quarter1Sales + StatsData(pintCount).Quarter2Sales + StatsData(pintCount).Quarter3Sales + StatsData(pintCount).Quarter4Sales
Next

pdecAverageSale = pdecTotalSales / 4

lblDisplayReport.Text = "The average sales amount is: " & pdecAverageSale.ToString("c")
End Sub

Private Sub btnShowMinimumPurchase_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnShowMinimumPurchase.Click
Dim pdecMinPurchase As Decimal
Dim pdecPurchase As Decimal
Dim pintCount As Integer
Dim pintFound As Integer = 0

pdecMinPurchase = StatsData(0).prodAmount * StatsData(0).prodPrice

For pintCount = 1 To StatsData.GetUpperBound(0)
pdecPurchase = StatsData(pintCount).prodAmount * StatsData(pintCount).prodPrice
If pdecPurchase < pdecMinPurchase Then
pdecMinPurchase = pdecPurchase
pintFound = pintCount
End If
Next

lblDisplayReport.Text = "The minimum purchase amount is: " & pdecMinPurchase.ToString("c") & " at location " & pintFound.ToString
End Sub

Private Sub btnShowMaxPurchase_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnShowMaxPurchase.Click
Dim pdecMaxPurchase As Decimal
Dim pdecPurchase As Decimal
Dim pintCount As Integer
Dim pintFound As Integer = 0

pdecMaxPurchase = 0

For pintCount = 1 To StatsData.GetUpperBound(0)
pdecPurchase = StatsData(pintCount).prodAmount * StatsData(pintCount).prodPrice
If pdecPurchase > pdecMaxPurchase Then
pdecMaxPurchase = pdecPurchase
pintFound = pintCount
End If
Next

lblDisplayReport.Text = "The minimum purchase amount is: " & pdecMaxPurchase.ToString("c") & " at location " & pintFound.ToString
End Sub
End Class
```

#5 _HAWK_

• Master(Of Foo)

Reputation: 1162
• Posts: 4,444
• Joined: 02-July 08

Re: Problem creating Minimum and Maximum Function

Posted 07 December 2012 - 10:03 PM

```Dim numbers As New List(Of Decimal)